CLValueMap.java

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

  2. import java.io.IOException;
  3. import java.util.LinkedHashMap;
  4. import java.util.Map;
  5. import java.util.Map.Entry;

  6. import com.fasterxml.jackson.annotation.JsonProperty;
  7. import com.syntifi.casper.sdk.exception.CLValueDecodeException;
  8. import com.syntifi.casper.sdk.exception.CLValueEncodeException;
  9. import com.syntifi.casper.sdk.exception.DynamicInstanceException;
  10. import com.syntifi.casper.sdk.exception.NoSuchTypeException;
  11. import com.syntifi.casper.sdk.model.clvalue.cltype.AbstractCLTypeWithChildren;
  12. import com.syntifi.casper.sdk.model.clvalue.cltype.CLTypeData;
  13. import com.syntifi.casper.sdk.model.clvalue.cltype.CLTypeMap;
  14. import com.syntifi.casper.sdk.model.clvalue.encdec.CLValueDecoder;
  15. import com.syntifi.casper.sdk.model.clvalue.encdec.CLValueEncoder;

  16. import lombok.EqualsAndHashCode;
  17. import lombok.Getter;
  18. import lombok.NoArgsConstructor;
  19. import lombok.Setter;

  20. /**
  21.  * Casper Map 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 CLValueMap extends AbstractCLValueWithChildren<Map<? extends AbstractCLValue<?, ?>, ? extends AbstractCLValue<?, ?>>, CLTypeMap> {
  33.     @JsonProperty("cl_type")
  34.     private CLTypeMap clType = new CLTypeMap();

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

  39.     @Override
  40.     public void encode(CLValueEncoder clve)
  41.             throws IOException, CLValueEncodeException, DynamicInstanceException, NoSuchTypeException {
  42.         setChildTypes();

  43.         CLValueI32 mapLength = new CLValueI32(getValue().size());
  44.         mapLength.encode(clve);
  45.         setBytes(mapLength.getBytes());

  46.         for (Entry<? extends AbstractCLValue<?, ?>, ? extends AbstractCLValue<?, ?>> entry : getValue().entrySet()) {
  47.             entry.getKey().encode(clve);
  48.             entry.getValue().encode(clve);
  49.             setBytes(getBytes() + entry.getKey().getBytes() + entry.getValue().getBytes());
  50.         }
  51.     }

  52.     @Override
  53.     public void decode(CLValueDecoder clvd)
  54.             throws IOException, CLValueDecodeException, DynamicInstanceException, NoSuchTypeException {
  55.         CLTypeData keyType = clType.getKeyValueTypes().getKeyType().getClTypeData();
  56.         CLTypeData valType = clType.getKeyValueTypes().getValueType().getClTypeData();

  57.         Map<AbstractCLValue<?, ?>, AbstractCLValue<?, ?>> map = new LinkedHashMap<>();
  58.         CLValueI32 mapLength = new CLValueI32(0);
  59.         mapLength.decode(clvd);

  60.         for (int i = 0; i < mapLength.getValue(); i++) {
  61.             AbstractCLValue<?, ?> key = CLTypeData.createCLValueFromCLTypeData(keyType);
  62.             if (key.getClType() instanceof AbstractCLTypeWithChildren) {
  63.                 ((AbstractCLTypeWithChildren) key.getClType())
  64.                         .setChildTypes(((AbstractCLTypeWithChildren) clType.getKeyValueTypes().getKeyType()).getChildTypes());
  65.             }
  66.             key.decode(clvd);

  67.             AbstractCLValue<?, ?> val = CLTypeData.createCLValueFromCLTypeData(valType);
  68.             if (val.getClType() instanceof AbstractCLTypeWithChildren) {
  69.                 ((AbstractCLTypeWithChildren) val.getClType())
  70.                         .setChildTypes(((AbstractCLTypeWithChildren) clType.getKeyValueTypes().getValueType()).getChildTypes());
  71.             }
  72.             val.decode(clvd);

  73.             map.put(key, val);
  74.         }

  75.         setValue(map);
  76.     }

  77.     @Override
  78.     protected void setChildTypes() {
  79.         Entry<? extends AbstractCLValue<?, ?>, ? extends AbstractCLValue<?, ?>> entry = getValue().entrySet().iterator().next();

  80.         clType.setKeyValueTypes(
  81.                 clType.new CLTypeMapEntryType(entry.getKey().getClType(), entry.getValue().getClType()));
  82.     }
  83. }