public abstract class FormatRepository<K extends Serializable> extends Object
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.
Modifier and Type | Field and Description |
---|---|
protected Class<?> |
context |
protected String |
filename
The resource filename.
|
protected Map<K,Format> |
formats
The computed formats.
|
protected String |
name |
protected boolean |
stored
Indicates whether the current state has been successfully
stored.
|
protected static File |
wd |
Constructor and Description |
---|
FormatRepository(Class<?> context,
String name)
Creates a new repository.
|
Modifier and Type | Method and Description |
---|---|
Format |
cache(K key)
Computes 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 |
store(File directory)
Stores computed formats relative to a given directory.
|
protected abstract Format |
toFormat(K key)
Computes the format for a key.
|
protected final Class<?> context
protected final String name
protected final String filename
protected final Map<K extends Serializable,Format> formats
protected boolean stored
protected static final File wd
public FormatRepository(Class<?> context, String name)
filename
) for this repository
is composed from a context class and a specific name. If the
resource is available, the stored formats are loaded.context
- the context class.name
- the name.public Format cache(K key)
toFormat(K)
.public void clear()
public boolean store()
public boolean store(File directory)
directory
- the directory. It is created if it does not
exists.true
if successful.see also the complete user documentation .