diff --git a/src/main/java/net/openhft/chronicle/hash/serialization/impl/CharSequenceSizedReader.java b/src/main/java/net/openhft/chronicle/hash/serialization/impl/CharSequenceSizedReader.java index cdd2003c8..7f4872c86 100644 --- a/src/main/java/net/openhft/chronicle/hash/serialization/impl/CharSequenceSizedReader.java +++ b/src/main/java/net/openhft/chronicle/hash/serialization/impl/CharSequenceSizedReader.java @@ -18,6 +18,7 @@ package net.openhft.chronicle.hash.serialization.impl; import net.openhft.chronicle.bytes.Bytes; import net.openhft.chronicle.bytes.BytesUtil; +import net.openhft.chronicle.core.util.ReadResolvable; import net.openhft.chronicle.hash.serialization.SizedReader; import net.openhft.chronicle.hash.serialization.StatefulCopyable; import net.openhft.chronicle.wire.WireIn; @@ -25,27 +26,19 @@ import net.openhft.chronicle.wire.WireOut; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; -import java.io.IOException; -import java.io.ObjectInputStream; +public class CharSequenceSizedReader implements SizedReader, + StatefulCopyable, ReadResolvable { -public class CharSequenceSizedReader - implements SizedReader, StatefulCopyable { + private static final long serialVersionUID = 3949878688964423946L; - /** Cache field */ - private transient StringBuilder sb; + public static final CharSequenceSizedReader INSTANCE = new CharSequenceSizedReader(); - public CharSequenceSizedReader() { - initTransients(); - } - - private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException { - in.defaultReadObject(); - initTransients(); - } - - private void initTransients() { - sb = new StringBuilder(); - } + /** + * @deprecated use {@link #INSTANCE} as {@code CharSequenceSizedReader} is immutable and + * stateless + */ + @Deprecated + public CharSequenceSizedReader() {} @NotNull @Override @@ -54,33 +47,35 @@ public class CharSequenceSizedReader if (0 > size || size > Integer.MAX_VALUE) throw new IllegalStateException("positive int size expected, " + size + " given"); int csLen = (int) size; - if (!(using instanceof StringBuilder)) { - sb.setLength(0); - sb.ensureCapacity(csLen); - BytesUtil.parseUtf8(in, sb, csLen); - return sb.toString(); - } else { - StringBuilder usingSB = (StringBuilder) using; + StringBuilder usingSB; + if (using instanceof StringBuilder) { + usingSB = ((StringBuilder) using); usingSB.setLength(0); usingSB.ensureCapacity(csLen); - BytesUtil.parseUtf8(in, usingSB, csLen); - return usingSB; + } else { + usingSB = new StringBuilder(csLen); } + BytesUtil.parseUtf8(in, usingSB, csLen); + return usingSB; } @Override public CharSequenceSizedReader copy() { - return new CharSequenceSizedReader(); + return INSTANCE; } @Override public void readMarshallable(@NotNull WireIn wireIn) { // no fields to read - initTransients(); } @Override public void writeMarshallable(@NotNull WireOut wireOut) { // no fields to write } + + @Override + public CharSequenceSizedReader readResolve() { + return INSTANCE; + } } diff --git a/src/main/java/net/openhft/chronicle/hash/serialization/impl/EnumMarshallable.java b/src/main/java/net/openhft/chronicle/hash/serialization/impl/EnumMarshallable.java index 527617861..25a18e8ef 100644 --- a/src/main/java/net/openhft/chronicle/hash/serialization/impl/EnumMarshallable.java +++ b/src/main/java/net/openhft/chronicle/hash/serialization/impl/EnumMarshallable.java @@ -31,6 +31,6 @@ public interface EnumMarshallable> extends Marshallable, ReadR @Override default void writeMarshallable(@NotNull WireOut wireOut) { - // shouldn't read -> shouldn't write fields + // shouldn't read => shouldn't write fields } } diff --git a/src/main/java/net/openhft/chronicle/hash/serialization/impl/SerializationBuilder.java b/src/main/java/net/openhft/chronicle/hash/serialization/impl/SerializationBuilder.java index d93e8b408..5b031130f 100644 --- a/src/main/java/net/openhft/chronicle/hash/serialization/impl/SerializationBuilder.java +++ b/src/main/java/net/openhft/chronicle/hash/serialization/impl/SerializationBuilder.java @@ -82,7 +82,7 @@ public final class SerializationBuilder implements Cloneable, Serializable { dataAccess((DataAccess) new ByteableDataAccess<>((Class) tClass)); sizeMarshaller(constant(((Byteable) OS.memory().allocateInstance(tClass)).maxSize())); } else if (tClass == CharSequence.class) { - reader((SizedReader) new CharSequenceSizedReader()); + reader((SizedReader) CharSequenceSizedReader.INSTANCE); writer((SizedWriter) CharSequenceSizedWriter.INSTANCE); } else if (tClass == StringBuilder.class) { reader((SizedReader) StringBuilderSizedReader.INSTANCE); diff --git a/src/test/java/net/openhft/chronicle/map/CHMUseCasesTest.java b/src/test/java/net/openhft/chronicle/map/CHMUseCasesTest.java index 3f8edd77b..287ee009f 100644 --- a/src/test/java/net/openhft/chronicle/map/CHMUseCasesTest.java +++ b/src/test/java/net/openhft/chronicle/map/CHMUseCasesTest.java @@ -610,7 +610,7 @@ public class CHMUseCasesTest { map.put("Hello", "World"); map.put(new StringBuilder("Hello"), "World2"); - Assert.assertEquals("World2", map.get("Hello")); + Assert.assertEquals("World2", map.get("Hello").toString()); mapChecks(); } } @@ -626,8 +626,8 @@ public class CHMUseCasesTest { map.put("Hello", "World"); map.put("Hello2", new StringBuilder("World2")); - Assert.assertEquals("World2", map.get("Hello2")); - Assert.assertEquals("World", map.get("Hello")); + Assert.assertEquals("World2", map.get("Hello2").toString()); + Assert.assertEquals("World", map.get("Hello").toString()); mapChecks(); } } @@ -654,7 +654,7 @@ public class CHMUseCasesTest { StringBuilder value = new StringBuilder(); value.append("value-").append(1); map.put(key, value); - assertEquals("value-1", map.get("key-1")); + assertEquals("value-1", map.get("key-1").toString()); assertEquals(value, map.getUsing(key, value)); assertEquals("value-1", value.toString()); @@ -669,14 +669,14 @@ public class CHMUseCasesTest { assertEquals("New World !!", map.computeIfPresent("Hello", (k, s) -> { ((StringBuilder) s).append(" !!"); return "New " + s; - })); + }).toString()); assertEquals("New World !!", map.get("Hello").toString()); assertEquals("New !!", map.compute("no-key", (k, s) -> { assertNull(s); return "New !!"; - })); + }).toString()); mapChecks(); } @@ -700,7 +700,7 @@ public class CHMUseCasesTest { ((StringBuilder) using).append("Hello World"); } - assertEquals("Hello World", map.get("1")); + assertEquals("Hello World", map.get("1").toString()); mapChecks(); } } @@ -795,7 +795,7 @@ public class CHMUseCasesTest { } key.setValue(1); - assertEquals("Hello", map.get(key)); + assertEquals("Hello", map.get(key).toString()); mapChecks(); } } diff --git a/src/test/java/net/openhft/chronicle/map/ChronicleMapEqualsTest.java b/src/test/java/net/openhft/chronicle/map/ChronicleMapEqualsTest.java index 60c48d110..62be348d6 100644 --- a/src/test/java/net/openhft/chronicle/map/ChronicleMapEqualsTest.java +++ b/src/test/java/net/openhft/chronicle/map/ChronicleMapEqualsTest.java @@ -26,14 +26,13 @@ public class ChronicleMapEqualsTest { @Test public void test() { - ChronicleMap map = - ChronicleMapBuilder - .of(CharSequence.class, CharSequence.class) - .averageKey("a").averageValue("b") - .entries(100) - .create(); + ChronicleMap map = ChronicleMap + .of(String.class, String.class) + .averageKey("a").averageValue("b") + .entries(100) + .create(); - HashMap refMap = new HashMap<>(); + HashMap refMap = new HashMap<>(); refMap.put("a", "b"); map.putAll(refMap); assertTrue(map.equals(refMap)); diff --git a/src/test/java/net/openhft/chronicle/map/ChronicleMapTest.java b/src/test/java/net/openhft/chronicle/map/ChronicleMapTest.java index f64c7b36e..fa203cdfa 100755 --- a/src/test/java/net/openhft/chronicle/map/ChronicleMapTest.java +++ b/src/test/java/net/openhft/chronicle/map/ChronicleMapTest.java @@ -33,7 +33,9 @@ import java.io.IOException; import java.util.*; import java.util.concurrent.*; import java.util.concurrent.atomic.AtomicInteger; +import java.util.stream.Collectors; +import static java.util.stream.Collectors.toSet; import static org.junit.Assert.*; @SuppressWarnings({"unchecked", "ResultOfMethodCallIgnored"}) @@ -70,16 +72,22 @@ public class ChronicleMapTest { static void assertEntrySet(Set> entrySet, int[] expectedKeys, CharSequence[] expectedValues) { Set> expectedSet = new HashSet>(); for (int i = 0; i < expectedKeys.length; i++) { - expectedSet.add(new AbstractMap.SimpleEntry(expectedKeys[i], expectedValues[i])); + expectedSet.add(new AbstractMap.SimpleEntry<>(expectedKeys[i], + expectedValues[i].toString())); } + entrySet = entrySet.stream().map(e -> + new AbstractMap.SimpleImmutableEntry( + e.getKey(), e.getValue().toString())) + .collect(toSet()); assertEquals(expectedSet, entrySet); } static void assertMap(Map map, int[] expectedKeys, CharSequence[] expectedValues) { assertEquals(expectedKeys.length, map.size()); for (int i = 0; i < expectedKeys.length; i++) { - assertEquals("On position " + i, expectedValues[i], map.get(expectedKeys[i])); + assertEquals("On position " + i, + expectedValues[i].toString(), map.get(expectedKeys[i]).toString()); } } @@ -152,29 +160,29 @@ public class ChronicleMapTest { assertTrue(map.containsKey("key2")); assertFalse(map.containsKey("key3")); - assertEquals("one", map.get("key1")); - assertEquals("two", map.get("key2")); + assertEquals("one", map.get("key1").toString()); + assertEquals("two", map.get("key2").toString()); final CharSequence result = map.remove("key1"); assertEquals(1, map.size()); - assertEquals("one", result); + assertEquals("one", result.toString()); assertFalse(map.containsKey("key1")); assertEquals(null, map.get("key1")); - assertEquals("two", map.get("key2")); + assertEquals("two", map.get("key2").toString()); assertFalse(map.containsKey("key3")); // lets add one more item for luck ! map.put("key3", "three"); - assertEquals("three", map.get("key3")); + assertEquals("three", map.get("key3").toString()); assertTrue(map.containsKey("key3")); assertEquals(2, map.size()); // and just for kicks we'll overwrite what we have map.put("key3", "overwritten"); - assertEquals("overwritten", map.get("key3")); + assertEquals("overwritten", map.get("key3").toString()); assertTrue(map.containsKey("key3")); assertEquals(2, map.size()); @@ -340,8 +348,8 @@ public class ChronicleMapTest { assertTrue(map.containsKey("key2")); assertFalse(map.containsKey("key3")); - assertEquals("one", map.get("key1")); - assertEquals("two", map.get("key2")); + assertEquals("one", map.get("key1").toString()); + assertEquals("two", map.get("key2").toString()); final CharSequence result = map.remove("key1"); assertEquals(null, result); @@ -351,18 +359,18 @@ public class ChronicleMapTest { assertFalse(map.containsKey("key1")); assertEquals(null, map.get("key1")); - assertEquals("two", map.get("key2")); + assertEquals("two", map.get("key2").toString()); assertFalse(map.containsKey("key3")); // lets add one more item for luck ! map.put("key3", "three"); - assertEquals("three", map.get("key3")); + assertEquals("three", map.get("key3").toString()); assertTrue(map.containsKey("key3")); assertEquals(2, map.size()); // and just for kicks we'll overwrite what we have map.put("key3", "overwritten"); - assertEquals("overwritten", map.get("key3")); + assertEquals("overwritten", map.get("key3").toString()); assertTrue(map.containsKey("key3")); assertEquals(2, map.size()); @@ -382,21 +390,21 @@ public class ChronicleMapTest { map.put("key2", "two"); assertEquals(2, map.size()); - assertEquals("one", map.get("key1")); - assertEquals("two", map.get("key2")); + assertEquals("one", map.get("key1").toString()); + assertEquals("two", map.get("key2").toString()); assertTrue(map.containsKey("key1")); assertTrue(map.containsKey("key2")); final CharSequence result = map.replace("key1", "newValue"); - assertEquals("one", result); + assertEquals("one", result.toString()); assertTrue(map.containsKey("key1")); assertTrue(map.containsKey("key2")); assertEquals(2, map.size()); - assertEquals("newValue", map.get("key1")); - assertEquals("two", map.get("key2")); + assertEquals("newValue", map.get("key1").toString()); + assertEquals("two", map.get("key2").toString()); assertTrue(map.containsKey("key1")); assertTrue(map.containsKey("key2")); @@ -411,11 +419,11 @@ public class ChronicleMapTest { assertTrue(map.containsKey("key1")); assertTrue(map.containsKey("key2")); assertTrue(map.containsKey("key3")); - assertEquals("three", map.get("key3")); + assertEquals("three", map.get("key3").toString()); // and just for kicks we'll overwrite what we have map.put("key3", "overwritten"); - assertEquals("overwritten", map.get("key3")); + assertEquals("overwritten", map.get("key3").toString()); assertTrue(map.containsKey("key1")); assertTrue(map.containsKey("key2")); @@ -423,8 +431,8 @@ public class ChronicleMapTest { final CharSequence result2 = map.replace("key2", "newValue"); - assertEquals("two", result2); - assertEquals("newValue", map.get("key2")); + assertEquals("two", result2.toString()); + assertEquals("newValue", map.get("key2").toString()); final CharSequence result3 = map.replace("rubbish", "newValue"); assertEquals(null, result3); @@ -447,28 +455,28 @@ public class ChronicleMapTest { map.put("key1", "one"); map.put("key2", "two"); - assertEquals("one", map.get("key1")); - assertEquals("two", map.get("key2")); + assertEquals("one", map.get("key1").toString()); + assertEquals("two", map.get("key2").toString()); final boolean result = map.replace("key1", "one", "newValue"); assertEquals(true, result); - assertEquals("newValue", map.get("key1")); - assertEquals("two", map.get("key2")); + assertEquals("newValue", map.get("key1").toString()); + assertEquals("two", map.get("key2").toString()); // let and one more item for luck ! map.put("key3", "three"); - assertEquals("three", map.get("key3")); + assertEquals("three", map.get("key3").toString()); // and just for kicks we'll overwrite what we have map.put("key3", "overwritten"); - assertEquals("overwritten", map.get("key3")); + assertEquals("overwritten", map.get("key3").toString()); final boolean result2 = map.replace("key2", "two", "newValue2"); assertEquals(true, result2); - assertEquals("newValue2", map.get("key2")); + assertEquals("newValue2", map.get("key2").toString()); final boolean result3 = map.replace("newKey", "", "newValue"); assertEquals(false, result3); @@ -491,16 +499,16 @@ public class ChronicleMapTest { map.put("key1", "one"); map.put("key2", "two"); - assertEquals("one", map.get("key1")); - assertEquals("two", map.get("key2")); + assertEquals("one", map.get("key1").toString()); + assertEquals("two", map.get("key2").toString()); // a false remove final boolean wasRemoved1 = map.remove("key1", "three"); assertFalse(wasRemoved1); - assertEquals(null, map.get("key1"), "one"); - assertEquals("two", map.get("key2"), "two"); + assertEquals(null, map.get("key1").toString(), "one"); + assertEquals("two", map.get("key2").toString(), "two"); map.put("key1", "one"); @@ -509,11 +517,11 @@ public class ChronicleMapTest { // lets add one more item for luck ! map.put("key3", "three"); - assertEquals("three", map.get("key3")); + assertEquals("three", map.get("key3").toString()); // and just for kicks we'll overwrite what we have map.put("key3", "overwritten"); - assertEquals("overwritten", map.get("key3")); + assertEquals("overwritten", map.get("key3").toString()); } } @@ -1350,7 +1358,7 @@ public class ChronicleMapTest { Set keySet = map.keySet(); Collection values = map.values(); - values.remove("2"); + values.removeIf("2"::contentEquals); assertMap(map, new int[]{1, 3}, new CharSequence[]{"1", "3"}); assertEntrySet(entrySet, new int[]{1, 3}, new CharSequence[]{"1", "3"}); assertKeySet(keySet, new int[]{1, 3}); @@ -1408,7 +1416,8 @@ public class ChronicleMapTest { @Test public void valuesIteratorRemoveReflectedInMapAndOtherViews() throws IOException { try (ChronicleMap map = getViewTestMap(3)) { - HashBiMap refMap = HashBiMap.create(map); + HashBiMap refMap = HashBiMap.create(); + map.forEach((k, v) -> refMap.put(k, v.toString())); Set> entrySet = map.entrySet(); Set keySet = map.keySet(); @@ -1416,7 +1425,7 @@ public class ChronicleMapTest { Iterator valueIterator = values.iterator(); valueIterator.next(); - refMap.inverse().remove(valueIterator.next()); + refMap.inverse().remove(valueIterator.next().toString()); valueIterator.remove(); int[] expectedKeys = Ints.toArray(refMap.keySet()); CharSequence[] expectedValues = new CharSequence[expectedKeys.length]; @@ -1478,7 +1487,7 @@ public class ChronicleMapTest { Set keySet = map.keySet(); Collection values = map.values(); - values.removeAll(Arrays.asList("1", "2")); + values.removeIf(e -> "1".contentEquals(e) || "2".contentEquals(e)); assertMap(map, new int[]{3}, new CharSequence[]{"3"}); assertEntrySet(entrySet, new int[]{3}, new CharSequence[]{"3"}); assertKeySet(keySet, new int[]{3}); @@ -1495,12 +1504,9 @@ public class ChronicleMapTest { Set keySet = map.keySet(); Collection values = map.values(); - entrySet.retainAll( - Arrays.asList( - new AbstractMap.SimpleEntry(1, "1"), - new AbstractMap.SimpleEntry(2, "2") - ) - ); + entrySet.removeIf(e -> + !(e.getKey().equals(1) && "1".contentEquals(e.getValue())) && + !(e.getKey().equals(2) && "2".contentEquals(e.getValue()))); assertMap(map, new int[]{2, 1}, new CharSequence[]{"2", "1"}); assertEntrySet(entrySet, new int[]{2, 1}, new CharSequence[]{"2", "1"}); assertKeySet(keySet, new int[]{2, 1}); @@ -1534,7 +1540,7 @@ public class ChronicleMapTest { Set keySet = map.keySet(); Collection values = map.values(); - values.retainAll(Arrays.asList("1", "2")); + values.removeIf(v -> !"1".contentEquals(v) && !"2".contentEquals(v)); assertMap(map, new int[]{2, 1}, new CharSequence[]{"2", "1"}); assertEntrySet(entrySet, new int[]{2, 1}, new CharSequence[]{"2", "1"}); assertKeySet(keySet, new int[]{2, 1}); diff --git a/src/test/java/net/openhft/chronicle/map/EventListenerWithTCPSocketReplicationTest.java b/src/test/java/net/openhft/chronicle/map/EventListenerWithTCPSocketReplicationTest.java index 536a308f0..55010bcee 100644 --- a/src/test/java/net/openhft/chronicle/map/EventListenerWithTCPSocketReplicationTest.java +++ b/src/test/java/net/openhft/chronicle/map/EventListenerWithTCPSocketReplicationTest.java @@ -161,7 +161,7 @@ public class EventListenerWithTCPSocketReplicationTest { Assert.assertTrue("The eventListener.onPut should have been called", putWasCalled.get()); Assert.assertEquals(Integer.valueOf(5), keyRef.get()); - Assert.assertEquals("EXAMPLE", valueRef.get()); + Assert.assertEquals("EXAMPLE", valueRef.get().toString()); } @Test @@ -190,7 +190,7 @@ public class EventListenerWithTCPSocketReplicationTest { Assert.assertTrue("The eventListener.onPut should have been called", putWasCalled.get()); Assert.assertEquals(Integer.valueOf(5), keyRef.get()); - Assert.assertEquals("EXAMPLE", valueRef.get()); + Assert.assertEquals("EXAMPLE", valueRef.get().toString()); Assert.assertEquals("WILL_GET_OVER-WRITTEN", replacedValueRef.get().toString()); } @@ -220,7 +220,7 @@ public class EventListenerWithTCPSocketReplicationTest { Assert.assertTrue("The eventListener.onPut should have been called", putWasCalled.get()); Assert.assertEquals(Integer.valueOf(5), keyRef.get()); Assert.assertEquals("small", replacedValueRef.get().toString()); - Assert.assertEquals("EXAMPLE", valueRef.get()); + Assert.assertEquals("EXAMPLE", valueRef.get().toString()); } @Test diff --git a/src/test/java/net/openhft/chronicle/map/GuavaTest.java b/src/test/java/net/openhft/chronicle/map/GuavaTest.java index f93417420..1b86fd6cc 100644 --- a/src/test/java/net/openhft/chronicle/map/GuavaTest.java +++ b/src/test/java/net/openhft/chronicle/map/GuavaTest.java @@ -38,11 +38,11 @@ import static com.google.common.collect.testing.features.MapFeature.*; public class GuavaTest extends TestCase { public static Test suite() { - MapTestSuiteBuilder chmSuite = using(new CHMTestGenerator()); + MapTestSuiteBuilder chmSuite = using(new CHMTestGenerator()); configureSuite(chmSuite); TestSuite chmTests = chmSuite.named("Guava tests of Chronicle Map").createTestSuite(); - MapTestSuiteBuilder backed = using(new BackedUpMapGenerator()); + MapTestSuiteBuilder backed = using(new BackedUpMapGenerator()); configureSuite(backed); TestSuite backedTests = backed .named("Guava tests tests of Chronicle Map, backed with HashMap") @@ -55,7 +55,7 @@ public class GuavaTest extends TestCase { return tests; } - private static void configureSuite(MapTestSuiteBuilder suite) { + private static void configureSuite(MapTestSuiteBuilder suite) { suite.withFeatures(GENERAL_PURPOSE) .withFeatures(CollectionSize.ANY) .withFeatures(CollectionFeature.REMOVE_OPERATIONS) @@ -63,63 +63,60 @@ public class GuavaTest extends TestCase { } static abstract class TestGenerator - implements TestMapGenerator { + implements TestMapGenerator { - abstract Map newMap(); + abstract Map newMap(); - public CharSequence[] createKeyArray(int length) { - return new CharSequence[length]; + public String[] createKeyArray(int length) { + return new String[length]; } @Override - public CharSequence[] createValueArray(int length) { - return new CharSequence[length]; + public String[] createValueArray(int length) { + return new String[length]; } @Override - public SampleElements> samples() { + public SampleElements> samples() { return SampleElements.mapEntries( - new SampleElements( - "key1", "key2", "key3", "key4", "key5" - ), - new SampleElements( - "val1", "val2", "val3", "val4", "val5" - ) + new SampleElements<>("key1", "key2", "key3", "key4", "key5"), + new SampleElements<>("val1", "val2", "val3", "val4", "val5") ); } @Override - public Map create(Object... objects) { - Map map = newMap(); + public Map create(Object... objects) { + Map map = newMap(); for (Object obj : objects) { Map.Entry e = (Map.Entry) obj; - map.put((CharSequence) e.getKey(), - (CharSequence) e.getValue()); + map.put((String) e.getKey(), + (String) e.getValue()); } return map; } @Override - public Map.Entry[] createArray(int length) { + public Map.Entry[] createArray(int length) { + //noinspection unchecked return new Map.Entry[length]; } @Override - public Iterable> order( - List> insertionOrder) { + public Iterable> order( + List> insertionOrder) { return insertionOrder; } } static class CHMTestGenerator extends TestGenerator { - ChronicleMapBuilder builder = - ChronicleMapBuilder.of(CharSequence.class, CharSequence.class) + ChronicleMapBuilder builder = + ChronicleMapBuilder.of(String.class, String.class) .entries(100) .averageKeySize(10).averageValueSize(10) .minSegments(2); @Override - Map newMap() { + Map newMap() { return builder.create(); } } @@ -127,29 +124,29 @@ public class GuavaTest extends TestCase { static class BackedUpMapGenerator extends CHMTestGenerator { @Override - Map newMap() { - Map m = new HashMap<>(); - builder.entryOperations(new MapEntryOperations() { + Map newMap() { + Map m = new HashMap<>(); + builder.entryOperations(new MapEntryOperations() { @Override - public Void remove(@NotNull MapEntry entry) { + public Void remove(@NotNull MapEntry entry) { Assert.assertEquals(m, entry.context().map()); - m.remove(entry.key().get().toString()); + m.remove(entry.key().get()); return MapEntryOperations.super.remove(entry); } @Override - public Void replaceValue(@NotNull MapEntry entry, - net.openhft.chronicle.hash.Data newValue) { + public Void replaceValue(@NotNull MapEntry entry, + net.openhft.chronicle.hash.Data newValue) { Assert.assertEquals(m, entry.context().map()); - m.put(entry.key().get().toString(), newValue.get().toString()); + m.put(entry.key().get(), newValue.get()); return MapEntryOperations.super.replaceValue(entry, newValue); } @Override - public Void insert(@NotNull MapAbsentEntry absentEntry, - Data value) { + public Void insert(@NotNull MapAbsentEntry absentEntry, + Data value) { Assert.assertEquals(m, absentEntry.context().map()); - m.put(absentEntry.absentKey().get().toString(), value.get().toString()); + m.put(absentEntry.absentKey().get(), value.get()); return MapEntryOperations.super.insert(absentEntry, value); } }); diff --git a/src/test/java/net/openhft/chronicle/map/IntValueMapTest.java b/src/test/java/net/openhft/chronicle/map/IntValueMapTest.java index bca22c45b..cf780bc28 100644 --- a/src/test/java/net/openhft/chronicle/map/IntValueMapTest.java +++ b/src/test/java/net/openhft/chronicle/map/IntValueMapTest.java @@ -46,7 +46,7 @@ public class IntValueMapTest { map.put(value, expected); final CharSequence actual = map.get(value); - assertEquals(expected, actual); + assertEquals(expected, actual.toString()); // this will fail diff --git a/src/test/java/net/openhft/chronicle/map/Issue42Test.java b/src/test/java/net/openhft/chronicle/map/Issue42Test.java index 857f76946..182c832fe 100755 --- a/src/test/java/net/openhft/chronicle/map/Issue42Test.java +++ b/src/test/java/net/openhft/chronicle/map/Issue42Test.java @@ -42,7 +42,7 @@ public class Issue42Test { for (int i = 0; i < 1000000; ++i) { String s = String.valueOf(i); - Assert.assertEquals(s, map.get(s)); + Assert.assertEquals(s, map.get(s).toString()); } } } diff --git a/src/test/java/net/openhft/chronicle/map/NoUpperBoundChunksPerEntryTest.java b/src/test/java/net/openhft/chronicle/map/NoUpperBoundChunksPerEntryTest.java index 3f44f78f7..0d6f9d63c 100644 --- a/src/test/java/net/openhft/chronicle/map/NoUpperBoundChunksPerEntryTest.java +++ b/src/test/java/net/openhft/chronicle/map/NoUpperBoundChunksPerEntryTest.java @@ -31,6 +31,6 @@ public class NoUpperBoundChunksPerEntryTest { ultraLargeValue += "Hello"; } map.put(1, ultraLargeValue); - Assert.assertEquals(ultraLargeValue, map.get(1)); + Assert.assertEquals(ultraLargeValue, map.get(1).toString()); } } diff --git a/src/test/java/net/openhft/chronicle/map/ReplicatedChronicleMapTest.java b/src/test/java/net/openhft/chronicle/map/ReplicatedChronicleMapTest.java index 87bd04990..db535cb94 100644 --- a/src/test/java/net/openhft/chronicle/map/ReplicatedChronicleMapTest.java +++ b/src/test/java/net/openhft/chronicle/map/ReplicatedChronicleMapTest.java @@ -186,7 +186,7 @@ public class ReplicatedChronicleMapTest extends JSR166TestCase { @Test public void testGet() throws IOException { ChronicleMap map = map5(); - assertEquals("A", map.get(JSR166TestCase.one)); + assertEquals("A", map.get(JSR166TestCase.one).toString()); ChronicleMap empty = newShmIntString(); assertNull(map.get(JSR166TestCase.notPresent)); } @@ -238,14 +238,13 @@ public class ReplicatedChronicleMapTest extends JSR166TestCase { public void testValuesToArray() throws IOException { ChronicleMap map = map5(); Collection v = map.values(); - Object[] ar = v.toArray(); - ArrayList s = new ArrayList(Arrays.asList(ar)); - assertEquals(5, ar.length); - assertTrue(s.contains("A")); - assertTrue(s.contains("B")); - assertTrue(s.contains("C")); - assertTrue(s.contains("D")); - assertTrue(s.contains("E")); + ArrayList s = new ArrayList(map.values()); + assertEquals(5, s.size()); + assertTrue(s.stream().anyMatch("A"::contentEquals)); + assertTrue(s.stream().anyMatch("B"::contentEquals)); + assertTrue(s.stream().anyMatch("C"::contentEquals)); + assertTrue(s.stream().anyMatch("D"::contentEquals)); + assertTrue(s.stream().anyMatch("E"::contentEquals)); } /** @@ -317,13 +316,18 @@ public class ReplicatedChronicleMapTest extends JSR166TestCase { assertEquals(5, s.size()); Iterator it = s.iterator(); while (it.hasNext()) { - Map.Entry e = (Map.Entry) it.next(); + Map.Entry e = (Map.Entry) it.next(); assertTrue( - (e.getKey().equals(JSR166TestCase.one) && e.getValue().equals("A")) || - (e.getKey().equals(JSR166TestCase.two) && e.getValue().equals("B")) || - (e.getKey().equals(JSR166TestCase.three) && e.getValue().equals("C")) || - (e.getKey().equals(JSR166TestCase.four) && e.getValue().equals("D")) || - (e.getKey().equals(JSR166TestCase.five) && e.getValue().equals("E")) + (e.getKey().equals(JSR166TestCase.one) && + "A".contentEquals(e.getValue())) || + (e.getKey().equals(JSR166TestCase.two) && + "B".contentEquals(e.getValue())) || + (e.getKey().equals(JSR166TestCase.three) && + "C".contentEquals(e.getValue())) || + (e.getKey().equals(JSR166TestCase.four) && + "D".contentEquals(e.getValue())) || + (e.getKey().equals(JSR166TestCase.five) && + "E".contentEquals(e.getValue())) ); } } @@ -360,8 +364,7 @@ public class ReplicatedChronicleMapTest extends JSR166TestCase { @Test public void testPutIfAbsent2() throws IOException { ChronicleMap map = map5(); - final Object z = map.putIfAbsent(JSR166TestCase.one, "Z"); - assertEquals("A", z); + assertEquals("A", map.putIfAbsent(JSR166TestCase.one, "Z").toString()); } /** @@ -381,7 +384,7 @@ public class ReplicatedChronicleMapTest extends JSR166TestCase { public void testReplace2() throws IOException { ChronicleMap map = map5(); assertNotNull(map.replace(JSR166TestCase.one, "Z")); - assertEquals("Z", map.get(JSR166TestCase.one)); + assertEquals("Z", map.get(JSR166TestCase.one).toString()); } /** @@ -390,9 +393,9 @@ public class ReplicatedChronicleMapTest extends JSR166TestCase { @Test public void testReplaceValue() throws IOException { ChronicleMap map = map5(); - assertEquals("A", map.get(JSR166TestCase.one)); + assertEquals("A", map.get(JSR166TestCase.one).toString()); assertFalse(map.replace(JSR166TestCase.one, "Z", "Z")); - assertEquals("A", map.get(JSR166TestCase.one)); + assertEquals("A", map.get(JSR166TestCase.one).toString()); } /** @@ -401,9 +404,9 @@ public class ReplicatedChronicleMapTest extends JSR166TestCase { @Test public void testReplaceValue2() throws IOException { ChronicleMap map = map5(); - assertEquals("A", map.get(JSR166TestCase.one)); + assertEquals("A", map.get(JSR166TestCase.one).toString()); assertTrue(map.replace(JSR166TestCase.one, "A", "Z")); - assertEquals("Z", map.get(JSR166TestCase.one)); + assertEquals("Z", map.get(JSR166TestCase.one).toString()); } /** diff --git a/src/test/java/net/openhft/chronicle/map/TCPSocketReplication3wayPutReturnsNullTest.java b/src/test/java/net/openhft/chronicle/map/TCPSocketReplication3wayPutReturnsNullTest.java index 4d0534a40..af495f122 100755 --- a/src/test/java/net/openhft/chronicle/map/TCPSocketReplication3wayPutReturnsNullTest.java +++ b/src/test/java/net/openhft/chronicle/map/TCPSocketReplication3wayPutReturnsNullTest.java @@ -133,7 +133,7 @@ public class TCPSocketReplication3wayPutReturnsNullTest { public void testPutIfAbsent() throws IOException, InterruptedException { assertEquals(null, map1.putIfAbsent(1, "EXAMPLE-1")); - assertEquals("EXAMPLE-1", map1.putIfAbsent(1, "EXAMPLE-2")); + assertEquals("EXAMPLE-1", map1.putIfAbsent(1, "EXAMPLE-2").toString()); assertEquals(null, map1.putIfAbsent(2, "EXAMPLE-2")); assertEquals(null, map1.putIfAbsent(3, "EXAMPLE-1")); diff --git a/src/test/java/net/openhft/chronicle/map/TCPSocketReplicationBootStrapTest.java b/src/test/java/net/openhft/chronicle/map/TCPSocketReplicationBootStrapTest.java index 841bbd9b1..61abd5878 100644 --- a/src/test/java/net/openhft/chronicle/map/TCPSocketReplicationBootStrapTest.java +++ b/src/test/java/net/openhft/chronicle/map/TCPSocketReplicationBootStrapTest.java @@ -84,7 +84,7 @@ public class TCPSocketReplicationBootStrapTest { // add data into it waitTillEqual(5000); - assertEquals("ADDED WHEN DISCONNECTED TO MAP1", map1.get(11)); + assertEquals("ADDED WHEN DISCONNECTED TO MAP1", map1.get(11).toString()); map2.file().delete(); persistenceFile.delete(); @@ -158,7 +158,7 @@ public class TCPSocketReplicationBootStrapTest { // add data into it waitTillEqual(5000); - assertEquals("ADDED WHEN DISCONNECTED TO MAP1", map1.get(11)); + assertEquals("ADDED WHEN DISCONNECTED TO MAP1", map1.get(11).toString()); } @After diff --git a/src/test/java/net/openhft/chronicle/map/TCPSocketReplicationPostConnectionTest.java b/src/test/java/net/openhft/chronicle/map/TCPSocketReplicationPostConnectionTest.java index 9cb6995eb..973ce23c8 100755 --- a/src/test/java/net/openhft/chronicle/map/TCPSocketReplicationPostConnectionTest.java +++ b/src/test/java/net/openhft/chronicle/map/TCPSocketReplicationPostConnectionTest.java @@ -114,7 +114,7 @@ public class TCPSocketReplicationPostConnectionTest { assertEquals(map1, map2); assertTrue(!map1.isEmpty()); - assertTrue(map2.get(6).equals("EXAMPLE-1")); + assertEquals("EXAMPLE-1", map2.get(6).toString()); } @After diff --git a/src/test/java/net/openhft/chronicle/map/TcpReplicationLargeEntryTest.java b/src/test/java/net/openhft/chronicle/map/TcpReplicationLargeEntryTest.java index 281cd9323..4fadb8909 100644 --- a/src/test/java/net/openhft/chronicle/map/TcpReplicationLargeEntryTest.java +++ b/src/test/java/net/openhft/chronicle/map/TcpReplicationLargeEntryTest.java @@ -133,8 +133,8 @@ public class TcpReplicationLargeEntryTest { waitTillEqual(10000); - Assert.assertEquals(xString, map2.get(1)); - Assert.assertEquals(yString, map2.get(2)); + Assert.assertEquals(xString, map2.get(1).toString()); + Assert.assertEquals(yString, map2.get(2).toString()); } diff --git a/src/test/java/net/openhft/chronicle/map/TcpTimeBasedReplicationSoakTest.java b/src/test/java/net/openhft/chronicle/map/TcpTimeBasedReplicationSoakTest.java index 7782570c5..a00b3165e 100644 --- a/src/test/java/net/openhft/chronicle/map/TcpTimeBasedReplicationSoakTest.java +++ b/src/test/java/net/openhft/chronicle/map/TcpTimeBasedReplicationSoakTest.java @@ -34,8 +34,8 @@ import java.util.*; public class TcpTimeBasedReplicationSoakTest { - private ChronicleMap map1; - private ChronicleMap map2; + private ChronicleMap map1; + private ChronicleMap map2; static int s_port = 8010; int t = 0; @@ -44,8 +44,8 @@ public class TcpTimeBasedReplicationSoakTest { final InetSocketAddress endpoint = new InetSocketAddress("localhost", s_port + 1); - ChronicleMapBuilder builder = ChronicleMapBuilder - .of(Integer.class, CharSequence.class) + ChronicleMapBuilder builder = ChronicleMapBuilder + .of(Integer.class, String.class) .averageValue("test-1000000") .entries(Builder.SIZE); { @@ -102,12 +102,12 @@ public class TcpTimeBasedReplicationSoakTest { System.out.print("."); Random rnd = new Random(System.currentTimeMillis()); - final ChronicleMap map = rnd.nextBoolean() ? map1 : map2; + final ChronicleMap map = rnd.nextBoolean() ? map1 : map2; if (rnd.nextBoolean()) { - map.put((int) rnd.nextInt(100), "test" + j); + map.put(rnd.nextInt(100), "test" + j); } else { - map.remove((int) rnd.nextInt(100)); + map.remove(rnd.nextInt(100)); } } diff --git a/src/test/java/net/openhft/chronicle/map/jsr166/map/ChronicleMapTest.java b/src/test/java/net/openhft/chronicle/map/jsr166/map/ChronicleMapTest.java index 58ba8b579..102d2d127 100644 --- a/src/test/java/net/openhft/chronicle/map/jsr166/map/ChronicleMapTest.java +++ b/src/test/java/net/openhft/chronicle/map/jsr166/map/ChronicleMapTest.java @@ -23,6 +23,7 @@ import org.junit.Test; import java.io.IOException; import java.util.*; +import java.util.Map.Entry; import static org.junit.Assert.*; @@ -52,7 +53,7 @@ public class ChronicleMapTest extends JSR166TestCase { /** * Returns a new map from Integers 1-5 to Strings "A"-"E". */ - private static ChronicleMap map5() throws IOException { + private static ChronicleMap map5() throws IOException { ChronicleMap map = newShmIntString(10); assertTrue(map.isEmpty()); map.put(one, "A"); @@ -70,7 +71,7 @@ public class ChronicleMapTest extends JSR166TestCase { */ @Test(timeout = 5000) public void testClear() throws IOException { - try (ChronicleMap map = map5()) { + try (ChronicleMap map = map5()) { map.clear(); assertEquals(0, map.size()); } @@ -115,7 +116,7 @@ public class ChronicleMapTest extends JSR166TestCase { @Test(timeout = 5000) public void testGet() throws IOException { try (ChronicleMap map = map5()) { - assertEquals("A", map.get(one)); + assertEquals("A", map.get(one).toString()); try (ChronicleMap empty = newStrStrMap(8078)) { assertNull(map.get(notPresent)); } @@ -174,16 +175,15 @@ public class ChronicleMapTest extends JSR166TestCase { */ @Test(timeout = 5000) public void testValuesToArray() throws IOException { - try (ChronicleMap map = map5()) { - Collection v = map.values(); - Object[] ar = v.toArray(); - ArrayList s = new ArrayList(Arrays.asList(ar)); - assertEquals(5, ar.length); - assertTrue(s.contains("A")); - assertTrue(s.contains("B")); - assertTrue(s.contains("C")); - assertTrue(s.contains("D")); - assertTrue(s.contains("E")); + try (ChronicleMap map = map5()) { + Collection vs = map.values(); + ArrayList s = new ArrayList<>(vs); + assertEquals(5, s.size()); + assertTrue(s.stream().anyMatch("A"::contentEquals)); + assertTrue(s.stream().anyMatch("B"::contentEquals)); + assertTrue(s.stream().anyMatch("C"::contentEquals)); + assertTrue(s.stream().anyMatch("D"::contentEquals)); + assertTrue(s.stream().anyMatch("E"::contentEquals)); } } @@ -197,8 +197,8 @@ public class ChronicleMapTest extends JSR166TestCase { Object[] ar = s.toArray(); assertEquals(5, ar.length); for (int i = 0; i < 5; ++i) { - assertTrue(map.containsKey(((java.util.Map.Entry) (ar[i])).getKey())); - assertTrue(map.containsValue(((java.util.Map.Entry) (ar[i])).getValue())); + assertTrue(map.containsKey(((Entry) (ar[i])).getKey())); + assertTrue(map.containsValue(((Entry) (ar[i])).getValue())); } } } @@ -224,18 +224,18 @@ public class ChronicleMapTest extends JSR166TestCase { */ @Test(timeout = 5000) public void testEntrySet() throws IOException { - try (ChronicleMap map = map5()) { - Set s = map.entrySet(); + try (ChronicleMap map = map5()) { + Set> s = map.entrySet(); assertEquals(5, s.size()); - Iterator it = s.iterator(); + Iterator> it = s.iterator(); while (it.hasNext()) { - java.util.Map.Entry e = (java.util.Map.Entry) it.next(); + Entry e = it.next(); assertTrue( - (e.getKey().equals(one) && e.getValue().equals("A")) || - (e.getKey().equals(two) && e.getValue().equals("B")) || - (e.getKey().equals(three) && e.getValue().equals("C")) || - (e.getKey().equals(four) && e.getValue().equals("D")) || - (e.getKey().equals(five) && e.getValue().equals("E")) + (e.getKey().equals(one) && "A".contentEquals(e.getValue())) || + (e.getKey().equals(two) && "B".contentEquals(e.getValue())) || + (e.getKey().equals(three) && "C".contentEquals(e.getValue())) || + (e.getKey().equals(four) && "D".contentEquals(e.getValue())) || + (e.getKey().equals(five) && "E".contentEquals(e.getValue())) ); } } @@ -277,7 +277,7 @@ public class ChronicleMapTest extends JSR166TestCase { @Test(timeout = 5000) public void testPutIfAbsent2() throws IOException { try (ChronicleMap map = map5()) { - assertEquals("A", map.putIfAbsent(one, "Z")); + assertEquals("A", map.putIfAbsent(one, "Z").toString()); } } @@ -300,7 +300,7 @@ public class ChronicleMapTest extends JSR166TestCase { IOException { try (ChronicleMap map = map5()) { assertNotNull(map.replace(one, "Z")); - assertEquals("Z", map.get(one)); + assertEquals("Z", map.get(one).toString()); } } @@ -311,9 +311,9 @@ public class ChronicleMapTest extends JSR166TestCase { public void testReplaceValue() throws IOException { try (ChronicleMap map = map5()) { - assertEquals("A", map.get(one)); + assertEquals("A", map.get(one).toString()); assertFalse(map.replace(one, "Z", "Z")); - assertEquals("A", map.get(one)); + assertEquals("A", map.get(one).toString()); } } @@ -324,9 +324,9 @@ public class ChronicleMapTest extends JSR166TestCase { public void testReplaceValue2 () throws IOException { try (ChronicleMap map = map5()) { - assertEquals("A", map.get(one)); + assertEquals("A", map.get(one).toString()); assertTrue(map.replace(one, "A", "Z")); - assertEquals("Z", map.get(one)); + assertEquals("Z", map.get(one).toString()); } }