Class Feature<T extends Feature<T,C>,C extends IFeatureContainer<T>>

java.lang.Object
com.amalgamasimulation.features.Feature<T,C>
Type Parameters:
T - base type of the features
C - base type of the feature container
All Implemented Interfaces:
IFeature

public abstract class Feature<T extends Feature<T,C>,C extends IFeatureContainer<T>> extends Object implements IFeature
Abstract base class with standard implementation of IFeature interface. Can be used as a base class for all feature classes.
Author:
Andrey Malykhanov
  • Field Summary

    Fields inherited from interface com.amalgamasimulation.features.IFeature

    NO_FEATURE
  • Constructor Summary

    Constructors
    Modifier
    Constructor
    Description
    protected
    Feature(C container)
    Constructor that must be called when creating instance of subtypes
  • Method Summary

    Modifier and Type
    Method
    Description
    <F, R> R
    calculateIfFeaturePresent(Class<F> featureClass, Function<F,R> function, R valueIfNotPresent)
    If the container of this feature has a feature of the specified type, then calculates the result with the function taking the feature as a parameter.
    Returns the container of this feature
    Returns a copy of this feature.
    <F> void
    doIfFeaturePresent(Class<F> featureClass, Consumer<F> consumer)
    If the container of this feature has a feature of the specified type, then executes the specified action with this feature.
    <F> F
    getFeature(Class<F> featureClass)
    Returns the feature of the specified type contained in the container of this feature, or null if there is no such feature
    <F> F
    getFeatureOrThrow(Class<F> featureClass)
    Returns the feature of the specified type contained in the container of this feature, or throws a NoSuchElementException with the relevant message 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> boolean
    hasFeature(Class<F> featureClass)
    Checks if the container of this feature has a feature of the specified type.
    void
    Initializes the feature.
     

    Methods inherited from class java.lang.Object

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

    Methods inherited from interface com.amalgamasimulation.features.IFeature

    featureClass
  • Constructor Details

    • Feature

      protected Feature(C container)
      Constructor that must be called when creating instance of subtypes
      Parameters:
      container - container of this feature
  • Method Details

    • hasFeature

      public <F> boolean hasFeature(Class<F> featureClass)
      Checks if the container of this feature has a feature of the specified type.
      Type Parameters:
      F - type of the feature being checked
      Parameters:
      featureClass - Class object of the type of the feature being checked
      Returns:
      true if the container of this feature contains a feature of the specified type, false otherwise
    • getFeature

      public <F> F getFeature(Class<F> featureClass)
      Returns the feature of the specified type contained in the container of this feature, or null if there is no such feature
      Type Parameters:
      F - type of the feature
      Parameters:
      featureClass - Class object of the type of the feature
      Returns:
      feature object, or null
    • getFeatureOrThrow

      public <F> F getFeatureOrThrow(Class<F> featureClass)
      Returns the feature of the specified type contained in the container of this feature, or throws a NoSuchElementException with the relevant message if there is no such feature
      Type Parameters:
      F - type of the feature
      Parameters:
      featureClass - Class object of the type of the feature
      Returns:
      feature object
    • doIfFeaturePresent

      public <F> void doIfFeaturePresent(Class<F> featureClass, Consumer<F> consumer)
      If the container of this feature has a feature of the specified type, then executes the specified action with this feature. Does nothing otherwise
      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)
      If the container of this feature 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.
      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 there is no matching feature in the container of this feature
      Returns:
      result of the calculation
    • getFeatures

      public <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. The order is exactly the same in which the features were added using IFeatureContainer.addFeature(F) to IFeatureContainer.
      Type Parameters:
      F - type of the feature
      Parameters:
      featureClass - Class object of the type of the feature
      Returns:
      list of features
    • container

      public C container()
      Returns the container of this feature
      Returns:
      container of this feature
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • initialize

      public void initialize()
      Description copied from interface: IFeature
      Initializes the feature. This method can often be used to search for other features that are present in the feature container that this feature belongs to.
      Specified by:
      initialize in interface IFeature
    • copy

      public IFeature copy()
      Description copied from interface: IFeature
      Returns a copy of this feature. If null is returned, then this feature is not included into a copy of the feature container created by FeatureContainer.copy() method.
      Specified by:
      copy in interface IFeature
      Returns:
      copy of this feature