Skip to content

Commit 55637e1

Browse files
authored
simplified MapUtil.copy (#796)
* simplified the MapUtil copy method since all callers were checking if source was null/empty.
1 parent 9c04d50 commit 55637e1

File tree

4 files changed

+47
-34
lines changed

4 files changed

+47
-34
lines changed

Diff for: core/src/main/java/com/microsoft/applicationinsights/TelemetryClient.java

+6-20
Original file line numberDiff line numberDiff line change
@@ -138,13 +138,8 @@ public void trackEvent(String name, Map<String, String> properties, Map<String,
138138

139139
EventTelemetry et = new EventTelemetry(name);
140140

141-
if (properties != null && properties.size() > 0) {
142-
MapUtil.copy(properties, et.getContext().getProperties());
143-
}
144-
145-
if (metrics != null && metrics.size() > 0) {
146-
MapUtil.copy(metrics, et.getMetrics());
147-
}
141+
MapUtil.copy(properties, et.getContext().getProperties());
142+
MapUtil.copy(metrics, et.getMetrics());
148143

149144
this.track(et);
150145
}
@@ -182,9 +177,7 @@ public void trackTrace(String message, SeverityLevel severityLevel, Map<String,
182177

183178
TraceTelemetry et = new TraceTelemetry(message, severityLevel);
184179

185-
if (properties != null && properties.size() > 0) {
186-
MapUtil.copy(properties, et.getContext().getProperties());
187-
}
180+
MapUtil.copy(properties, et.getContext().getProperties());
188181

189182
this.track(et);
190183
}
@@ -252,9 +245,7 @@ public void trackMetric(String name, double value, Integer sampleCount, Double m
252245
mt.setMin(min);
253246
mt.setMax(max);
254247
mt.setStandardDeviation(stdDev);
255-
if (properties != null && properties.size() > 0) {
256-
MapUtil.copy(properties, mt.getProperties());
257-
}
248+
MapUtil.copy(properties, mt.getProperties());
258249
this.track(mt);
259250
}
260251

@@ -289,13 +280,8 @@ public void trackException(Exception exception, Map<String, String> properties,
289280

290281
ExceptionTelemetry et = new ExceptionTelemetry(exception);
291282

292-
if (properties != null && properties.size() > 0) {
293-
MapUtil.copy(properties, et.getContext().getProperties());
294-
}
295-
296-
if (metrics != null && metrics.size() > 0) {
297-
MapUtil.copy(metrics, et.getMetrics());
298-
}
283+
MapUtil.copy(properties, et.getContext().getProperties());
284+
MapUtil.copy(metrics, et.getMetrics());
299285

300286
this.track(et);
301287
}

Diff for: core/src/main/java/com/microsoft/applicationinsights/internal/util/MapUtil.java

+6-4
Original file line numberDiff line numberDiff line change
@@ -38,18 +38,20 @@ public class MapUtil
3838
/**
3939
* Copies entries from the source map to the target map, overwrites any values in target.
4040
* Filters out null values if target is a {@link ConcurrentHashMap}.
41-
* @param source the source map. Cannot be null.
41+
* @param source the source map. If null or empty, this is a nop.
4242
* @param target the target map. Cannot be null.
4343
* @param <Value> The type of the values in both maps
4444
* @throws IllegalArgumentException if either {@code source} or {@code target} are null.
4545
*/
4646
public static <Value> void copy(Map<String, Value> source, Map<String, Value> target) {
47-
if (source == null) {
48-
throw new IllegalArgumentException("source must not be null");
49-
}
5047
if (target == null) {
5148
throw new IllegalArgumentException("target must not be null");
5249
}
50+
51+
if (source == null || source.isEmpty()) {
52+
return;
53+
}
54+
5355
for (Map.Entry<String,Value> entry : source.entrySet()) {
5456
String key = entry.getKey();
5557
if (Strings.isNullOrEmpty(key)) {

Diff for: core/src/main/java/com/microsoft/applicationinsights/telemetry/TelemetryContext.java

+2-6
Original file line numberDiff line numberDiff line change
@@ -200,12 +200,8 @@ public void initialize(TelemetryContext source) {
200200
if (Strings.isNullOrEmpty(this.instrumentationKey) && !Strings.isNullOrEmpty(source.getInstrumentationKey()))
201201
setInstrumentationKey(source.getInstrumentationKey());
202202

203-
if (source.tags != null && source.tags.size() > 0) {
204-
MapUtil.copy(source.tags, this.tags);
205-
}
206-
if (source.properties != null && source.properties.size() > 0) {
207-
MapUtil.copy(source.properties, this.properties);
208-
}
203+
MapUtil.copy(source.tags, this.tags);
204+
MapUtil.copy(source.properties, this.properties);
209205
}
210206

211207
public InternalContext getInternal() {

Diff for: core/src/test/java/com/microsoft/applicationinsights/internal/util/MapUtilTest.java

+33-4
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,43 @@
11
package com.microsoft.applicationinsights.internal.util;
22

3-
import org.junit.Assert;
3+
import org.junit.*;
4+
import org.junit.rules.ExpectedException;
5+
46
import java.util.HashMap;
57
import java.util.Map;
68
import java.util.concurrent.ConcurrentHashMap;
79

8-
import org.junit.Test;
10+
import static org.junit.Assert.*;
11+
import static org.mockito.Mockito.*;
912

1013
public class MapUtilTest {
1114

15+
@Rule
16+
public ExpectedException expected = ExpectedException.none();
17+
18+
@Test
19+
public void targetCannotBeNullInCopy() {
20+
expected.expect(IllegalArgumentException.class);
21+
MapUtil.copy(new HashMap<String, String>(), null);
22+
}
23+
24+
@Test
25+
public void copyIsNoOpIfSourceIsNullOrEmpty() {
26+
Map<String, String> source = mock(Map.class);
27+
Map<String, String> target = mock(Map.class);
28+
when(source.size()).thenReturn(0);
29+
30+
MapUtil.copy(source, target);
31+
// nothing should be put into target
32+
verify(target, never()).put(anyString(), anyString());
33+
verify(source, never()).get(any());
34+
35+
reset(target);
36+
37+
MapUtil.copy(null, target);
38+
verify(target, never()).put(anyString(), anyString());
39+
}
40+
1241
@Test
1342
public void testCopyIntoHashMap() {
1443
Map<String, String> source = new HashMap<>();
@@ -18,7 +47,7 @@ public void testCopyIntoHashMap() {
1847
source.put("key2", null);
1948

2049
MapUtil.copy(source, target);
21-
Assert.assertEquals(2, target.size());
50+
assertEquals(2, target.size());
2251
}
2352

2453
@Test
@@ -30,6 +59,6 @@ public void testCopyIntoConcurrentHashMap() {
3059
source.put("key2", null);
3160

3261
MapUtil.copy(source, target);
33-
Assert.assertEquals(1, target.size());
62+
assertEquals(1, target.size());
3463
}
3564
}

0 commit comments

Comments
 (0)