Interface IFeatureContainer<T extends IFeature>

Type Parameters:
T - base type for all the features contained in this feature container
All Known Implementing Classes:
FeatureContainer

public interface IFeatureContainer<T extends IFeature>
Interface to be implemented by all feature containers. A feature container is a class representing some entity with multiple aspects of behavior. These aspects are called features and implement IFeature interface.

This design pattern is useful in simulation models where different entities (e.g., vehicles, machines, or other entities) can have various aspects or "features" that need to be handled in a flexible, sharable and reusable way.

Author:
Andrey Malykhanov, Vitaliy Chernenko
  • 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.
    <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
    default <F> F
    getFeatureOrThrow(Class<F> featureClass)
    Returns the feature of the specified type contained in this feature container, 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> 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.
    default <F extends T>
    F
    replaceFeature(F feature)
    Replaces the existing feature with the specified new feature.
    <F extends T>
    F
    replaceFeature(Class<F> featureClass, F feature)
    Replaces the existing feature of the specified type with the specified new feature.
  • Method Details

    • initialize

      void initialize()
      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.

    • features

      List<T> features()
      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
      Returns:
      list of all features contained in this feature container
    • addFeature

      <F extends T> F addFeature(F feature)
      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
      Type Parameters:
      F - type of the feature being added
      Parameters:
      feature - feature being added
      Returns:
      reference to the feature being added
    • addFeature

      <F extends T> F addFeature(F feature, boolean toBeginningOfFeaturesList)
      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
      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
    • hasFeature

      <F> boolean hasFeature(Class<F> featureClass)
      Checks if this container 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 this container contains a feature of the specified type, false otherwise
    • replaceFeature

      <F extends T> F replaceFeature(Class<F> featureClass, F feature)
      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.
      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
    • replaceFeature

      default <F extends T> F replaceFeature(F feature)
      Replaces the existing feature with the specified new feature. The container must contain a feature of the specified feature's type. Otherwise and IllegalArgumentException will be thrown.
      Type Parameters:
      F - type of the feature being replaced
      Parameters:
      feature - feature that replaces the existing one
      Returns:
      reference to the feature newly replaced feature
    • getFeature

      <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
      Type Parameters:
      F - type of the feature
      Parameters:
      featureClass - Class object of the type of the feature
      Returns:
      feature object, or null
    • getFeatureOrThrow

      default <F> F getFeatureOrThrow(Class<F> featureClass)
      Returns the feature of the specified type contained in this feature container, 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
    • getOptionalFeature

      <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
      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

      <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. 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

      <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. 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 no feature of this type is contained in this container
      Returns:
      result of the calculation
    • calculateIfFeaturePresent

      <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. Otherwise, returns the value specified in calculated by supplierIfNotPresent lambda.
      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
    • getFeatures

      <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 addFeature(F).
      Type Parameters:
      F - type of the feature
      Parameters:
      featureClass - Class object of the type of the feature
      Returns:
      list of features