CLValueTuple2.java

  1. package com.syntifi.casper.sdk.model.clvalue;

  2. import com.fasterxml.jackson.annotation.JsonProperty;
  3. import com.syntifi.casper.sdk.exception.CLValueDecodeException;
  4. import com.syntifi.casper.sdk.exception.CLValueEncodeException;
  5. import com.syntifi.casper.sdk.exception.DynamicInstanceException;
  6. import com.syntifi.casper.sdk.exception.NoSuchTypeException;
  7. import com.syntifi.casper.sdk.model.clvalue.cltype.AbstractCLTypeWithChildren;
  8. import com.syntifi.casper.sdk.model.clvalue.cltype.CLTypeData;
  9. import com.syntifi.casper.sdk.model.clvalue.cltype.CLTypeTuple2;
  10. import com.syntifi.casper.sdk.model.clvalue.encdec.CLValueDecoder;
  11. import com.syntifi.casper.sdk.model.clvalue.encdec.CLValueEncoder;
  12. import lombok.EqualsAndHashCode;
  13. import lombok.Getter;
  14. import lombok.NoArgsConstructor;
  15. import lombok.Setter;
  16. import org.javatuples.Pair;

  17. import java.io.IOException;
  18. import java.util.Arrays;

  19. /**
  20.  * Casper Tuple2 CLValue implementation
  21.  *
  22.  * @author Alexandre Carvalho
  23.  * @author Andre Bertolace
  24.  * @see AbstractCLValue
  25.  * @since 0.0.1
  26.  */
  27. @Getter
  28. @Setter
  29. @EqualsAndHashCode(callSuper = true)
  30. @NoArgsConstructor
  31. public class CLValueTuple2
  32.         extends AbstractCLValueWithChildren<Pair<? extends AbstractCLValue<?, ?>, ? extends AbstractCLValue<?, ?>>, CLTypeTuple2> {
  33.     @JsonProperty("cl_type")
  34.     private CLTypeTuple2 clType = new CLTypeTuple2();

  35.     public CLValueTuple2(Pair<? extends AbstractCLValue<?, ?>, ? extends AbstractCLValue<?, ?>> value) {
  36.         this.setValue(value);
  37.         setChildTypes();
  38.     }

  39.     @Override
  40.     public void encode(CLValueEncoder clve, boolean encodeType) throws IOException, NoSuchTypeException, CLValueEncodeException {
  41.         setChildTypes();
  42.         getValue().getValue0().encode(clve, false);
  43.         getValue().getValue1().encode(clve, false);
  44.         setBytes(getValue().getValue0().getBytes() + getValue().getValue1().getBytes());
  45.         if (encodeType) {
  46.             this.encodeType(clve);
  47.         }
  48.     }

  49.     @Override
  50.     public void decode(CLValueDecoder clvd)
  51.             throws IOException, CLValueDecodeException, DynamicInstanceException, NoSuchTypeException {
  52.         CLTypeData childTypeData1 = clType.getChildClTypeData(0);
  53.         CLTypeData childTypeData2 = clType.getChildClTypeData(1);

  54.         AbstractCLValue<?, ?> child1 = CLTypeData.createCLValueFromCLTypeData(childTypeData1);
  55.         if (child1.getClType() instanceof AbstractCLTypeWithChildren) {
  56.             ((AbstractCLTypeWithChildren) child1.getClType())
  57.                     .setChildTypes(((AbstractCLTypeWithChildren) clType.getChildTypes().get(0)).getChildTypes());
  58.         }
  59.         child1.decode(clvd);

  60.         AbstractCLValue<?, ?> child2 = CLTypeData.createCLValueFromCLTypeData(childTypeData2);
  61.         if (child2.getClType() instanceof AbstractCLTypeWithChildren) {
  62.             ((AbstractCLTypeWithChildren) child2.getClType())
  63.                     .setChildTypes(((AbstractCLTypeWithChildren) clType.getChildTypes().get(1)).getChildTypes());
  64.         }
  65.         child2.decode(clvd);

  66.         setValue(new Pair<>(child1, child2));
  67.         setBytes(getValue().getValue0().getBytes() + getValue().getValue1().getBytes());
  68.     }

  69.     @Override
  70.     protected void setChildTypes() {
  71.         clType.setChildTypes(Arrays.asList(getValue().getValue0().getClType(), getValue().getValue1().getClType()));
  72.     }
  73. }