-
Notifications
You must be signed in to change notification settings - Fork 459
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add method for synchronized weak map creation
DEVSIX-7608
Eugene Bochilo
committed
Jul 17, 2023
1 parent
af9e26d
commit ca5ef56
Showing
3 changed files
with
279 additions
and
0 deletions.
There are no files selected for viewing
113 changes: 113 additions & 0 deletions
113
commons/src/main/java/com/itextpdf/commons/datastructures/ConcurrentWeakMap.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
package com.itextpdf.commons.datastructures; | ||
|
||
import java.util.Collection; | ||
import java.util.Collections; | ||
import java.util.Map; | ||
import java.util.Set; | ||
import java.util.WeakHashMap; | ||
|
||
/** | ||
* Concurrent weak hash map implementation. | ||
* | ||
* @param <K> type of the keys | ||
* @param <V> type of the values | ||
*/ | ||
public class ConcurrentWeakMap<K,V> implements Map<K, V> { | ||
private final Map<K, V> map = Collections.synchronizedMap(new WeakHashMap<>()); | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
@Override | ||
public int size() { | ||
return map.size(); | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
@Override | ||
public boolean isEmpty() { | ||
return map.isEmpty(); | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
@Override | ||
public boolean containsKey(Object key) { | ||
return map.containsKey(key); | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
@Override | ||
public boolean containsValue(Object value) { | ||
return map.containsValue(value); | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
@Override | ||
public V get(Object key) { | ||
return map.get(key); | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
@Override | ||
public V put(K key, V value) { | ||
return map.put(key, value); | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
@Override | ||
public V remove(Object key) { | ||
return map.remove(key); | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
@Override | ||
public void putAll(Map<? extends K, ? extends V> m) { | ||
map.putAll(m); | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
@Override | ||
public void clear() { | ||
map.clear(); | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
@Override | ||
public Set<K> keySet() { | ||
return map.keySet(); | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
@Override | ||
public Collection<V> values() { | ||
return map.values(); | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
@Override | ||
public Set<Entry<K, V>> entrySet() { | ||
return map.entrySet(); | ||
} | ||
} |
162 changes: 162 additions & 0 deletions
162
commons/src/test/java/com/itextpdf/commons/datastructures/ConcurrentWeakMapTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,162 @@ | ||
package com.itextpdf.commons.datastructures; | ||
|
||
import com.itextpdf.test.ExtendedITextTest; | ||
import com.itextpdf.test.annotations.type.UnitTest; | ||
|
||
import java.util.Collection; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import org.junit.Assert; | ||
import org.junit.Test; | ||
import org.junit.experimental.categories.Category; | ||
|
||
@Category(UnitTest.class) | ||
public class ConcurrentWeakMapTest extends ExtendedITextTest { | ||
@Test | ||
public void sizeTest() { | ||
ConcurrentWeakMap<Integer, Integer> map = new ConcurrentWeakMap<>(); | ||
map.put(5, 6); | ||
map.put(3, 0); | ||
map.put(6, 2); | ||
map.put(5, 2); | ||
Assert.assertEquals(3, map.size()); | ||
} | ||
|
||
@Test | ||
public void isEmptyMapNotEmptyTest() { | ||
ConcurrentWeakMap<Integer, Integer> map = new ConcurrentWeakMap<>(); | ||
map.put(5, 6); | ||
Assert.assertFalse(map.isEmpty()); | ||
} | ||
|
||
@Test | ||
public void isEmptyMapEmptyTest() { | ||
ConcurrentWeakMap<Integer, Integer> map = new ConcurrentWeakMap<>(); | ||
Assert.assertTrue(map.isEmpty()); | ||
} | ||
|
||
@Test | ||
public void containsKeyTrueTest() { | ||
ConcurrentWeakMap<Integer, Integer> map = new ConcurrentWeakMap<>(); | ||
map.put(5, 6); | ||
Assert.assertTrue(map.containsKey(5)); | ||
} | ||
|
||
@Test | ||
public void containsKeyFalseTest() { | ||
ConcurrentWeakMap<Integer, Integer> map = new ConcurrentWeakMap<>(); | ||
map.put(5, 6); | ||
Assert.assertFalse(map.containsKey(6)); | ||
} | ||
|
||
@Test | ||
public void containsValueTrueTest() { | ||
ConcurrentWeakMap<Integer, Integer> map = new ConcurrentWeakMap<>(); | ||
map.put(5, 6); | ||
Assert.assertTrue(map.containsValue(6)); | ||
} | ||
|
||
@Test | ||
public void containsValueFalseTest() { | ||
ConcurrentWeakMap<Integer, Integer> map = new ConcurrentWeakMap<>(); | ||
map.put(5, 6); | ||
Assert.assertFalse(map.containsValue(5)); | ||
} | ||
|
||
@Test | ||
public void getTest() { | ||
ConcurrentWeakMap<Integer, Integer> map = new ConcurrentWeakMap<>(); | ||
map.put(5, 6); | ||
Assert.assertEquals(6, (int) map.get(5)); | ||
} | ||
|
||
@Test | ||
public void putTest() { | ||
ConcurrentWeakMap<Integer, Integer> map = new ConcurrentWeakMap<>(); | ||
map.put(5, 6); | ||
Assert.assertEquals(6, (int) map.put(5, 10)); | ||
} | ||
|
||
@Test | ||
public void removeTest() { | ||
ConcurrentWeakMap<Integer, Integer> map = new ConcurrentWeakMap<>(); | ||
map.put(5, 6); | ||
Assert.assertEquals(6, (int) map.remove(5)); | ||
} | ||
|
||
@Test | ||
public void putAllTest() { | ||
ConcurrentWeakMap<Integer, Integer> map = new ConcurrentWeakMap<>(); | ||
map.put(5, 6); | ||
|
||
Map<Integer, Integer> anotherMap = new HashMap<>(); | ||
anotherMap.put(5, 10); | ||
anotherMap.put(4, 3); | ||
anotherMap.put(3, 7); | ||
|
||
map.putAll(anotherMap); | ||
|
||
Assert.assertEquals(10, (int) map.get(5)); | ||
Assert.assertEquals(3, map.size()); | ||
} | ||
|
||
@Test | ||
public void clearTest() { | ||
ConcurrentWeakMap<Integer, Integer> map = new ConcurrentWeakMap<>(); | ||
map.put(5, 6); | ||
map.put(3, 5); | ||
map.put(2, 8); | ||
|
||
map.clear(); | ||
|
||
Assert.assertEquals(0, map.size()); | ||
} | ||
|
||
@Test | ||
public void keySetTest() { | ||
ConcurrentWeakMap<Integer, Integer> map = new ConcurrentWeakMap<>(); | ||
map.put(5, 6); | ||
|
||
Map<Integer, Integer> anotherMap = new HashMap<>(); | ||
anotherMap.put(5, 10); | ||
anotherMap.put(4, 3); | ||
anotherMap.put(3, 7); | ||
|
||
map.putAll(anotherMap); | ||
|
||
Assert.assertEquals(anotherMap.keySet(), map.keySet()); | ||
} | ||
|
||
@Test | ||
public void valuesTest() { | ||
ConcurrentWeakMap<Integer, Integer> map = new ConcurrentWeakMap<>(); | ||
map.put(5, 6); | ||
|
||
Map<Integer, Integer> anotherMap = new HashMap<>(); | ||
anotherMap.put(5, 10); | ||
anotherMap.put(4, 3); | ||
anotherMap.put(3, 7); | ||
|
||
map.putAll(anotherMap); | ||
|
||
Collection<Integer> values = map.values(); | ||
Assert.assertEquals(3, values.size()); | ||
Assert.assertTrue(values.contains(10)); | ||
Assert.assertFalse(values.contains(6)); | ||
} | ||
|
||
@Test | ||
public void entrySetTest() { | ||
ConcurrentWeakMap<Integer, Integer> map = new ConcurrentWeakMap<>(); | ||
map.put(5, 6); | ||
|
||
Map<Integer, Integer> anotherMap = new HashMap<>(); | ||
anotherMap.put(5, 10); | ||
anotherMap.put(4, 3); | ||
anotherMap.put(3, 7); | ||
|
||
map.putAll(anotherMap); | ||
|
||
Assert.assertEquals(anotherMap.entrySet(), map.entrySet()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters