From 93e6626e45216b9da3eb700f63136256ee5dbbdd Mon Sep 17 00:00:00 2001 From: Peter Lawrey Date: Mon, 30 Apr 2018 16:29:21 +0100 Subject: [PATCH] #151 BytesMarshallable and interface value classes not supported. --- .../chronicle/map/ChronicleMapBuilder.java | 14 +++++++++---- .../chronicle/map/ChronicleMapTest.java | 20 +++++++++++++++++++ 2 files changed, 30 insertions(+), 4 deletions(-) diff --git a/src/main/java/net/openhft/chronicle/map/ChronicleMapBuilder.java b/src/main/java/net/openhft/chronicle/map/ChronicleMapBuilder.java index 210750dcd..b2011ccc0 100755 --- a/src/main/java/net/openhft/chronicle/map/ChronicleMapBuilder.java +++ b/src/main/java/net/openhft/chronicle/map/ChronicleMapBuilder.java @@ -21,6 +21,7 @@ import net.openhft.chronicle.algo.MemoryUnit; import net.openhft.chronicle.algo.hashing.LongHashFunction; import net.openhft.chronicle.bytes.Byteable; import net.openhft.chronicle.bytes.Bytes; +import net.openhft.chronicle.bytes.BytesMarshallable; import net.openhft.chronicle.core.Jvm; import net.openhft.chronicle.core.OS; import net.openhft.chronicle.hash.ChronicleHashBuilder; @@ -43,10 +44,7 @@ import org.jetbrains.annotations.NotNull; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import java.io.File; -import java.io.FileNotFoundException; -import java.io.IOException; -import java.io.RandomAccessFile; +import java.io.*; import java.lang.reflect.InvocationTargetException; import java.nio.ByteBuffer; import java.nio.channels.FileChannel; @@ -653,6 +651,14 @@ public final class ChronicleMapBuilder implements * @see #actualChunkSize(int) */ public ChronicleMapBuilder averageValue(V averageValue) { + Class valueClass = averageValue.getClass(); + if (BytesMarshallable.class.isAssignableFrom(valueClass) && + valueBuilder.tClass.isInterface()) { + if (Serializable.class.isAssignableFrom(valueClass)) + LOG.warn("BytesMarshallable " + valueClass + " will be serialized as Serializable as the value class is an interface"); + else + throw new IllegalArgumentException("Using BytesMarshallable and an interface value type not supported"); + } Objects.requireNonNull(averageValue); checkSizeIsStaticallyKnown(valueBuilder, "Value"); this.averageValue = averageValue; diff --git a/src/test/java/net/openhft/chronicle/map/ChronicleMapTest.java b/src/test/java/net/openhft/chronicle/map/ChronicleMapTest.java index a2fa122f9..761a3fd16 100755 --- a/src/test/java/net/openhft/chronicle/map/ChronicleMapTest.java +++ b/src/test/java/net/openhft/chronicle/map/ChronicleMapTest.java @@ -19,6 +19,7 @@ package net.openhft.chronicle.map; import com.google.common.collect.HashBiMap; import com.google.common.primitives.Ints; +import net.openhft.chronicle.bytes.BytesMarshallable; import net.openhft.chronicle.bytes.BytesStore; import net.openhft.chronicle.core.values.IntValue; import net.openhft.chronicle.core.values.LongValue; @@ -1863,6 +1864,25 @@ public class ChronicleMapTest { tmpFile.delete(); } + @Test(expected = IllegalArgumentException.class) + public void testBytesMarshallableMustBeConcreteValueType() { + try (ChronicleMap map = ChronicleMapBuilder + .of(CharSequence.class, BMSUper.class) + .entries(1) + .averageKey("hello") + .averageValue(new BMClass()) + .create()) { + map.put("hi", new BMClass()); + } + } + + interface BMSUper { + + } + + static class BMClass implements BytesMarshallable, BMSUper { + + } private static final class IncrementRunnable implements Runnable { private final ChronicleMap map;