com.martiansoftware.trivialpersist
Class Record

java.lang.Object
  extended bycom.martiansoftware.trivialpersist.Record
All Implemented Interfaces:
Cloneable, Comparable

public class Record
extends Object
implements Comparable, Cloneable

A trivially persistent object. Records contain any number of fields of the following data types:

Floats or doubles may be added in the future.

The names and types of fields are defined in a <tablename>.schema file in the Store's base directory. The .schema file contains one line for each field, in the form fieldname=fieldtype. A silly example:

File order.schema
product_code=string
quantity=long
ordertime=date
rushdelivery=boolean

Author:
Marty Lamb TODO: remove some repetitive code

Method Summary
 Record asWriteable()
          Returns a copy of this Record that may be modified and saved.
 int compareTo(Object o)
          Compares this Record's key with that of the specified object.
 void delete()
          Deletes this record from the store (and disk).
 boolean getBoolean(String fieldName)
          Retrieves a boolean value from this record
 Date getDate(String fieldName)
          Retrieves a date field from this record
 String getDate(String fieldName, DateFormat format)
          Retrieves a date field from this record as a String.
 double getDouble(String fieldName)
          Retrieves a double value from this record
 Key getKey()
          Returns this record's key
 long getLong(String fieldName)
          Retrieves a long value from this record
 Object getObject(String fieldName)
          Returns the Object backing the specified field.
 String getString(String fieldName)
          Retrieves a string field from this record
 String getString(String fieldName, String defaultValue)
          Retrieves a string field from this record
 Table getTable()
          Returns the Table to which this Record belongs
 boolean hasValue(String fieldName)
          Returns true iff this record has any value set for the specified field
 boolean isDirty()
          Returns true iff this Record has unsaved changes
 boolean isNew()
          Returns true iff this Record has never been saved
 boolean isReadOnly()
          Returns true iff this Record is read-only
 Record save()
          Saves this record to disk.
 Record setBoolean(String fieldName, boolean value)
          Sets a boolean value for this record
 Record setDate(String fieldName, Date value)
          Sets a date field for this record.
 Record setDate(String fieldName, String value, DateFormat format)
          Sets a date field for this record
 Record setDouble(String fieldName, double value)
          Sets a double value for this record
 Record setLong(String fieldName, long value)
          Sets a long value for this record
 Record setString(String fieldName, String value)
          Sets a string field in this record
 Record unset(String fieldName)
          Removes any value set for the specified field (if any).
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

hasValue

public boolean hasValue(String fieldName)
Returns true iff this record has any value set for the specified field

Parameters:
fieldName - the name of the field to check for a value
Returns:
true iff this record has any value set for the specified field

setString

public Record setString(String fieldName,
                        String value)
Sets a string field in this record

Parameters:
fieldName - the name of the field to set
value - the string value to set
Returns:
a reference to this Record (allows chaining of set methods)

getString

public String getString(String fieldName)
Retrieves a string field from this record

Parameters:
fieldName - the name of the field to retrieve
Returns:
the requested string value, or null if no value is associated with the requested field

getString

public String getString(String fieldName,
                        String defaultValue)
Retrieves a string field from this record

Parameters:
fieldName - the name of the field to retrieve
defaultValue - the default value to return if the specified field has a null value
Returns:
the value of the specified string field, or the specified default value if the string field contains a null value

setDate

public Record setDate(String fieldName,
                      Date value)
Sets a date field for this record.

Parameters:
fieldName - the name of the field to set
value - the Date value to set
Returns:
a reference to this Record (allows chaining of set methods)

setDate

public Record setDate(String fieldName,
                      String value,
                      DateFormat format)
Sets a date field for this record

Parameters:
fieldName - the name of the field to set
value - the date value to set, encoded as a string
format - the date format to use for converting the specified string to a java.util.Date. If a conversion error occurs, an IllegalArgumentException is thrown.
Returns:
a reference to this Record (allows chaining of set methods)

