Skip to content

Commit ca5ef56

Browse files
author
Eugene Bochilo
committed
Add method for synchronized weak map creation
DEVSIX-7608
1 parent af9e26d commit ca5ef56

File tree

3 files changed

+279
-0
lines changed

3 files changed

+279
-0
lines changed
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
package com.itextpdf.commons.datastructures;
2+
3+
import java.util.Collection;
4+
import java.util.Collections;
5+
import java.util.Map;
6+
import java.util.Set;
7+
import java.util.WeakHashMap;
8+
9+
/**
10+
* Concurrent weak hash map implementation.
11+
*
12+
* @param <K> type of the keys
13+
* @param <V> type of the values
14+
*/
15+
public class ConcurrentWeakMap<K,V> implements Map<K, V> {
16+
private final Map<K, V> map = Collections.synchronizedMap(new WeakHashMap<>());
17+
18+
/**
19+
* {@inheritDoc}
20+
*/
21+
@Override
22+
public int size() {
23+
return map.size();
24+
}
25+
26+
/**
27+
* {@inheritDoc}
28+
*/
29+
@Override
30+
public boolean isEmpty() {
31+
return map.isEmpty();
32+
}
33+
34+
/**
35+
* {@inheritDoc}
36+
*/
37+
@Override
38+
public boolean containsKey(Object key) {
39+
return map.containsKey(key);
40+
}
41+
42+
/**
43+
* {@inheritDoc}
44+
*/
45+
@Override
46+
public boolean containsValue(Object value) {
47+
return map.containsValue(value);
48+
}
49+
50+
/**
51+
* {@inheritDoc}
52+
*/
53+
@Override
54+
public V get(Object key) {
55+
return map.get(key);
56+
}
57+
58+
/**
59+
* {@inheritDoc}
60+
*/
61+
@Override
62+
public V put(K key, V value) {
63+
return map.put(key, value);
64+
}
65+
66+
/**
67+
* {@inheritDoc}
68+
*/
69+
@Override
70+
public V remove(Object key) {
71+
return map.remove(key);
72+
}
73+
74+
/**
75+
* {@inheritDoc}
76+
*/
77+
@Override
78+
public void putAll(Map<? extends K, ? extends V> m) {
79+
map.putAll(m);
80+
}
81+
82+
/**
83+
* {@inheritDoc}
84+
*/
85+
@Override
86+
public void clear() {
87+
map.clear();
88+
}
89+
90+
/**
91+
* {@inheritDoc}
92+
*/
93+
@Override
94+
public Set<K> keySet() {
95+
return map.keySet();
96+
}
97+
98+
/**
99+
* {@inheritDoc}
100+
*/
101+
@Override
102+
public Collection<V> values() {
103+
return map.values();
104+
}
105+
106+
/**
107+
* {@inheritDoc}
108+
*/
109+
@Override
110+
public Set<Entry<K, V>> entrySet() {
111+
return map.entrySet();
112+
}
113+
}
Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
package com.itextpdf.commons.datastructures;
2+
3+
import com.itextpdf.test.ExtendedITextTest;
4+
import com.itextpdf.test.annotations.type.UnitTest;
5+
6+
import java.util.Collection;
7+
import java.util.HashMap;
8+
import java.util.Map;
9+
import org.junit.Assert;
10+
import org.junit.Test;
11+
import org.junit.experimental.categories.Category;
12+
13+
@Category(UnitTest.class)
14+
public class ConcurrentWeakMapTest extends ExtendedITextTest {
15+
@Test
16+
public void sizeTest() {
17+
ConcurrentWeakMap<Integer, Integer> map = new ConcurrentWeakMap<>();
18+
map.put(5, 6);
19+
map.put(3, 0);
20+
map.put(6, 2);
21+
map.put(5, 2);
22+
Assert.assertEquals(3, map.size());
23+
}
24+
25+
@Test
26+
public void isEmptyMapNotEmptyTest() {
27+
ConcurrentWeakMap<Integer, Integer> map = new ConcurrentWeakMap<>();
28+
map.put(5, 6);
29+
Assert.assertFalse(map.isEmpty());
30+
}
31+
32+
@Test
33+
public void isEmptyMapEmptyTest() {
34+
ConcurrentWeakMap<Integer, Integer> map = new ConcurrentWeakMap<>();
35+
Assert.assertTrue(map.isEmpty());
36+
}
37+
38+
@Test
39+
public void containsKeyTrueTest() {
40+
ConcurrentWeakMap<Integer, Integer> map = new ConcurrentWeakMap<>();
41+
map.put(5, 6);
42+
Assert.assertTrue(map.containsKey(5));
43+
}
44+
45+
@Test
46+
public void containsKeyFalseTest() {
47+
ConcurrentWeakMap<Integer, Integer> map = new ConcurrentWeakMap<>();
48+
map.put(5, 6);
49+
Assert.assertFalse(map.containsKey(6));
50+
}
51+
52+
@Test
53+
public void containsValueTrueTest() {
54+
ConcurrentWeakMap<Integer, Integer> map = new ConcurrentWeakMap<>();
55+
map.put(5, 6);
56+
Assert.assertTrue(map.containsValue(6));
57+
}
58+
59+
@Test
60+
public void containsValueFalseTest() {
61+
ConcurrentWeakMap<Integer, Integer> map = new ConcurrentWeakMap<>();
62+
map.put(5, 6);
63+
Assert.assertFalse(map.containsValue(5));
64+
}
65+
66+
@Test
67+
public void getTest() {
68+
ConcurrentWeakMap<Integer, Integer> map = new ConcurrentWeakMap<>();
69+
map.put(5, 6);
70+
Assert.assertEquals(6, (int) map.get(5));
71+
}
72+
73+
@Test
74+
public void putTest() {
75+
ConcurrentWeakMap<Integer, Integer> map = new ConcurrentWeakMap<>();
76+
map.put(5, 6);
77+
Assert.assertEquals(6, (int) map.put(5, 10));
78+
}
79+
80+
@Test
81+
public void removeTest() {
82+
ConcurrentWeakMap<Integer, Integer> map = new ConcurrentWeakMap<>();
83+
map.put(5, 6);
84+
Assert.assertEquals(6, (int) map.remove(5));
85+
}
86+
87+
@Test
88+
public void putAllTest() {
89+
ConcurrentWeakMap<Integer, Integer> map = new ConcurrentWeakMap<>();
90+
map.put(5, 6);
91+
92+
Map<Integer, Integer> anotherMap = new HashMap<>();
93+
anotherMap.put(5, 10);
94+
anotherMap.put(4, 3);
95+
anotherMap.put(3, 7);
96+
97+
map.putAll(anotherMap);
98+
99+
Assert.assertEquals(10, (int) map.get(5));
100+
Assert.assertEquals(3, map.size());
101+
}
102+
103+
@Test
104+
public void clearTest() {
105+
ConcurrentWeakMap<Integer, Integer> map = new ConcurrentWeakMap<>();
106+
map.put(5, 6);
107+
map.put(3, 5);
108+
map.put(2, 8);
109+
110+
map.clear();
111+
112+
Assert.assertEquals(0, map.size());
113+
}
114+
115+
@Test
116+
public void keySetTest() {
117+
ConcurrentWeakMap<Integer, Integer> map = new ConcurrentWeakMap<>();
118+
map.put(5, 6);
119+
120+
Map<Integer, Integer> anotherMap = new HashMap<>();
121+
anotherMap.put(5, 10);
122+
anotherMap.put(4, 3);
123+
anotherMap.put(3, 7);
124+
125+
map.putAll(anotherMap);
126+
127+
Assert.assertEquals(anotherMap.keySet(), map.keySet());
128+
}
129+
130+
@Test
131+
public void valuesTest() {
132+
ConcurrentWeakMap<Integer, Integer> map = new ConcurrentWeakMap<>();
133+
map.put(5, 6);
134+
135+
Map<Integer, Integer> anotherMap = new HashMap<>();
136+
anotherMap.put(5, 10);
137+
anotherMap.put(4, 3);
138+
anotherMap.put(3, 7);
139+
140+
map.putAll(anotherMap);
141+
142+
Collection<Integer> values = map.values();
143+
Assert.assertEquals(3, values.size());
144+
Assert.assertTrue(values.contains(10));
145+
Assert.assertFalse(values.contains(6));
146+
}
147+
148+
@Test
149+
public void entrySetTest() {
150+
ConcurrentWeakMap<Integer, Integer> map = new ConcurrentWeakMap<>();
151+
map.put(5, 6);
152+
153+
Map<Integer, Integer> anotherMap = new HashMap<>();
154+
anotherMap.put(5, 10);
155+
anotherMap.put(4, 3);
156+
anotherMap.put(3, 7);
157+
158+
map.putAll(anotherMap);
159+
160+
Assert.assertEquals(anotherMap.entrySet(), map.entrySet());
161+
}
162+
}

sharpenConfiguration.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@
3737
<fileset reason="DEVSIX-3221 - The way of working with processes in c# is very different from Java. So it can'not be ported manually">
3838
<file path="com/itextpdf/commons/utils/SystemUtil.java"/>
3939
</fileset>
40+
<fileset reason="Dictionaries are different in java and .net therefor manual porting is required">
41+
<file path="com/itextpdf/commons/datastructures/ConcurrentWeakMap.java" />
42+
<file path="com/itextpdf/commons/datastructures/ConcurrentWeakMapTest.java" />
43+
</fileset>
4044
<fileset reason="SystemUtil is a manual class, and the methods of this class can have input and output other than java despite similar names.
4145
Therefore, the test for this class also cannot be ported automatically">
4246
<file path="com/itextpdf/commons/utils/SystemUtilTest.java"/>

0 commit comments

Comments
 (0)