Package net.sf.basedb.util.units
Class UnitCache
java.lang.Object
net.sf.basedb.util.units.UnitCache
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) $
-
Nested Class Summary
Modifier and TypeClassDescription(package private) static class
Units are cached by (quantity,symbol) combination. -
Field Summary
Modifier and TypeFieldDescriptionprivate boolean
private DbControl
private Map<UnitCache.CacheKey,
Unit> -
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionFinds a unit with a given symbol for a given quantity.Finds a unit with a given symbol for a given quantity.Finds a unit with a given symbol for a given quantity.Get the DbControl that is currently used for database access.boolean
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
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
Set the DbControl to use for database access
-
Field Details
-
dc
-
query
-
units
-
autoReload
private boolean autoReload
-
-
Constructor Details
-
UnitCache
public UnitCache()Create a new unit cache. Before it can be used aDbControl
must be set bysetDbControl(DbControl)
. -
UnitCache
Create a new unit cache which is ready to be used immediately.- Parameters:
dc
- The DbControl to use for database access
-
-
Method Details
-
setDbControl
Set the DbControl to use for database access -
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, aConnectionClosedException
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
Finds a unit with a given symbol for a given quantity.- Parameters:
quantityId
- The ID of the quantitysymbol
- The symbol of the unit- Returns:
- A unit object, or null if no unit is found
-
findUnit
Finds a unit with a given symbol for a given quantity.- Parameters:
quantityId
- The system ID of the quantitysymbol
- The symbol of the unit- Returns:
- A unit object, or null if no unit is found
-
findUnit
Finds a unit with a given symbol for a given quantity.- Parameters:
quantity
- The quantitysymbol
- The symbol of the unit- Returns:
- A unit object, or null if no unit is found
-
lookupUnitInDb
Lookup a unit in the database and optionally add it to the cache.
-