Skip to content

Commit b758038

Browse files
committed
- TrackingMap.getWrappedMap() added
- TrackingMap.get() counts as accessed if the value associated to the key was null.
1 parent 8c434d5 commit b758038

4 files changed

Lines changed: 76 additions & 12 deletions

File tree

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ To include in your project:
77
<dependency>
88
<groupId>com.cedarsoftware</groupId>
99
<artifactId>java-util</artifactId>
10-
<version>1.20.1</version>
10+
<version>1.20.2</version>
1111
</dependency>
1212
```
1313
Like **java-util** and find it useful? **Tip** bitcoin: 1MeozsfDpUALpnu3DntHWXxoPJXvSAXmQA
@@ -46,6 +46,9 @@ Including in java-util:
4646
* **UrlInvocationHandler** - Use to easily communicate with RESTful JSON servers, especially ones that implement a Java interface that you have access to.
4747

4848
Version History
49+
* 1.20.2
50+
* TrackingMap changed so that .get(key) that returns null, but key was inside Map, still counts as access.
51+
* TrackingMap.getWrappedMap() added so that you can fetch the wrapped Map.
4952
* 1.20.1
5053
* TrackingMap changed so that .put() does not mark the key as accessed.
5154
* 1.20.0

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<groupId>com.cedarsoftware</groupId>
66
<artifactId>java-util</artifactId>
77
<packaging>jar</packaging>
8-
<version>1.20.1</version>
8+
<version>1.20.2</version>
99
<description>Java Utilities</description>
1010
<url>https://github.com/jdereg/java-util</url>
1111

src/main/java/com/cedarsoftware/util/TrackingMap.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,16 @@ public TrackingMap(Map<K, V> map) {
3939

4040
public V get(Object key) {
4141
V value = internalMap.get(key);
42-
if (value != null) {
43-
readKeys.add((K)key);
42+
if (value == null)
43+
{
44+
if (containsKey(key))
45+
{
46+
readKeys.add((K)key);
47+
}
48+
}
49+
else
50+
{
51+
readKeys.add((K) key);
4452
}
4553
return value;
4654
}
@@ -121,4 +129,6 @@ public void informAdditionalUsage(TrackingMap<K, V> additional) {
121129
}
122130

123131
public Set<K> keysUsed() { return readKeys; }
132+
133+
public Map getWrappedMap() { return internalMap; }
124134
}

src/test/java/com/cedarsoftware/util/TestTrackingMap.java

Lines changed: 59 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,24 @@
33
import org.junit.Test;
44
import org.junit.runner.RunWith;
55
import org.mockito.Mock;
6-
import org.powermock.api.mockito.PowerMockito;
7-
import org.powermock.core.classloader.annotations.PrepareForTest;
86
import org.powermock.modules.junit4.PowerMockRunner;
97

10-
import java.util.*;
8+
import java.util.Collection;
9+
import java.util.HashMap;
10+
import java.util.HashSet;
11+
import java.util.LinkedHashMap;
12+
import java.util.Map;
13+
import java.util.Set;
1114

1215
import static junit.framework.TestCase.assertFalse;
13-
import static org.junit.Assert.*;
14-
import static org.mockito.Matchers.any;
16+
import static org.junit.Assert.assertEquals;
17+
import static org.junit.Assert.assertNotEquals;
18+
import static org.junit.Assert.assertNotNull;
19+
import static org.junit.Assert.assertTrue;
20+
import static org.junit.Assert.fail;
1521
import static org.mockito.Mockito.verify;
1622

1723
@SuppressWarnings("ResultOfMethodCallIgnored")
18-
@PrepareForTest({TrackingMap.class, Map.class})
1924
@RunWith(PowerMockRunner.class)
2025
public class TestTrackingMap
2126
{
@@ -326,7 +331,7 @@ public void testConstructWithNull()
326331
}
327332

328333
@Test
329-
public void testPutCountsAsAccess()
334+
public void testPuDoesNotCountAsAccess()
330335
{
331336
TrackingMap trackMap = new TrackingMap(new CaseInsensitiveMap());
332337
trackMap.put("k", "kite");
@@ -335,7 +340,53 @@ public void testPutCountsAsAccess()
335340
assert trackMap.keysUsed().size() == 0;
336341

337342
trackMap.put("K", "kilo");
338-
assert trackMap.keysUsed().size() == 1;
343+
assert trackMap.keysUsed().size() == 0;
339344
assert trackMap.size() == 2;
340345
}
346+
347+
@Test
348+
public void testContainsKeyNotCoundOnNonExistentKey()
349+
{
350+
TrackingMap trackMap = new TrackingMap(new CaseInsensitiveMap());
351+
trackMap.put("y", "yankee");
352+
trackMap.put("z", "zulu");
353+
354+
trackMap.containsKey("f");
355+
356+
assert trackMap.keysUsed().size() == 0;
357+
}
358+
359+
@Test
360+
public void testGetNotCoundOnNonExistentKey()
361+
{
362+
TrackingMap trackMap = new TrackingMap(new CaseInsensitiveMap());
363+
trackMap.put("y", "yankee");
364+
trackMap.put("z", "zulu");
365+
366+
trackMap.get("f");
367+
368+
assert trackMap.keysUsed().size() == 0;
369+
}
370+
371+
@Test
372+
public void testGetOfNullValueCountsAsAccess()
373+
{
374+
TrackingMap trackMap = new TrackingMap(new CaseInsensitiveMap());
375+
376+
trackMap.put("y", null);
377+
trackMap.put("z", "zulu");
378+
379+
trackMap.get("y");
380+
381+
assert trackMap.keysUsed().size() == 1;
382+
}
383+
384+
@Test
385+
public void testFetchInternalMap()
386+
{
387+
TrackingMap trackMap = new TrackingMap(new CaseInsensitiveMap());
388+
assert trackMap.getWrappedMap() instanceof CaseInsensitiveMap;
389+
trackMap = new TrackingMap(new HashMap());
390+
assert trackMap.getWrappedMap() instanceof HashMap;
391+
}
341392
}

0 commit comments

Comments
 (0)