getDate

public Date getDate(String fieldName)
Retrieves a date field from this record

Parameters:
fieldName - the name of the date field to retrieve
Returns:
the requested date field, or null if no date value is set for the specified field name

getDate

public String getDate(String fieldName,
                      DateFormat format)
Retrieves a date field from this record as a String.

Parameters:
fieldName - the name of the date field to retrieve
format - the date format to use in converting the date value to a string
Returns:
the converted date, or an empty string if no date value is set for the specified field name

setBoolean

public Record setBoolean(String fieldName,
                         boolean value)
Sets a boolean value for this record

Parameters:
fieldName - the name of the field to set
value - the boolean value to set
Returns:
a reference to this Record (allows chaining of set methods)

getBoolean

public boolean getBoolean(String fieldName)
Retrieves a boolean value from this record

Parameters:
fieldName - the name of the boolean field to retrieve
Returns:
the requested boolean value, or false if no boolean value is set for the specified field.
See Also:
hasValue(String)

setLong

public Record setLong(String fieldName,
                      long value)
Sets a long value for this record

Parameters:
fieldName - the name of the field to set
value - the long value to set
Returns:
a reference to this Record (allows chaining of set methods)

setDouble

public Record setDouble(String fieldName,
                        double value)
Sets a double value for this record

Parameters:
fieldName - the name of the field to set
value - the double value to set
Returns:
a reference to this Record (allows chaining of set methods)

getLong

public long getLong(String fieldName)
Retrieves a long value from this record

Parameters:
fieldName - the name of the long value field to retrieve
Returns:
the requested long value, or zero of no long value is set for the specified field name
See Also:
hasValue(String)

getDouble

public double getDouble(String fieldName)
Retrieves a double value from this record

Parameters:
fieldName - the name of the double value field to retrieve
Returns:
the requested double value, or zero of no double value is set for the specified field name
See Also:
hasValue(String)

getObject

public Object getObject(String fieldName)
Returns the Object backing the specified field. No type checks or schema checks are performed, and no case conversion is performed on the field name. This is used only to support the FieldComparator class.

Parameters:
fieldName - the name of the field Object to retrieve
Returns:
the specified Object, or null if no such Object exists

unset

public Record unset(String fieldName)
Removes any value set for the specified field (if any). If no value is set for the specified field, or even if the field name is not valid for this table's schema, this method has no effect.

Parameters:
fieldName - the name of the field to unset.
Returns:
a reference to this Record (allows chaining of set methods)

save

public Record save()
            throws TrivialPersistException
Saves this record to disk. TODO: handle status changes reflected in the filename's :info section

Throws:
TrivialPersistException - if an error of any kind occurs while saving.

delete

public void delete()
            throws TrivialPersistException
Deletes this record from the store (and disk).

Throws:
TrivialPersistException - if an error of any kind occurs while deleting this record

compareTo

public int compareTo(Object o)
Compares this Record's key with that of the specified object.

Specified by:
compareTo in interface Comparable
Parameters:
o - the other Record to check

getKey

public Key getKey()
Returns this record's key

Returns:
this record's key

isNew

public boolean isNew()
Returns true iff this Record has never been saved

Returns:
true iff this Record has never been saved

getTable

public Table getTable()
Returns the Table to which this Record belongs

Returns:
the Table to which this Record belongs

isDirty

public boolean isDirty()
Returns true iff this Record has unsaved changes

Returns:
true iff this Record has unsaved changes

isReadOnly

public boolean isReadOnly()
Returns true iff this Record is read-only

Returns:
true iff this Record is read-only

asWriteable

public Record asWriteable()
Returns a copy of this Record that may be modified and saved.

Returns:
a copy of this Record that may be modified and saved.


Copyright © 2004, Martian Software, Inc.. All Rights Reserved.
For the latest version and documentation, please visit http://www.martiansoftware.com/trivialpersist