Class CachedFileContents<T>

  • Type Parameters:
    T - the type of cached contents

    public class CachedFileContents<T>
    extends java.lang.Object
    A cache for file contents. The cache allows closing a file and saving in memory its contents (or some related information). It can then be used to check if the contents are still valid at some later time. Typical usage flow is:

    
     Object fileRepresentation = // ...
     File toWrite = // ...
     // Write file contents and update in memory representation
     CachedFileContents<Object> contents = new CachedFileContents<Object>(toWrite);
     contents.closed(fileRepresentation);
    
     // Later, when data is needed:
     if (contents.isValid()) {
         fileRepresentation = contents.getCache();
     } else {
         // Re-read the file and recreate the file representation
     }
     
    • Constructor Summary

      Constructors 
      Constructor Description
      CachedFileContents​(java.io.File file)
      Creates a new contents.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void closed​(T cache)
      Should be called when the file's contents are set and the file closed.
      T getCache()
      Obtains the cached data set with closed(Object) if the file has not been modified since closed(Object) was invoked.
      java.io.File getFile()
      Obtains the file used for caching.
      boolean isValid()
      Are the cached contents still valid? If this method determines that the file has been modified since the last time closed(Object) was invoked.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • CachedFileContents

        public CachedFileContents​(java.io.File file)
        Creates a new contents. When the file is written, closed(Object) should be invoked to set the cache.
        Parameters:
        file - the file
    • Method Detail

      • closed

        public void closed​(@Nullable
                           T cache)
        Should be called when the file's contents are set and the file closed. This will save the cache and register the file's timestamp to later detect if it has been modified.

        This method can be called as many times as the file has been written.

        Parameters:
        cache - an optional cache to save
      • isValid

        public boolean isValid()
        Are the cached contents still valid? If this method determines that the file has been modified since the last time closed(Object) was invoked.
        Returns:
        are the cached contents still valid? If this method returns false, the cache is cleared
      • getCache

        @Nullable
        public T getCache()
        Obtains the cached data set with closed(Object) if the file has not been modified since closed(Object) was invoked.
        Returns:
        the last cached data or null if the file has been modified since closed(Object) has been invoked
      • getFile

        public java.io.File getFile()
        Obtains the file used for caching.
        Returns:
        the file; this file always exists and contains the old (cached) contents of the file