Class CalculationPartMap<S,T extends FeatureInput,U,E extends Exception>

Object
CalculationPartMap<S,T,U,E>
Type Parameters:
S - result-type
T - feature input-type
U - key-type
E - 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

    Constructors
    Modifier
    Constructor
    Description
    protected
    CalculationPartMap(int cacheSize)
    Creates for a particular cache-size.
  • Method Summary

    Modifier and Type
    Method
    Description
    abstract boolean
    equals(Object other)
     
    protected abstract S
    execute(T input, U key)
    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
    getOrNull(U key)
    Gets an existing result for the current input from the cache.
    abstract int
     
    protected boolean
    hasKey(U key)
    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
    put(U key, S result)
    Assigns a evaluation result to the cache for a particular key.

    Methods inherited from class java.lang.Object

    clone, finalize, getClass, notify, notifyAll, toString, wait, wait, wait
  • 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

      public S getOrCalculate(T input, U key) throws E
      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 interface ResettableCalculation
    • equals

      public abstract boolean equals(Object other)
      Overrides:
      equals in class Object
    • hashCode

      public abstract int hashCode()
      Overrides:
      hashCode in class Object
    • getOrNull

      protected S getOrNull(U key)
      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

      protected boolean hasKey(U key)
      Does a result exist for the current input, and a particular key?
      Parameters:
      key - the key.
      Returns:
      true iff a result exists.
    • put

      protected void put(U key, S result)
      Assigns a evaluation result to the cache for a particular key.
      Parameters:
      key - the key.
      result - the result to assign.
    • execute

      protected abstract S execute(T input, U key) throws E
      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.