public class UnitCache
extends java.lang.Object
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.
Modifier and Type | Class and Description |
---|---|
(package private) static class |
UnitCache.CacheKey
Units are cached by (quantity,symbol) combination.
|
Modifier and Type | Field and Description |
---|---|
private boolean |
autoReload |
private DbControl |
dc |
private ItemQuery<Unit> |
query |
private java.util.Map<UnitCache.CacheKey,Unit> |
units |
Constructor and Description |
---|
UnitCache()
Create a new unit cache.
|
UnitCache(DbControl dc)
Create a new unit cache which is ready to be used immediately.
|
Modifier and Type | Method and Description |
---|---|
Unit |
findUnit(int quantityId,
java.lang.String symbol)
Finds a unit with a given symbol for a given quantity.
|
Unit |
findUnit(Quantity quantity,
java.lang.String symbol)
Finds a unit with a given symbol for a given quantity.
|
Unit |
findUnit(java.lang.String quantityId,
java.lang.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
|
private DbControl dc
private java.util.Map<UnitCache.CacheKey,Unit> units
private boolean autoReload
public UnitCache()
DbControl
must be set by setDbControl(DbControl)
.public UnitCache(DbControl dc)
dc
- The DbControl to use for database accesspublic void setDbControl(DbControl dc)
public DbControl getDbControl()
public void resetCache()
public void setAutoReload(boolean autoReload)
ConnectionClosedException
is thrown when trying to
find a unit. The default value for this setting is FALSE.autoReload
- TRUE to automatically reload the unit with
the current DbControlpublic boolean isAutoReload()
public Unit findUnit(int quantityId, java.lang.String symbol)
quantityId
- The ID of the quantitysymbol
- The symbol of the unitpublic Unit findUnit(java.lang.String quantityId, java.lang.String symbol)
quantityId
- The system ID of the quantitysymbol
- The symbol of the unitpublic Unit findUnit(Quantity quantity, java.lang.String symbol)
quantity
- The quantitysymbol
- The symbol of the unitprivate Unit lookupUnitInDb(UnitCache.CacheKey key, boolean addToCache)