com.std.util
Class Persistent<U extends java.io.Serializable>

java.lang.Object
extended by com.std.util.Stateful<U>
extended by com.std.util.Persistent<U>
Type Parameters:
U - class to be wrapped
Direct Known Subclasses:
PersistentAppointmentSet

public class Persistent<U extends java.io.Serializable>
extends Stateful<U>

Persistent is a Stateful object that provides utilities to save and load to a file, and keeps track of the status of that file and whether or not it exists.

Author:
xxx

Constructor Summary
Persistent(U target)
          creates a new persistent object with the given target
 
Method Summary
 java.lang.String getFileName()
           
 boolean isModified()
           
 void load()
          loads this object from a file
 void load(boolean discardOld)
          loads this object from a file
 void load(java.lang.String fileName)
          loads this object from a file
 void load(java.lang.String fileName, boolean discardOld)
          loads this object from a file
 void save()
          saves this object to its previously specified file
 void save(java.lang.String fileName)
          saves this object to a file
 void save(java.lang.String fileName, boolean overwriteOld)
          saves this object to a file
protected  void setModified()
          sets that this object has been changed to be different than what is on file
 void setTarget(U target)
          sets this object to a new instance
 void setTarget(U target, boolean discardOld)
          sets this object to a new instance
 
Methods inherited from class com.std.util.Stateful
getTarget, onChanged
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Persistent

public Persistent(U target)
creates a new persistent object with the given target

Parameters:
target -
Throws:
java.lang.IllegalArgumentException - if the target is null
Method Detail

getFileName

public java.lang.String getFileName()
Returns:
the name of the file this object represents

isModified

public boolean isModified()
Returns:
true iff this set has been modified since the last save or load operation

load

public void load()
throws java.io.IOException,
java.lang.ClassNotFoundException
loads this object from a file

Throws:
java.io.IOException
java.lang.IllegalArgumentException - when the file hasn't been previously saved
java.lang.IllegalStateException - when changes have been made since the last save or load operation
java.lang.ClassNotFoundException

load

public void load(boolean discardOld)
throws java.io.IOException,
java.lang.ClassNotFoundException
loads this object from a file

Parameters:
discardOld - setting this to true will automatically discard any changes to the current object. setting this to false will throw an IllegalStateException when the object has been changed since the last save or load
Throws:
java.io.IOException
java.lang.IllegalArgumentException - when the file hasn't been previously saved
java.lang.IllegalStateException - when changes have been made since the last save or load operation and discardOld is false.
java.lang.ClassNotFoundException

load

public void load(java.lang.String fileName)
throws java.io.IOException,
java.lang.ClassNotFoundException
loads this object from a file

Parameters:
fileName - the name of the file to load from. if this is null, default to the current file name
Throws:
java.io.IOException
java.lang.IllegalArgumentException - when fileName is null and the file hasn't been previously saved
java.lang.IllegalStateException - when changes have been made since the last save or load operation
java.lang.ClassNotFoundException

load

public void load(java.lang.String fileName,
boolean discardOld)
throws java.io.IOException,
java.lang.ClassNotFoundException
loads this object from a file

Parameters:
fileName - the name of the file to load from. if this is null, default to the current file name
discardOld - setting this to true will automatically discard any changes to the current object. setting this to false will throw an IllegalStateException when the object has been changed since the last save or load
Throws:
java.io.IOException
java.lang.IllegalArgumentException - when fileName is null and the file hasn't been previously saved
java.lang.IllegalStateException - when changes have been made since the last save or load operation and discardOld is false.
java.lang.ClassNotFoundException

save

public void save()
throws java.io.IOException
saves this object to its previously specified file

Throws:
java.io.IOException
java.lang.IllegalArgumentException - when the file hasn't been previously saved

save

public void save(java.lang.String fileName)
throws java.io.IOException
saves this object to a file

Parameters:
fileName - the name of the file to save to. if this is null, default to the current file name
Throws:
java.io.IOException
java.lang.IllegalArgumentException - when fileName is null and the file hasn't been previously saved
java.lang.IllegalStateException - when fileName exists but is not this object

save

public void save(java.lang.String fileName,
boolean overwriteOld)
throws java.io.IOException
saves this object to a file

Parameters:
fileName - the name of the file to save to. if this is null, default to the current file name
overwriteOld - setting this to true will automatically overwrite any file with the specified file name. setting this to false will throw an IllegalStateException when fileName already exists
Throws:
java.io.IOException
java.lang.IllegalArgumentException - when fileName is null and the file hasn't been previously saved
java.lang.IllegalStateException - when fileName exists but is not this object and overwriteOld is false.

setModified

protected void setModified()
sets that this object has been changed to be different than what is on file


setTarget

public void setTarget(U target)
sets this object to a new instance

Overrides:
setTarget in class Stateful<U extends java.io.Serializable>
Parameters:
target - the object to replace with
Throws:
java.io.IOException
java.lang.IllegalArgumentException - when the new target is null
java.lang.IllegalStateException - when changes have been made since the last save or load operation

setTarget

public void setTarget(U target,
boolean discardOld)
sets this object to a new instance

Parameters:
target - the object to replace with
discardOld - setting this to true will automatically discard any changes to the current object. setting this to false will throw an IllegalStateException when the object has been changed since the last save or load
Throws:
java.io.IOException
java.lang.IllegalArgumentException - when the new target is null
java.lang.IllegalStateException - when changes have been made since the last save or load operation and discardOld is false.