Skip to content

Commit e44e197

Browse files
committed
Add ability to deep clone a hit #52
1 parent a0c7937 commit e44e197

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

src/main/java/com/brsanthu/googleanalytics/request/GoogleAnalyticsRequest.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1891,4 +1891,25 @@ public T occurredAt(ZonedDateTime value) {
18911891
public ZonedDateTime occurredAt() {
18921892
return occurredAt;
18931893
}
1894+
1895+
public T deepClone() {
1896+
try {
1897+
GoogleAnalyticsRequest<T> clonedReq = this.getClass().newInstance();
1898+
clonedReq.occurredAt = ZonedDateTime.now();
1899+
clonedReq.parms = cloneMap(parms);
1900+
clonedReq.customDimensions = cloneMap(customDimensions);
1901+
clonedReq.customMetrics = cloneMap(customMetrics);
1902+
clonedReq.delegateExecutor = delegateExecutor;
1903+
1904+
return (T) clonedReq;
1905+
} catch (Exception e) {
1906+
throw new RuntimeException("Exception while deep cloning " + this, e);
1907+
}
1908+
}
1909+
1910+
private <K, V> Map<K, V> cloneMap(Map<K, V> input) {
1911+
Map<K, V> output = new HashMap<>();
1912+
output.putAll(input);
1913+
return output;
1914+
}
18941915
}

src/test/java/com/brsanthu/googleanalytics/GoogleAnalyticsTest.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import com.brsanthu.googleanalytics.request.DefaultRequest;
2222
import com.brsanthu.googleanalytics.request.GoogleAnalyticsParameter;
2323
import com.brsanthu.googleanalytics.request.GoogleAnalyticsResponse;
24+
import com.brsanthu.googleanalytics.request.ScreenViewHit;
2425

2526
public class GoogleAnalyticsTest {
2627

@@ -213,4 +214,15 @@ void testAutoQueueTimeBatch() throws Exception {
213214
assertThat(Integer.parseInt(resp2.getRequestParams().get(QUEUE_TIME.getParameterName()))).isCloseTo(0, within(100));
214215
}
215216

217+
@Test
218+
void testDeepClone() throws Exception {
219+
ScreenViewHit screenhit1 = ga.screenView().adwordsId("test1");
220+
ScreenViewHit screenhit2 = screenhit1.deepClone();
221+
222+
screenhit1.adwordsId("test1updated");
223+
224+
assertThat(screenhit1).isNotSameAs(screenhit2);
225+
assertThat(screenhit1.adwordsId()).isEqualTo("test1updated");
226+
assertThat(screenhit2.adwordsId()).isEqualTo("test1");
227+
}
216228
}

0 commit comments

Comments
 (0)