com.revusky.oreo
Class DefaultRecord

java.lang.Object
  |
  +--com.revusky.oreo.DefaultRecord
All Implemented Interfaces:
java.lang.Cloneable, java.io.Externalizable, Record, java.io.Serializable
Direct Known Subclasses:
AntiRecord

public class DefaultRecord
extends java.lang.Object
implements Record, java.io.Externalizable, java.lang.Cloneable

A concrete implementation of the Record interface In this implementation, the records can be written out as a human-readable string. This is the scheme used for serialization. DefaultRecord implements the java.io.Externalizable interface in order to avoid the general-purpose serialization scheme. The goal was that the resulting storage format should be human-readable and modifiable in a text editor in a pinch.

Author:
Serialized Form

Field Summary
static java.lang.String STATUS_KEY
          The special reserved name of the status pseudo-key.
static java.lang.String TYPE_KEY
          The special reserved name of the type pseudo-key.
 
Constructor Summary
DefaultRecord()
           
 
Method Summary
 void checkValidity()
          Check if the record's data is valid.
 void clearFields()
          set the fields to their default values.
 boolean equals(java.lang.Object o)
           
 void freeze()
          make this record immutable.
 java.lang.Object get(FieldDescriptor field)
          Low-level method to query the value of a field in a Record.
protected  java.lang.Object get(int i)
           
 java.lang.Object get(java.lang.Object o)
           
 java.lang.Object get(java.lang.String fieldname)
          Method to get the value of a field by name.
 RecordDescriptor getMetadata()
           
 Record getMutableCopy()
          create a clone.
 java.lang.Object getPrimaryKey()
          Retrieve the value of the record's primary key.
 java.lang.String getType()
           
 boolean isImmutable()
          Once a record is immutable, attempts to change data will result in an ImmutableDataException being thrown.
 boolean isStale()
          Has this record been deleted or superseded in the associated DataSource?
protected  void postCheck()
          Called after checking the fields.
protected  void preCheck()
          Called before checking the fields.
 void readExternal(java.io.ObjectInput in)
           
 void set(FieldDescriptor field, java.lang.Object value)
          Low-level method to set an individual field value concrete implementations will probably wrap this.
protected  void set(int i, java.lang.Object value)
           
 void set(java.lang.String fieldname, java.lang.Object value)
          Method to set the value of a field by name.
 void setMetadata(RecordDescriptor descriptor)
          Method only used internally.
 void setPrimaryKey(java.lang.Object o)
          set the value of this record's primary key.
 java.lang.String toString()
          Provides a default string representation of the record for a human-readable flat-file persistence scheme.
 void writeExternal(java.io.ObjectOutput out)
           
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

TYPE_KEY

public static final java.lang.String TYPE_KEY
The special reserved name of the type pseudo-key.

STATUS_KEY

public static final java.lang.String STATUS_KEY
The special reserved name of the status pseudo-key.
Constructor Detail

DefaultRecord

public DefaultRecord()
Method Detail

isImmutable

public boolean isImmutable()
Once a record is immutable, attempts to change data will result in an ImmutableDataException being thrown.
Specified by:
isImmutable in interface Record
Returns:
boolean value representing whether the record is immutable.

checkValidity

public final void checkValidity()
                         throws java.io.IOException
Check if the record's data is valid.
Throws:
java.io.IOException - if any of the fields are not valid. The appropriate error message is encoded in the exception.

isStale

public boolean isStale()
Description copied from interface: Record
Has this record been deleted or superseded in the associated DataSource?
Specified by:
isStale in interface Record

getPrimaryKey

public java.lang.Object getPrimaryKey()
Description copied from interface: Record
Retrieve the value of the record's primary key.
Specified by:
getPrimaryKey in interface Record
Following copied from interface: com.revusky.oreo.Record
Returns:
the value of this record's primary key, or null if it has not been set.

setPrimaryKey

public void setPrimaryKey(java.lang.Object o)
Description copied from interface: Record
set the value of this record's primary key. The record must be in a mutable state AND the primary key must be unset.
Specified by:
setPrimaryKey in interface Record

toString

