Skip to content

Commit

Permalink
feat: update Decoder
Browse files Browse the repository at this point in the history
1. add lambda to Decoder;
2. add unit tests of Decoder.
  • Loading branch information
Deng-Ran committed Jan 12, 2023
1 parent 028111e commit 1b50668
Show file tree
Hide file tree
Showing 2 changed files with 225 additions and 0 deletions.
160 changes: 160 additions & 0 deletions src/main/java/org/indunet/fastproto/Decoder.java
Original file line number Diff line number Diff line change
Expand Up @@ -435,162 +435,322 @@ public Decoder readInt16(int offset) {
return this.put(CodecUtils.int16Type(this.bytes, offset, this.byteOrder));
}

public Decoder readInt16(int offset, Function<Integer, ?> formula) {
return this.put(formula.apply(CodecUtils.int16Type(this.bytes, offset, this.byteOrder)));
}

public Decoder readInt16(int offset, String name) {
return this.put(name, CodecUtils.int16Type(this.bytes, offset, this.byteOrder));
}

public Decoder readInt16(int offset, String name, Function<Integer, ?> formula) {
return this.put(name, formula.apply(CodecUtils.int16Type(this.bytes, offset, this.byteOrder)));
}

public Decoder readInt16(int offset, ByteOrder byteOrder) {
return this.put(CodecUtils.int16Type(this.bytes, offset, byteOrder));
}

public Decoder readInt16(int offset, ByteOrder byteOrder, Function<Integer, ?> formula) {
return this.put(formula.apply(CodecUtils.int16Type(this.bytes, offset, byteOrder)));
}

public Decoder readInt16(int offset, ByteOrder byteOrder, String name) {
return this.put(name, CodecUtils.int16Type(this.bytes, offset, byteOrder));
}

public Decoder readInt16(int offset, ByteOrder byteOrder, String name, Function<Integer, ?> formula) {
return this.put(name, formula.apply(CodecUtils.int16Type(this.bytes, offset, byteOrder)));
}

public Decoder readInt16() {
return this.put(CodecUtils.int16Type(this.byteBuffer, this.byteBuffer.getReadIndex(), this.byteOrder));
}

public Decoder readInt16(Function<Integer, ?> formula) {
return this.put(formula.apply(CodecUtils.int16Type(this.byteBuffer, this.byteBuffer.getReadIndex(), this.byteOrder)));
}

public Decoder readInt16(String name) {
return this.put(name, CodecUtils.int16Type(this.byteBuffer, this.byteBuffer.getReadIndex(), this.byteOrder));
}

public Decoder readInt16(String name, Function<Integer, ?> formula) {
return this.put(name, formula.apply(CodecUtils.int16Type(this.byteBuffer, this.byteBuffer.getReadIndex(), this.byteOrder)));
}

public Decoder readInt16(ByteOrder byteOrder) {
return this.put(CodecUtils.int16Type(this.byteBuffer, this.byteBuffer.getReadIndex(), byteOrder));
}

public Decoder readInt16(ByteOrder byteOrder, Function<Integer, ?> formula) {
return this.put(formula.apply(CodecUtils.int16Type(this.byteBuffer, this.byteBuffer.getReadIndex(), byteOrder)));
}

public Decoder readInt16(ByteOrder byteOrder, String name) {
return this.put(name, CodecUtils.int16Type(this.byteBuffer, this.byteBuffer.getReadIndex(), byteOrder));
}

public Decoder readInt16(ByteOrder byteOrder, String name, Function<Integer, ?> formula) {
return this.put(name, formula.apply(CodecUtils.int16Type(this.byteBuffer, this.byteBuffer.getReadIndex(), byteOrder)));
}

public Decoder readInt32(int offset) {
return this.put(CodecUtils.int32Type(this.bytes, offset, this.byteOrder));
}

public Decoder readInt32(int offset, Function<Integer, ?> formula) {
return this.put(formula.apply(CodecUtils.int32Type(this.bytes, offset, this.byteOrder)));
}

public Decoder readInt32(int offset, String name) {
return this.put(name, CodecUtils.int32Type(this.bytes, offset, this.byteOrder));
}

public Decoder readInt32(int offset, String name, Function<Integer, ?> formula) {
return this.put(name, formula.apply(CodecUtils.int32Type(this.bytes, offset, this.byteOrder)));
}

public Decoder readInt32(int offset, ByteOrder byteOrder) {
return this.put(CodecUtils.int32Type(this.bytes, offset, byteOrder));
}

public Decoder readInt32(int offset, ByteOrder byteOrder, Function<Integer, ?> formula) {
return this.put(formula.apply(CodecUtils.int32Type(this.bytes, offset, byteOrder)));
}

