Package com.amalgamasimulation.features
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 featuresC- 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 -
Method Summary
Modifier and TypeMethodDescription<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 featurecopy()Returns a copy of this feature.<F> voiddoIfFeaturePresent(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> FgetFeature(Class<F> featureClass) Returns the feature of the specified type contained in the container of this feature, ornullif there is no such feature<F> FgetFeatureOrThrow(Class<F> featureClass) Returns the feature of the specified type contained in the container of this feature, 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> booleanhasFeature(Class<F> featureClass) Checks if the container of this feature has a feature of the specified type.voidInitializes the feature.toString()Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods inherited from interface com.amalgamasimulation.features.IFeature
featureClass
-
Constructor Details
-
Feature
Constructor that must be called when creating instance of subtypes- Parameters:
container- container of this feature
-
-
Method Details
-
hasFeature
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-Classobject of the type of the feature being checked- Returns:
trueif the container of this feature contains a feature of the specified type,falseotherwise
-
getFeature
Returns the feature of the specified type contained in the container of this feature, 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 the container of this feature, 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
-
doIfFeaturePresent
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-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) 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 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 there is no matching feature in the container of this feature- 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 usingIFeatureContainer.addFeature(F)toIFeatureContainer.- Type Parameters:
F- type of the feature- Parameters:
featureClass-Classobject of the type of the feature- Returns:
- list of features
-
container
Returns the container of this feature- Returns:
- container of this feature
-
toString
-
initialize
public void initialize()Description copied from interface:IFeatureInitializes 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:
initializein interfaceIFeature
-
copy
Description copied from interface:IFeatureReturns a copy of this feature. Ifnullis returned, then this feature is not included into a copy of the feature container created byFeatureContainer.copy()method.
-