org.openstreetmap.josm.io
Class CacheCustomContent<T extends java.lang.Throwable>

java.lang.Object
  extended by org.openstreetmap.josm.io.CacheCustomContent<T>
Type Parameters:
T - a Throwable that may be thrown during updateData(), use RuntimeException if no exception must be handled.
Direct Known Subclasses:
GettingStarted.MotdContent, OsmApi.CapabilitiesCache, TMSLayer.CachedAttributionBingAerialTileSource.BingAttributionData

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


Field Summary
private  byte[] data
          Where the data will be stored
private  java.lang.String ident
          The ident that identifies the stored file.
static int INTERVAL_ALWAYS
          Common intervals
static int INTERVAL_DAILY
           
static int INTERVAL_HOURLY
           
static int INTERVAL_MONTHLY
           
static int INTERVAL_NEVER
           
static int INTERVAL_WEEKLY
           
private  java.io.File path
          The (file-)path where the data will be stored
private  int updateInterval
          How often to update the cached version
 
Constructor Summary
CacheCustomContent(java.lang.String ident, int updateInterval)
          Initializes the class.
 
Method Summary
 void flushData()
          Flushes the data from memory.
 byte[] getData()
          Returns the data without performing any updates
 java.lang.String getDataString()
          Returns the data without performing any updates
protected  boolean isCacheValid()
          This function serves as a comfort hook to perform additional checks if the cache is valid
private  void loadFromDisk()
          Tries to load the data using the given ident from disk.
private  void saveToDisk()
          Stores the data to disk
protected abstract  byte[] updateData()
          This function will be executed when an update is required.
 byte[] updateForce()
          Executes an update regardless of updateInterval
 java.lang.String updateForceString()
          Executes an update regardless of updateInterval
 byte[] updateIfRequired()
          Updates data if required
 java.lang.String updateIfRequiredString()
          Updates data if required
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

INTERVAL_ALWAYS

public static final int INTERVAL_ALWAYS
Common intervals

See Also:
Constant Field Values

INTERVAL_HOURLY

public static final int INTERVAL_HOURLY
See Also:
Constant Field Values

INTERVAL_DAILY

public static final int INTERVAL_DAILY
See Also:
Constant Field Values

INTERVAL_WEEKLY

public static final int INTERVAL_WEEKLY
See Also:
Constant Field Values

INTERVAL_MONTHLY

public static final int INTERVAL_MONTHLY
See Also:
Constant Field Values

INTERVAL_NEVER

public static final int INTERVAL_NEVER
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

public 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 -
updateInterval -
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.

Returns:
the data to cache
Throws:
T extends java.lang.Throwable

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

updateIfRequired

public byte[] updateIfRequired()
                        throws T extends java.lang.Throwable
Updates data if required

Returns:
Returns the data
Throws:
T extends java.lang.Throwable

updateIfRequiredString

public java.lang.String updateIfRequiredString()
                                        throws T extends java.lang.Throwable
Updates data if required

Returns:
Returns the data as string
Throws:
T extends java.lang.Throwable

updateForce

public byte[] updateForce()
                   throws T extends java.lang.Throwable
Executes an update regardless of updateInterval

Returns:
Returns the data
Throws:
T extends java.lang.Throwable

updateForceString

public java.lang.String updateForceString()
                                   throws T extends java.lang.Throwable
Executes an update regardless of updateInterval

Returns:
Returns the data as String
Throws:
T extends java.lang.Throwable

getData

public byte[] getData()
               throws T extends java.lang.Throwable
Returns the data without performing any updates

Returns:
the data
Throws:
T extends java.lang.Throwable

getDataString

public java.lang.String getDataString()
                               throws T extends java.lang.Throwable
Returns the data without performing any updates

Returns:
the data as String
Throws:
T extends java.lang.Throwable

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

Throws:
T extends java.lang.Throwable

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



JOSM