Rearrange and reformat code.

This commit is contained in:
Peter Lawrey
2016-01-01 09:34:41 +00:00
parent ab12d46956
commit 22b8a6b5fb
54 changed files with 305 additions and 312 deletions
@@ -23,7 +23,7 @@ import net.openhft.lang.model.Byteable;
* Writer for objects, which themselves are bytes sequence in some sense:
* {@link Byteable Byteables}, {@code byte[]} arrays, other primitive arrays, "flat" objects
* (with primitives fields only), particularly boxed primitive types.
*
* <p>
* <p>This interface is called "BytesInterop" because it allows to work with object as they
* are already marshalled to {@code Bytes}: {@linkplain #startsWith(Bytes, Object) compare}
* with other {@code Bytes}, i. e. interoperate objects and {@code Bytes}.
@@ -41,7 +41,7 @@ public interface BytesInterop<E> extends BytesWriter<E> {
* {@code bytes}' is positioned at the first byte to compare. {@code bytes}' limit
* is unspecified. {@code bytes}' position and limit shouldn't be altered during
* this call.
* @param e the object to serialize virtually and compare with the given {@code bytes}
* @param e the object to serialize virtually and compare with the given {@code bytes}
* @return if the given {@code bytes} starts with the given {@code e} object's serialized form
*/
boolean startsWith(Bytes bytes, E e);
@@ -26,18 +26,18 @@ import java.io.Serializable;
* Deserializer (object from {@link Bytes}, mirroring the {@link BytesWriter}, i. e. assuming the
* length of the serialized form isn't written in the beginning of the serialized form itself,
* but managed by {@link ChronicleHash} implementation and passed to the reading methods.
*
* <p>
* <p>Implementation example:<pre><code>
* class LongPair { long first, second; }
*
* <p>
* enum LongPairArrayReader implements BytesReader&lt;LongPair[]&gt; {
* INSTANCE;
*
* <p>
* &#064;Override
* public LongPair[] read(Bytes bytes, long size) {
* return read(bytes, size, null);
* }
*
* <p>
* &#064;Override
* public LongPair[] read(Bytes bytes, long size, LongPair[] toReuse) {
* if (size &gt; Integer.MAX_VALUE * 16L)
@@ -72,13 +72,13 @@ public interface BytesReader<E> extends Serializable {
/**
* Reads and returns the object from {@code [position(), position() + size]} bytes of the given
* {@link Bytes}.
*
* <p>
* <p>Implementation of this method should increment the {@code bytes}' position by the given
* size, i. e. "consume" the bytes of the deserialized object. It must not alter the
* {@code bytes}' {@link Bytes#limit() limit} and contents.
*
* @param bytes the {@code Bytes} to read the object from
* @param size the size of the serialized form of the returned object
* @param size the size of the serialized form of the returned object
* @return the object read from the bytes
* @see BytesWriter#write(Bytes, Object)
* @see #read(Bytes, long, Object)
@@ -93,8 +93,8 @@ public interface BytesReader<E> extends Serializable {
* be {@code null}, in this case this method should behave exactly the same as
* {@link #read(Bytes, long)} method does.
*
* @param bytes the {@code Bytes} to read the object from
* @param size the size of the serialized form of the returned object
* @param bytes the {@code Bytes} to read the object from
* @param size the size of the serialized form of the returned object
* @param toReuse the object to read the deserialized data into
* @return the object read from the bytes, either reused or newly created
* @see #read(Bytes, long)
@@ -26,18 +26,18 @@ import java.io.Serializable;
* of any object before actual serialization 2) doesn't include that length in the serialized
* form itself, assuming it will be passed by the {@link ChronicleHash} into
* {@link BytesReader#read(Bytes, long)} deserialization method.
*
* <p>
* <p>Implementation example: <pre><code>
* class LongPair { long first, second; }
*
* <p>
* enum LongPairArrayWriter implements BytesWriter&lt;LongPair[]&gt; {
* INSTANCE;
*
* <p>
* &#064;Override
* public long size(LongPair[] longPairs) {
* return longPairs.length * 16L;
* }
*
* <p>
* &#064;Override
* public void write(Bytes bytes, LongPair[] longPairs) {
* for (LongPair pair : longPairs) {
@@ -66,7 +66,7 @@ public interface BytesWriter<E> extends Serializable {
/**
* Serializes the given object to the given {@code bytes}, without writing the length of the
* serialized form itself.
*
* <p>
* <p>Implementation of this method should increment the {@code bytes}' {@linkplain
* Bytes#position() position} by {@link #size(Object) size(e)}. The given object should
* be written into these range between the initial {@code bytes}' position and the position
@@ -74,7 +74,7 @@ public interface BytesWriter<E> extends Serializable {
* {@code bytes}' {@linkplain Bytes#limit() limit} and contents outside of the specified range.
*
* @param bytes the {@code Bytes} to write the given object to
* @param e the object to serialize to the given bytes
* @param e the object to serialize to the given bytes
* @see BytesReader#read(Bytes, long)
*/
void write(Bytes bytes, E e);
@@ -22,10 +22,17 @@ import static net.openhft.lang.io.IOTools.stopBitLength;
public final class SizeMarshallers {
private SizeMarshallers() {
}
public static SizeMarshaller stopBit() {
return StopBit.INSTANCE;
}
public static SizeMarshaller constant(long size) {
return new ConstantSizeMarshaller(size);
}
private enum StopBit implements SizeMarshaller {
INSTANCE;
@@ -60,10 +67,6 @@ public final class SizeMarshallers {
}
}
public static SizeMarshaller constant(long size) {
return new ConstantSizeMarshaller(size);
}
private static class ConstantSizeMarshaller implements SizeMarshaller {
private static final long serialVersionUID = 0L;
@@ -103,6 +106,4 @@ public final class SizeMarshallers {
return size;
}
}
private SizeMarshallers() {}
}
@@ -22,25 +22,12 @@ import net.openhft.lang.threadlocal.Provider;
import net.openhft.lang.threadlocal.ThreadLocalCopies;
public abstract class BasicCopyingMetaBytesInterop<E, W> implements MetaBytesInterop<E, W> {
private static final long serialVersionUID = 0L;
static final Provider<DirectBytesBuffer> provider =
Provider.of(DirectBytesBuffer.class);
static abstract class BasicCopyingMetaBytesInteropProvider<E, I,
MI extends MetaBytesInterop<E, I>> implements MetaProvider<E, I, MI> {
private static final long serialVersionUID = 0L;
@Override
public ThreadLocalCopies getCopies(ThreadLocalCopies copies) {
return provider.getCopies(copies);
}
}
private static final long serialVersionUID = 0L;
final DirectBytesBuffer buffer;
transient long size;
transient long hash;
protected BasicCopyingMetaBytesInterop(DirectBytesBuffer buffer) {
this.buffer = buffer;
}
@@ -67,4 +54,14 @@ public abstract class BasicCopyingMetaBytesInterop<E, W> implements MetaBytesInt
public void write(W writer, Bytes bytes, E e) {
bytes.write(buffer.buffer, buffer.buffer.position(), buffer.buffer.remaining());
}
static abstract class BasicCopyingMetaBytesInteropProvider<E, I,
MI extends MetaBytesInterop<E, I>> implements MetaProvider<E, I, MI> {
private static final long serialVersionUID = 0L;
@Override
public ThreadLocalCopies getCopies(ThreadLocalCopies copies) {
return provider.getCopies(copies);
}
}
}
@@ -25,6 +25,9 @@ import net.openhft.lang.io.serialization.BytesMarshaller;
*/
public final class BytesReaders {
private BytesReaders() {
}
/**
* Returns a {@link BytesReader} wrapping the given {@link BytesMarshaller}. One of the
* bridge methods between general serialization API from {@link net.openhft.lang} and
@@ -44,7 +47,7 @@ public final class BytesReaders {
* #fromBytesMarshaller(BytesMarshaller) fromBytesMarshaller(m)} call, {@code null} otherwise.
*
* @param reader reader to extract {@code BytesMarshaller} from
* @param <E> type of the objects marshalled
* @param <E> type of the objects marshalled
* @return {@code BytesMarshaller} from which the specified reader was created, or {@code null}
*/
public static <E> BytesMarshaller<E> getBytesMarshaller(BytesReader<E> reader) {
@@ -70,7 +73,4 @@ public final class BytesReaders {
return (E) marshaller.read(bytes, toReuse);
}
}
private BytesReaders() {
}
}
@@ -27,6 +27,12 @@ public abstract class CopyingMetaBytesInterop<E, W> extends BasicCopyingMetaByte
private static final long serialVersionUID = 1L;
private static final long MAX_REASONABLE_SERIALIZED_SIZE = MemoryUnit.MEGABYTES.toBytes(16L);
transient W writer;
transient E cur;
protected CopyingMetaBytesInterop(DirectBytesBuffer buffer) {
super(buffer);
}
public static void checkMaxSizeStillReasonable(long maxSize, Exception ex) {
if (maxSize > MAX_REASONABLE_SERIALIZED_SIZE) {
@@ -39,17 +45,6 @@ public abstract class CopyingMetaBytesInterop<E, W> extends BasicCopyingMetaByte
}
}
transient W writer;
transient E cur;
protected CopyingMetaBytesInterop(DirectBytesBuffer buffer) {
super(buffer);
}
DirectBytesBuffer buffer() {
return buffer;
}
public static <E, M extends BytesMarshaller<E>>
MetaBytesInterop<E, M> forBytesMarshaller(Serializable bufferIdentity) {
return new DirectBytesBuffer(bufferIdentity).forBytesMarshaller;
@@ -66,6 +61,15 @@ public abstract class CopyingMetaBytesInterop<E, W> extends BasicCopyingMetaByte
return new BytesMarshallerCopyingMetaBytesInteropProvider<>(mutable, maxSize);
}
public static <E, W extends BytesWriter<E>>
MetaProvider<E, W, CopyingMetaBytesInterop<E, W>> providerForBytesWriter(boolean mutable) {
return new BytesWriterCopyingMetaBytesInteropProvider<>(mutable);
}
DirectBytesBuffer buffer() {
return buffer;
}
private static class BytesMarshallerCopyingMetaBytesInteropProvider<E,
M extends BytesMarshaller<E>>
extends BasicCopyingMetaBytesInteropProvider<E, M, CopyingMetaBytesInterop<E, M>> {
@@ -89,11 +93,6 @@ public abstract class CopyingMetaBytesInterop<E, W> extends BasicCopyingMetaByte
}
}
public static <E, W extends BytesWriter<E>>
MetaProvider<E, W, CopyingMetaBytesInterop<E, W>> providerForBytesWriter(boolean mutable) {
return new BytesWriterCopyingMetaBytesInteropProvider<>(mutable);
}
private static class BytesWriterCopyingMetaBytesInteropProvider<E, W extends BytesWriter<E>>
extends BasicCopyingMetaBytesInteropProvider<E, W, CopyingMetaBytesInterop<E, W>> {
private static final long serialVersionUID = 0L;
@@ -105,7 +104,7 @@ public abstract class CopyingMetaBytesInterop<E, W> extends BasicCopyingMetaByte
@Override
public CopyingMetaBytesInterop<E, W> get(ThreadLocalCopies copies,
CopyingMetaBytesInterop<E, W> originalMetaWriter, W writer, E e) {
CopyingMetaBytesInterop<E, W> originalMetaWriter, W writer, E e) {
DirectBytesBuffer.ForBytesWriter forBytesWriter =
provider.get(copies, originalMetaWriter.buffer()).forBytesWriter;
forBytesWriter.init(writer, e, mutable);
@@ -33,15 +33,6 @@ public abstract class DataValueMetaBytesInterop<E>
super(buffer);
}
void init(BytesWriter<E> writer, E e, long size) {
Bytes buffer = this.buffer.obtain(size, true);
writer.write(buffer, e);
buffer.flip();
assert buffer.remaining() == size;
this.size = size;
hash = 0L;
}
public static <E> MetaBytesInterop<E, BytesWriter<E>> forIdentity(Serializable bufferIdentity) {
return new DirectBytesBuffer(bufferIdentity).forDataValueWriter;
}
@@ -51,8 +42,17 @@ public abstract class DataValueMetaBytesInterop<E>
return new InteropProvider<>(eClass);
}
void init(BytesWriter<E> writer, E e, long size) {
Bytes buffer = this.buffer.obtain(size, true);
writer.write(buffer, e);
buffer.flip();
assert buffer.remaining() == size;
this.size = size;
hash = 0L;
}
private static class InteropProvider<E> extends BasicCopyingMetaBytesInteropProvider<
E, BytesWriter<E>, MetaBytesInterop<E, BytesWriter<E>>> {
E, BytesWriter<E>, MetaBytesInterop<E, BytesWriter<E>>> {
private static final long serialVersionUID = 0L;
private final Class<E> eClass;
@@ -84,7 +84,7 @@ public abstract class DataValueMetaBytesInterop<E>
if (e instanceof Byteable)
return metaByteableInterop;
DirectBytesBuffer.ForDataValueWriter forDataValueWriter =
provider.get(copies, ((DataValueMetaBytesInterop)originalMetaWriter).buffer)
provider.get(copies, ((DataValueMetaBytesInterop) originalMetaWriter).buffer)
.forDataValueWriter;
forDataValueWriter.init(writer, e, size);
return forDataValueWriter;