Class CacheCustomContent<T extends Throwable>

java.lang.Object
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:
CachedAttributionBingAerialTileSource.BingAttributionData, GettingStarted.MotdContent, OsmApi.CapabilitiesCache

public abstract class CacheCustomContent<T extends Throwable> extends 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[]
    Where the data will be stored
    private final String
    The ident that identifies the stored file.
    static final int
    Update interval meaning an update is always needed
    static final int
    Update interval meaning an update is needed each day
    static final int
    Update interval meaning an update is needed each hour
    static final int
    Update interval meaning an update is needed each month
    static final int
    Update interval meaning an update is never needed
    static final int
    Update interval meaning an update is needed each week
    private final File
    The (file-)path where the data will be stored
    private final int
    How often to update the cached version
  • Constructor Summary

    Constructors
    Modifier
    Constructor
    Description
    protected
    CacheCustomContent(String ident, int updateInterval)
    Initializes the class.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Flushes the data from memory.
    byte[]
    Returns the data without performing any updates
    Returns the data without performing any updates
    protected boolean
    This function serves as a comfort hook to perform additional checks if the cache is valid
    protected abstract boolean
    Checks underlying resource is not accessed in offline mode.
    private void
    Tries to load the data using the given ident from disk.
    private boolean
     
    private void
    Stores the data to disk
    protected abstract byte[]
    This function will be executed when an update is required.
    private byte[]
    Executes an update regardless of updateInterval
    Executes an update regardless of updateInterval
    byte[]
    Updates data if required
    Updates data if required

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • INTERVAL_ALWAYS

      public static final int INTERVAL_ALWAYS
      Update interval meaning an update is always needed
      See Also:
    • 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:
    • data

      private byte[] data
      Where the data will be stored
    • ident

      private final String ident
      The ident that identifies the stored file. Includes file-ending.
    • path

      private final File path
      The (file-)path where the data will be stored
    • updateInterval

      private final int updateInterval
      How often to update the cached version
  • Constructor Details

    • CacheCustomContent

      protected CacheCustomContent(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 Details

    • updateData

      protected abstract byte[] updateData() throws T
      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 - a 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
    • 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
      Updates data if required
      Returns:
      Returns the data
      Throws:
      T - if an error occurs
    • updateIfRequiredString

      public String updateIfRequiredString() throws T
      Updates data if required
      Returns:
      Returns the data as string
      Throws:
      T - if an error occurs
    • updateForce

      private byte[] updateForce() throws T
      Executes an update regardless of updateInterval
      Returns:
      Returns the data
      Throws:
      T - if an error occurs
    • updateForceString

      public String updateForceString() throws T
      Executes an update regardless of updateInterval
      Returns:
      Returns the data as String
      Throws:
      T - if an error occurs
    • getData

      public byte[] getData() throws T
      Returns the data without performing any updates
      Returns:
      the data
      Throws:
      T - if an error occurs
    • getDataString

      public String getDataString() throws T
      Returns the data without performing any updates
      Returns:
      the data as String
      Throws:
      T - if an error occurs
    • loadFromDisk

      private void loadFromDisk() throws T
      Tries to load the data using the given ident from disk. If this fails, data will be updated, unless run in offline mode
      Throws:
      T - a 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