Use updated BytesMarshallable with default implementations

This commit is contained in:
Peter Lawrey
2016-04-22 15:30:35 +01:00
parent f3a511e51e
commit 8ef1f1e647
6 changed files with 40 additions and 52 deletions
+1 -3
View File
@@ -47,7 +47,7 @@
<groupId>net.openhft</groupId>
<artifactId>chronicle-bom</artifactId>
<type>pom</type>
<version>1.12.10</version>
<version>1.12.14-SNAPSHOT</version>
<scope>import</scope>
</dependency>
@@ -64,7 +64,6 @@
<dependency>
<groupId>net.openhft</groupId>
<artifactId>chronicle-values</artifactId>
<version>1.0.7-alpha</version>
</dependency>
<dependency>
@@ -95,7 +94,6 @@
<dependency>
<groupId>net.openhft</groupId>
<artifactId>chronicle-algorithms</artifactId>
<version>1.1.7</version>
</dependency>
<dependency>
@@ -16,6 +16,7 @@
package net.openhft.chronicle.hash.serialization.impl;
import net.openhft.chronicle.core.util.ObjectUtils;
import net.openhft.chronicle.hash.Data;
import net.openhft.chronicle.hash.serialization.DataAccess;
import net.openhft.chronicle.hash.serialization.SizedReader;
@@ -62,8 +63,8 @@ public abstract class InstanceCreatingMarshaller<T> implements Marshallable {
*/
protected T createInstance() {
try {
return tClass.newInstance();
} catch (InstantiationException | IllegalAccessException e) {
return ObjectUtils.newInstance(tClass);
} catch (Exception e) {
throw new IllegalStateException("Some of default marshallers, chosen for the type\n" +
tClass + " by default, delegates to \n" +
this.getClass().getName() + " which assumes the type has a public no-arg\n" +
@@ -16,7 +16,6 @@
package net.openhft.chronicle.map;
import net.openhft.chronicle.bytes.Bytes;
import net.openhft.chronicle.bytes.BytesMarshallable;
import org.junit.Assert;
import org.junit.Test;
@@ -37,7 +36,6 @@ public class BytesMarshallableValueTest {
}
public static class Value implements BytesMarshallable {
int x;
String foo;
@@ -46,19 +44,6 @@ public class BytesMarshallableValueTest {
this.foo = foo;
}
public Value() {
}
@Override
public void readMarshallable(Bytes<?> bytes) {
x = bytes.readInt();
foo = bytes.readUtf8();
}
@Override
public void writeMarshallable(Bytes bytes) {
bytes.writeInt(x);
bytes.writeUtf8(foo);
}
}
}
@@ -17,8 +17,9 @@
package net.openhft.chronicle.map;
import com.google.common.primitives.Chars;
import net.openhft.chronicle.bytes.Bytes;
import net.openhft.chronicle.bytes.BytesIn;
import net.openhft.chronicle.bytes.BytesMarshallable;
import net.openhft.chronicle.bytes.BytesOut;
import net.openhft.chronicle.core.Jvm;
import net.openhft.chronicle.core.util.SerializableFunction;
import net.openhft.chronicle.core.values.*;
@@ -101,7 +102,7 @@ interface IData extends BytesMarshallable {
}
@Override
public void readMarshallable(@NotNull Bytes in) throws IllegalStateException {
public void readMarshallable(@NotNull BytesIn in) throws IllegalStateException {
long magic = in.readLong();
if (magic != MAGIC)
throw new AssertionError("Start " + Long.toHexString(magic));
@@ -113,7 +114,7 @@ interface IData extends BytesMarshallable {
}
@Override
public void writeMarshallable(@NotNull Bytes out) {
public void writeMarshallable(@NotNull BytesOut out) {
out.writeLong(MAGIC);
out.writeUTFΔ(text);
out.writeInt(number);
@@ -15,7 +15,7 @@
*/
package net.openhft.chronicle.map;/*
* Copyright 2013 peter.lawrey Lawrey
* Copyright 2013 peter.lawrey
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -30,8 +30,9 @@ package net.openhft.chronicle.map;/*
* limitations under the License.
*/
import net.openhft.chronicle.bytes.Bytes;
import net.openhft.chronicle.bytes.BytesIn;
import net.openhft.chronicle.bytes.BytesMarshallable;
import net.openhft.chronicle.bytes.BytesOut;
import org.jetbrains.annotations.NotNull;
enum BuySell {
@@ -54,7 +55,7 @@ public class SampleValues implements BytesMarshallable {
long jj = 123456789;
@Override
public void readMarshallable(@NotNull Bytes in) throws IllegalStateException {
public void readMarshallable(@NotNull BytesIn in) throws IllegalStateException {
aa = in.readUtf8();
bb = in.readUtf8();
cc = (BuySell) in.readEnum(BuySell.class);
@@ -68,7 +69,7 @@ public class SampleValues implements BytesMarshallable {
}
@Override
public void writeMarshallable(@NotNull Bytes out) {
public void writeMarshallable(@NotNull BytesOut out) {
out.writeUtf8(aa);
out.writeUtf8(bb);
out.writeEnum(cc);
@@ -17,7 +17,9 @@
package net.openhft.chronicle.map.example;
import net.openhft.chronicle.bytes.Bytes;
import net.openhft.chronicle.bytes.BytesIn;
import net.openhft.chronicle.bytes.BytesMarshallable;
import net.openhft.chronicle.bytes.BytesOut;
import net.openhft.chronicle.hash.serialization.BytesReader;
import net.openhft.chronicle.hash.serialization.BytesWriter;
import net.openhft.chronicle.hash.serialization.impl.EnumMarshallable;
@@ -31,9 +33,24 @@ import java.util.List;
public class PointListSerializationTest {
static class A {
String str_;
List<B> list_;
@Test
public void testComplexSerialization() {
try (ChronicleMap<String, A> map = ChronicleMapBuilder
.of(String.class, A.class)
.valueMarshaller(AMarshaller.INSTANCE)
.entries(5)
.averageKeySize(4)
.averageValueSize(1000)
.create()) {
A obj_A = new A();
obj_A.str_ = "a";
obj_A.list_ = new ArrayList<>();
B b = new B();
b.str_ = "b";
obj_A.list_.add(b);
map.put("KEY1", obj_A);
map.get("KEY1");
}
}
enum AMarshaller implements BytesReader<A>, BytesWriter<A>, EnumMarshallable<AMarshaller> {
@@ -86,37 +103,22 @@ public class PointListSerializationTest {
}
}
static class A {
String str_;
List<B> list_;
}
static class B implements BytesMarshallable {
String str_;
@Override
public void readMarshallable(Bytes in) {
public void readMarshallable(BytesIn in) {
str_ = in.readUtf8();
}
@Override
public void writeMarshallable(Bytes out) {
public void writeMarshallable(BytesOut out) {
out.writeUtf8(str_);
}
}
@Test
public void testComplexSerialization() {
try (ChronicleMap<String, A> map = ChronicleMapBuilder
.of(String.class, A.class)
.valueMarshaller(AMarshaller.INSTANCE)
.entries(5)
.averageKeySize(4)
.averageValueSize(1000)
.create()) {
A obj_A = new A();
obj_A.str_ = "a";
obj_A.list_ = new ArrayList<>();
B b = new B();
b.str_ = "b";
obj_A.list_.add(b);
map.put("KEY1", obj_A);
map.get("KEY1");
}
}
}