Class UnitCache

java.lang.Object
net.sf.basedb.util.units.UnitCache

public class UnitCache
extends Object
Helper class for optimized lookup of units by symbol. This class will cache units by their symbol and quantity, making repeated lookup for the same unit only hit the database the first time.

Note! The implementation of the cache is not thread-safe. If multiple threads are going to use the cache at the same time they must synchronize externally.

Version:
2.9
Author:
Nicklas
Last modified
$Date: 2017-05-22 14:35:27 +0200 (må, 22 maj 2017) $
  • Field Details

  • Constructor Details

    • UnitCache

      public UnitCache()
      Create a new unit cache. Before it can be used a DbControl must be set by setDbControl(DbControl).
    • UnitCache

      public UnitCache​(DbControl dc)
      Create a new unit cache which is ready to be used immediately.
      Parameters:
      dc - The DbControl to use for database access
  • Method Details

    • setDbControl

      public void setDbControl​(DbControl dc)
      Set the DbControl to use for database access
    • getDbControl

      public DbControl getDbControl()
      Get the DbControl that is currently used for database access.
    • resetCache

      public void resetCache()
      Clear the cache of all units.
    • setAutoReload

      public void setAutoReload​(boolean autoReload)
      Specifies if a cached unit that may have been loaded by a closed DbControl should be reloaded with the current DbControl or not. This assumes that the cache is currently using a connected DbControl. If not, a ConnectionClosedException is thrown when trying to find a unit. The default value for this setting is FALSE.
      Parameters:
      autoReload - TRUE to automatically reload the unit with the current DbControl
    • isAutoReload

      public boolean isAutoReload()
      If units that are no longer connected to a DbControl should automatcally by reloaded with the current DbControl or not.
    • findUnit

      public Unit findUnit​(int quantityId, String symbol)
      Finds a unit with a given symbol for a given quantity.
      Parameters:
      quantityId - The ID of the quantity
      symbol - The symbol of the unit
      Returns:
      A unit object, or null if no unit is found
    • findUnit

      public Unit findUnit​(String quantityId, String symbol)
      Finds a unit with a given symbol for a given quantity.
      Parameters:
      quantityId - The system ID of the quantity
      symbol - The symbol of the unit
      Returns:
      A unit object, or null if no unit is found
    • findUnit

      public Unit findUnit​(Quantity quantity, String symbol)
      Finds a unit with a given symbol for a given quantity.
      Parameters:
      quantity - The quantity
      symbol - The symbol of the unit
      Returns:
      A unit object, or null if no unit is found
    • lookupUnitInDb

      private Unit lookupUnitInDb​(UnitCache.CacheKey key, boolean addToCache)
      Lookup a unit in the database and optionally add it to the cache.