Skip to content

Commit

Permalink
Add builder methods Subject to allow method chaining (close #303)
Browse files Browse the repository at this point in the history
PR #369
  • Loading branch information
matus-tomlein authored Jan 11, 2024
1 parent 285339e commit 94e9c52
Show file tree
Hide file tree
Showing 2 changed files with 158 additions and 8 deletions.
138 changes: 130 additions & 8 deletions src/main/java/com/snowplowanalytics/snowplow/tracker/Subject.java
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,17 @@ public void setUserId(String userId) {
}
}

/**
* Sets the User ID and returns itself
*
* @param userId a user id string
* @return itself
*/
public Subject userId(String userId) {
this.setUserId(userId);
return this;
}

/**
* Sets the screen res parameter
*
Expand All @@ -245,6 +256,18 @@ public void setScreenResolution(int width, int height) {
}
}

/**
* Sets the screen res parameter and returns itself
*
* @param width a width integer
* @param height a height integer
* @return itself
*/
public Subject screenResolution(int width, int height) {
this.setScreenResolution(width, height);
return this;
}

/**
* Sets the view port parameter
*
Expand All @@ -258,6 +281,18 @@ public void setViewPort(int width, int height) {
}
}

/**
* Sets the view port parameter and returns itself
*
* @param width a width integer
* @param height a height integer
* @return itself
*/
public Subject viewPort(int width, int height) {
this.setViewPort(width, height);
return this;
}

/**
* Sets the color depth parameter
*
Expand All @@ -269,6 +304,17 @@ public void setColorDepth(int depth) {
}
}

/**
* Sets the color depth parameter and returns itself
*
* @param depth a color depth integer
* @return itself
*/
public Subject colorDepth(int depth) {
this.setColorDepth(depth);
return this;
}

/**
* Sets the timezone parameter. Note that timezone is set by default to the server's timezone
* (`TimeZone tz = Calendar.getInstance().getTimeZone().getID()`);
Expand All @@ -281,6 +327,19 @@ public void setTimezone(String timezone) {
}
}

/**
* Sets the timezone parameter and returns itself.
* Note that timezone is set by default to the server's timezone
* (`TimeZone tz = Calendar.getInstance().getTimeZone().getID()`)
*
* @param timezone a timezone string
* @return itself
*/
public Subject timezone(String timezone) {
this.setTimezone(timezone);
return this;
}

/**
* Sets the language parameter
*
Expand All @@ -293,8 +352,18 @@ public void setLanguage(String language) {
}

/**
* User inputted ip address for the
* subject.
* Sets the language parameter and returns itself
*
* @param language a language string
* @return itself
*/
public Subject language(String language) {
this.setLanguage(language);
return this;
}

/**
* User inputted ip address for the subject.
*
* @param ipAddress an ip address
*/
Expand All @@ -305,8 +374,18 @@ public void setIpAddress(String ipAddress) {
}

/**
* User inputted useragent for the
* subject.
* Sets the user inputted ip address for the subject and returns itself
*
* @param ipAddress a ipAddress string
* @return itself
*/
public Subject ipAddress(String ipAddress) {
this.setIpAddress(ipAddress);
return this;
}

/**
* User inputted useragent for the subject.
*
* @param useragent a useragent
*/
Expand All @@ -317,8 +396,18 @@ public void setUseragent(String useragent) {
}

/**
* User inputted Domain User Id for the
* subject.
* Sets the user inputted useragent for the subject and returns itself
*
* @param useragent a useragent string
* @return itself
*/
public Subject useragent(String useragent) {
this.setUseragent(useragent);
return this;
}

/**
* User inputted Domain User Id for the subject.
*
* @param domainUserId a domain user id
*/
Expand All @@ -329,8 +418,18 @@ public void setDomainUserId(String domainUserId) {
}

/**
* User inputted Domain Session ID for the
* subject.
* Sets the user inputted Domain User Id for the subject and returns itself
*
* @param domainUserId a domainUserId string
* @return itself
*/
public Subject domainUserId(String domainUserId) {
this.setDomainUserId(domainUserId);
return this;
}

/**
* User inputted Domain Session ID for the subject.
*
* @param domainSessionId a domain session id
*/
Expand All @@ -340,6 +439,17 @@ public void setDomainSessionId(String domainSessionId) {
}
}

/**
* Sets the user inputted Domain Session ID for the subject and returns itself
*
* @param domainSessionId a domainSessionId string
* @return itself
*/
public Subject domainSessionId(String domainSessionId) {
this.setDomainSessionId(domainSessionId);
return this;
}

/**
* User inputted Network User ID for the subject.
* This overrides the network user ID set by the Collector in response Cookies.
Expand All @@ -352,6 +462,18 @@ public void setNetworkUserId(String networkUserId) {
}
}

/**
* Sets the user inputted Network User ID for the subject and returns itself.
* This overrides the network user ID set by the Collector in response Cookies.
*
* @param networkUserId a networkUserId string
* @return itself
*/
public Subject networkUserId(String networkUserId) {
this.setNetworkUserId(networkUserId);
return this;
}

/**
* Gets the Subject pairs.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,34 @@ public void testGetSubject() {
assertEquals(expected, subject.getSubject());
}

@Test
public void testBuilderMethods() {
Subject subject = new Subject();
subject
.userId("user1")
.screenResolution(100, 150)
.viewPort(150, 100)
.colorDepth(10)
.timezone("America/Toronto")
.language("EN")
.ipAddress("127.0.0.1")
.useragent("useragent")
.domainUserId("duid")
.domainSessionId("sessionid")
.networkUserId("nuid");
assertEquals("user1", subject.getSubject().get("uid"));
assertEquals("100x150", subject.getSubject().get("res"));
assertEquals("150x100", subject.getSubject().get("vp"));
assertEquals("10", subject.getSubject().get("cd"));
assertEquals("America/Toronto", subject.getSubject().get("tz"));
assertEquals("EN", subject.getSubject().get("lang"));
assertEquals("127.0.0.1", subject.getSubject().get("ip"));
assertEquals("useragent", subject.getSubject().get("ua"));
assertEquals("duid", subject.getSubject().get("duid"));
assertEquals("sessionid", subject.getSubject().get("sid"));
assertEquals("nuid", subject.getSubject().get("tnuid"));
}

@Test
public void testCreateFromConfig() {
SubjectConfiguration subjectConfig = new SubjectConfiguration()
Expand Down

0 comments on commit 94e9c52

Please sign in to comment.