Skip to main content
summaryrefslogtreecommitdiffstats
path: root/common
diff options
context:
space:
mode:
authorBrian Vosburgh2016-01-28 18:59:07 +0000
committerBrian Vosburgh2017-05-18 22:35:51 +0000
commit2482daff01310995722fd5abe9452a347e673fb6 (patch)
tree27f523e051bb6c62143fccb6df8843e2d8d4bf4a /common
parent992195860fb65621aabfd088ca9ef1f2485033d0 (diff)
downloadwebtools.dali-2482daff01310995722fd5abe9452a347e673fb6.tar.gz
webtools.dali-2482daff01310995722fd5abe9452a347e673fb6.tar.xz
webtools.dali-2482daff01310995722fd5abe9452a347e673fb6.zip
add ReadWriteLockMapWrapper
Diffstat (limited to 'common')
-rw-r--r--common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/collection/MapTools.java59
-rw-r--r--common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/collection/ReadWriteLockMapWrapper.java175
-rw-r--r--common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/collection/JptCommonUtilityCollectionTests.java3
-rw-r--r--common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/collection/ReadWriteLockMapWrapperTests.java259
4 files changed, 491 insertions, 5 deletions
diff --git a/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/collection/MapTools.java b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/collection/MapTools.java
index 109b38bf02..2c56c345fd 100644
--- a/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/collection/MapTools.java
+++ b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/collection/MapTools.java
@@ -1,5 +1,5 @@
/*******************************************************************************
- * Copyright (c) 2013, 2015 Oracle. All rights reserved.
+ * Copyright (c) 2013, 2016 Oracle. All rights reserved.
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v1.0, which accompanies this distribution
* and is available at http://www.eclipse.org/legal/epl-v10.html.
@@ -14,6 +14,9 @@ import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
+import java.util.concurrent.locks.Lock;
+import java.util.concurrent.locks.ReadWriteLock;
+import java.util.concurrent.locks.ReentrantReadWriteLock;
import org.eclipse.jpt.common.utility.factory.Factory;
import org.eclipse.jpt.common.utility.internal.ClassTools;
import org.eclipse.jpt.common.utility.internal.ObjectTools;
@@ -210,6 +213,7 @@ public final class MapTools {
* passing the values to the specified key transformer.
* @see Map#putAll(Map)
*/
+ @SafeVarargs
public static <K, V, E extends V> void addAll(Map<K, V> map, Transformer<? super E, ? extends K> keyTransformer, E... values) {
for (E value : values) {
add(map, value, keyTransformer);
@@ -244,6 +248,7 @@ public final class MapTools {
* passing the elements to the specified key transformer.
* @see Map#putAll(Map)
*/
+ @SafeVarargs
public static <K, V, E> void addAll(Map<K, V> map, Transformer<? super E, ? extends K> keyTransformer, Transformer<? super E, ? extends V> valueTransformer, E... elements) {
for (E element : elements) {
add(map, element, keyTransformer, valueTransformer);
@@ -492,7 +497,7 @@ public final class MapTools {
* will remain in the new map.
*/
public static <K, V> HashMap<K, V> invert(Map<? extends V, ? extends K> map) {
- HashMap<K, V> result = new HashMap<K, V>((int) (map.size() / 0.75));
+ HashMap<K, V> result = new HashMap<>((int) (map.size() / 0.75));
for (Map.Entry<? extends V, ? extends K> entry : map.entrySet()) {
result.put(entry.getValue(), entry.getKey());
}
@@ -507,7 +512,7 @@ public final class MapTools {
* values of the specified map.
*/
public static <K, V> HashMap<K, V> filter(Map<? extends K, ? extends V> map, Predicate<? super V> filter) {
- HashMap<K, V> result = new HashMap<K, V>((int) (map.size() / 0.75));
+ HashMap<K, V> result = new HashMap<>((int) (map.size() / 0.75));
for (Map.Entry<? extends K, ? extends V> entry : map.entrySet()) {
V value = entry.getValue();
if (filter.evaluate(value)) {
@@ -525,7 +530,7 @@ public final class MapTools {
* values of the specified map.
*/
public static <K, V1, V2> HashMap<K, V2> transform(Map<? extends K, V1> map, Transformer<? super V1, ? extends V2> transformer) {
- HashMap<K, V2> result = new HashMap<K, V2>((int) (map.size() / 0.75));
+ HashMap<K, V2> result = new HashMap<>((int) (map.size() / 0.75));
for (Map.Entry<? extends K, ? extends V1> entry : map.entrySet()) {
result.put(entry.getKey(), transformer.transform(entry.getValue()));
}
@@ -533,6 +538,52 @@ public final class MapTools {
}
+ // ********** read/write lock wrapper **********
+
+ /**
+ * Return a wrapper of the specified map that uses an nonfair
+ * reentrant read/write lock to control access to the map.
+ * The wrapper provides a simplified
+ * "map" interface (i.e. it does not implement the "view" methods).
+ * The wrapper uses blocking calls when acquiring the appropriate lock
+ * (see {@link Lock#lock()}).
+ * @param <K> the type of keys maintained by the map
+ * @param <V> the type of values maintained by the map
+ */
+ public static <K, V> ReadWriteLockMapWrapper<K, V> readWriteLockWrapper(Map<K, V> map) {
+ return readWriteLockWrapper(map, false);
+ }
+
+ /**
+ * Return a wrapper of the specified map that uses a
+ * reentrant read/write lock with the specified fairness policy
+ * to control access to the map.
+ * The wrapper provides a simplified
+ * "map" interface (i.e. it does not implement the "view" methods).
+ * The wrapper uses blocking calls when acquiring the appropriate lock
+ * (see {@link Lock#lock()}).
+ * @param <K> the type of keys maintained by the map
+ * @param <V> the type of values maintained by the map
+ */
+ public static <K, V> ReadWriteLockMapWrapper<K, V> readWriteLockWrapper(Map<K, V> map, boolean fair) {
+ return readWriteLockWrapper(map, new ReentrantReadWriteLock(fair));
+ }
+
+ /**
+ * Return a wrapper of the specified map that uses the specified
+ * read/write lock to control access to the map.
+ * The wrapper provides a simplified
+ * "map" interface (i.e. it does not implement the "view" methods).
+ * The wrapper uses blocking calls when acquiring the appropriate lock
+ * (see {@link Lock#lock()}).
+ * @param <K> the type of keys maintained by the map
+ * @param <V> the type of values maintained by the map
+ */
+ public static <K, V> ReadWriteLockMapWrapper<K, V> readWriteLockWrapper(Map<K, V> map, ReadWriteLock lock) {
+ return new ReadWriteLockMapWrapper<>(map, lock);
+ }
+
+
// ********** constructor **********
/**
diff --git a/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/collection/ReadWriteLockMapWrapper.java b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/collection/ReadWriteLockMapWrapper.java
new file mode 100644
index 0000000000..92a22ef15c
--- /dev/null
+++ b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/collection/ReadWriteLockMapWrapper.java
@@ -0,0 +1,175 @@
+/*******************************************************************************
+ * Copyright (c) 2016 Oracle. All rights reserved.
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0, which accompanies this distribution
+ * and is available at http://www.eclipse.org/legal/epl-v10.html.
+ *
+ * Contributors:
+ * Oracle - initial API and implementation
+ ******************************************************************************/
+package org.eclipse.jpt.common.utility.internal.collection;
+
+import java.util.Map;
+import java.util.concurrent.locks.Lock;
+import java.util.concurrent.locks.ReadWriteLock;
+
+/**
+ * A {@link Map} wrapper that uses a read/write lock
+ * to control concurrent access to the map. The wrapper provides a simplified
+ * "map" interface (i.e. it does not implement the "view" methods).
+ * The wrapper uses blocking calls when acquiring the appropriate lock
+ * (see {@link Lock#lock()}).
+ *
+ * @param <K> the type of keys maintained by the map
+ * @param <V> the type of values maintained by the map
+ */
+public class ReadWriteLockMapWrapper<K, V> {
+ private final Map<K, V> map;
+ @SuppressWarnings("unused")
+ private final ReadWriteLock lock;
+ private final Lock readLock;
+ private final Lock writeLock;
+
+
+ /**
+ * Construct a wrapper for the specified map
+ * that uses the specified read/write lock.
+ */
+ public ReadWriteLockMapWrapper(Map<K, V> map, ReadWriteLock lock) {
+ super();
+ if (map == null) {
+ throw new NullPointerException();
+ }
+ this.map = map;
+ if (lock == null) {
+ throw new NullPointerException();
+ }
+ this.lock = lock;
+ this.readLock = lock.readLock();
+ this.writeLock = lock.writeLock();
+ }
+
+
+ // ********** read-only methods **********
+
+ /**
+ * @see Map#size()
+ */
+ public int size() {
+ this.readLock.lock();
+ try {
+ return this.map.size();
+ } finally {
+ this.readLock.unlock();
+ }
+ }
+
+ /**
+ * @see Map#isEmpty()
+ */
+ public boolean isEmpty() {
+ this.readLock.lock();
+ try {
+ return this.map.isEmpty();
+ } finally {
+ this.readLock.unlock();
+ }
+ }
+
+ /**
+ * @see Map#containsValue(Object)
+ */
+ public boolean containsValue(Object value) {
+ this.readLock.lock();
+ try {
+ return this.map.containsValue(value);
+ } finally {
+ this.readLock.unlock();
+ }
+ }
+
+ /**
+ * @see Map#containsKey(Object)
+ */
+ public boolean containsKey(Object key) {
+ this.readLock.lock();
+ try {
+ return this.map.containsKey(key);
+ } finally {
+ this.readLock.unlock();
+ }
+ }
+
+ /**
+ * @see Map#get(Object)
+ */
+ public V get(Object key) {
+ this.readLock.lock();
+ try {
+ return this.map.get(key);
+ } finally {
+ this.readLock.unlock();
+ }
+ }
+
+ @Override
+ public String toString() {
+ this.readLock.lock();
+ try {
+ return this.map.toString();
+ } finally {
+ this.readLock.unlock();
+ }
+ }
+
+
+ // ********** write methods **********
+
+ /**
+ * @see Map#put(Object, Object)
+ */
+ public V put(K key, V value) {
+ this.writeLock.lock();
+ try {
+ return this.map.put(key, value);
+ } finally {
+ this.writeLock.unlock();
+ }
+ }
+
+ /**
+ * @see Map#remove(Object)
+ */
+ public V remove(Object key) {
+ this.writeLock.lock();
+ try {
+ return this.map.remove(key);
+ } finally {
+ this.writeLock.unlock();
+ }
+ }
+
+ /**
+ * @see Map#putAll(Map)
+ */
+ public void putAll(Map<? extends K, ? extends V> m) {
+ this.writeLock.lock();
+ try {
+ this.map.putAll(m);
+ } finally {
+ this.writeLock.unlock();
+ }
+ }
+
+ /**
+ * @see Map#clear()
+ */
+ public void clear() {
+ this.writeLock.lock();
+ try {
+ this.map.clear();
+ } finally {
+ this.writeLock.unlock();
+ }
+ }
+}
diff --git a/common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/collection/JptCommonUtilityCollectionTests.java b/common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/collection/JptCommonUtilityCollectionTests.java
index 77363e4645..f968a3dec2 100644
--- a/common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/collection/JptCommonUtilityCollectionTests.java
+++ b/common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/collection/JptCommonUtilityCollectionTests.java
@@ -1,5 +1,5 @@
/*******************************************************************************
- * Copyright (c) 2005, 2015 Oracle. All rights reserved.
+ * Copyright (c) 2005, 2016 Oracle. All rights reserved.
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v1.0, which accompanies this distribution
* and is available at http://www.eclipse.org/legal/epl-v10.html.
@@ -29,6 +29,7 @@ public class JptCommonUtilityCollectionTests {
suite.addTestSuite(MapToolsTests.class);
suite.addTestSuite(NullElementListTests.class);
suite.addTestSuite(NullListTests.class);
+ suite.addTestSuite(ReadWriteLockMapWrapperTests.class);
suite.addTestSuite(RepeatingElementListTests.class);
suite.addTestSuite(SynchronizedBagTests.class);
suite.addTestSuite(TightMapTests.class);
diff --git a/common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/collection/ReadWriteLockMapWrapperTests.java b/common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/collection/ReadWriteLockMapWrapperTests.java
new file mode 100644
index 0000000000..0c8bdbc750
--- /dev/null
+++ b/common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/collection/ReadWriteLockMapWrapperTests.java
@@ -0,0 +1,259 @@
+/*******************************************************************************
+ * Copyright (c) 2016 Oracle. All rights reserved.
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0, which accompanies this distribution
+ * and is available at http://www.eclipse.org/legal/epl-v10.html.
+ *
+ * Contributors:
+ * Oracle - initial API and implementation
+ ******************************************************************************/
+package org.eclipse.jpt.common.utility.tests.internal.collection;
+
+import java.util.HashMap;
+import java.util.Map;
+import org.eclipse.jpt.common.utility.internal.collection.ReadWriteLockMapWrapper;
+import org.eclipse.jpt.common.utility.internal.collection.MapTools;
+import junit.framework.TestCase;
+
+@SuppressWarnings("nls")
+public class ReadWriteLockMapWrapperTests
+ extends TestCase
+{
+ private Map<String, String> map;
+ private ReadWriteLockMapWrapper<String, String> wrapper;
+
+ public ReadWriteLockMapWrapperTests(String name) {
+ super(name);
+ }
+
+ @Override
+ protected void setUp() throws Exception {
+ super.setUp();
+ this.map = this.buildMap();
+ this.wrapper = this.buildWrapper(this.map);
+ }
+
+ protected Map<String, String> buildMap() {
+ return new HashMap<>();
+ }
+
+ protected ReadWriteLockMapWrapper<String, String> buildWrapper(Map<String, String> m) {
+ return MapTools.readWriteLockWrapper(m);
+ }
+
+ public void testSize() throws Exception {
+ assertEquals(this.map.size(), this.wrapper.size());
+ this.map.put("foo", "foo value 1");
+ assertEquals(this.map.size(), this.wrapper.size());
+ this.map.put("foo", "foo value 2");
+ assertEquals(this.map.size(), this.wrapper.size());
+ this.map.put("bar", "bar value");
+ assertEquals(this.map.size(), this.wrapper.size());
+ this.map.put("baz", "baz value");
+ assertEquals(this.map.size(), this.wrapper.size());
+ }
+
+ public void testIsEmpty() throws Exception {
+ assertEquals(this.map.isEmpty(), this.wrapper.isEmpty());
+ this.map.put("foo", "foo value 1");
+ assertEquals(this.map.isEmpty(), this.wrapper.isEmpty());
+ this.map.put("foo", "foo value 2");
+ assertEquals(this.map.isEmpty(), this.wrapper.isEmpty());
+ this.map.put("bar", "bar value");
+ assertEquals(this.map.isEmpty(), this.wrapper.isEmpty());
+ this.map.put("baz", "baz value");
+ assertEquals(this.map.isEmpty(), this.wrapper.isEmpty());
+ }
+
+ public void testContainsValue() throws Exception {
+ assertEquals(this.map.containsValue("foo value 1"), this.wrapper.containsValue("foo value 1"));
+ assertEquals(this.map.containsValue("foo value 2"), this.wrapper.containsValue("foo value 2"));
+
+ this.map.put("foo", "foo value 1");
+ assertEquals(this.map.containsValue("foo value 1"), this.wrapper.containsValue("foo value 1"));
+ assertEquals(this.map.containsValue("foo value 2"), this.wrapper.containsValue("foo value 2"));
+
+ this.map.put("foo", "foo value 2");
+ assertEquals(this.map.containsValue("foo value 1"), this.wrapper.containsValue("foo value 1"));
+ assertEquals(this.map.containsValue("foo value 2"), this.wrapper.containsValue("foo value 2"));
+
+ this.map.put("bar", "bar value");
+ assertEquals(this.map.containsValue("foo value 1"), this.wrapper.containsValue("foo value 1"));
+ assertEquals(this.map.containsValue("foo value 2"), this.wrapper.containsValue("foo value 2"));
+ assertEquals(this.map.containsValue("bar value"), this.wrapper.containsValue("bar value"));
+
+ this.map.put("baz", "baz value");
+ assertEquals(this.map.containsValue("foo value 2"), this.wrapper.containsValue("foo value 2"));
+ assertEquals(this.map.containsValue("foo value 1"), this.wrapper.containsValue("foo value 1"));
+ assertEquals(this.map.containsValue("bar value"), this.wrapper.containsValue("bar value"));
+ assertEquals(this.map.containsValue("baz value"), this.wrapper.containsValue("baz value"));
+ }
+
+ public void testContainsKey() throws Exception {
+ assertEquals(this.map.containsKey("foo"), this.wrapper.containsKey("foo"));
+ assertEquals(this.map.containsKey("bar"), this.wrapper.containsKey("bar"));
+
+ this.map.put("foo", "foo value 1");
+ assertEquals(this.map.containsKey("foo"), this.wrapper.containsKey("foo"));
+ assertEquals(this.map.containsKey("bar"), this.wrapper.containsKey("bar"));
+
+ this.map.put("foo", "foo value 2");
+ assertEquals(this.map.containsKey("foo"), this.wrapper.containsKey("foo"));
+ assertEquals(this.map.containsKey("bar"), this.wrapper.containsKey("bar"));
+
+ this.map.put("bar", "bar value");
+ assertEquals(this.map.containsKey("foo"), this.wrapper.containsKey("foo"));
+ assertEquals(this.map.containsKey("bar"), this.wrapper.containsKey("bar"));
+ assertEquals(this.map.containsKey("baz"), this.wrapper.containsKey("baz"));
+
+ this.map.put("baz", "baz value");
+ assertEquals(this.map.containsKey("foo"), this.wrapper.containsKey("foo"));
+ assertEquals(this.map.containsKey("bar"), this.wrapper.containsKey("bar"));
+ assertEquals(this.map.containsKey("baz"), this.wrapper.containsKey("baz"));
+ }
+
+ public void testGet() throws Exception {
+ assertEquals(this.map.get("foo"), this.wrapper.get("foo"));
+ assertEquals(this.map.get("bar"), this.wrapper.get("bar"));
+
+ this.map.put("foo", "foo value 1");
+ assertEquals(this.map.get("foo"), this.wrapper.get("foo"));
+ assertEquals(this.map.get("bar"), this.wrapper.get("bar"));
+
+ this.map.put("foo", "foo value 2");
+ assertEquals(this.map.get("foo"), this.wrapper.get("foo"));
+ assertEquals(this.map.get("bar"), this.wrapper.get("bar"));
+
+ this.map.put("bar", "bar value");
+ assertEquals(this.map.get("foo"), this.wrapper.get("foo"));
+ assertEquals(this.map.get("bar"), this.wrapper.get("bar"));
+ assertEquals(this.map.get("baz"), this.wrapper.get("baz"));
+
+ this.map.put("baz", "baz value");
+ assertEquals(this.map.get("foo"), this.wrapper.get("foo"));
+ assertEquals(this.map.get("bar"), this.wrapper.get("bar"));
+ assertEquals(this.map.get("baz"), this.wrapper.get("baz"));
+ }
+
+ public void testToString() throws Exception {
+ assertEquals(this.map.toString(), this.wrapper.toString());
+
+ this.map.put("foo", "foo value 1");
+ assertEquals(this.map.toString(), this.wrapper.toString());
+
+ this.map.put("foo", "foo value 2");
+ assertEquals(this.map.toString(), this.wrapper.toString());
+
+ this.map.put("bar", "bar value");
+ assertEquals(this.map.toString(), this.wrapper.toString());
+
+ this.map.put("baz", "baz value");
+ assertEquals(this.map.toString(), this.wrapper.toString());
+ }
+
+ public void testPut() throws Exception {
+ assertEquals(this.map.get("foo"), this.wrapper.get("foo"));
+ assertEquals(this.map.get("bar"), this.wrapper.get("bar"));
+
+ assertEquals(null, this.wrapper.put("foo", "foo value 1"));
+ assertEquals(this.map.get("foo"), this.wrapper.get("foo"));
+ assertEquals(this.map.get("bar"), this.wrapper.get("bar"));
+
+ assertEquals("foo value 1", this.wrapper.put("foo", "foo value 2"));
+ assertEquals(this.map.get("foo"), this.wrapper.get("foo"));
+ assertEquals(this.map.get("bar"), this.wrapper.get("bar"));
+
+ assertEquals(null, this.wrapper.put("bar", "bar value"));
+ assertEquals(this.map.get("foo"), this.wrapper.get("foo"));
+ assertEquals(this.map.get("bar"), this.wrapper.get("bar"));
+ assertEquals(this.map.get("baz"), this.wrapper.get("baz"));
+
+ assertEquals(null, this.wrapper.put("baz", "baz value"));
+ assertEquals(this.map.get("foo"), this.wrapper.get("foo"));
+ assertEquals(this.map.get("bar"), this.wrapper.get("bar"));
+ assertEquals(this.map.get("baz"), this.wrapper.get("baz"));
+ }
+
+ public void testRemove() throws Exception {
+ this.wrapper.put("foo", "foo value 1");
+ this.wrapper.put("foo", "foo value 2");
+ this.wrapper.put("bar", "bar value");
+ this.wrapper.put("baz", "baz value");
+ assertEquals(this.map.get("foo"), this.wrapper.get("foo"));
+ assertEquals(this.map.get("bar"), this.wrapper.get("bar"));
+ assertEquals(this.map.get("baz"), this.wrapper.get("baz"));
+
+ assertEquals("baz value", this.wrapper.remove("baz"));
+ assertEquals(this.map.get("foo"), this.wrapper.get("foo"));
+ assertEquals(this.map.get("bar"), this.wrapper.get("bar"));
+ assertEquals(this.map.get("baz"), this.wrapper.get("baz"));
+
+ assertEquals("bar value", this.wrapper.remove("bar"));
+ assertEquals(this.map.get("foo"), this.wrapper.get("foo"));
+ assertEquals(this.map.get("bar"), this.wrapper.get("bar"));
+ assertEquals(this.map.get("baz"), this.wrapper.get("baz"));
+
+ assertEquals("foo value 2", this.wrapper.remove("foo"));
+ assertEquals(this.map.get("foo"), this.wrapper.get("foo"));
+ assertEquals(this.map.get("bar"), this.wrapper.get("bar"));
+ assertEquals(this.map.get("baz"), this.wrapper.get("baz"));
+
+ assertEquals(null, this.wrapper.remove("foo"));
+ assertEquals(this.map.get("foo"), this.wrapper.get("foo"));
+ assertEquals(this.map.get("bar"), this.wrapper.get("bar"));
+ assertEquals(this.map.get("baz"), this.wrapper.get("baz"));
+ }
+
+ public void testPutAll() throws Exception {
+ Map<String, String> map2 = new HashMap<>();
+ map2.put("foo", "foo value 1");
+ map2.put("foo", "foo value 2");
+ map2.put("bar", "bar value");
+ map2.put("baz", "baz value");
+ this.wrapper.putAll(map2);
+ assertEquals("foo value 2", this.map.get("foo"));
+ assertEquals("bar value", this.map.get("bar"));
+ assertEquals("baz value", this.map.get("baz"));
+ assertEquals(this.map.get("foo"), this.wrapper.get("foo"));
+ assertEquals(this.map.get("bar"), this.wrapper.get("bar"));
+ assertEquals(this.map.get("baz"), this.wrapper.get("baz"));
+ }
+
+ public void testClear() throws Exception {
+ this.wrapper.put("foo", "foo value 1");
+ this.wrapper.put("foo", "foo value 2");
+ this.wrapper.put("bar", "bar value");
+ this.wrapper.put("baz", "baz value");
+ assertEquals(this.map.get("foo"), this.wrapper.get("foo"));
+ assertEquals(this.map.get("bar"), this.wrapper.get("bar"));
+ assertEquals(this.map.get("baz"), this.wrapper.get("baz"));
+
+ this.wrapper.clear();
+ assertTrue(this.wrapper.isEmpty());
+ assertEquals(this.map.get("foo"), this.wrapper.get("foo"));
+ assertEquals(this.map.get("bar"), this.wrapper.get("bar"));
+ assertEquals(this.map.get("baz"), this.wrapper.get("baz"));
+ }
+
+ public void testCtor_NPE_map() throws Exception {
+ boolean exCaught = false;
+ try {
+ ReadWriteLockMapWrapper<String, String> w = MapTools.readWriteLockWrapper(null);
+ fail("bogus: " + w);
+ } catch (NullPointerException ex) {
+ exCaught = true;
+ }
+ assertTrue(exCaught);
+ }
+
+ public void testCtor_NPE_lock() throws Exception {
+ boolean exCaught = false;
+ try {
+ ReadWriteLockMapWrapper<String, String> w = MapTools.readWriteLockWrapper(this.map, null);
+ fail("bogus: " + w);
+ } catch (NullPointerException ex) {
+ exCaught = true;
+ }
+ assertTrue(exCaught);
+ }
+}

Back to the top