Class Factory<C,D extends FactoryData,R>

java.lang.Object
edu.ie3.datamodel.io.factory.Factory<C,D,R>
Type Parameters:
C - Type of the intended target class.
D - Type of the "flat" information.
R - Type of the intended return type (might differ slightly from target class (cf. TimeBasedValueFactory)).
Direct Known Subclasses:
EntityFactory, IdCoordinateFactory, TimeBasedValueFactory

public abstract class Factory<C,D extends FactoryData,R> extends Object
Abstract factory class, that is able to transfer specific "flat" information in to actual model class instances.
  • Field Details

    • log

      public static final org.slf4j.Logger log
  • Constructor Details

    • Factory

      protected Factory(Class<? extends C>... supportedClasses)
  • Method Details

    • getSupportedClasses

      public List<Class<? extends C>> getSupportedClasses()
    • get

      public Optional<R> get(D data)
      Builds entity with data from given EntityData object after doing all kinds of checks on the data
      Parameters:
      data - EntityData (or subclass) containing the data
      Returns:
      An entity wrapped in Option if successful, an empty option otherwise
    • buildModel

      protected abstract R buildModel(D data)
      Builds model with data from given FactoryData object. Throws FactoryException if something goes wrong.
      Parameters:
      data - FactoryData (or subclass) containing the data
      Returns:
      model created from data
      Throws:
      FactoryException - if the model cannot be build
    • getFields

      protected abstract List<Set<String>> getFields(D data)
      Returns list of sets of attribute names that the entity requires to be built. At least one of these sets needs to be delivered for entity creation to be successful.
      Parameters:
      data - EntityData (or subclass) containing the data
      Returns:
      list of possible attribute sets
    • validateParameters

      protected int validateParameters(D data, Set<String>... fieldSets)
      Validates the factory specific constructor parameters in two ways. 1) the biggest set of the provided field sets is compared against fields the class implements. If this test passes then we know for sure that the field names at least in the biggest constructor are equal to the provided factory strings 2) if 1) passes, the provided entity data (which is equal to the data e.g. read from the outside) is compared to all available constructor parameters provided by the fieldSets Array. If we find exactly one constructor, that matches the field names we can proceed. Otherwise a detailed exception message is thrown.
      Parameters:
      data - the entity containing at least the entity class as well a mapping of the provided field name strings to its value (e.g. a headline of a csv to column values)
      fieldSets - a set containing all available constructor combinations as field names
      Returns:
      the index of the set in the fieldSets array that fits the provided entity data
    • getFieldsString

      protected static StringBuilder getFieldsString(Set<String>... fieldSets)
    • newSet

      protected TreeSet<String> newSet(String... attributes)
      Creates a new set of attribute names from given list of attributes. This method should always be used when returning attribute sets, i.e. through getFields(FactoryData).
      Parameters:
      attributes - attribute names
      Returns:
      new set exactly containing attribute names
    • expandSet

      protected TreeSet<String> expandSet(Set<String> attributeSet, String... more)
      Expands a set of attributes with further attributes. This method should always be used when returning attribute sets, i.e. through getting the needed fields. The set maintains a lexicographic order, that is case-insensitive.
      Parameters:
      attributeSet - set of attributes to expand
      more - attribute names to expand given set with
      Returns:
      new set exactly containing given attribute set plus additional attributes