renamed to putWith() to putMapped()

This commit is contained in:
Rob Austin
2014-12-10 15:33:25 +00:00
parent 7d1f3106d9
commit 8d347e4f28
9 changed files with 55 additions and 55 deletions
@@ -197,17 +197,17 @@ public interface ChronicleMap<K, V> extends ConcurrentMap<K, V>, ChronicleHash {
* @param <R> return type.
* @return the result of the function, or null if there is no entry for the key.
*/
<R> R mapForKey(K key, @NotNull Function<? super V, R> function);
<R> R getMapped(K key, @NotNull Function<? super V, R> function);
/**
* Apply a mutator to the value for a key and return a result. A write lock is assumed. <p> If
* Apply a unaryOperator to the value for a key and return a result. A write lock is assumed. <p> If
* there is no entry for this key null will be returned
*
* @param key to apply the mapping to
* @param mutator to alter the value and calculate a result
* @param unaryOperator to alter the value and calculate a result
* @return the result of the function.
*/
V putWith(K key, @NotNull Mutator<V> mutator);
V putMapped(K key, @NotNull UnaryOperator<V> unaryOperator);
/**
* exports all the entries to a {@link java.io.File} storing them in JSON format, an attempt is
@@ -123,7 +123,7 @@ class StatelessChronicleMap<K, V> implements ChronicleMap<K, V>, Closeable, Clon
PUT_ALL_WITHOUT_ACC,
HASH_CODE,
MAP_FOR_KEY,
UPDATE_FOR_KEY
PUT_MAPPED
}
@@ -482,7 +482,7 @@ class StatelessChronicleMap<K, V> implements ChronicleMap<K, V>, Closeable, Clon
}
@Nullable
public <R> R mapForKey(@Nullable K key, @NotNull Function<? super V, R> function) {
public <R> R getMapped(@Nullable K key, @NotNull Function<? super V, R> function) {
if (key == null)
throw keyNotNullNPE();
return fetchObject(MAP_FOR_KEY, key, function);
@@ -491,10 +491,10 @@ class StatelessChronicleMap<K, V> implements ChronicleMap<K, V>, Closeable, Clon
@Nullable
@Override
public V putWith(@Nullable K key, @NotNull Mutator<V> mutator) {
public V putMapped(@Nullable K key, @NotNull UnaryOperator<V> unaryOperator) {
if (key == null)
throw keyNotNullNPE();
return fetchObject(UPDATE_FOR_KEY, key, mutator);
return fetchObject(PUT_MAPPED, key, unaryOperator);
}
@@ -1417,8 +1417,8 @@ class StatelessServerConnector<K, V> {
case MAP_FOR_KEY:
return mapForKey(reader, writer, sizeLocation);
case UPDATE_FOR_KEY:
return updateForKey(reader, writer, sizeLocation);
case PUT_MAPPED:
return putMapped(reader, writer, sizeLocation);
default:
throw new IllegalStateException("unsupported event=" + event);
@@ -1430,7 +1430,7 @@ class StatelessServerConnector<K, V> {
final K key = keyReaderWithSize.read(reader, null);
final Function<V, ?> function = (Function<V, ?>) reader.readObject();
try {
Object result = map.mapForKey(key, function);
Object result = map.getMapped(key, function);
writer.writeObject(result);
} catch (Throwable e) {
LOG.info("", e);
@@ -1442,11 +1442,11 @@ class StatelessServerConnector<K, V> {
}
@Nullable
public Work updateForKey(@NotNull ByteBufferBytes reader, @NotNull Bytes writer, long sizeLocation) {
public Work putMapped(@NotNull ByteBufferBytes reader, @NotNull Bytes writer, long sizeLocation) {
final K key = keyReaderWithSize.read(reader, null);
final Mutator<V> mutator = (Mutator<V>) reader.readObject();
final UnaryOperator<V> unaryOperator = (UnaryOperator<V>) reader.readObject();
try {
Object result = map.putWith(key, mutator);
Object result = map.putMapped(key, unaryOperator);
writer.writeObject(result);
} catch (Throwable e) {
LOG.info("", e);
@@ -11,7 +11,7 @@ import java.io.Serializable;
*/
public interface Mutator<T> extends Serializable {
public interface UnaryOperator<T> extends Serializable {
/**
* Applies this mutator to the given mutable argument.
*
@@ -551,7 +551,7 @@ class VanillaChronicleMap<K, KI, MKI extends MetaBytesInterop<K, ? super KI>,
}
@Override
public <R> R mapForKey(K key, @NotNull Function<? super V, R> function) {
public <R> R getMapped(K key, @NotNull Function<? super V, R> function) {
try (ReadContext<K, V> entry = lookupUsing(key, null,
false, false, LockType.READ_LOCK)) {
return entry.present() ? function.apply(entry.value()) : null;
@@ -559,7 +559,7 @@ class VanillaChronicleMap<K, KI, MKI extends MetaBytesInterop<K, ? super KI>,
}
@Override
public V putWith(K key, @NotNull Mutator<V> mutator) {
public V putMapped(K key, @NotNull UnaryOperator<V> unaryOperator) {
final V using = (vClass.equals(CharSequence.class)) ? (V) new StringBuilder() : null;
@@ -576,7 +576,7 @@ class VanillaChronicleMap<K, KI, MKI extends MetaBytesInterop<K, ? super KI>,
if (entry.value() == null)
return null;
V result = mutator.update(entry.value());
V result = unaryOperator.update(entry.value());
if (entry.value() instanceof StringBuilder) {
((StringBuilder) using).setLength(0);
((StringBuilder) using).append((CharSequence) result);
@@ -38,13 +38,13 @@ public class CHMUseCasesTest {
map.put("Hello", "World");
assertEquals("World", map.get("Hello"));
assertEquals("New World", map.mapForKey("Hello", new Function<String, String>() {
assertEquals("New World", map.getMapped("Hello", new Function<String, String>() {
@Override
public String apply(String s) {
return "New " + s;
}
}));
assertEquals(null, map.mapForKey("No key", new Function<String, String>() {
assertEquals(null, map.getMapped("No key", new Function<String, String>() {
@Override
public String apply(String s) {
return "New " + s;
@@ -73,7 +73,7 @@ public class CHMUseCasesTest {
map.put("Hello", "World");
map.putWith("Hello", new Mutator<String>() {
map.putMapped("Hello", new UnaryOperator<String>() {
@Override
public String update(String s) {
return "New " + s;
@@ -109,14 +109,14 @@ public class CHMUseCasesTest {
assertNull(map.getUsing(key, value));
assertEquals("New World", map.mapForKey("Hello", new
assertEquals("New World", map.getMapped("Hello", new
Function<CharSequence, CharSequence>() {
@Override
public CharSequence apply(CharSequence s) {
return "New " + s;
}
}));
assertEquals(null, map.mapForKey("No key", new
assertEquals(null, map.getMapped("No key", new
Function<CharSequence, CharSequence>() {
@Override
public CharSequence apply(CharSequence s) {
@@ -124,8 +124,8 @@ public class CHMUseCasesTest {
}
}));
assertEquals("New World !!", map.putWith("Hello", new
Mutator<CharSequence>() {
assertEquals("New World !!", map.putMapped("Hello", new
UnaryOperator<CharSequence>() {
@Override
public CharSequence update(CharSequence s) {
((StringBuilder) s).append(" !!");
@@ -135,8 +135,8 @@ public class CHMUseCasesTest {
assertEquals("New World !!", map.get("Hello").toString());
assertEquals(null, map.putWith("no-key", new
Mutator<CharSequence>() {
assertEquals(null, map.putMapped("no-key", new
UnaryOperator<CharSequence>() {
@Override
public CharSequence update(CharSequence s) {
((StringBuilder) s).append("!!");
@@ -268,13 +268,13 @@ public class CHMUseCasesTest {
assertEquals(null, map.get(3));
assertEquals(null, map.get(4));
assertEquals((Integer) 110, map.mapForKey(1, new Function<Integer, Integer>() {
assertEquals((Integer) 110, map.getMapped(1, new Function<Integer, Integer>() {
@Override
public Integer apply(Integer s) {
return 10 * s;
}
}));
assertEquals(null, map.mapForKey(-1, new Function<Integer, Integer>() {
assertEquals(null, map.getMapped(-1, new Function<Integer, Integer>() {
@Override
public Integer apply(Integer s) {
return 10 * s;
@@ -282,7 +282,7 @@ public class CHMUseCasesTest {
}));
try {
map.putWith(1, new Mutator<Integer>() {
map.putMapped(1, new UnaryOperator<Integer>() {
@Override
public Integer update(Integer s) {
return s + 1;
@@ -311,13 +311,13 @@ public class CHMUseCasesTest {
assertEquals(null, map.get(3L));
assertEquals(null, map.get(4L));
assertEquals((Long) 110L, map.mapForKey(1L, new Function<Long, Long>() {
assertEquals((Long) 110L, map.getMapped(1L, new Function<Long, Long>() {
@Override
public Long apply(Long s) {
return 10 * s;
}
}));
assertEquals(null, map.mapForKey(-1L, new Function<Long, Long>() {
assertEquals(null, map.getMapped(-1L, new Function<Long, Long>() {
@Override
public Long apply(Long s) {
return 10 * s;
@@ -325,7 +325,7 @@ public class CHMUseCasesTest {
}));
try {
map.putWith(1L, new Mutator<Long>() {
map.putMapped(1L, new UnaryOperator<Long>() {
@Override
public Long update(Long s) {
return s + 1;
@@ -354,13 +354,13 @@ public class CHMUseCasesTest {
assertEquals(null, map.get(3.0));
assertEquals(null, map.get(4.0));
assertEquals((Double) 110.0, map.mapForKey(1.0, new Function<Double, Double>() {
assertEquals((Double) 110.0, map.getMapped(1.0, new Function<Double, Double>() {
@Override
public Double apply(Double s) {
return 10 * s;
}
}));
assertEquals(null, map.mapForKey(-1.0, new Function<Double, Double>() {
assertEquals(null, map.getMapped(-1.0, new Function<Double, Double>() {
@Override
public Double apply(Double s) {
return 10 * s;
@@ -368,7 +368,7 @@ public class CHMUseCasesTest {
}));
try {
map.putWith(1.0, new Mutator<Double>() {
map.putMapped(1.0, new UnaryOperator<Double>() {
@Override
public Double update(Double s) {
return s + 1;
@@ -401,20 +401,20 @@ public class CHMUseCasesTest {
map.put(key1, value1);
assertTrue(Arrays.equals(new byte[]{11, 11}, map.mapForKey(key1, new Function<byte[], byte[]>() {
assertTrue(Arrays.equals(new byte[]{11, 11}, map.getMapped(key1, new Function<byte[], byte[]>() {
@Override
public byte[] apply(byte[] s) {
return Arrays.copyOf(s, 2);
}
})));
assertEquals(null, map.mapForKey(key2, new Function<byte[], byte[]>() {
assertEquals(null, map.getMapped(key2, new Function<byte[], byte[]>() {
@Override
public byte[] apply(byte[] s) {
return Arrays.copyOf(s, 2);
}
}));
assertTrue(Arrays.equals(new byte[]{12, 10}, map.putWith(key1, new Mutator<byte[]>() {
assertTrue(Arrays.equals(new byte[]{12, 10}, map.putMapped(key1, new UnaryOperator<byte[]>() {
@Override
public byte[] update(byte[] s) {
s[0]++;
@@ -458,10 +458,10 @@ public class CHMUseCasesTest {
};
map.put(key1, value1);
assertBBEquals(ByteBuffer.wrap(new byte[]{11, 11}), map.mapForKey(key1, function));
assertEquals(null, map.mapForKey(key2, function));
assertBBEquals(ByteBuffer.wrap(new byte[]{11, 11}), map.getMapped(key1, function));
assertEquals(null, map.getMapped(key2, function));
assertBBEquals(ByteBuffer.wrap(new byte[]{12, 10}), map.putWith(key1, new Mutator<ByteBuffer>() {
assertBBEquals(ByteBuffer.wrap(new byte[]{12, 10}), map.putMapped(key1, new UnaryOperator<ByteBuffer>() {
@Override
public ByteBuffer update(ByteBuffer s) {
s.put(0, (byte) (s.get(0) + 1));
@@ -568,10 +568,10 @@ public class CHMUseCasesTest {
return slice;
}
};
assertBBEquals(ByteBuffer.wrap(new byte[]{11, 11}), map.mapForKey(key1, function));
assertEquals(null, map.mapForKey(key2, function));
assertBBEquals(ByteBuffer.wrap(new byte[]{11, 11}), map.getMapped(key1, function));
assertEquals(null, map.getMapped(key2, function));
assertBBEquals(ByteBuffer.wrap(new byte[]{12, 10}), map.putWith(key1, new Mutator<ByteBuffer>() {
assertBBEquals(ByteBuffer.wrap(new byte[]{12, 10}), map.putMapped(key1, new UnaryOperator<ByteBuffer>() {
@Override
public ByteBuffer update(ByteBuffer s) {
s.put(0, (byte) (s.get(0) + 1));
@@ -218,13 +218,13 @@ public class ReplicationCheckingMap<K, V> implements ChronicleMap<K, V> {
}
@Override
public <R> R mapForKey(K key, @NotNull Function<? super V, R> function) {
return map1.mapForKey(key, function);
public <R> R getMapped(K key, @NotNull Function<? super V, R> function) {
return map1.getMapped(key, function);
}
@Override
public V putWith(K key, @NotNull Mutator<V> mutator) {
return map1.putWith(key, mutator);
public V putMapped(K key, @NotNull UnaryOperator<V> unaryOperator) {
return map1.putMapped(key, unaryOperator);
}
@Override
@@ -111,7 +111,7 @@ public class StatelessClientTest {
.class, StringBuilder.class)
.putReturnsNull(true)
.statelessClient(new InetSocketAddress("localhost", port)).create()) {
String actual = statelessMap.mapForKey(10, ToString.INSTANCE);
String actual = statelessMap.getMapped(10, ToString.INSTANCE);
assertEquals("Hello World", actual);
}
@@ -131,7 +131,7 @@ public class StatelessClientTest {
try (ChronicleMap<Integer, StringBuilder> statelessMap = ChronicleMapBuilder.of(Integer
.class, StringBuilder.class)
.statelessClient(new InetSocketAddress("localhost", port)).create()) {
String actual = statelessMap.mapForKey(11, ToString.INSTANCE);
String actual = statelessMap.getMapped(11, ToString.INSTANCE);
assertEquals(null, actual);
}
@@ -170,13 +170,13 @@ public class StatelessChronicleMapTest extends JSR166TestCase {
}
@Override
public <R> R mapForKey(K key, @NotNull Function<? super V, R> function) {
return d.mapForKey(key, function);
public <R> R getMapped(K key, @NotNull Function<? super V, R> function) {
return d.getMapped(key, function);
}
@Override
public V putWith(K key, @NotNull Mutator<V> mutator) {
return d.putWith(key, mutator);
public V putMapped(K key, @NotNull UnaryOperator<V> unaryOperator) {
return d.putMapped(key, unaryOperator);
}
@Override