public java.lang.String toString()
Provides a default string representation of the record for a human-readable flat-file persistence scheme.
Overrides:
toString in class java.lang.Object
Returns:
string of form field=value \n field=value \n\n

preCheck

protected void preCheck()
                 throws java.io.IOException
Called before checking the fields. Does nothing in the base implementation. Overriding this method allows you to apply certain (possibly ad hoc) fixups before doing any validity checks. Basically, this gives a class a chance to massage data that wouldn't pass the validity check.

postCheck

protected void postCheck()
                  throws java.io.IOException
Called after checking the fields. Does nothing in the base implementaiton. If you want more, possibly cross-field or cross-record validity checks, override this method and put the checks here. If the data flunks, throw the appropriate exception.

freeze

public void freeze()
            throws java.io.IOException
Description copied from interface: Record
make this record immutable. This will typically only be called by a MutableDataSource object.
Specified by:
freeze in interface Record

get

public final java.lang.Object get(java.lang.String fieldname)
Description copied from interface: Record
Method to get the value of a field by name. Concrete implementations may wrap this with a higher-level get/set API.
Specified by:
get in interface Record
Following copied from interface: com.revusky.oreo.Record
Parameters:
fieldname -  
Throws:
InvalidFieldException - if there is no field of that name.

set

public final void set(java.lang.String fieldname,
                      java.lang.Object value)
Description copied from interface: Record
Method to set the value of a field by name. Concrete implementations may wrap this with a higher-level get/set API.
Specified by:
set in interface Record
Following copied from interface: com.revusky.oreo.Record
Parameters:
fieldname - the name of the field to set.
Throws:
InvalidFieldException - if there is no field of that name.
ImmutableDataException - if this record is immutable @see #getMutableCopy()

get

public final java.lang.Object get(FieldDescriptor field)
Description copied from interface: Record
Low-level method to query the value of a field in a Record. concrete implementations will probably wrap this.
Specified by:
get in interface Record
Following copied from interface: com.revusky.oreo.Record
Parameters:
field - to query.
Returns:
an Object wrapper around field value.

set

public final void set(FieldDescriptor field,
                      java.lang.Object value)
Description copied from interface: Record
Low-level method to set an individual field value concrete implementations will probably wrap this.
Specified by:
set in interface Record
Following copied from interface: com.revusky.oreo.Record
Parameters:
field - to set.
value - Object wrapping the value
Throws:
InvalidFieldException - if there is no field of that name.
ImmutableDataException - if this record is immutable @see #getMutableCopy()

getMetadata

public final RecordDescriptor getMetadata()
Specified by:
getMetadata in interface Record
Following copied from interface: com.revusky.oreo.Record
Returns:
an object that describes the data held in this record.
See Also:
RecordDescriptor

readExternal

public void readExternal(java.io.ObjectInput in)
                  throws java.io.IOException,
                         java.lang.ClassNotFoundException
Specified by:
readExternal in interface java.io.Externalizable

writeExternal

public void writeExternal(java.io.ObjectOutput out)
                   throws java.io.IOException
Specified by:
writeExternal in interface java.io.Externalizable

getMutableCopy

public Record getMutableCopy()
create a clone. A shallow copy of the values array should be enough for immutable records Note that a newly cloned object has its immutable flag set to false, so that it is in a modifiable state.
Specified by:
getMutableCopy in interface Record

set

protected final void set(int i,
                         java.lang.Object value)

get

protected final java.lang.Object get(int i)

get

public java.lang.Object get(java.lang.Object o)

clearFields

public void clearFields()
set the fields to their default values.
Specified by:
clearFields in interface Record
Following copied from interface: com.revusky.oreo.Record
Throws:
ImmutableDataException - if this record is immutable @see #getMutableCopy()

equals

public boolean equals(java.lang.Object o)
Overrides:
equals in class java.lang.Object

setMetadata

public final void setMetadata(RecordDescriptor descriptor)
Description copied from interface: Record
Method only used internally.
Specified by:
setMetadata in interface Record

getType

public final java.lang.String getType()
Specified by:
getType in interface Record
Following copied from interface: com.revusky.oreo.Record
Returns:
the name of this record type subclasses must implement this. e.g. "reminder"