Class CacheCustomContent<T extends java.lang.Throwable>

    • Field Summary

      Fields 
      Modifier and Type Field Description
      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
      Update interval meaning an update is always needed
      static int INTERVAL_DAILY
      Update interval meaning an update is needed each day
      static int INTERVAL_HOURLY
      Update interval meaning an update is needed each hour
      static int INTERVAL_MONTHLY
      Update interval meaning an update is needed each month
      static int INTERVAL_NEVER
      Update interval meaning an update is never needed
      static int INTERVAL_WEEKLY
      Update interval meaning an update is needed each week
      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

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

      All Methods Instance Methods Abstract Methods Concrete Methods 
      Modifier and Type Method Description
      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
      protected abstract boolean isOffline()
      Checks underlying resource is not accessed in offline mode.
      private void loadFromDisk()
      Tries to load the data using the given ident from disk.
      private boolean needsUpdate()  
      private void saveToDisk()
      Stores the data to disk
      protected abstract byte[] updateData()
      This function will be executed when an update is required.
      private 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_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
      • 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.
        Returns:
        the data to cache
        Throws:
        T - a Throwable
        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
      • 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
        Returns:
        Returns the data
        Throws:
        T - if an error occurs
        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 - if an error occurs
        T extends java.lang.Throwable
      • updateForce

        private byte[] updateForce()
                            throws T extends java.lang.Throwable
        Executes an update regardless of updateInterval
        Returns:
        Returns the data
        Throws:
        T - if an error occurs
        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 - if an error occurs
        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 - if an error occurs
        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 - if an error occurs
        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, unless run in offline mode
        Throws:
        T - a Throwable
        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