Skip to content

Commit

Permalink
Add method for synchronized weak map creation
Browse files Browse the repository at this point in the history
DEVSIX-7608
Eugene Bochilo committed Jul 17, 2023

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
1 parent af9e26d commit ca5ef56
Showing 3 changed files with 279 additions and 0 deletions.
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();
}
}
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());
}
}
4 changes: 4 additions & 0 deletions sharpenConfiguration.xml
Original file line number Diff line number Diff line change
@@ -37,6 +37,10 @@
<fileset reason="DEVSIX-3221 - The way of working with processes in c# is very different from Java. So it can'not be ported manually">
<file path="com/itextpdf/commons/utils/SystemUtil.java"/>
</fileset>
<fileset reason="Dictionaries are different in java and .net therefor manual porting is required">
<file path="com/itextpdf/commons/datastructures/ConcurrentWeakMap.java" />
<file path="com/itextpdf/commons/datastructures/ConcurrentWeakMapTest.java" />
</fileset>
<fileset reason="SystemUtil is a manual class, and the methods of this class can have input and output other than java despite similar names.
Therefore, the test for this class also cannot be ported automatically">
<file path="com/itextpdf/commons/utils/SystemUtilTest.java"/>

0 comments on commit ca5ef56

Please sign in to comment.