public Decoder readInt32(int offset, ByteOrder byteOrder, String name) {
return this.put(name, CodecUtils.int32Type(this.bytes, offset, byteOrder));
}

public Decoder readInt32(int offset, ByteOrder byteOrder, String name, Function<Integer, ?> formula) {
return this.put(name, formula.apply(CodecUtils.int32Type(this.bytes, offset, byteOrder)));
}

public Decoder readInt32() {
return this.put(CodecUtils.int32Type(this.byteBuffer, this.byteBuffer.getReadIndex(), this.byteOrder));
}

public Decoder readInt32(Function<Integer, ?> formula) {
return this.put(formula.apply(CodecUtils.int32Type(this.byteBuffer, this.byteBuffer.getReadIndex(), this.byteOrder)));
}

public Decoder readInt32(String name) {
return this.put(name, CodecUtils.int32Type(this.byteBuffer, this.byteBuffer.getReadIndex(), this.byteOrder));
}

public Decoder readInt32(String name, Function<Integer, ?> formula) {
return this.put(name, formula.apply(CodecUtils.int32Type(this.byteBuffer, this.byteBuffer.getReadIndex(), this.byteOrder)));
}

public Decoder readInt32(ByteOrder byteOrder) {
return this.put(CodecUtils.int32Type(this.byteBuffer, this.byteBuffer.getReadIndex(), byteOrder));
}

public Decoder readInt32(ByteOrder byteOrder, Function<Integer, ?> formula) {
return this.put(formula.apply(CodecUtils.int32Type(this.byteBuffer, this.byteBuffer.getReadIndex(), byteOrder)));
}

public Decoder readInt32(ByteOrder byteOrder, String name) {
return this.put(name, CodecUtils.int32Type(this.byteBuffer, this.byteBuffer.getReadIndex(), byteOrder));
}

public Decoder readInt32(ByteOrder byteOrder, String name, Function<Integer, ?> formula) {
return this.put(name, formula.apply(CodecUtils.int32Type(this.byteBuffer, this.byteBuffer.getReadIndex(), byteOrder)));
}

public Decoder readInt64(int offset) {
return this.put(CodecUtils.int64Type(this.bytes, offset, this.byteOrder));
}

public Decoder readInt64(int offset, Function<Long, ?> formula) {
return this.put(formula.apply(CodecUtils.int64Type(this.bytes, offset, this.byteOrder)));
}

public Decoder readInt64(int offset, String name) {
return this.put(name, CodecUtils.int64Type(this.bytes, offset, this.byteOrder));
}

public Decoder readInt64(int offset, String name, Function<Long, ?> formula) {
return this.put(name, formula.apply(CodecUtils.int64Type(this.bytes, offset, this.byteOrder)));
}

public Decoder readInt64(int offset, ByteOrder byteOrder) {
return this.put(CodecUtils.int64Type(this.bytes, offset, byteOrder));
}

public Decoder readInt64(int offset, ByteOrder byteOrder, Function<Long, ?> formula) {
return this.put(formula.apply(CodecUtils.int64Type(this.bytes, offset, byteOrder)));
}

public Decoder readInt64(int offset, ByteOrder byteOrder, String name) {
return this.put(name, CodecUtils.int64Type(this.bytes, offset, byteOrder));
}

public Decoder readInt64(int offset, ByteOrder byteOrder, String name, Function<Long, ?> formula) {
return this.put(name, formula.apply(CodecUtils.int64Type(this.bytes, offset, byteOrder)));
}

public Decoder readInt64() {
return this.put(CodecUtils.int64Type(this.byteBuffer, this.byteBuffer.getReadIndex(), this.byteOrder));
}

public Decoder readInt64(Function<Long, ?> formula) {
return this.put(formula.apply(CodecUtils.int64Type(this.byteBuffer, this.byteBuffer.getReadIndex(), this.byteOrder)));
}

public Decoder readInt64(String name) {
return this.put(name, CodecUtils.int64Type(this.byteBuffer, this.byteBuffer.getReadIndex(), this.byteOrder));
}

public Decoder readInt64(String name, Function<Long, ?> formula) {
return this.put(name, formula.apply(CodecUtils.int64Type(this.byteBuffer, this.byteBuffer.getReadIndex(), this.byteOrder)));
}

public Decoder readInt64(ByteOrder byteOrder) {
return this.put(CodecUtils.int64Type(this.byteBuffer, this.byteBuffer.getReadIndex(), byteOrder));
}

public Decoder readInt64(ByteOrder byteOrder, Function<Long, ?> formula) {
return this.put(formula.apply(CodecUtils.int64Type(this.byteBuffer, this.byteBuffer.getReadIndex(), byteOrder)));
}

