Package eu.bandm.tools.format
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.
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 -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedConstructs an instance with empty state.protectedFormatRepository(File in) Constructs an instance and loads the state from a file.protectedConstructs an instance and loads the state from a stream file. -
Method Summary
Modifier and TypeMethodDescriptionComputes or retrieves the format for a key.voidclear()Removes all computed or loaded formats.booleanisStored()Returns whether the current state has been successfully stored.booleanStores computed formats in the given file.booleanstore(OutputStream drain) Stores computed formats to the given output stream.protected abstract FormatComputes the format for a key.
-
Field Details
-
formats
The computed formats. -
stored
protected boolean storedIndicates whether the current state has been successfully stored.
-
-
Constructor Details
-
FormatRepository
protected FormatRepository()Constructs an instance with empty state. -
FormatRepository
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 offormats.- Throws:
IOException- if data souce cannot be readClassNotFoundException- if data souce cannot be de-serialized
-
FormatRepository
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 offormats.- Throws:
IOException- if data souce cannot be readClassNotFoundException- 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
Computes or retrieves the format for a key. If the format has already been computed or loaded, it is retrieved. Otherwise, it is computed withtoFormat(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
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
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
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!)
-