diff --git a/src/main/java/net/openhft/chronicle/hash/serialization/internal/BooleanMarshaller.java b/src/main/java/net/openhft/chronicle/hash/serialization/internal/BooleanMarshaller.java new file mode 100644 index 000000000..4c3482c53 --- /dev/null +++ b/src/main/java/net/openhft/chronicle/hash/serialization/internal/BooleanMarshaller.java @@ -0,0 +1,97 @@ +/* + * Copyright (C) 2015 higherfrequencytrading.com + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program. If not, see . + */ + +package net.openhft.chronicle.hash.serialization.internal; + +import net.openhft.chronicle.hash.hashing.LongHashFunction; +import net.openhft.chronicle.hash.serialization.BytesInterop; +import net.openhft.chronicle.hash.serialization.BytesReader; +import net.openhft.chronicle.hash.serialization.SizeMarshaller; +import net.openhft.lang.io.Bytes; +import org.jetbrains.annotations.NotNull; + +public enum BooleanMarshaller + implements BytesInterop, BytesReader, SizeMarshaller { + INSTANCE; + + @Override + public long size(@NotNull Boolean e) { + return 1L; + } + + @Override + public int sizeEncodingSize(long size) { + return 0; + } + + @Override + public long minEncodableSize() { + return 1L; + } + + @Override + public int minSizeEncodingSize() { + return 0; + } + + @Override + public int maxSizeEncodingSize() { + return 0; + } + + @Override + public void writeSize(Bytes bytes, long size) { + assert size == 1L; + // do nothing + } + + @Override + public boolean startsWith(@NotNull Bytes bytes, @NotNull Boolean e) { + return e == (bytes.readByte(bytes.position()) != 0); + } + + @Override + public boolean equivalent(@NotNull Boolean a, @NotNull Boolean b) { + return a.booleanValue() == b.booleanValue(); + } + + @Override + public long hash(@NotNull LongHashFunction hashFunction, @NotNull Boolean e) { + return hashFunction.hashByte(e ? (byte) 'Y' : 0); + } + + @Override + public void write(@NotNull Bytes bytes, @NotNull Boolean e) { + bytes.writeByte(e ? 'Y' : 0); + } + + @Override + public long readSize(Bytes bytes) { + return 1L; + } + + @NotNull + @Override + public Boolean read(@NotNull Bytes bytes, long size) { + return bytes.readByte() != 0; + } + + @NotNull + @Override + public Boolean read(@NotNull Bytes bytes, long size, Boolean toReuse) { + return bytes.readByte() != 0; + } +} diff --git a/src/main/java/net/openhft/chronicle/hash/serialization/internal/SerializationBuilder.java b/src/main/java/net/openhft/chronicle/hash/serialization/internal/SerializationBuilder.java index f239f560c..106c6f248 100755 --- a/src/main/java/net/openhft/chronicle/hash/serialization/internal/SerializationBuilder.java +++ b/src/main/java/net/openhft/chronicle/hash/serialization/internal/SerializationBuilder.java @@ -59,7 +59,7 @@ public final class SerializationBuilder implements Cloneable, Serializable { } private static final List knownJDKImmutableClasses = Arrays.asList( - String.class, Byte.class, Short.class, Character.class, Integer.class, + String.class, Boolean.class, Byte.class, Short.class, Character.class, Integer.class, Float.class, Long.class, Double.class, BigDecimal.class, BigInteger.class, URL.class ); @@ -137,6 +137,10 @@ public final class SerializationBuilder implements Cloneable, Serializable { } else if (eClass == StringBuilder.class) { reader((BytesReader) CharSequenceReader.ofStringBuilder()); writer((BytesWriter) CharSequenceWriter.instance()); + } else if (eClass == Boolean.class) { + sizeMarshaller(BooleanMarshaller.INSTANCE); + reader((BytesReader) BooleanMarshaller.INSTANCE); + interop((BytesInterop) BooleanMarshaller.INSTANCE); } else if (eClass == Long.class) { sizeMarshaller(LongMarshaller.INSTANCE); reader((BytesReader) LongMarshaller.INSTANCE);