public Decoder readInt64(ByteOrder byteOrder, String name) {
return this.put(name, CodecUtils.int64Type(this.byteBuffer, this.byteBuffer.getReadIndex(), byteOrder));
}

public Decoder readInt64(ByteOrder byteOrder, String name, Function<Long, ?> formula) {
return this.put(name, formula.apply(CodecUtils.int64Type(this.byteBuffer, this.byteBuffer.getReadIndex(), byteOrder)));
}

public Decoder readFloat(int offset) {
return this.put(CodecUtils.floatType(this.bytes, offset, this.byteOrder));
}

public Decoder readFloat(int offset, Function<Float, ?> formula) {
return this.put(formula.apply(CodecUtils.floatType(this.bytes, offset, this.byteOrder)));
}

public Decoder readFloat(int offset, String name) {
return this.put(name, CodecUtils.floatType(this.bytes, offset, this.byteOrder));
}

public Decoder readFloat(int offset, String name, Function<Float, ?> formula) {
return this.put(name, formula.apply(CodecUtils.floatType(this.bytes, offset, this.byteOrder)));
}

public Decoder readFloat(int offset, ByteOrder byteOrder) {
return this.put(CodecUtils.floatType(this.bytes, offset, byteOrder));
}

public Decoder readFloat(int offset, ByteOrder byteOrder, Function<Float, ?> formula) {
return this.put(formula.apply(CodecUtils.floatType(this.bytes, offset, byteOrder)));
}

public Decoder readFloat(int offset, ByteOrder byteOrder, String name) {
return this.put(name, CodecUtils.floatType(this.bytes, offset, byteOrder));
}

public Decoder readFloat(int offset, ByteOrder byteOrder, String name, Function<Float, ?> formula) {
return this.put(name, formula.apply(CodecUtils.floatType(this.bytes, offset, byteOrder)));
}

public Decoder readFloat() {
return this.put(CodecUtils.floatType(this.byteBuffer, this.byteBuffer.getReadIndex(), this.byteOrder));
}

public Decoder readFloat(Function<Float, ?> formula) {
return this.put(formula.apply(CodecUtils.floatType(this.byteBuffer, this.byteBuffer.getReadIndex(), this.byteOrder)));
}

public Decoder readFloat(String name) {
return this.put(name, CodecUtils.floatType(this.byteBuffer, this.byteBuffer.getReadIndex(), this.byteOrder));
}

public Decoder readFloat(String name, Function<Float, ?> formula) {
return this.put(name, formula.apply(CodecUtils.floatType(this.byteBuffer, this.byteBuffer.getReadIndex(), this.byteOrder)));
}

public Decoder readFloat(ByteOrder byteOrder) {
return this.put(CodecUtils.floatType(this.byteBuffer, this.byteBuffer.getReadIndex(), byteOrder));
}

public Decoder readFloat(ByteOrder byteOrder, Function<Float, ?> formula) {
return this.put(formula.apply(CodecUtils.floatType(this.byteBuffer, this.byteBuffer.getReadIndex(), byteOrder)));
}

public Decoder readFloat(ByteOrder byteOrder, String name) {
return this.put(name, CodecUtils.floatType(this.byteBuffer, this.byteBuffer.getReadIndex(), byteOrder));
}

public Decoder readFloat(ByteOrder byteOrder, String name, Function<Float, ?> formula) {
return this.put(name, formula.apply(CodecUtils.floatType(this.byteBuffer, this.byteBuffer.getReadIndex(), byteOrder)));
}

public Decoder readDouble(int offset) {
return this.put(CodecUtils.doubleType(this.bytes, offset, this.byteOrder));
}

public Decoder readDouble(int offset, Function<Double, ?> formula) {
return this.put(formula.apply(CodecUtils.doubleType(this.bytes, offset, this.byteOrder)));
}

public Decoder readDouble(int offset, String name) {
return this.put(name, CodecUtils.doubleType(this.bytes, offset, this.byteOrder));
}

public Decoder readDouble(int offset, String name, Function<Double, ?> formula) {
return this.put(name, formula.apply(CodecUtils.doubleType(this.bytes, offset, this.byteOrder)));
}

public Decoder readDouble(int offset, ByteOrder byteOrder) {
return this.put(CodecUtils.doubleType(this.bytes, offset, byteOrder));
}

public Decoder readDouble(int offset, ByteOrder byteOrder, Function<Double, ?> formula) {
return this.put(formula.apply(CodecUtils.doubleType(this.bytes, offset, byteOrder)));
}

public Decoder readDouble(int offset, ByteOrder byteOrder, String name) {
return this.put(name, CodecUtils.doubleType(this.bytes, offset, byteOrder));
}

public Decoder readDouble(int offset, ByteOrder byteOrder, String name, Function<Double, ?> formula) {
return this.put(name, formula.apply(CodecUtils.doubleType(this.bytes, offset, byteOrder)));
}

