Class CalculatedValueCache

  • All Implemented Interfaces:
    ICalculatedValueCache

    public class CalculatedValueCache
    extends Object
    implements ICalculatedValueCache
    This class is intended as a value cache that is able to perform calculations in a background thread. By default it is configured for smooth updates, which means that on re-calculation of values, the old values will be returned until the calculation is done.

    The CalculatedValueCache uses implementations of ICalculatedValueCacheKey as the key for the value cache. Usually the internal default implementations for column or row position, or the column-row coordinates should fit most of the use cases.

    • Constructor Detail

      • CalculatedValueCache

        public CalculatedValueCache​(ILayer layer,
                                    boolean useColumnAsKey,
                                    boolean useRowAsKey)
        Creates a new CalculatedValueCache for the specified layer that performs smooth updates of the calculated values.

        Setting one or both key flags to true will enable automatic cache key resolution dependent on the configuration. Setting both values to false will leave the developer to use getCalculatedValue(int, int, ICalculatedValueCacheKey, boolean, ICalculator) as it is not possible to determine the ICalculatedValueCacheKey automatically.

        Parameters:
        layer - The layer to which the CalculatedValueCache is connected.
        useColumnAsKey - Flag to specify if the column position should be used as cache key.
        useRowAsKey - Flag to specify if the row position should be used as cache key.
      • CalculatedValueCache

        public CalculatedValueCache​(ILayer layer,
                                    boolean useColumnAsKey,
                                    boolean useRowAsKey,
                                    boolean smoothUpdates)
        Creates a new CalculatedValueCache for the specified layer. This constructor additionally allows to specify if the updates of the calculated values should be performed smoothly or not. That means if a value needs to be recalculated, on smooth updating the old value will be returned until the new value is calculated. Non-smooth updates will return null until the re-calculation is done.

        Setting one or both key flags to true will enable automatic cache key resolution dependent on the configuration. Setting both values to false will leave the developer to use getCalculatedValue(int, int, ICalculatedValueCacheKey, boolean, ICalculator) as it is not possible to determine the ICalculatedValueCacheKey automatically.

        Parameters:
        layer - The layer to which the CalculatedValueCache is connected.
        useColumnAsKey - Flag to specify if the column position should be used as cache key.
        useRowAsKey - Flag to specify if the row position should be used as cache key.
        smoothUpdates - Flag to specify if the update of the calculated values should be performed smoothly.
    • Method Detail

      • getCalculatedValue

        public Object getCalculatedValue​(int columnPosition,
                                         int rowPosition,
                                         boolean calculateInBackground,
                                         ICalculator calculator)
        Description copied from interface: ICalculatedValueCache
        Returns the calculated value for the specified column and row position. If there is no calculated value for that coordinates in the cache or there is a potentially stale value, the re-calculation of the value is executed.

        This method tries to use a predefined cache key dependent on the configuration of this CalculatedValueCache.

        Specified by:
        getCalculatedValue in interface ICalculatedValueCache
        Parameters:
        columnPosition - The column position of the requested value.
        rowPosition - The row position of the requested value.
        calculateInBackground - Flag to specify whether the value calculation should be processed in the background or not. Setting this value to false will cause calculation in the UI thread, which is usually necessary in case of exporting and printing.
        calculator - The ICalculator that is used for calculating the values.
        Returns:
        The value for the given coordinates.
      • getCalculatedValue

        public Object getCalculatedValue​(int columnPosition,
                                         int rowPosition,
                                         ICalculatedValueCacheKey key,
                                         boolean calculateInBackground,
                                         ICalculator calculator)
        Description copied from interface: ICalculatedValueCache
        Returns the calculated value for the specified column and row position. If there is no calculated value for that coordinates in the cache or there is a potentially stale value, the re-calculation of the value is executed.

        This method uses the given ICalculatedValueCacheKey instead of determining the cache key out of the CalculatedValueCache key configuration.

        Specified by:
        getCalculatedValue in interface ICalculatedValueCache
        Parameters:
        columnPosition - The column position of the requested value.
        rowPosition - The row position of the requested value.
        key - The key that is used by this CalculatedValueCache.
        calculateInBackground - Flag to specify whether the value calculation should be processed in the background or not. Setting this value to false will cause calculation in the UI thread, which is usually necessary in case of exporting and printing.
        calculator - The ICalculator that is used for calculating the values.
        Returns:
        The value for the given coordinates.
      • clearCache

        public void clearCache()
        Description copied from interface: ICalculatedValueCache
        Clear the internal cache. Doing this will result in triggering new calculations. If the values where calculated before, using the cache copy still the already calculated values will be returned until the new calculation is done.
        Specified by:
        clearCache in interface ICalculatedValueCache
      • killCache

        public void killCache()
        Description copied from interface: ICalculatedValueCache
        Kills all cached values. The internal cache aswell as the cache copy to support smooth updates of values. This is necessary because on structural changes, e.g. deleting/adding rows, the cache copy would return false values.
        Specified by:
        killCache in interface ICalculatedValueCache
      • addToCache

        protected void addToCache​(ICalculatedValueCacheKey key,
                                  Object value)
        Adds the given value to the cache and the cache-copy. This way the new calculated value gets propagated to both cache instances.
        Parameters:
        key - The key to which the calculated value belongs to.
        value - The value for the given coordinates to be cached.
      • setLayer

        public void setLayer​(ILayer layer)
        Description copied from interface: ICalculatedValueCache
        Set the layer that should be used by this CalculatedValueCache to trigger updates after the calculation processing is done. Necessary if the caching is connected to a data provider for example, which is not able to fire events itself.
        Specified by:
        setLayer in interface ICalculatedValueCache
        Parameters:
        layer - The ILayer that should be used to fire the CellVisualChangeEvent after the background calculation process is done.