Package org.openstreetmap.josm.io
Class CacheCustomContent<T extends java.lang.Throwable>
- java.lang.Object
-
- org.openstreetmap.josm.io.CacheCustomContent<T>
-
- Type Parameters:
T- aThrowablethat may be thrown duringupdateData(), useRuntimeExceptionif no exception must be handled.
- Direct Known Subclasses:
CachedAttributionBingAerialTileSource.BingAttributionData,GettingStarted.MotdContent,OsmApi.CapabilitiesCache
public abstract class CacheCustomContent<T extends java.lang.Throwable> extends java.lang.Object
Use this class if you want to cache and store a single file that gets updated regularly. Unless you flush() it will be kept in memory. If you want to cache a lot of data and/or files, use CacheFiles.- Since:
- 1450
-
-
Field Summary
Fields Modifier and Type Field Description private byte[]dataWhere the data will be storedprivate java.lang.StringidentThe ident that identifies the stored file.static intINTERVAL_ALWAYSUpdate interval meaning an update is always neededstatic intINTERVAL_DAILYUpdate interval meaning an update is needed each daystatic intINTERVAL_HOURLYUpdate interval meaning an update is needed each hourstatic intINTERVAL_MONTHLYUpdate interval meaning an update is needed each monthstatic intINTERVAL_NEVERUpdate interval meaning an update is never neededstatic intINTERVAL_WEEKLYUpdate interval meaning an update is needed each weekprivate java.io.FilepathThe (file-)path where the data will be storedprivate intupdateIntervalHow often to update the cached version
-
Constructor Summary
Constructors Modifier Constructor Description protectedCacheCustomContent(java.lang.String ident, int updateInterval)Initializes the class.
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description voidflushData()Flushes the data from memory.byte[]getData()Returns the data without performing any updatesjava.lang.StringgetDataString()Returns the data without performing any updatesprotected booleanisCacheValid()This function serves as a comfort hook to perform additional checks if the cache is validprotected abstract booleanisOffline()Checks underlying resource is not accessed in offline mode.private voidloadFromDisk()Tries to load the data using the given ident from disk.private booleanneedsUpdate()private voidsaveToDisk()Stores the data to diskprotected abstract byte[]updateData()This function will be executed when an update is required.private byte[]updateForce()Executes an update regardless of updateIntervaljava.lang.StringupdateForceString()Executes an update regardless of updateIntervalbyte[]updateIfRequired()Updates data if requiredjava.lang.StringupdateIfRequiredString()Updates data if required
-
-
-
Field Detail
-
INTERVAL_ALWAYS
public static final int INTERVAL_ALWAYS
Update interval meaning an update is always needed- See Also:
- Constant Field Values
-
INTERVAL_HOURLY
public static final int INTERVAL_HOURLY
Update interval meaning an update is needed each hour
-
INTERVAL_DAILY
public static final int INTERVAL_DAILY
Update interval meaning an update is needed each day
-
INTERVAL_WEEKLY
public static final int INTERVAL_WEEKLY
Update interval meaning an update is needed each week
-
INTERVAL_MONTHLY
public static final int INTERVAL_MONTHLY
Update interval meaning an update is needed each month
-
INTERVAL_NEVER
public static final int INTERVAL_NEVER
Update interval meaning an update is never needed- See Also:
- Constant Field Values
-
data
private byte[] data
Where the data will be stored
-
ident
private final java.lang.String ident
The ident that identifies the stored file. Includes file-ending.
-
path
private final java.io.File path
The (file-)path where the data will be stored
-
updateInterval
private final int updateInterval
How often to update the cached version
-
-
Constructor Detail
-
CacheCustomContent
protected CacheCustomContent(java.lang.String ident, int updateInterval)
Initializes the class. Note that all read data will be stored in memory until it is flushed by flushData().- Parameters:
ident- ident that identifies the stored file. Includes file-ending.updateInterval- update interval in seconds. -1 means always
-
-
Method Detail
-
updateData
protected abstract byte[] updateData() throws T extends java.lang.Throwable
This function will be executed when an update is required. It has to be implemented by the inheriting class and should use a worker if it has a long wall time as the function is executed in the current thread.
-
isCacheValid
protected boolean isCacheValid()
This function serves as a comfort hook to perform additional checks if the cache is valid- Returns:
- True if the cached copy is still valid
-
needsUpdate
private boolean needsUpdate()
-
isOffline
protected abstract boolean isOffline()
Checks underlying resource is not accessed in offline mode.- Returns:
- whether resource is accessed in offline mode
-
updateIfRequired
public byte[] updateIfRequired() throws T extends java.lang.Throwable
Updates data if required
-
updateIfRequiredString
public java.lang.String updateIfRequiredString() throws T extends java.lang.Throwable
Updates data if required
-
updateForce
private byte[] updateForce() throws T extends java.lang.Throwable
Executes an update regardless of updateInterval
-
updateForceString
public java.lang.String updateForceString() throws T extends java.lang.Throwable
Executes an update regardless of updateInterval
-
getData
public byte[] getData() throws T extends java.lang.Throwable
Returns the data without performing any updates
-
getDataString
public java.lang.String getDataString() throws T extends java.lang.Throwable
Returns the data without performing any updates
-
loadFromDisk
private void loadFromDisk() throws T extends java.lang.Throwable
Tries to load the data using the given ident from disk. If this fails, data will be updated, unless run in offline mode
-
saveToDisk
private void saveToDisk()
Stores the data to disk
-
flushData
public void flushData()
Flushes the data from memory. Class automatically reloads it from disk or updateData() if required
-
-