public Decoder readDouble() {
return this.put(CodecUtils.doubleType(this.byteBuffer, this.byteBuffer.getReadIndex(), this.byteOrder));
}

public Decoder readDouble(Function<Double, ?> formula) {
return this.put(formula.apply(CodecUtils.doubleType(this.byteBuffer, this.byteBuffer.getReadIndex(), this.byteOrder)));
}

public Decoder readDouble(String name) {
return this.put(name, CodecUtils.doubleType(this.byteBuffer, this.byteBuffer.getReadIndex(), this.byteOrder));
}

public Decoder readDouble(String name, Function<Double, ?> formula) {
return this.put(name, formula.apply(CodecUtils.doubleType(this.byteBuffer, this.byteBuffer.getReadIndex(), this.byteOrder)));
}

public Decoder readDouble(ByteOrder byteOrder) {
return this.put(CodecUtils.doubleType(this.byteBuffer, this.byteBuffer.getReadIndex(), byteOrder));
}

public Decoder readDouble(ByteOrder byteOrder, Function<Double, ?> formula) {
return this.put(formula.apply(CodecUtils.doubleType(this.byteBuffer, this.byteBuffer.getReadIndex(), byteOrder)));
}

public Decoder readDouble(ByteOrder byteOrder, String name) {
return this.put(name, CodecUtils.doubleType(this.byteBuffer, this.byteBuffer.getReadIndex(), byteOrder));
}

public Decoder readDouble(ByteOrder byteOrder, String name, Function<Double, ?> formula) {
return this.put(name, formula.apply(CodecUtils.doubleType(this.byteBuffer, this.byteBuffer.getReadIndex(), byteOrder)));
}

public boolean getAsBoolean() {
return (Boolean) this.last;
}
Expand Down
65 changes: 65 additions & 0 deletions src/test/java/org/indunet/fastproto/DecoderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,71 @@ public void testRead3() {
assertEquals(2.71, actual.get("double64"));
}

@Test
public void testRead4() {
val bytes = new byte[] {
0x01,
0x01, 0x02,
0x01,
0x01, 0x02,
0x03, 0x04, 0x05, 0x06,
0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08
};
val actual = FastProto.parse(bytes)
.defaultByteOrder(ByteOrder.BIG)
.readByte("byte1", x -> x * 0.1)
.readShort("short16", x -> x * 10)
.readInt8("int8", x -> x * 0.2)
.readInt16("int16", x -> x / 10)
.readInt32("int32", x -> x / 100.0)
.readInt64("int64", x -> x * 0.01)
.getAsMap();

assertEquals((byte) 0x01 * 0.1, actual.get("byte1"));
assertEquals((short) 0x0102 * 10, actual.get("short16"));
assertEquals(0x01 * 0.2, actual.get("int8"));
assertEquals(0x0102 / 10, actual.get("int16"));
assertEquals(0x03040506 / 100.0, actual.get("int32"));
assertEquals(0x0102030405060708l * 0.01, actual.get("int64"));
}

@Test
public void testRead5() {
val bytes = new byte[] {
0x01,
0x01, 0x02,
0x03, 0x04, 0x05, 0x06,
0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08
};
val actual = FastProto.parse(bytes)
.defaultByteOrder(ByteOrder.LITTLE)
.readUInt8("uint8", x -> x * 0.1)
.readUInt16("uint16", x -> x * 10)
.readUInt32("uint32", x -> x / 100.0)
.readUInt64("uint64", x -> x.multiply(BigInteger.valueOf(10l)))
.getAsMap();

assertEquals(0x01 * 0.1, actual.get("uint8"));
assertEquals(0x0201 * 10, actual.get("uint16"));
assertEquals(0x06050403l / 100.0, actual.get("uint32"));
assertEquals(BigInteger.valueOf(0x0807060504030201l).multiply(BigInteger.valueOf(10l)), actual.get("uint64"));
}

@Test
public void testRead6() {
val bytes = new byte[12];
System.arraycopy(BinaryUtils.valueOf(3.14f), 0, bytes, 0, FloatType.SIZE);
System.arraycopy(BinaryUtils.valueOf(2.71), 0, bytes, 4, DoubleType.SIZE);

val actual = FastProto.parse(bytes)
.readFloat("float32", x -> x * 0.1)
.readDouble("double64", x -> x * 10)
.getAsMap();

assertEquals(3.14f * 0.1, actual.get("float32"));
assertEquals(2.71 * 10, actual.get("double64"));
}

@Test
public void testGetAsType() {
val bytes = new byte[]{1, 2, 3, 0, 0, 0, 1, 0};
Expand Down

0 comments on commit 1b50668

Please sign in to comment.