Class CalculationPartMap<S,T extends FeatureInput,U,E extends Exception>
Object
org.anchoranalysis.feature.calculate.part.CalculationPartMap<S,T,U,E>
- Type Parameters:
S- result-typeT- feature input-typeU- key-typeE- an exception thrown if something goes wrong during the calculation
- All Implemented Interfaces:
ResettableCalculation
public abstract class CalculationPartMap<S,T extends FeatureInput,U,E extends Exception> extends Object implements ResettableCalculation
Similar to a
CalculationPart but stores several evaluation results, differentiated by a
key.- Author:
- Owen Feehan
-
Constructor Summary
Constructors Modifier Constructor Description protectedCalculationPartMap(int cacheSize)Creates for a particular cache-size. -
Method Summary
Modifier and Type Method Description abstract booleanequals(Object other)protected abstract Sexecute(T input, U key)Calculates a result for a particular input and key.SgetOrCalculate(T input, U key)Executes the operation and returns a result, either by doing the calculation, or retrieving a cached-result from a previous execution.protected SgetOrNull(U key)Gets an existing result for the current input from the cache.abstract inthashCode()protected booleanhasKey(U key)Does a result exist for the current input, and a particular key?voidinvalidate()Invalidates the cache, removing any items already stored.intnumberItemsCurrentlyStored()Number of items currently stored in cache.protected voidput(U key, S result)Assigns a evaluation result to the cache for a particular key.
-
Constructor Details
-
CalculationPartMap
protected CalculationPartMap(int cacheSize)Creates for a particular cache-size.- Parameters:
cacheSize- cache-size to use for the keys.
-
-
Method Details
-
getOrCalculate
Executes the operation and returns a result, either by doing the calculation, or retrieving a cached-result from a previous execution.- Parameters:
input- used to calculate a result, if there is no value already cached. Ignored if a result already exists.key- the key, which determines if a result already exists or not.- Returns:
- the result of the calculation.
- Throws:
E- if the calculation cannot finish, for whatever reason.E extends Exception
-
numberItemsCurrentlyStored
public int numberItemsCurrentlyStored()Number of items currently stored in cache.- Returns:
- then number of items.
-
invalidate
public void invalidate()Invalidates the cache, removing any items already stored.- Specified by:
invalidatein interfaceResettableCalculation
-
equals
-
hashCode
public abstract int hashCode() -
getOrNull
Gets an existing result for the current input from the cache.- Parameters:
key- the key that identifies the existing result.- Returns:
- a cached-result, or null if it doesn't exist.
-
hasKey
Does a result exist for the current input, and a particular key?- Parameters:
key- the key.- Returns:
- true iff a result exists.
-
put
Assigns a evaluation result to the cache for a particular key.- Parameters:
key- the key.result- the result to assign.
-
execute
Calculates a result for a particular input and key.
-