AbstractCLTypeWithChildren.java

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

  2. import com.fasterxml.jackson.annotation.JsonIgnore;
  3. import com.syntifi.casper.sdk.exception.NoSuchTypeException;
  4. import lombok.EqualsAndHashCode;
  5. import lombok.Getter;
  6. import lombok.Setter;

  7. import java.lang.reflect.InvocationTargetException;
  8. import java.util.ArrayList;
  9. import java.util.LinkedHashMap;
  10. import java.util.List;
  11. import java.util.Map.Entry;

  12. /**
  13.  * Base class for all types which have an array of child types
  14.  *
  15.  * @author Alexandre Carvalho
  16.  * @author Andre Bertolace
  17.  * @since 0.0.1
  18.  */
  19. @Setter
  20. @Getter
  21. @EqualsAndHashCode(callSuper = false, of = {"childTypes"})
  22. public abstract class AbstractCLTypeWithChildren extends AbstractCLType {

  23.     @JsonIgnore
  24.     private List<AbstractCLType> childTypes = new ArrayList<>();

  25.     private List<Object> childTypeObjects;

  26.     protected void setChildTypeObjects(List<Object> childTypeObjects)
  27.             throws InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException,
  28.             NoSuchMethodException, SecurityException, NoSuchTypeException {
  29.         this.childTypeObjects = childTypeObjects;
  30.         this.loadCLTypes(childTypeObjects);
  31.     }

  32.     protected List<Object> getChildTypeObjects() {
  33.         if (this.childTypeObjects == null) {
  34.             this.childTypeObjects = new ArrayList<>();
  35.         }
  36.         this.childTypeObjects.clear();

  37.         for (AbstractCLType childType : getChildTypes()) {
  38.             if (childType instanceof AbstractCLTypeBasic) {
  39.                 this.childTypeObjects.add(childType.getTypeName());
  40.             } else {
  41.                 this.childTypeObjects.add(childType);
  42.             }
  43.         }

  44.         return this.childTypeObjects;
  45.     }

  46.     @JsonIgnore
  47.     public CLTypeData getChildClTypeData(int index) throws NoSuchTypeException {
  48.         return CLTypeData.getTypeByName(getChildTypes().get(index).getTypeName());
  49.     }

  50.     protected void loadCLTypes(List<Object> childTypeObjects)
  51.             throws InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException,
  52.             NoSuchMethodException, SecurityException, NoSuchTypeException {
  53.         childTypes.clear();

  54.         if (childTypeObjects != null) {
  55.             for (Object childTypeObject : childTypeObjects) {
  56.                 addChildType(childTypeObject, childTypes);
  57.             }
  58.         }
  59.     }

  60.     private void addChildType(Object childTypeObject, List<AbstractCLType> parent)
  61.             throws InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException,
  62.             NoSuchMethodException, SecurityException, NoSuchTypeException {
  63.         if (childTypeObject instanceof String) {
  64.             parent.add(CLTypeData.getTypeByName(childTypeObject.toString()).getClTypeClass().getConstructor()
  65.                     .newInstance());
  66.         } else if (childTypeObject instanceof ArrayList) {
  67.             for (Object child : (ArrayList<?>) childTypeObject) {
  68.                 addChildType(child, parent);
  69.             }
  70.         } else if (childTypeObject instanceof LinkedHashMap) {
  71.             LinkedHashMap<?, ?> subChildTypes = (LinkedHashMap<?, ?>) childTypeObject;

  72.             for (Entry<?, ?> entry : subChildTypes.entrySet()) {
  73.                 AbstractCLType nextParent = CLTypeData.getTypeByName(entry.getKey().toString()).getClTypeClass()
  74.                         .getConstructor().newInstance();
  75.                 parent.add(nextParent);
  76.                 addChildType(entry.getValue(), ((AbstractCLTypeWithChildren) nextParent).getChildTypes());
  77.             }
  78.         }
  79.     }
  80. }