Package com.amalgamasimulation.features
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 TypeMethodDescription<F extends T>
FaddFeature(F feature) Adds the specified feature to this container.<F extends T>
FaddFeature(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> voiddoIfFeaturePresent(Class<F> featureClass, Consumer<F> consumer) If this container has a feature of the specified type, then executes the specified action with this feature.features()Returns the list of all features contained in this feature container.<F> FgetFeature(Class<F> featureClass) Returns the feature of the specified type contained in this feature container, ornullif there is no such featuredefault <F> FgetFeatureOrThrow(Class<F> featureClass) Returns the feature of the specified type contained in this feature container, or throws aNoSuchElementExceptionwith 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) <F> booleanhasFeature(Class<F> featureClass) Checks if this container has a feature of the specified type.voidInitializes the feature container and callsIFeature.initialize()method for all the features contained in this container.default <F extends T>
FreplaceFeature(F feature) Replaces the existing feature with the specified new feature.<F extends T>
FreplaceFeature(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 callsIFeature.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
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
Adds the specified feature to this container. The feature is added to the end of the features list. Throws aRuntimeExceptionif 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
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 aRuntimeExceptionif 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 addedtoBeginningOfFeaturesList- iftrue, 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
Checks if this container has a feature of the specified type.- Type Parameters:
F- type of the feature being checked- Parameters:
featureClass-Classobject of the type of the feature being checked- Returns:
trueif this container contains a feature of the specified type,falseotherwise
-
replaceFeature
Replaces the existing feature of the specified type with the specified new feature. The container must contain a feature of the specified type. Otherwise andIllegalArgumentExceptionwill be thrown.- Type Parameters:
F- type of the feature being replaced- Parameters:
featureClass-Classobject of the type of the feature being replacedfeature- feature that replaces the existing one- Returns:
- reference to the feature newly replaced feature
-
replaceFeature
Replaces the existing feature with the specified new feature. The container must contain a feature of the specified feature's type. Otherwise andIllegalArgumentExceptionwill 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
Returns the feature of the specified type contained in this feature container, ornullif there is no such feature- Type Parameters:
F- type of the feature- Parameters:
featureClass-Classobject of the type of the feature- Returns:
- feature object, or
null
-
getFeatureOrThrow
Returns the feature of the specified type contained in this feature container, or throws aNoSuchElementExceptionwith the relevant message if there is no such feature- Type Parameters:
F- type of the feature- Parameters:
featureClass-Classobject of the type of the feature- Returns:
- feature object
-
getOptionalFeature
-
doIfFeaturePresent
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-Classobject of the type of the featureconsumer- 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 invalueIfNotPresentargument.- Type Parameters:
F- type of the featureR- type of the calculation result- Parameters:
featureClass-Classobject of the type of the featurefunction-Functionthat takes the found feature as an argument and does the calculationvalueIfNotPresent- 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 bysupplierIfNotPresentlambda.- Type Parameters:
F- type of the featureR- type of the calculation result- Parameters:
featureClass-Classobject of the type of the featurefunction-Functionthat takes the found feature as an argument and does the calculationsupplierIfNotPresent- 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
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 usingaddFeature(F).- Type Parameters:
F- type of the feature- Parameters:
featureClass-Classobject of the type of the feature- Returns:
- list of features
-