Class FeatureContainer<T extends IFeature>

java.lang.Object
com.amalgamasimulation.features.FeatureContainer<T>
Type Parameters:
T - base type for all the features contained in this feature container
All Implemented Interfaces:
IFeatureContainer<T>

public class FeatureContainer<T extends IFeature> extends Object implements IFeatureContainer<T>
Base class with standard implementation of IFeatureContainer interface.
Author:
Andrey Malykhanov, Vitaliy Chernenko
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    protected Map<Class<?>,T>
     
    protected List<T>
     
    protected boolean
     
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    <F extends T>
    F
    addFeature(F feature)
    Adds the specified feature to this container.
    <F extends T>
    F
    addFeature(F feature, boolean toBeginningOfFeaturesList)
    Adds the specified feature to this container.
    <F, R> R
    calculateIfFeaturePresent(Class<F> featureClass, Function<F,R> function, Supplier<R> supplierIfNotPresent)
    If this container has a feature of the specified type, then calculates the result with the function taking the feature as a parameter.
    <F, R> R
    calculateIfFeaturePresent(Class<F> featureClass, Function<F,R> function, R valueIfNotPresent)
    If this container has a feature of the specified type, then calculates the result with the function taking the feature as a parameter.
    Returns the copy of this feature container.
    <F> void
    doIfFeaturePresent(Class<F> featureClass, Consumer<F> consumer)
    If this container has a feature of the specified type, then executes the specified action with this feature.
    Returns the list of all features contained in this feature container.
    <F> F
    getFeature(Class<F> featureClass)
    Returns the feature of the specified type contained in this feature container, or null if there is no such feature
    <F> List<F>
    getFeatures(Class<F> featureClass)
    Returns a list of features that are inheritors of the specified type contained in the container of this feature.
    <F> Optional<F>
    getOptionalFeature(Class<F> featureClass)
    Returns the Optional instance containing the feature of the specified type contained in this feature container, or empty Optional if there is no such feature
    <F> boolean
    hasFeature(Class<F> featureClass)
    Checks if this container has a feature of the specified type.
    void
    Initializes the feature container and calls IFeature.initialize() method for all the features contained in this container.
    <F extends T>
    F
    replaceFeature(Class<F> featureClass, F feature)
    Replaces the existing feature of the specified type with the specified new feature.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

    Methods inherited from interface com.amalgamasimulation.features.IFeatureContainer

    getFeatureOrThrow, replaceFeature
  • Field Details

    • features

      protected Map<Class<?>,T extends IFeature> features
    • featuresList

      protected List<T extends IFeature> featuresList
    • initialized

      protected boolean initialized
  • Constructor Details

    • FeatureContainer

      public FeatureContainer()
  • Method Details

    • addFeature

      public <F extends T> F addFeature(F feature)
      Description copied from interface: IFeatureContainer
      Adds the specified feature to this container. The feature is added to the end of the features list. Throws a RuntimeException if a feature of the same non-anonymous class is already contained in this container
      Specified by:
      addFeature in interface IFeatureContainer<T extends IFeature>
      Type Parameters:
      F - type of the feature being added
      Parameters:
      feature - feature being added
      Returns:
      reference to the feature being added
    • hasFeature

      public <F> boolean hasFeature(Class<F> featureClass)
      Description copied from interface: IFeatureContainer
      Checks if this container has a feature of the specified type.
      Specified by:
      hasFeature in interface IFeatureContainer<T extends IFeature>
      Type Parameters:
      F - type of the feature being checked
      Parameters:
      featureClass - Class object of the type of the feature being checked
      Returns:
      true if this container contains a feature of the specified type, false otherwise
    • addFeature

      public <F extends T> F addFeature(F feature, boolean toBeginningOfFeaturesList)
      Description copied from interface: IFeatureContainer
      Adds the specified feature to this container. Allows the user to specify whether to add the feature to the beginning or to the end of the features list. Throws a RuntimeException if a feature of the same non-anonymous class is already contained in this container
      Specified by:
      addFeature in interface IFeatureContainer<T extends IFeature>
      Type Parameters:
      F - type of the feature being added
      Parameters:
      feature - feature being added
      toBeginningOfFeaturesList - if true, then the feature will be added to the beginning of the features list, otherwise if will be added to the end of the features list
      Returns:
      reference to the feature being added
    • replaceFeature

      public <F extends T> F replaceFeature(Class<F> featureClass, F feature)
      Description copied from interface: IFeatureContainer
      Replaces the existing feature of the specified type with the specified new feature. The container must contain a feature of the specified type. Otherwise and IllegalArgumentException will be thrown.
      Specified by:
      replaceFeature in interface IFeatureContainer<T extends IFeature>
      Type Parameters:
      F - type of the feature being replaced
      Parameters:
      featureClass - Class object of the type of the feature being replaced
      feature - feature that replaces the existing one
      Returns:
      reference to the feature newly replaced feature
    • getFeature

      public <F> F getFeature(Class<F> featureClass)
      Description copied from interface: IFeatureContainer
      Returns the feature of the specified type contained in this feature container, or null if there is no such feature
      Specified by:
      getFeature in interface IFeatureContainer<T extends IFeature>
      Type Parameters:
      F - type of the feature
      Parameters:
      featureClass - Class object of the type of the feature
      Returns:
      feature object, or null
    • getOptionalFeature

      public <F> Optional<F> getOptionalFeature(Class<F> featureClass)
      Description copied from interface: IFeatureContainer
      Returns the Optional instance containing the feature of the specified type contained in this feature container, or empty Optional if there is no such feature
      Specified by:
      getOptionalFeature in interface IFeatureContainer<T extends IFeature>
      Type Parameters:
      F - type of the feature
      Parameters:
      featureClass - Class object of the type of the feature
      Returns:
      Optional instance containing the feature of the specified type contained in this feature container, or empty Optional if there is no such feature
    • doIfFeaturePresent

      public <F> void doIfFeaturePresent(Class<F> featureClass, Consumer<F> consumer)
      Description copied from interface: IFeatureContainer
      If this container has a feature of the specified type, then executes the specified action with this feature. Does nothing otherwise
      Specified by:
      doIfFeaturePresent in interface IFeatureContainer<T extends IFeature>
      Type Parameters:
      F - type of the feature
      Parameters:
      featureClass - Class object of the type of the feature
      consumer - action taking the found feature as a context
    • calculateIfFeaturePresent

      public <F, R> R calculateIfFeaturePresent(Class<F> featureClass, Function<F,R> function, R valueIfNotPresent)
      Description copied from interface: IFeatureContainer
      If this container has a feature of the specified type, then calculates the result with the function taking the feature as a parameter. Otherwise, returns the value specified in valueIfNotPresent argument.
      Specified by:
      calculateIfFeaturePresent in interface IFeatureContainer<T extends IFeature>
      Type Parameters:
      F - type of the feature
      R - type of the calculation result
      Parameters:
      featureClass - Class object of the type of the feature
      function - Function that takes the found feature as an argument and does the calculation
      valueIfNotPresent - value that is returned if no feature of this type is contained in this container
      Returns:
      result of the calculation
    • calculateIfFeaturePresent

      public <F, R> R calculateIfFeaturePresent(Class<F> featureClass, Function<F,R> function, Supplier<R> supplierIfNotPresent)
      Description copied from interface: IFeatureContainer
      If this container has a feature of the specified type, then calculates the result with the function taking the feature as a parameter. Otherwise, returns the value specified in calculated by supplierIfNotPresent lambda.
      Specified by:
      calculateIfFeaturePresent in interface IFeatureContainer<T extends IFeature>
      Type Parameters:
      F - type of the feature
      R - type of the calculation result
      Parameters:
      featureClass - Class object of the type of the feature
      function - Function that takes the found feature as an argument and does the calculation
      supplierIfNotPresent - lambda that is called to calculate the result if no feature of this type is contained in this container
      Returns:
      result of the calculation
    • initialize

      public void initialize()
      Description copied from interface: IFeatureContainer
      Initializes the feature container and calls IFeature.initialize() method for all the features contained in this container.

      This method must be called after all the features have been added to the feature container and before any of these features are used.

      Specified by:
      initialize in interface IFeatureContainer<T extends IFeature>
    • getFeatures

      public <F> List<F> getFeatures(Class<F> featureClass)
      Description copied from interface: IFeatureContainer
      Returns a list of features that are inheritors of the specified type contained in the container of this feature. The order is exactly the same in which the features were added using IFeatureContainer.addFeature(F).
      Specified by:
      getFeatures in interface IFeatureContainer<T extends IFeature>
      Type Parameters:
      F - type of the feature
      Parameters:
      featureClass - Class object of the type of the feature
      Returns:
      list of features
    • features

      public List<T> features()
      Description copied from interface: IFeatureContainer
      Returns the list of all features contained in this feature container. The order of the features is always the same for each container but specified at the level of implementors of this interface
      Specified by:
      features in interface IFeatureContainer<T extends IFeature>
      Returns:
      list of all features contained in this feature container
    • copy

      public FeatureContainer<T> copy()
      Returns the copy of this feature container. The returned copy also contains the copies of all features contained in this container produced by calling their Feature.copy() method.

      If for some feature Feature.copy() method returns null, such feature is not included into the copy.

      Returns:
      copy of this feature container