Class FormatRepository<K extends Serializable>
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:
static final FormatRepository
in class
C
with context set to C
.
static final
formats in C
initialized by calls to cache(K)
.
C
to compute the formats.
store(File)
to store the
computed 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 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
Modifier and TypeFieldDescriptionprotected final Class<?>
The class relative to which the repository will be stored as an resource.protected final String
The resource filename.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
ModifierConstructorDescriptionprotected
FormatRepository
(Class<?> context, String name) Creates a new repository. -
Method Summary
Modifier and TypeMethodDescriptionComputes or retrieves the format for a key.void
clear()
Removes all computed or loaded formats.boolean
store()
Stores computed formats relative to the current working directory.boolean
Stores computed formats relative to a given directory.protected abstract Format
Computes the format for a key.
-
Field Details
-
context
The class relative to which the repository will be stored as an resource. -
name
A name to identify the repository, if more than one are installed. -
filename
-
formats
The computed formats. -
stored
protected boolean storedIndicates whether the current state has been successfully stored. -
wd
Cache for the working directory (at the time of class loading)
-
-
Constructor Details
-
FormatRepository
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
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)
. -
clear
public void clear()Removes all computed or loaded formats. -
toFormat
Computes the format for a key. -
store
public boolean store()Stores computed formats relative to the current working directory. as stored inwd
. -
store
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 fromfilename
. 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.
-