diff --git a/src/main/java/net/openhft/chronicle/map/ChronicleMap.java b/src/main/java/net/openhft/chronicle/map/ChronicleMap.java index 6d6935513..42ae93618 100644 --- a/src/main/java/net/openhft/chronicle/map/ChronicleMap.java +++ b/src/main/java/net/openhft/chronicle/map/ChronicleMap.java @@ -106,7 +106,8 @@ public interface ChronicleMap extends SharedHashMap, ConcurrentMapLa */ default void replaceAll(BiFunction function) { Objects.requireNonNull(function); - forEach((k, v) -> { + + final BiConsumer action = (k, v) -> { while (!replace(k, v, function.apply(k, v))) { // v changed or k is gone if ((v = get(k)) == null) { @@ -114,7 +115,9 @@ public interface ChronicleMap extends SharedHashMap, ConcurrentMapLa break; } } - }); + }; + + forEach(action); } /** diff --git a/src/main/java/net/openhft/collections/ConcurrentHashMap.java b/src/main/java/net/openhft/collections/ConcurrentHashMap.java index a600cb611..bc6ad8f2d 100644 --- a/src/main/java/net/openhft/collections/ConcurrentHashMap.java +++ b/src/main/java/net/openhft/collections/ConcurrentHashMap.java @@ -1470,7 +1470,7 @@ public class ConcurrentHashMap extends AbstractMap return (v = get(key)) == null ? defaultValue : v; } - public void forEach(BiConsumer action) { + /* public void forEach(BiConsumer action) { if (action == null) throw new NullPointerException(); Node[] t; if ((t = table) != null) { @@ -1479,9 +1479,9 @@ public class ConcurrentHashMap extends AbstractMap action.accept(p.key, p.val); } } - } + }*/ - public void replaceAll(BiFunction function) { + /* public void replaceAll(BiFunction function) { if (function == null) throw new NullPointerException(); Node[] t; if ((t = table) != null) { @@ -1498,7 +1498,7 @@ public class ConcurrentHashMap extends AbstractMap } } } - } + }*/ /** * If the specified key is not already associated with a value, attempts to compute its value using the diff --git a/src/main/java/net/openhft/collections/VanillaSharedMap.java b/src/main/java/net/openhft/collections/VanillaSharedMap.java index 60694ef2b..470428838 100644 --- a/src/main/java/net/openhft/collections/VanillaSharedMap.java +++ b/src/main/java/net/openhft/collections/VanillaSharedMap.java @@ -38,6 +38,7 @@ import java.io.IOException; import java.lang.reflect.Array; import java.nio.channels.FileChannel; import java.util.*; +import java.util.function.BiConsumer; import static java.lang.Thread.currentThread; @@ -68,6 +69,20 @@ abstract class AbstractVanillaSharedMap extends net.openhft.collections.Co private final ObjectSerializer objectSerializer; private SharedHashMapBuilder builder; + + // todo consider a better way to do this so that is is not creating an entry object each time + public void forEach(BiConsumer action) { + if (action == null) throw new NullPointerException(); + + final EntryIterator entryIterator = new EntryIterator(); + + for (Entry e = entryIterator.next(); entryIterator.hasNext(); e = entryIterator.next()) { + action.accept(e.getKey(), e.getValue()); + } + + } + + private static int figureBufferAllocationFactor(SharedHashMapBuilder builder) { // if expected map size is about 1000, seems rather wasteful to allocate // key and value serialization buffers each x64 of expected entry size.. @@ -322,6 +337,7 @@ abstract class AbstractVanillaSharedMap extends net.openhft.collections.Co */ @Override public V put(K key, V value) { + super.put(key, value); return put0(key, value, true); } diff --git a/src/test/java/net/openhft/chronicle/map/ChronicleMap8Test.java b/src/test/java/net/openhft/chronicle/map/ChronicleMap8Test.java index 8a795f94e..e273fb5f1 100644 --- a/src/test/java/net/openhft/chronicle/map/ChronicleMap8Test.java +++ b/src/test/java/net/openhft/chronicle/map/ChronicleMap8Test.java @@ -201,7 +201,10 @@ public class ChronicleMap8Test extends JSR166TestCase { @Test public void testCompute4() { ChronicleMap map = map5(); - map.compute(one, (x, y) -> null); + final BiFunction remappingFunction = (x, y) -> { + return null; + }; + map.compute(one, remappingFunction); assertFalse(map.containsKey(one)); } @@ -259,9 +262,12 @@ public class ChronicleMap8Test extends JSR166TestCase { @Test public void testReplaceAll() { ChronicleMap map = map5(); - map.replaceAll((x, y) -> { + + final BiFunction function = (x, y) -> { return x > 3 ? "Z" : y; - }); + }; + + map.replaceAll(function); assertEquals("A", map.get(one)); assertEquals("B", map.get(two)); assertEquals("C", map.get(three));