Class FormatRepository<K extends Serializable>

java.lang.Object
eu.bandm.tools.format.FormatRepository<K>

public abstract class FormatRepository<K extends Serializable> extends Object
A map-like store for formats with serialized resource backing store and on-demand computation.

The intended use is to bundle constant formats bound to static final fields of a class C. If no further action is taken, these are computed at the time class C is loaded. In order to precompute them offline, so that loading class C merely deserializes the formats, do the following:

  • Declare a static final FormatRepository in class C with context set to C.
  • Declare static final formats in C initialized by calls to cache(K).
  • Load class C to compute the formats.
  • Call store(File) to store the computed formats.
  • Install the resulting file to the location from where the class is loaded.

    For using this class you have to derive a sub-class and implement the only abstract method, toFormat(K), which carries out the effective generation of format objects.
    A practical way is to use java.lang.String as the K parameter, and call a parser (like metajava FormatClosure to derive the format objects from the character sequence forming the key.

    This class can also be used in conjunction with dynamically produced formats. The caching functionality is completely available in the dynamic case. But beware that persistent storage of a dynamic collection of formats is only efficient if the actual members do not vary too much from one run of the application to the other, or if a superset of potential members can be approximated ahead of time.

    • Field Summary

      Fields
      Modifier and Type
      Field
      Description
      protected final Class<?>
      The class relative to which the repository will be stored as an resource.
      protected final String
      The resource filename.
      protected final Map<K,Format>
      The computed formats.
      protected final String
      A name to identify the repository, if more than one are installed.
      protected boolean
      Indicates whether the current state has been successfully stored.
      protected static final File
      Cache for the working directory (at the time of class loading)
    • Constructor Summary

      Constructors
      Modifier
      Constructor
      Description
      protected
      FormatRepository(Class<?> context, String name)
      Creates a new repository.
    • Method Summary

      Modifier and Type
      Method
      Description
      cache(K key)
      Computes or retrieves the format for a key.
      void
      Removes all computed or loaded formats.
      boolean
      Stores computed formats relative to the current working directory.
      boolean
      store(File directory)
      Stores computed formats relative to a given directory.
      protected abstract Format
      toFormat(K key)
      Computes the format for a key.

      Methods inherited from class java.lang.Object

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

      • context

        protected final Class<?> context
        The class relative to which the repository will be stored as an resource.
      • name

        protected final String name
        A name to identify the repository, if more than one are installed.
      • filename

        protected final String filename
        The resource filename. Always set to String.valueOf(context) + "." + name
      • formats

        protected final Map<K extends Serializable,Format> formats
        The computed formats.
      • stored

        protected boolean stored
        Indicates whether the current state has been successfully stored.
      • wd

        protected static final File wd
        Cache for the working directory (at the time of class loading)
    • Constructor Details

      • FormatRepository

        protected FormatRepository(Class<?> context, String name)
        Creates a new repository. The resource name (i.e. filename) for this repository is composed from a context class and a specific name. If the resource is available, the stored formats are loaded.
        Parameters:
        context - the context class.
        name - the name.
    • Method Details

      • cache

        public Format cache(K key)
        Computes or retrieves the format for a key. If the format has already been computed or loaded, it is retrieved. Otherwise, it is computed with toFormat(K).
      • clear

        public void clear()
        Removes all computed or loaded formats.
      • toFormat

        protected abstract Format toFormat(K key)
        Computes the format for a key.
      • store

        public boolean store()
        Stores computed formats relative to the current working directory. as stored in wd.
      • store

        public boolean store(File directory)
        Stores computed formats relative to a given directory. Creates a file directory/filename. Must be installed into the class tree manually in order to be loadable as a resource. The file name is taken from filename. Only first successful invocation will store, all others just return true.
        Parameters:
        directory - where to store the data. This directory is created if it does not exists.
        Returns:
        true if successfully stored with this or an earlier invocation.