Author: david.lloyd(a)jboss.com
Date: 2009-11-02 17:31:39 -0500 (Mon, 02 Nov 2009)
New Revision: 5578
Modified:
remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/CopyOnWriteHashMap.java
Log:
Compact the copy, if possible
Modified:
remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/CopyOnWriteHashMap.java
===================================================================
---
remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/CopyOnWriteHashMap.java 2009-11-02
22:18:11 UTC (rev 5577)
+++
remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/CopyOnWriteHashMap.java 2009-11-02
22:31:39 UTC (rev 5578)
@@ -68,7 +68,7 @@
} else {
final Map<K, V> copy = copy(map);
copy.put(key, value);
- this.map = copy;
+ writeCopy(copy);
}
return null;
}
@@ -87,7 +87,7 @@
} else {
final Map<K, V> copy = copy(map);
copy.remove(key);
- this.map = copy;
+ writeCopy(copy);
}
return true;
}
@@ -109,7 +109,7 @@
} else {
final Map<K, V> copy = copy(map);
copy.put(key, newValue);
- this.map = copy;
+ writeCopy(copy);
}
return true;
}
@@ -131,7 +131,7 @@
} else {
final Map<K, V> copy = copy(map);
copy.put(key, value);
- this.map = copy;
+ writeCopy(copy);
}
}
return old;
@@ -173,7 +173,7 @@
} else {
final Map<K, V> copy = copy(map);
copy.put(key, value);
- this.map = copy;
+ writeCopy(copy);
}
return old;
}
@@ -190,7 +190,7 @@
} else {
final Map<K, V> copy = copy(map);
copy.remove(key);
- this.map = copy;
+ writeCopy(copy);
}
}
return old;
@@ -213,17 +213,21 @@
for (Entry<? extends K, ? extends V> entry : orig.entrySet()) {
copy.put(entry.getKey(), entry.getValue());
}
- if (copy.isEmpty()) {
- map = emptyMap();
- } else if (copy.size() == 1) {
- final Entry<K, V> entry = copy.entrySet().iterator().next();
- map = singletonMap(entry.getKey(), entry.getValue());
- } else {
- map = copy;
- }
+ writeCopy(copy);
}
}
+ private void writeCopy(final Map<K, V> copy) {
+ if (copy.isEmpty()) {
+ map = emptyMap();
+ } else if (copy.size() == 1) {
+ final Entry<K, V> entry = copy.entrySet().iterator().next();
+ map = singletonMap(entry.getKey(), entry.getValue());
+ } else {
+ map = copy;
+ }
+ }
+
public void clear() {
synchronized (writeLock) {
map = emptyMap();
Show replies by thread