Skip to content

Commit

Permalink
issues/251 - Refactor removal of duplicate code
Browse files Browse the repository at this point in the history
  • Loading branch information
meywood committed Mar 7, 2024
1 parent ccce34b commit 9ae710e
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,13 @@ protected void setJsonBytes(String bytes) {
}
}

protected void encodeType(final SerializerBuffer ser) throws NoSuchTypeException {
super.encodeType(ser);
encodeChildTypes(ser);
}

protected abstract void encodeChildTypes(final SerializerBuffer ser) throws NoSuchTypeException;

/**
* Encodes the bytes of the child type, if the child value is not present but the type is known from the parent type
* info, the childDataType is used to encode the bytes of the child rather than the child value.
Expand Down
6 changes: 2 additions & 4 deletions src/main/java/com/casper/sdk/model/clvalue/CLValueList.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,8 @@ protected void serializeValue(final SerializerBuffer ser) throws ValueSerializat
}

@Override
protected void encodeType(final SerializerBuffer ser) throws NoSuchTypeException {
super.encodeType(ser);

byte val = (getClType().getListType().getClTypeData().getSerializationTag());
protected void encodeChildTypes(final SerializerBuffer ser) throws NoSuchTypeException {
final byte val = (getClType().getListType().getClTypeData().getSerializationTag());
ser.writeU8(val);
}

Expand Down
3 changes: 1 addition & 2 deletions src/main/java/com/casper/sdk/model/clvalue/CLValueMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,7 @@ protected void serializeValue(final SerializerBuffer ser) throws ValueSerializat
}

@Override
protected void encodeType(final SerializerBuffer ser) throws NoSuchTypeException {
super.encodeType(ser);
protected void encodeChildTypes(final SerializerBuffer ser) throws NoSuchTypeException {

final byte keyTypeTag = (getClType().getKeyValueTypes().getKeyType().getClTypeData().getSerializationTag());
ser.writeU8(keyTypeTag);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,7 @@ public void deserializeCustom(final DeserializerBuffer deser) throws Exception {
}

@Override
protected void encodeType(final SerializerBuffer ser) throws NoSuchTypeException {
super.encodeType(ser);

protected void encodeChildTypes(final SerializerBuffer ser) throws NoSuchTypeException {
final Optional<AbstractCLValue<?, ?>> child = getValue();
if (child.isPresent()) {
encodeChildType(ser, child.get(), this.getClType().getOptionType().getClTypeData());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,7 @@ protected void serializeValue(final SerializerBuffer ser) throws ValueSerializat
}

@Override
protected void encodeType(final SerializerBuffer ser) throws NoSuchTypeException {
super.encodeType(ser);
protected void encodeChildTypes(final SerializerBuffer ser) throws NoSuchTypeException {
encodeChildType(ser, this.getValue().getValue0(), getClType().getChildClTypeData(0));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,7 @@ protected void serializeValue(final SerializerBuffer ser) throws ValueSerializat
}

@Override
protected void encodeType(final SerializerBuffer ser) throws NoSuchTypeException {
super.encodeType(ser);

protected void encodeChildTypes(final SerializerBuffer ser) throws NoSuchTypeException {
encodeChildType(ser, this.getValue().getValue0(), getClType().getChildClTypeData(0));
encodeChildType(ser, this.getValue().getValue1(), getClType().getChildClTypeData(1));
}
Expand Down
30 changes: 14 additions & 16 deletions src/main/java/com/casper/sdk/model/clvalue/CLValueTuple3.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,58 +37,56 @@ public class CLValueTuple3 extends
private CLTypeTuple3 clType = new CLTypeTuple3();

@JsonSetter("cl_type")
public void setClType(CLTypeTuple3 clType) {
public void setClType(final CLTypeTuple3 clType) {
this.clType = clType;
childTypesSet();
}

public CLValueTuple3(Triplet<? extends AbstractCLValue<?, ?>, ? extends AbstractCLValue<?, ?>, ? extends AbstractCLValue<?, ?>> value) throws ValueSerializationException {
public CLValueTuple3(final Triplet<? extends AbstractCLValue<?, ?>, ? extends AbstractCLValue<?, ?>, ? extends AbstractCLValue<?, ?>> value) throws ValueSerializationException {
setChildTypes(value);
this.setValue(value);
}

@Override
protected void serializeValue(SerializerBuffer ser) throws ValueSerializationException {
SerializerBuffer serVal = new SerializerBuffer();
protected void serializeValue(final SerializerBuffer ser) throws ValueSerializationException {
final SerializerBuffer serVal = new SerializerBuffer();
setChildTypes(this.getValue());
getValue().getValue0().serialize(serVal);
getValue().getValue1().serialize(serVal);
getValue().getValue2().serialize(serVal);
byte[] bytes = serVal.toByteArray();
final byte[] bytes = serVal.toByteArray();
ser.writeByteArray(bytes);
this.setBytes(Hex.toHexString(bytes));
}

@Override
protected void encodeType(SerializerBuffer ser) throws NoSuchTypeException {
super.encodeType(ser);

protected void encodeChildTypes(SerializerBuffer ser) throws NoSuchTypeException {
encodeChildType(ser, this.getValue().getValue0(), getClType().getChildClTypeData(0));
encodeChildType(ser, this.getValue().getValue1(), getClType().getChildClTypeData(1));
encodeChildType(ser, this.getValue().getValue2(), getClType().getChildClTypeData(2));
}

@Override
public void deserializeCustom(DeserializerBuffer deser) throws Exception {
CLTypeData childTypeData1 = clType.getChildClTypeData(0);
CLTypeData childTypeData2 = clType.getChildClTypeData(1);
CLTypeData childTypeData3 = clType.getChildClTypeData(2);
public void deserializeCustom(final DeserializerBuffer deser) throws Exception {
final CLTypeData childTypeData1 = clType.getChildClTypeData(0);
final CLTypeData childTypeData2 = clType.getChildClTypeData(1);
final CLTypeData childTypeData3 = clType.getChildClTypeData(2);

AbstractCLValue<?, ?> child1 = CLTypeData.createCLValueFromCLTypeData(childTypeData1);
final AbstractCLValue<?, ?> child1 = CLTypeData.createCLValueFromCLTypeData(childTypeData1);
if (child1.getClType() instanceof AbstractCLTypeWithChildren) {
((AbstractCLTypeWithChildren) child1.getClType())
.setChildTypes(((AbstractCLTypeWithChildren) clType.getChildTypes().get(0)).getChildTypes());
}
child1.deserializeCustom(deser);

AbstractCLValue<?, ?> child2 = CLTypeData.createCLValueFromCLTypeData(childTypeData2);
final AbstractCLValue<?, ?> child2 = CLTypeData.createCLValueFromCLTypeData(childTypeData2);
if (child2.getClType() instanceof AbstractCLTypeWithChildren) {
((AbstractCLTypeWithChildren) child2.getClType())
.setChildTypes(((AbstractCLTypeWithChildren) clType.getChildTypes().get(1)).getChildTypes());
}
child2.deserializeCustom(deser);

AbstractCLValue<?, ?> child3 = CLTypeData.createCLValueFromCLTypeData(childTypeData3);
final AbstractCLValue<?, ?> child3 = CLTypeData.createCLValueFromCLTypeData(childTypeData3);
if (child3.getClType() instanceof AbstractCLTypeWithChildren) {
((AbstractCLTypeWithChildren) child3.getClType())
.setChildTypes(((AbstractCLTypeWithChildren) clType.getChildTypes().get(2)).getChildTypes());
Expand All @@ -99,7 +97,7 @@ public void deserializeCustom(DeserializerBuffer deser) throws Exception {
}

@Override
protected void setChildTypes(Triplet<? extends AbstractCLValue<?, ?>, ? extends AbstractCLValue<?, ?>, ? extends AbstractCLValue<?, ?>> value) {
protected void setChildTypes(final Triplet<? extends AbstractCLValue<?, ?>, ? extends AbstractCLValue<?, ?>, ? extends AbstractCLValue<?, ?>> value) {
if (value.getValue0() != null && value.getValue1() != null && value.getValue2() != null) {
clType.setChildTypes(Arrays.asList(value.getValue0().getClType(), value.getValue1().getClType(),
value.getValue2().getClType()));
Expand Down

0 comments on commit 9ae710e

Please sign in to comment.