S
- result-type of the calculationT
- feature input-typepublic abstract class CalculationPart<S,T extends FeatureInput> extends Object implements ResettableCalculation
As many features repeat the same calculation partially to determine a result, this provides a
mechanism to avoid repeating the same calculation, internally within a Feature
.
The result value is cached, until invalidate()
is called.
All sub-classes must implements an equivalence-relation via equals()
that checks if
two feature-calculations are identical. This allows the user to search through a collection of
CalculationPart
s to find one with identical parameters and re-use it.
Important note: It is therefore absolutely necessary to make sure every class has a
well-defined equals()
and hashCode()
that covers all relevant parameterization.
Constructor and Description |
---|
CalculationPart() |
Modifier and Type | Method and Description |
---|---|
abstract boolean |
equals(Object other) |
protected abstract S |
execute(T input)
This performs the actual calculation when needed.
|
S |
getOrCalculate(T input)
Executes the operation and returns a result, either by doing the calculation, or retrieving a
cached-result from previously.
|
boolean |
hasCachedResult()
Has the calculation already been executed, with a cached result existing?
|
abstract int |
hashCode() |
void |
invalidate()
Resets the cached-calculation, so the next call to doOperationWithParameters() is guaranteed
to calculation the operation, and store the value in the cache.
|
public S getOrCalculate(T input) throws FeatureCalculationException
input
- If there is no cached-value, and the calculation occurs, this input is used.
Otherwise ignored.FeatureCalculationException
- if the calculation cannot finish for whatever reason.public boolean hasCachedResult()
public void invalidate()
ResettableCalculation
invalidate
in interface ResettableCalculation
protected abstract S execute(T input) throws FeatureCalculationException
invalidate()
is called.input
- the input to the calculation.FeatureCalculationException
- if the calculation cannot be successfully completed.Copyright © 2010–2023 Owen Feehan, ETH Zurich, University of Zurich, Hoffmann-La Roche. All rights reserved.