2.17.2: 2011-06-17

net.sf.basedb.util.units
Class UnitCache

java.lang.Object
  extended by 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: 2008-10-02 13:55:17 +0200 (Thu, 02 Oct 2008) $

Nested Class Summary
(package private) static class UnitCache.CacheKey
          Units are cached by (quantity,symbol) combination.
 
Field Summary
private  boolean autoReload
           
private  DbControl dc
           
private  ItemQuery<Unit> query
           
private  Map<UnitCache.CacheKey,Unit> units
           
 
Constructor Summary
UnitCache()
          Create a new unit cache.
UnitCache(DbControl dc)
          Create a new unit cache which is ready to be used immediately.
 
Method Summary
 Unit findUnit(int quantityId, String symbol)
          Finds a unit with a given symbol for a given quantity.
 Unit findUnit(Quantity quantity, String symbol)
          Finds a unit with a given symbol for a given quantity.
 Unit findUnit(String quantityId, String symbol)
          Finds a unit with a given symbol for a given quantity.
 DbControl getDbControl()
          Get the DbControl that is currently used for database access.
 boolean isAutoReload()
          If units that are no longer connected to a DbControl should automatcally by reloaded with the current DbControl or not.
private  Unit lookupUnitInDb(UnitCache.CacheKey key, boolean addToCache)
          Lookup a unit in the database and optionally add it to the cache.
 void resetCache()
          Clear the cache of all units.
 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.
 void setDbControl(DbControl dc)
          Set the DbControl to use for database access
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

dc

private DbControl dc

query

private ItemQuery<Unit> query

units

private Map<UnitCache.CacheKey,Unit> units

autoReload

private boolean autoReload
Constructor Detail

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 Detail

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.


2.17.2: 2011-06-17