Class FormatRepository<K extends Serializable>

java.lang.Object
eu.bandm.tools.format.FormatRepository<K>
Type Parameters:
K - the keys under which the formats are stored.

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 cache constant formats. 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 @code java.lang.String} as the K parameter, and call a parser to derive the format objects from the character sequence forming the key.

The current state can be written to a stream or a file, and can be reloaded from a stream or a file. Writing is possible only once for each new cache state.

This class can also be used in conjunction with dynamically produced formats. The caching functionality is completely available in the dynamic case.

  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    protected final Map<K,Format>
    The computed formats.
    protected boolean
    Indicates whether the current state has been successfully stored.
  • Constructor Summary

    Constructors
    Modifier
    Constructor
    Description
    protected
    Constructs an instance with empty state.
    protected
    Constructs an instance and loads the state from a file.
    protected
    Constructs an instance and loads the state from a stream file.
  • 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
    Returns whether the current state has been successfully stored.
    boolean
    store(File drain)
    Stores computed formats in the given file.
    boolean
    Stores computed formats to the given output stream.
    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

    • 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.
  • Constructor Details

    • FormatRepository

      protected FormatRepository()
      Constructs an instance with empty state.
    • FormatRepository

      protected FormatRepository(File in) throws IOException, ClassNotFoundException
      Constructs an instance and loads the state from a file.
      Parameters:
      in - the file to load the formats from. Must contain a Java standard serialization of the map type of formats.
      Throws:
      IOException - if data souce cannot be read
      ClassNotFoundException - if data souce cannot be de-serialized
    • FormatRepository

      protected FormatRepository(InputStream in) throws IOException, ClassNotFoundException
      Constructs an instance and loads the state from a stream file.
      Parameters:
      in - the stream to load the formats from. Must contain a Java standard serialization of the map type of formats.
      Throws:
      IOException - if data souce cannot be read
      ClassNotFoundException - if data souce cannot be de-serialized
  • Method Details

    • isStored

      public boolean isStored()
      Returns whether the current state has been successfully stored.
      Returns:
      whether the current state has been successfully stored.
    • 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).
      Parameters:
      key - under which the format is stored and from which the format can be computed.
      Returns:
      the format calculated for the key
    • clear

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

      protected abstract Format toFormat(K key)
      Computes the format for a key.
      Parameters:
      key - under which the format is stored and from which the format can be computed.
      Returns:
      the format calculated for the key
    • store

      public boolean store(File drain)
      Stores computed formats in the given file. For each state, only the first successful invocation of one of the store methods will store, all others just return true.
      Parameters:
      drain - where to store the data.
      Returns:
      whether successfully stored with this or an earlier invocation. (That earlier invocation can be with a different paremeter!)
    • store

      public boolean store(OutputStream drain)
      Stores computed formats to the given output stream. For each state, only the first successful invocation of one of the store methods will store, all others just return true.
      Parameters:
      drain - where to store the data.
      Returns:
      whether successfully stored with this or an earlier invocation. (That earlier invocation can be with a different paremeter!)