public abstract class ObjectListFile<T> extends ListFile
ListFile
to
support serialisation and deserialisation in a user defined way. This is useful when
lists of configuration data should be kept in a simple, human readable format.The format to be used for (de-) serialisation is defined by the actual implementation of this class. This allows also the use of arbitrary constructors.
You should sure consider the use of XML or the serialisation support from java.io package.
Constructor and Description |
---|
ObjectListFile(java.io.File file)
Instantiate this class with the file that holds the data.
|
Modifier and Type | Method and Description |
---|---|
abstract java.lang.String |
format(T type)
Implement this method to specify how the object should be formated as string.
|
java.util.List<T> |
loadList()
Loads and returns a list of objects from the file specified with the constructor.
|
abstract T |
parse(java.lang.String string)
Implement this method to reconstruct an object from its serialised form.
|
void |
storeList(java.util.List<? extends T> list,
java.lang.String header)
Writes the list to the file specified with the constructor.
|
public ObjectListFile(java.io.File file)
file
- to be read or written.public java.util.List<T> loadList() throws java.io.IOException
java.io.IOException
- if the file could not be read, or if the parse
method threw an IOExceptionparse(String)
public void storeList(java.util.List<? extends T> list, java.lang.String header) throws java.io.IOException
list
- to be stored in the fileheader
- optional comment, may span multiple linesjava.io.IOException
- if the file could not be written.public abstract java.lang.String format(T type)
storeList()
. If it returns null, the respective
object will be ignored and not be written to file.type
- to be formatedpublic abstract T parse(java.lang.String string) throws java.io.IOException
loadList()
. If it returns null, the respective line will be ignored, if it throws an
IOException parsing of the file will stop, and loadList()
will return the exception.string
- to be parsedjava.io.IOException
- if parsing should be interrupted