Package com.amalgamasimulation.features
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>
Base class with standard implementation of
IFeatureContainer interface.- Author:
- Andrey Malykhanov, Vitaliy Chernenko
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected boolean -
Constructor Summary
Constructors -
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.copy()Returns the copy of this feature container.<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 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.<F extends T>
FreplaceFeature(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, waitMethods inherited from interface com.amalgamasimulation.features.IFeatureContainer
getFeatureOrThrow, replaceFeature
-
Field Details
-
features
-
featuresList
-
initialized
protected boolean initialized
-
-
Constructor Details
-
FeatureContainer
public FeatureContainer()
-
-
Method Details
-
addFeature
Description copied from interface:IFeatureContainerAdds 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- Specified by:
addFeaturein interfaceIFeatureContainer<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
Description copied from interface:IFeatureContainerChecks if this container has a feature of the specified type.- Specified by:
hasFeaturein interfaceIFeatureContainer<T extends IFeature>- 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
-
addFeature
Description copied from interface:IFeatureContainerAdds 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- Specified by:
addFeaturein interfaceIFeatureContainer<T extends IFeature>- 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
-
replaceFeature
Description copied from interface:IFeatureContainerReplaces 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.- Specified by:
replaceFeaturein interfaceIFeatureContainer<T extends IFeature>- 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
-
getFeature
Description copied from interface:IFeatureContainerReturns the feature of the specified type contained in this feature container, ornullif there is no such feature- Specified by:
getFeaturein interfaceIFeatureContainer<T extends IFeature>- Type Parameters:
F- type of the feature- Parameters:
featureClass-Classobject of the type of the feature- Returns:
- feature object, or
null
-
getOptionalFeature
Description copied from interface:IFeatureContainerReturns theOptionalinstance containing the feature of the specified type contained in this feature container, or emptyOptionalif there is no such feature- Specified by:
getOptionalFeaturein interfaceIFeatureContainer<T extends IFeature>- Type Parameters:
F- type of the feature- Parameters:
featureClass-Classobject of the type of the feature- Returns:
Optionalinstance containing the feature of the specified type contained in this feature container, or emptyOptionalif there is no such feature
-
doIfFeaturePresent
Description copied from interface:IFeatureContainerIf this container has a feature of the specified type, then executes the specified action with this feature. Does nothing otherwise- Specified by:
doIfFeaturePresentin interfaceIFeatureContainer<T extends IFeature>- 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
public <F,R> R calculateIfFeaturePresent(Class<F> featureClass, Function<F, R> function, R valueIfNotPresent) Description copied from interface:IFeatureContainerIf 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.- Specified by:
calculateIfFeaturePresentin interfaceIFeatureContainer<T extends IFeature>- 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
public <F,R> R calculateIfFeaturePresent(Class<F> featureClass, Function<F, R> function, Supplier<R> supplierIfNotPresent) Description copied from interface:IFeatureContainerIf 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.- Specified by:
calculateIfFeaturePresentin interfaceIFeatureContainer<T extends IFeature>- 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
-
initialize
public void initialize()Description copied from interface:IFeatureContainerInitializes 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.
- Specified by:
initializein interfaceIFeatureContainer<T extends IFeature>
-
getFeatures
Description copied from interface:IFeatureContainerReturns 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 usingIFeatureContainer.addFeature(F).- Specified by:
getFeaturesin interfaceIFeatureContainer<T extends IFeature>- Type Parameters:
F- type of the feature- Parameters:
featureClass-Classobject of the type of the feature- Returns:
- list of features
-
features
Description copied from interface:IFeatureContainerReturns 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:
featuresin interfaceIFeatureContainer<T extends IFeature>- Returns:
- list of all features contained in this feature container
-
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 theirFeature.copy()method.If for some feature
Feature.copy()method returnsnull, such feature is not included into the copy.- Returns:
- copy of this feature container
-