diff --git a/src/main/java/net/openhft/chronicle/map/ChronicleMap.java b/src/main/java/net/openhft/chronicle/map/ChronicleMap.java index 50e4a4c68..115c598dc 100755 --- a/src/main/java/net/openhft/chronicle/map/ChronicleMap.java +++ b/src/main/java/net/openhft/chronicle/map/ChronicleMap.java @@ -203,12 +203,11 @@ public interface ChronicleMap extends ConcurrentMap, ChronicleHash { * Apply a mutator to the value for a key and return a result. A write lock is assumed.

If * there is no entry for this key null will be returned * - * @param return type. * @param key to apply the mapping to * @param mutator to alter the value and calculate a result * @return the result of the function. */ - R putWith(K key, @NotNull Mutator mutator); + V putWith(K key, @NotNull Mutator mutator); /** * exports all the entries to a {@link java.io.File} storing them in JSON format, an attempt is diff --git a/src/main/java/net/openhft/chronicle/map/Mutator.java b/src/main/java/net/openhft/chronicle/map/Mutator.java index 412d66178..5d527604b 100755 --- a/src/main/java/net/openhft/chronicle/map/Mutator.java +++ b/src/main/java/net/openhft/chronicle/map/Mutator.java @@ -8,15 +8,15 @@ import java.io.Serializable; *

This is not functional as it can alter an argument. * * @param the type of the mutable input - * @param the type of the result of the mutator + */ -public interface Mutator extends Serializable { +public interface Mutator extends Serializable { /** * Applies this mutator to the given mutable argument. * * @param t the mutator argument * @return the result */ - R update(T t); + T update(T t); } diff --git a/src/main/java/net/openhft/chronicle/map/ReplicatedChronicleMap.java b/src/main/java/net/openhft/chronicle/map/ReplicatedChronicleMap.java index c01c4e5c7..4aa7a37d6 100644 --- a/src/main/java/net/openhft/chronicle/map/ReplicatedChronicleMap.java +++ b/src/main/java/net/openhft/chronicle/map/ReplicatedChronicleMap.java @@ -763,7 +763,7 @@ final class ReplicatedChronicleMap implements ChronicleMap, Closeable, Clon @Nullable @Override - public R putWith(@Nullable K key, @NotNull Mutator mutator) { + public V putWith(@Nullable K key, @NotNull Mutator mutator) { if (key == null) throw keyNotNullNPE(); return fetchObject(UPDATE_FOR_KEY, key, mutator); diff --git a/src/main/java/net/openhft/chronicle/map/TcpReplicator.java b/src/main/java/net/openhft/chronicle/map/TcpReplicator.java index a2616f745..efec51608 100644 --- a/src/main/java/net/openhft/chronicle/map/TcpReplicator.java +++ b/src/main/java/net/openhft/chronicle/map/TcpReplicator.java @@ -1440,7 +1440,7 @@ class StatelessServerConnector { @Nullable public Work updateForKey(@NotNull ByteBufferBytes reader, @NotNull Bytes writer, long sizeLocation) { final K key = keyReaderWithSize.read(reader, null); - final Mutator mutator = (Mutator) reader.readObject(); + final Mutator mutator = (Mutator) reader.readObject(); try { Object result = map.putWith(key, mutator); writer.writeObject(result); diff --git a/src/main/java/net/openhft/chronicle/map/VanillaChronicleMap.java b/src/main/java/net/openhft/chronicle/map/VanillaChronicleMap.java index 4a27b2ac0..c55448db0 100755 --- a/src/main/java/net/openhft/chronicle/map/VanillaChronicleMap.java +++ b/src/main/java/net/openhft/chronicle/map/VanillaChronicleMap.java @@ -559,51 +559,29 @@ class VanillaChronicleMap, } @Override - public R putWith(K key, @NotNull Mutator mutator) { + public V putWith(K key, @NotNull Mutator mutator) { - if (!(CharSequence.class.isAssignableFrom(vClass))) { + final V using = (vClass.equals(CharSequence.class)) ? (V) new StringBuilder() : null; - try (WriteContext entry = lookupUsing(key, null, - false, false, LockType.WRITE_LOCK)) { + try (WriteContext entry = lookupUsing(key, using, + false, false, LockType.WRITE_LOCK)) { - if (entry.value() == null) + if (CharSequence.class.isAssignableFrom(vClass)) { + CharSequence value = (CharSequence) entry.value(); + if (entry.value() == null || value.length() == 0) return null; - - if (!entry.wasPresent()) - return null; - - R result = mutator.update(entry.value()); - ((MutableLockedEntry) entry).value(result); - return result; } - } - - // special case - spoke to peter about this and he wants to handle strings as a - // special case - CharSequence using; - if (vClass.equals(CharSequence.class)) - using = new StringBuilder(); - else - using = null; - - try (WriteContext entry = lookupUsing(key, (V) using, - false, using == null, LockType.WRITE_LOCK)) { - - CharSequence value = (CharSequence) entry.value(); - if (entry.value() == null || value.length() == 0) + if (entry.value() == null) return null; - final R result = mutator.update(entry.value()); - - if (value instanceof StringBuilder) { - StringBuilder using1 = (StringBuilder) using; - using1.setLength(0); - using1.append((CharSequence) result); + V result = mutator.update(entry.value()); + if (entry.value() instanceof StringBuilder) { + ((StringBuilder) using).setLength(0); + ((StringBuilder) using).append((CharSequence) result); } else ((MutableLockedEntry) entry).value(result); - return result; } @@ -1699,7 +1677,7 @@ class VanillaChronicleMap, // notify the context that the entry was created if (lock instanceof WriteLocked) - ((WriteLocked) lock).wasPresent(true); + ((WriteLocked) lock).created(true); return result; } finally { @@ -2707,7 +2685,7 @@ class VanillaChronicleMap, extends MutableLockedEntry implements WriteContext { - private boolean wasPresent = true; + private boolean created = false; WriteLocked(SegmentState segmentState) { super(segmentState); @@ -2716,12 +2694,12 @@ class VanillaChronicleMap, /** * @return if the entry was created */ - public boolean wasPresent() { - return wasPresent; + public boolean created() { + return created; } - public void wasPresent(boolean wasPresent) { - this.wasPresent = wasPresent; + public void created(boolean created) { + this.created = created; } } diff --git a/src/main/java/net/openhft/chronicle/map/WriteContext.java b/src/main/java/net/openhft/chronicle/map/WriteContext.java index 9147b9a92..cdd666a9d 100644 --- a/src/main/java/net/openhft/chronicle/map/WriteContext.java +++ b/src/main/java/net/openhft/chronicle/map/WriteContext.java @@ -38,6 +38,6 @@ public interface WriteContext extends Context { /** * @returns true if the entry was previously present */ - boolean wasPresent(); + boolean created(); } diff --git a/src/test/java/net/openhft/chronicle/map/CHMUseCasesTest.java b/src/test/java/net/openhft/chronicle/map/CHMUseCasesTest.java index 06e0176ec..97ae1c182 100644 --- a/src/test/java/net/openhft/chronicle/map/CHMUseCasesTest.java +++ b/src/test/java/net/openhft/chronicle/map/CHMUseCasesTest.java @@ -74,7 +74,7 @@ public class CHMUseCasesTest { map.put("Hello", "World"); - map.putWith("Hello", new Mutator() { + map.putWith("Hello", new Mutator() { @Override public String update(String s) { return "New " + s; @@ -126,7 +126,7 @@ public class CHMUseCasesTest { })); assertEquals("New World !!", map.putWith(new StringBuilder("Hello"), new - Mutator() { + Mutator() { @Override public CharSequence update(CharSequence s) { ((StringBuilder) s).append(" !!"); @@ -137,7 +137,7 @@ public class CHMUseCasesTest { assertEquals("New World !!", map.get("Hello").toString()); assertEquals(null, map.putWith(new StringBuilder("no-key"), new - Mutator() { + Mutator() { @Override public CharSequence update(CharSequence s) { ((StringBuilder) s).append("!!"); @@ -283,7 +283,7 @@ public class CHMUseCasesTest { })); try { - map.putWith(1, new Mutator() { + map.putWith(1, new Mutator() { @Override public Integer update(Integer s) { return s + 1; @@ -326,7 +326,7 @@ public class CHMUseCasesTest { })); try { - map.putWith(1L, new Mutator() { + map.putWith(1L, new Mutator() { @Override public Long update(Long s) { return s + 1; @@ -369,7 +369,7 @@ public class CHMUseCasesTest { })); try { - map.putWith(1.0, new Mutator() { + map.putWith(1.0, new Mutator() { @Override public Double update(Double s) { return s + 1; @@ -415,7 +415,7 @@ public class CHMUseCasesTest { } })); - assertTrue(Arrays.equals(new byte[]{12, 10}, map.putWith(key1, new Mutator() { + assertTrue(Arrays.equals(new byte[]{12, 10}, map.putWith(key1, new Mutator() { @Override public byte[] update(byte[] s) { s[0]++; @@ -461,7 +461,7 @@ public class CHMUseCasesTest { assertBBEquals(ByteBuffer.wrap(new byte[]{11, 11}), map.mapForKey(key1, function)); assertEquals(null, map.mapForKey(key2, function)); - assertBBEquals(ByteBuffer.wrap(new byte[]{12, 10}), map.putWith(key1, new Mutator() { + assertBBEquals(ByteBuffer.wrap(new byte[]{12, 10}), map.putWith(key1, new Mutator() { @Override public ByteBuffer update(ByteBuffer s) { s.put(0, (byte) (s.get(0) + 1)); @@ -556,7 +556,7 @@ public class CHMUseCasesTest { assertBBEquals(ByteBuffer.wrap(new byte[]{11, 11}), map.mapForKey(key1, function)); assertEquals(null, map.mapForKey(key2, function)); - assertBBEquals(ByteBuffer.wrap(new byte[]{12, 10}), map.putWith(key1, new Mutator() { + assertBBEquals(ByteBuffer.wrap(new byte[]{12, 10}), map.putWith(key1, new Mutator() { @Override public ByteBuffer update(ByteBuffer s) { s.put(0, (byte) (s.get(0) + 1)); diff --git a/src/test/java/net/openhft/chronicle/map/ReplicationCheckingMap.java b/src/test/java/net/openhft/chronicle/map/ReplicationCheckingMap.java index 4e6ca162b..757a728c5 100644 --- a/src/test/java/net/openhft/chronicle/map/ReplicationCheckingMap.java +++ b/src/test/java/net/openhft/chronicle/map/ReplicationCheckingMap.java @@ -223,7 +223,7 @@ public class ReplicationCheckingMap implements ChronicleMap { } @Override - public R putWith(K key, @NotNull Mutator mutator) { + public V putWith(K key, @NotNull Mutator mutator) { return map1.putWith(key, mutator); } diff --git a/src/test/java/net/openhft/chronicle/map/jrs166/map/StatelessChronicleMapTest.java b/src/test/java/net/openhft/chronicle/map/jrs166/map/StatelessChronicleMapTest.java index 4da1cc290..5d7a9551c 100755 --- a/src/test/java/net/openhft/chronicle/map/jrs166/map/StatelessChronicleMapTest.java +++ b/src/test/java/net/openhft/chronicle/map/jrs166/map/StatelessChronicleMapTest.java @@ -175,7 +175,7 @@ public class StatelessChronicleMapTest extends JSR166TestCase { } @Override - public R putWith(K key, @NotNull Mutator mutator) { + public V putWith(K key, @NotNull Mutator mutator) { return d.putWith(key, mutator); }