CLValueFixedList.java

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

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

  17. import java.io.IOException;
  18. import java.util.LinkedList;
  19. import java.util.List;

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

  35.     public CLValueFixedList(List<? extends AbstractCLValue<?, ?>> value) {
  36.         this.setValue(value);
  37.         setListType();
  38.     }

  39.     @Override
  40.     public void encode(CLValueEncoder clve, boolean encodeType) throws IOException, NoSuchTypeException, CLValueEncodeException {
  41.         setListType();

  42.         setBytes("");
  43.         for (AbstractCLValue<?, ?> child : getValue()) {
  44.             child.encode(clve, false);
  45.             setBytes(getBytes() + child.getBytes());
  46.         }
  47.         if (encodeType) {
  48.             this.encodeType(clve);
  49.         }
  50.     }

  51.     @Override
  52.     public void decode(CLValueDecoder clvd)
  53.             throws IOException, CLValueDecodeException, DynamicInstanceException, NoSuchTypeException {
  54.         CLTypeData childrenType = getClType().getListType().getClTypeData();

  55.         List<AbstractCLValue<?, ?>> list = new LinkedList<>();

  56.         boolean hasMoreItems = true;
  57.         do {
  58.             AbstractCLValue<?, ?> child = CLTypeData.createCLValueFromCLTypeData(childrenType);
  59.             if (child.getClType() instanceof AbstractCLTypeWithChildren) {
  60.                 ((AbstractCLTypeWithChildren) child.getClType())
  61.                         .setChildTypes(((AbstractCLTypeWithChildren) clType.getListType()).getChildTypes());
  62.             }
  63.             try {
  64.                 child.decode(clvd);
  65.                 list.add(child);
  66.             } catch (BufferEndCLValueDecodeException bede) {
  67.                 hasMoreItems = false;
  68.             }
  69.         } while (hasMoreItems);

  70.         setValue(list);
  71.     }

  72.     protected void setListType() {
  73.         clType.setListType(getValue().get(0).getClType());
  74.     }
  75. }