Class CalculationPartMap<S,T extends FeatureInput,U,E extends Exception>
Object
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
- Direct Known Subclasses:
CalculateIncrementalOperationMap
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
ConstructorsModifierConstructorDescriptionprotected
CalculationPartMap
(int cacheSize) Creates for a particular cache-size. -
Method Summary
Modifier and TypeMethodDescriptionabstract boolean
protected abstract S
Calculates a result for a particular input and key.getOrCalculate
(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 S
Gets an existing result for the current input from the cache.abstract int
hashCode()
protected boolean
Does a result exist for the current input, and a particular key?void
Invalidates the cache, removing any items already stored.int
Number of items currently stored in cache.protected void
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.
-
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:
invalidate
in 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.- Parameters:
input
- the input.key
- the key.- Returns:
- the evaluated result.
- Throws:
E
- if something goes wrong during calculation.
-