From bf0b515889779ac9feb6a5205b86e97549e6aab0 Mon Sep 17 00:00:00 2001 From: asofold Date: Thu, 17 Jul 2014 14:40:33 +0200 Subject: [PATCH] Add removeAll. Considering to use this map for otherwise fully synchronized maps, e.g. with the chat checks. --- .../utilities/ds/LinkedHashMapCOW.java | 23 ++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/NCPCommons/src/main/java/fr/neatmonster/nocheatplus/utilities/ds/LinkedHashMapCOW.java b/NCPCommons/src/main/java/fr/neatmonster/nocheatplus/utilities/ds/LinkedHashMapCOW.java index 7dec6ab7..e76beb31 100644 --- a/NCPCommons/src/main/java/fr/neatmonster/nocheatplus/utilities/ds/LinkedHashMapCOW.java +++ b/NCPCommons/src/main/java/fr/neatmonster/nocheatplus/utilities/ds/LinkedHashMapCOW.java @@ -110,7 +110,7 @@ public class LinkedHashMapCOW implements Map { } @Override - public V put(K key, V value) { + public V put(final K key, final V value) { final V out; synchronized (this) { final LinkedHashMap newMap = copyMap(); @@ -121,7 +121,7 @@ public class LinkedHashMapCOW implements Map { } @Override - public void putAll(Map m) { + public void putAll(final Map m) { synchronized (this) { final LinkedHashMap newMap = copyMap(); newMap.putAll(m); @@ -130,7 +130,7 @@ public class LinkedHashMapCOW implements Map { } @Override - public V remove(Object key) { + public V remove(final Object key) { final V out; synchronized (this) { final LinkedHashMap newMap = copyMap(); @@ -139,6 +139,23 @@ public class LinkedHashMapCOW implements Map { } return out; } + + /** + * Remove all given keys.
+ * Not the most efficient implementation, copying the map and then removing + * keys, but still better than iterating remove(key). + * + * @param keys + */ + public void removeAll(final Collection keys) { + synchronized (this) { + final LinkedHashMap newMap = copyMap(); + for (final K key : keys) { + newMap.remove(key); + } + this.map = newMap; + } + } @Override public int size() {