CLTypeData.java

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

  2. import com.syntifi.casper.sdk.exception.DynamicInstanceException;
  3. import com.syntifi.casper.sdk.exception.NoSuchTypeException;
  4. import com.syntifi.casper.sdk.model.clvalue.AbstractCLValue;
  5. import com.syntifi.casper.sdk.model.clvalue.CLValueAny;
  6. import com.syntifi.casper.sdk.model.clvalue.CLValueBool;
  7. import com.syntifi.casper.sdk.model.clvalue.CLValueByteArray;
  8. import com.syntifi.casper.sdk.model.clvalue.CLValueFixedList;
  9. import com.syntifi.casper.sdk.model.clvalue.CLValueI32;
  10. import com.syntifi.casper.sdk.model.clvalue.CLValueI64;
  11. import com.syntifi.casper.sdk.model.clvalue.CLValueKey;
  12. import com.syntifi.casper.sdk.model.clvalue.CLValueList;
  13. import com.syntifi.casper.sdk.model.clvalue.CLValueMap;
  14. import com.syntifi.casper.sdk.model.clvalue.CLValueOption;
  15. import com.syntifi.casper.sdk.model.clvalue.CLValuePublicKey;
  16. import com.syntifi.casper.sdk.model.clvalue.CLValueResult;
  17. import com.syntifi.casper.sdk.model.clvalue.CLValueString;
  18. import com.syntifi.casper.sdk.model.clvalue.CLValueTuple1;
  19. import com.syntifi.casper.sdk.model.clvalue.CLValueTuple2;
  20. import com.syntifi.casper.sdk.model.clvalue.CLValueTuple3;
  21. import com.syntifi.casper.sdk.model.clvalue.CLValueU128;
  22. import com.syntifi.casper.sdk.model.clvalue.CLValueU256;
  23. import com.syntifi.casper.sdk.model.clvalue.CLValueU32;
  24. import com.syntifi.casper.sdk.model.clvalue.CLValueU512;
  25. import com.syntifi.casper.sdk.model.clvalue.CLValueU64;
  26. import com.syntifi.casper.sdk.model.clvalue.CLValueU8;
  27. import com.syntifi.casper.sdk.model.clvalue.CLValueURef;
  28. import com.syntifi.casper.sdk.model.clvalue.CLValueUnit;
  29. import com.syntifi.casper.sdk.model.storedvalue.StoredValue;
  30. import lombok.AccessLevel;
  31. import lombok.AllArgsConstructor;
  32. import lombok.Getter;

  33. import java.lang.reflect.InvocationTargetException;

  34. /**
  35.  * Casper CLType definitions and type mappings
  36.  * <p>
  37.  * All types must be listed and mapped here.
  38.  *
  39.  * @author Alexandre Carvalho
  40.  * @author Andre Bertolace
  41.  * @see StoredValue
  42.  * @since 0.0.1
  43.  */
  44. @Getter
  45. @AllArgsConstructor(access = AccessLevel.PRIVATE)
  46. public enum CLTypeData {
  47.     BOOL(AbstractCLType.BOOL, (byte) 0x0, CLValueBool.class, CLTypeBool.class),
  48.     I32(AbstractCLType.I32, (byte) 0x1, CLValueI32.class, CLTypeI32.class),
  49.     I64(AbstractCLType.I64, (byte) 0x2, CLValueI64.class, CLTypeI64.class),
  50.     U8(AbstractCLType.U8, (byte) 0x3, CLValueU8.class, CLTypeU8.class),
  51.     U32(AbstractCLType.U32, (byte) 0x4, CLValueU32.class, CLTypeU32.class),
  52.     U64(AbstractCLType.U64, (byte) 0x5, CLValueU64.class, CLTypeU64.class),
  53.     U128(AbstractCLType.U128, (byte) 0x6, CLValueU128.class, CLTypeU128.class),
  54.     U256(AbstractCLType.U256, (byte) 0x7, CLValueU256.class, CLTypeU256.class),
  55.     U512(AbstractCLType.U512, (byte) 0x8, CLValueU512.class, CLTypeU512.class),
  56.     UNIT(AbstractCLType.UNIT, (byte) 0x9, CLValueUnit.class, CLTypeUnit.class),
  57.     STRING(AbstractCLType.STRING, (byte) 0xA, CLValueString.class, CLTypeString.class),
  58.     UREF(AbstractCLType.UREF, (byte) 0xB, CLValueURef.class, CLTypeURef.class),
  59.     KEY(AbstractCLType.KEY, (byte) 0xC, CLValueKey.class, CLTypeKey.class),
  60.     OPTION(AbstractCLType.OPTION, (byte) 0xD, CLValueOption.class, CLTypeOption.class),
  61.     LIST(AbstractCLType.LIST, (byte) 0xE, CLValueList.class, CLTypeList.class),
  62.     FIXED_LIST(AbstractCLType.FIXED_LIST, (byte) 0xF, CLValueFixedList.class, CLTypeFixedList.class),
  63.     RESULT(AbstractCLType.RESULT, (byte) 0x10, CLValueResult.class, CLTypeResult.class),
  64.     MAP(AbstractCLType.MAP, (byte) 0x11, CLValueMap.class, CLTypeMap.class),
  65.     TUPLE1(AbstractCLType.TUPLE1, (byte) 0x12, CLValueTuple1.class, CLTypeTuple1.class),
  66.     TUPLE2(AbstractCLType.TUPLE2, (byte) 0x13, CLValueTuple2.class, CLTypeTuple2.class),
  67.     TUPLE3(AbstractCLType.TUPLE3, (byte) 0x14, CLValueTuple3.class, CLTypeTuple3.class),
  68.     ANY(AbstractCLType.ANY, (byte) 0x15, CLValueAny.class, CLTypeAny.class),
  69.     PUBLIC_KEY(AbstractCLType.PUBLIC_KEY, (byte) 0x16, CLValuePublicKey.class, CLTypePublicKey.class),
  70.     BYTE_ARRAY(AbstractCLType.BYTE_ARRAY, (byte) 0x17, CLValueByteArray.class, CLTypeByteArray.class);

  71.     private final String clTypeName;
  72.     private final byte serializationTag;
  73.     private final Class<? extends AbstractCLValue<?, ?>> clazz;
  74.     private final Class<? extends AbstractCLType> clTypeClass;

  75.     /**
  76.      * Retrieve CLType by its serialization tag
  77.      *
  78.      * @param serializationTag the serialization tag to find
  79.      * @return the requested {@link CLTypeData}
  80.      * @throws NoSuchTypeException raised when the clType is not valid/found
  81.      */
  82.     public static CLTypeData getTypeBySerializationTag(byte serializationTag) throws NoSuchTypeException {
  83.         for (CLTypeData clType : values()) {
  84.             if (clType.serializationTag == serializationTag) {
  85.                 return clType;
  86.             }
  87.         }
  88.         throw new NoSuchTypeException();
  89.     }

  90.     /**
  91.      * Retrieve CLValue implementation class from CLType name
  92.      *
  93.      * @param name the type's name
  94.      * @return the {@link Class} object holding the requested
  95.      * {@link AbstractCLValue}
  96.      * @throws NoSuchTypeException raised when the clType is not valid/found
  97.      */
  98.     public static Class<?> getClassByName(String name) throws NoSuchTypeException {
  99.         for (CLTypeData clType : values()) {
  100.             if (clType.clTypeName.equals(name)) {
  101.                 return clType.getClazz();
  102.             }
  103.         }
  104.         throw new NoSuchTypeException();
  105.     }

  106.     /**
  107.      * Retrieve CLType class from CLType name
  108.      *
  109.      * @param name the type's name
  110.      * @return the {@link Class} object holding the requested {@link AbstractCLType}
  111.      * @throws NoSuchTypeException raised when the clType is not valid/found
  112.      */
  113.     public static Class<?> getCLTypeClassByName(String name) throws NoSuchTypeException {
  114.         for (CLTypeData clType : values()) {
  115.             if (clType.clTypeName.equals(name)) {
  116.                 return clType.getClTypeClass();
  117.             }
  118.         }
  119.         throw new NoSuchTypeException();
  120.     }

  121.     /**
  122.      * Retrieve CLType from its name
  123.      *
  124.      * @param name the type's name
  125.      * @return the requested {@link CLTypeData}
  126.      * @throws NoSuchTypeException raised when the clType is not valid/found
  127.      */
  128.     public static CLTypeData getTypeByName(String name) throws NoSuchTypeException {
  129.         for (CLTypeData clType : values()) {
  130.             if (clType.clTypeName.equals(name)) {
  131.                 return clType;
  132.             }
  133.         }
  134.         throw new NoSuchTypeException();
  135.     }

  136.     /**
  137.      * Dynamically instantiate a CLValue when needed for decoding children objects
  138.      *
  139.      * @param clValueName the name of the {@link AbstractCLValue} to instantiate
  140.      * @return the desired {@link AbstractCLValue} implementation
  141.      * @throws DynamicInstanceException error while dynamically instantiating the
  142.      *                                  clValue
  143.      * @throws NoSuchTypeException      raised when the clType is not valid/found
  144.      */
  145.     public static AbstractCLValue<?, ?> createCLValueFromCLTypeName(String clValueName)
  146.             throws DynamicInstanceException, NoSuchTypeException {
  147.         return CLTypeData.createCLValueFromCLTypeData(CLTypeData.getTypeByName(clValueName));
  148.     }

  149.     /**
  150.      * Dynamically instantiate a CLValue when needed for decoding children objects
  151.      *
  152.      * @param clTypeData the {@link CLTypeData} to instantiate
  153.      * @return the desired {@link AbstractCLValue} implementation
  154.      * @throws DynamicInstanceException error while dynamically instantiating the
  155.      *                                  clValue
  156.      */
  157.     public static AbstractCLValue<?, ?> createCLValueFromCLTypeData(CLTypeData clTypeData)
  158.             throws DynamicInstanceException {
  159.         Class<?> clazz = clTypeData.getClazz();

  160.         try {
  161.             return (AbstractCLValue<?, ?>) clazz.getConstructor().newInstance();
  162.         } catch (InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException
  163.                 | NoSuchMethodException | SecurityException e) {
  164.             throw new DynamicInstanceException(String.format("Error while instantiating %s", clazz.getName()), e);
  165.         }
  166.     }

  167.     /**
  168.      * Dynamically instantiate a CLType when needed for decoding children objects
  169.      *
  170.      * @param clTypeName the name of the {@link AbstractCLType} to instantiate
  171.      * @return the desired {@link AbstractCLType} implementation
  172.      * @throws DynamicInstanceException error while dynamically instantiating the
  173.      *                                  clValue
  174.      * @throws NoSuchTypeException      raised when the clType is not valid/found
  175.      */
  176.     public static AbstractCLType createCLTypeFromCLTypeName(String clTypeName)
  177.             throws DynamicInstanceException, NoSuchTypeException {
  178.         return CLTypeData.createCLTypeFromCLTypeData(CLTypeData.getTypeByName(clTypeName));
  179.     }

  180.     /**
  181.      * Dynamically instantiate a CLType when needed for decoding children objects
  182.      *
  183.      * @param clTypeData the {@link CLTypeData} to instantiate
  184.      * @return the desired {@link AbstractCLType} implementation
  185.      * @throws DynamicInstanceException error while dynamically instantiating the
  186.      *                                  clValue
  187.      */
  188.     public static AbstractCLType createCLTypeFromCLTypeData(CLTypeData clTypeData) throws DynamicInstanceException {
  189.         Class<?> clazz = clTypeData.getClTypeClass();

  190.         try {
  191.             return (AbstractCLType) clazz.getConstructor().newInstance();
  192.         } catch (InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException
  193.                 | NoSuchMethodException | SecurityException e) {
  194.             throw new DynamicInstanceException(String.format("Error while instantiating %s", clazz.getName()), e);
  195.         }
  196.     }
  197. }