|
2.17.2: 2011-06-17 | ||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object net.sf.basedb.util.units.UnitCache
public class 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.
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 |
---|
private DbControl dc
private ItemQuery<Unit> query
private Map<UnitCache.CacheKey,Unit> units
private boolean autoReload
Constructor Detail |
---|
public UnitCache()
DbControl
must be set by setDbControl(DbControl)
.
public UnitCache(DbControl dc)
dc
- The DbControl to use for database accessMethod Detail |
---|
public 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, String symbol)
quantityId
- The ID of the quantitysymbol
- The symbol of the unit
public Unit findUnit(String quantityId, String symbol)
quantityId
- The system ID of the quantitysymbol
- The symbol of the unit
public Unit findUnit(Quantity quantity, String symbol)
quantity
- The quantitysymbol
- The symbol of the unit
private Unit lookupUnitInDb(UnitCache.CacheKey key, boolean addToCache)
|
2.17.2: 2011-06-17 | ||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |