edu.umd.cloud9.io
Class Schema

java.lang.Object
  extended by edu.umd.cloud9.io.Schema
All Implemented Interfaces:
Cloneable

public class Schema
extends Object
implements Cloneable

Description of a Tuple's structure. The Schema class keeps track of column names, data types, and default values. The following code fragment illustrates the use of this class:

 public static final Schema MYSCHEMA = new Schema();
 static {
        MYSCHEMA.addField("token", String.class, "");
        MYSCHEMA.addField("int", Integer.class, new Integer(1));
 }
 

The following field types are allowed:

Schema instances can be locked to prevent further changes. Any attempt to alter a locked Schema will result in a runtime exception being thrown. If a Schema is not locked, callers are free to add new fields and edit default values.

New Tuple instances can be created directly from Schema objects through the use of the instantiate() method. A call to that method implicitly locks the Schema.

Acknowledgments: much of this code was adapted from the Prefuse Visualization Toolkit.

Author:
Jimmy Lin

Constructor Summary
Schema()
          Creates a new empty Schema.
Schema(int n)
          Creates a new empty Schema with a starting capacity for a given number of fields.
Schema(String[] names, Class<?>[] types)
          Create a new Schema consisting of the given field names and types.
Schema(String[] names, Class<?>[] types, Object[] defaults)
          Create a new Schema consisting of the given field names, types, and default field values.
 
Method Summary
 void addField(String name, Class<?> type)
          Adds a field to this Schema.
 void addField(String name, Class<?> type, Object defaultValue)
          Adds a field to this schema.
 Object clone()
          Creates a copy of this Schema.
 boolean equals(Object o)
          Compares this Schema with another one for equality.
 Object getDefault(int index)
          Returns the default value of the field at the given position.
 Object getDefault(String field)
          Returns the default value of the field with the given name.
 int getFieldCount()
          Returns the number of fields in this Schema.
 int getFieldIndex(String field)
          Returns the position of a field given its name.
 String getFieldName(int index)
          Returns the name of the field at the given position.
 Class<?> getFieldType(int index)
          Returns the type of the field at the given position.
 Class<?> getFieldType(String field)
          Returns the type of the field given its name.
 int hashCode()
          Computes a hashcode for this schema.
 Tuple instantiate()
          Instantiate a new Tuple instance with this Schema.
 Tuple instantiate(Object... objects)
          Instantiate a new Tuple instance with this Schema.
 boolean isLocked()
          Checks if this schema is locked.
 Schema lockSchema()
          Locks the Schema, preventing any additional changes.
 void setDefault(int index, Object val)
          Sets the default value for the given field.
 void setDefault(String field, boolean val)
          Set the default value for the given field as a boolean.
 void setDefault(String field, double val)
          Set the default value for the given field as a double.
 void setDefault(String field, float val)
          Set the default value for the given field as a float.
 void setDefault(String field, int val)
          Sets the default value for the given field as an int.
 void setDefault(String field, long val)
          Set the default value for the given field as a long.
 void setDefault(String field, Object val)
          Sets the default value for the given field.
 String toString()
          Returns a descriptive String for this schema.
 
Methods inherited from class java.lang.Object
getClass, notify, notifyAll, wait, wait, wait
 

Constructor Detail

Schema

public Schema()
Creates a new empty Schema.


Schema

public Schema(int n)
Creates a new empty Schema with a starting capacity for a given number of fields.

Parameters:
n - the number of columns in this schema

Schema

public Schema(String[] names,
              Class<?>[] types)
Create a new Schema consisting of the given field names and types.

Parameters:
names - the field names
types - the field types (as Class instances)

Schema

public Schema(String[] names,
              Class<?>[] types,
              Object[] defaults)
Create a new Schema consisting of the given field names, types, and default field values.

Parameters:
names - the field names
types - the field types (as Class instances)
defaults - the default values for each field
Method Detail

clone

public Object clone()
Creates a copy of this Schema. Cloned copies of a locked Schema will not inherit the locked status.

Overrides:
clone in class Object
See Also:
Object.clone()

lockSchema

public Schema lockSchema()
Locks the Schema, preventing any additional changes. Locked Schemas cannot be unlocked! Cloned copies of a locked schema will not inherit this locked status.

Returns:
a reference to this schema

isLocked

public boolean isLocked()
Checks if this schema is locked. Locked Schemas can not be edited.

Returns:
true if this Schema is locked, false otherwise

addField

public void addField(String name,
                     Class<?> type)
Adds a field to this Schema.

Parameters:
name - the field name
type - the field type (as a Class instance)
Throws:
IllegalArgumentException - if either name or type are null or the name already exists in this schema.

addField

public void addField(String name,
                     Class<?> type,
                     Object defaultValue)
Adds a field to this schema.

Parameters:
name - the field name
type - the field type (as a Class instance)
Throws:
IllegalArgumentException - if either name or type are null or the name already exists in this schema.

getFieldCount

public int getFieldCount()
Returns the number of fields in this Schema.

Returns:
the number of fields in this Schema

getFieldName

public String getFieldName(int index)
Returns the name of the field at the given position.

Parameters:
index - the field index
Returns:
the field name

getFieldIndex

public int getFieldIndex(String field)
Returns the position of a field given its name.

Parameters:
field - the field name
Returns:
the field position index

getFieldType

public Class<?> getFieldType(int index)
Returns the type of the field at the given position.

Parameters:
index - the column index
Returns:
the column type

getFieldType

public Class<?> getFieldType(String field)
Returns the type of the field given its name.

Parameters:
field - the field name
Returns:
the field type

getDefault

public Object getDefault(int index)
Returns the default value of the field at the given position.

Parameters:
index - the field index
Returns:
the field's default value

getDefault

public Object getDefault(String field)
Returns the default value of the field with the given name.

Parameters:
field - the field name
Returns:
the field's default value

setDefault

public void setDefault(int index,
                       Object val)
Sets the default value for the given field.

Parameters:
index - the index position of the field to set the default for
val - the new default value

setDefault

public void setDefault(String field,
                       Object val)
Sets the default value for the given field.

Parameters:
field - the name of field to set the default for
val - the new default value

setDefault

public void setDefault(String field,
                       int val)
Sets the default value for the given field as an int.

Parameters:
field - the name of field to set the default for
val - the new default value

setDefault

public void setDefault(String field,
                       long val)
Set the default value for the given field as a long.

Parameters:
field - the name of field to set the default for
val - the new default value

setDefault

public void setDefault(String field,
                       float val)
Set the default value for the given field as a float.

Parameters:
field - the name of field to set the default for
val - the new default value

setDefault

public void setDefault(String field,
                       double val)
Set the default value for the given field as a double.

Parameters:
field - the name of field to set the default for
val - the new default value

setDefault

public void setDefault(String field,
                       boolean val)
Set the default value for the given field as a boolean.

Parameters:
field - the name of field to set the default for
val - the new default value

equals

public boolean equals(Object o)
Compares this Schema with another one for equality.

Overrides:
equals in class Object

hashCode

public int hashCode()
Computes a hashcode for this schema.

Overrides:
hashCode in class Object

toString

public String toString()
Returns a descriptive String for this schema.

Overrides:
toString in class Object

instantiate

public Tuple instantiate()
Instantiate a new Tuple instance with this Schema. Fields of the newly instantiated Tuple are set to default value.

Returns:
a new Tuple with this Schema

instantiate

public Tuple instantiate(Object... objects)
Instantiate a new Tuple instance with this Schema.

Parameters:
objects - values of each field
Returns:
a new Tuple with this Schema