diff --git a/cory-core/src/main/java/moe/rafal/cory/serdes/PacketPacker.java b/cory-core/src/main/java/moe/rafal/cory/serdes/PacketPacker.java index 15c8f8e..883cde2 100644 --- a/cory-core/src/main/java/moe/rafal/cory/serdes/PacketPacker.java +++ b/cory-core/src/main/java/moe/rafal/cory/serdes/PacketPacker.java @@ -33,21 +33,21 @@ public interface PacketPacker extends Closeable { PacketPacker packString(String value) throws IOException; - PacketPacker packBoolean(boolean value) throws IOException; + PacketPacker packBoolean(Boolean value) throws IOException; - PacketPacker packInt(int value) throws IOException; + PacketPacker packInt(Integer value) throws IOException; - PacketPacker packByte(byte value) throws IOException; + PacketPacker packByte(Byte value) throws IOException; - PacketPacker packLong(long value) throws IOException; + PacketPacker packLong(Long value) throws IOException; PacketPacker packUUID(UUID value) throws IOException; - PacketPacker packShort(short value) throws IOException; + PacketPacker packShort(Short value) throws IOException; - PacketPacker packFloat(float value) throws IOException; + PacketPacker packFloat(Float value) throws IOException; - PacketPacker packDouble(double value) throws IOException; + PacketPacker packDouble(Double value) throws IOException; PacketPacker packMapHeader(int value) throws IOException; diff --git a/cory-core/src/main/java/moe/rafal/cory/serdes/PacketUnpacker.java b/cory-core/src/main/java/moe/rafal/cory/serdes/PacketUnpacker.java index 902d084..219fd3e 100644 --- a/cory-core/src/main/java/moe/rafal/cory/serdes/PacketUnpacker.java +++ b/cory-core/src/main/java/moe/rafal/cory/serdes/PacketUnpacker.java @@ -35,21 +35,21 @@ public interface PacketUnpacker extends Closeable { String unpackString() throws IOException; - boolean unpackBoolean() throws IOException; + Boolean unpackBoolean() throws IOException; - int unpackInt() throws IOException; + Integer unpackInt() throws IOException; - byte unpackByte() throws IOException; + Byte unpackByte() throws IOException; - long unpackLong() throws IOException; + Long unpackLong() throws IOException; UUID unpackUUID() throws IOException; - short unpackShort() throws IOException; + Short unpackShort() throws IOException; - float unpackFloat() throws IOException; + Float unpackFloat() throws IOException; - double unpackDouble() throws IOException; + Double unpackDouble() throws IOException; int unpackMapHeader() throws IOException; diff --git a/cory-serdes-msgpack/pom.xml b/cory-serdes-msgpack/pom.xml index 4b929f1..dd6ca84 100644 --- a/cory-serdes-msgpack/pom.xml +++ b/cory-serdes-msgpack/pom.xml @@ -22,6 +22,12 @@ 0.9.5 compile + + com.pivovarit + throwing-function + 1.5.1 + compile + moe.rafal cory-core diff --git a/cory-serdes-msgpack/src/main/java/moe/rafal/cory/serdes/MessagePackPacketPacker.java b/cory-serdes-msgpack/src/main/java/moe/rafal/cory/serdes/MessagePackPacketPacker.java index 7a8d4e2..fd00fa9 100644 --- a/cory-serdes-msgpack/src/main/java/moe/rafal/cory/serdes/MessagePackPacketPacker.java +++ b/cory-serdes-msgpack/src/main/java/moe/rafal/cory/serdes/MessagePackPacketPacker.java @@ -17,11 +17,13 @@ package moe.rafal.cory.serdes; +import com.pivovarit.function.ThrowingBiConsumer; import java.io.IOException; import java.time.Duration; import java.time.Instant; import java.util.UUID; import org.msgpack.core.MessageBufferPacker; +import org.msgpack.core.MessagePacker; class MessagePackPacketPacker implements PacketPacker { @@ -51,62 +53,50 @@ public PacketPacker packPayload(byte[] value) throws IOException { @Override public PacketPacker packString(String value) throws IOException { - underlyingPacker.packString(value); - return this; + return packOrNil(value, MessagePacker::packString); } @Override - public PacketPacker packBoolean(boolean value) throws IOException { - underlyingPacker.packBoolean(value); - return this; + public PacketPacker packBoolean(Boolean value) throws IOException { + return packOrNil(value, MessagePacker::packBoolean); } @Override - public PacketPacker packInt(int value) throws IOException { - underlyingPacker.packInt(value); - return this; + public PacketPacker packInt(Integer value) throws IOException { + return packOrNil(value, MessagePacker::packInt); } @Override - public PacketPacker packByte(byte value) throws IOException { - underlyingPacker.packByte(value); - return this; + public PacketPacker packByte(Byte value) throws IOException { + return packOrNil(value, MessagePacker::packByte); } @Override - public PacketPacker packLong(long value) throws IOException { - underlyingPacker.packLong(value); - return this; + public PacketPacker packLong(Long value) throws IOException { + return packOrNil(value, MessagePacker::packLong); } @Override public PacketPacker packUUID(UUID value) throws IOException { - if (value == null) { - underlyingPacker.packNil(); - return this; - } - - underlyingPacker.packLong(value.getMostSignificantBits()); - underlyingPacker.packLong(value.getLeastSignificantBits()); - return this; + return packOrNil(value, (packer, ignored) -> { + packer.packLong(value.getMostSignificantBits()); + packer.packLong(value.getLeastSignificantBits()); + }); } @Override - public PacketPacker packShort(short value) throws IOException { - underlyingPacker.packShort(value); - return this; + public PacketPacker packShort(Short value) throws IOException { + return packOrNil(value, MessagePacker::packShort); } @Override - public PacketPacker packFloat(float value) throws IOException { - underlyingPacker.packFloat(value); - return this; + public PacketPacker packFloat(Float value) throws IOException { + return packOrNil(value, MessagePacker::packFloat); } @Override - public PacketPacker packDouble(double value) throws IOException { - underlyingPacker.packDouble(value); - return this; + public PacketPacker packDouble(Double value) throws IOException { + return packOrNil(value, MessagePacker::packDouble); } @Override @@ -151,4 +141,15 @@ public PacketPacker flush() throws IOException { public void close() throws IOException { underlyingPacker.close(); } + + private PacketPacker packOrNil(T value, + ThrowingBiConsumer packFunction) throws IOException { + if (value == null) { + underlyingPacker.packNil(); + return this; + } + + packFunction.accept(underlyingPacker, value); + return this; + } } diff --git a/cory-serdes-msgpack/src/main/java/moe/rafal/cory/serdes/MessagePackPacketUnpacker.java b/cory-serdes-msgpack/src/main/java/moe/rafal/cory/serdes/MessagePackPacketUnpacker.java index a05d505..685b636 100644 --- a/cory-serdes-msgpack/src/main/java/moe/rafal/cory/serdes/MessagePackPacketUnpacker.java +++ b/cory-serdes-msgpack/src/main/java/moe/rafal/cory/serdes/MessagePackPacketUnpacker.java @@ -19,6 +19,7 @@ import static org.msgpack.core.MessageFormat.NIL; +import com.pivovarit.function.ThrowingFunction; import java.io.IOException; import java.time.Duration; import java.time.Instant; @@ -59,23 +60,23 @@ public String unpackString() throws IOException { } @Override - public boolean unpackBoolean() throws IOException { + public Boolean unpackBoolean() throws IOException { return underlyingUnpacker.unpackBoolean(); } @Override - public int unpackInt() throws IOException { - return underlyingUnpacker.unpackInt(); + public Integer unpackInt() throws IOException { + return unpackOrNil(MessageUnpacker::unpackInt); } @Override - public byte unpackByte() throws IOException { - return underlyingUnpacker.unpackByte(); + public Byte unpackByte() throws IOException { + return unpackOrNil(MessageUnpacker::unpackByte); } @Override - public long unpackLong() throws IOException { - return underlyingUnpacker.unpackLong(); + public Long unpackLong() throws IOException { + return unpackOrNil(MessageUnpacker::unpackLong); } @Override @@ -90,18 +91,18 @@ public UUID unpackUUID() throws IOException { } @Override - public short unpackShort() throws IOException { - return underlyingUnpacker.unpackShort(); + public Short unpackShort() throws IOException { + return unpackOrNil(MessageUnpacker::unpackShort); } @Override - public float unpackFloat() throws IOException { - return underlyingUnpacker.unpackFloat(); + public Float unpackFloat() throws IOException { + return unpackOrNil(MessageUnpacker::unpackFloat); } @Override - public double unpackDouble() throws IOException { - return underlyingUnpacker.unpackDouble(); + public Double unpackDouble() throws IOException { + return unpackOrNil(MessageUnpacker::unpackDouble); } @Override @@ -150,4 +151,9 @@ public boolean hasNextNilValue() throws IOException { public void close() throws IOException { underlyingUnpacker.close(); } + + private T unpackOrNil(ThrowingFunction unpackFunction) + throws IOException { + return hasNextNilValue() ? null : unpackFunction.apply(underlyingUnpacker); + } }