prefuse.data
Class Schema

java.lang.Object
  extended by prefuse.data.Schema
All Implemented Interfaces:
java.lang.Cloneable

public class Schema
extends java.lang.Object
implements java.lang.Cloneable

The Schema class represents a description of a Table's columns, including column names, data types, and default values. New Table instances can be created directly from Schema objects through the use of the instantiate() method. If a schema is subsequently changed, instantiated table instances are not affected, keeping their original schema.

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, clients are free to add new columns and edit default values.

Author:
jeffrey heer

Constructor Summary
Schema()
          Creates a new empty schema.
Schema(int ncols)
          Creates a new empty schema with a starting capacity for a given number of columns.
Schema(java.lang.String[] names, java.lang.Class[] types)
          Create a new schema consisting of the given column names and types.
Schema(java.lang.String[] names, java.lang.Class[] types, java.lang.Object[] defaults)
          Create a new schema consisting of the given column names, types, and default column values.
 
Method Summary
 void addColumn(java.lang.String name, java.lang.Class type)
          Add a column to this schema.
 void addColumn(java.lang.String name, java.lang.Class type, java.lang.Object defaultValue)
          Add a column to this schema.
 void addInterpolatedColumn(java.lang.String name, java.lang.Class type)
          Add an interpolated column with a null default value.
 void addInterpolatedColumn(java.lang.String name, java.lang.Class type, java.lang.Object dflt)
          Add a new interpolated column to this data schema.
 java.lang.Object clone()
          Creates a copy of this Schema.
 boolean equals(java.lang.Object o)
          Compares this schema with another one for equality.
 int getColumnCount()
          Get the number of columns in this schema.
 int getColumnIndex(java.lang.String field)
          The column index for the column with the given name.
 java.lang.String getColumnName(int col)
          The name of the column at the given position.
 java.lang.Class getColumnType(int col)
          The type of the column at the given position.
 java.lang.Class getColumnType(java.lang.String field)
          The type of the column with the given name.
 java.lang.Object getDefault(int col)
          The default value of the column at the given position.
 java.lang.Object getDefault(java.lang.String field)
          The default value of the column with the given name.
 int hashCode()
          Computes a hashcode for this schema.
protected  void initLookup()
          Lazily construct the lookup table for this schema.
 Table instantiate()
          Instantiate this schema as a new Table instance.
 Table instantiate(int nrows)
          Instantiate this schema as a new Table instance.
 boolean isAssignableFrom(Schema s)
          Indicates if values from a given Schema can be safely assigned to data using this Schema.
 boolean isLocked()
          Indicates if this schema is locked.
 Schema lockSchema()
          Locks the schema, preventing any additional changes.
 void setDefault(int col, java.lang.Object val)
          Set the default value for the given field.
 void setDefault(java.lang.String field, boolean val)
          Set the default value for the given field as a boolean.
 void setDefault(java.lang.String field, double val)
          Set the default value for the given field as a double.
 void setDefault(java.lang.String field, float val)
          Set the default value for the given field as a float.
 void setDefault(java.lang.String field, int val)
          Set the default value for the given field as an int.
 void setDefault(java.lang.String field, long val)
          Set the default value for the given field as a long.
 void setDefault(java.lang.String field, java.lang.Object val)
          Set the default value for the given field.
 void setInterpolatedDefault(java.lang.String field, boolean val)
          Set default values for the current, start, and end columns of an interpolated column as a boolean.
 void setInterpolatedDefault(java.lang.String field, double val)
          Set default values for the current, start, and end columns of an interpolated column as a double.
 void setInterpolatedDefault(java.lang.String field, float val)
          Set default values for the current, start, and end columns of an interpolated column as a float.
 void setInterpolatedDefault(java.lang.String field, int val)
          Set default values for the current, start, and end columns of an interpolated column as an int.
 void setInterpolatedDefault(java.lang.String field, long val)
          Set default values for the current, start, and end columns of an interpolated column as a long.
 void setInterpolatedDefault(java.lang.String field, java.lang.Object val)
          Set default values for the current, start, and end columns of an interpolated column.
 java.lang.String toString()
          Returns a descriptive String for this schema.
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 

Constructor Detail

Schema

public Schema()
Creates a new empty schema.


Schema

public Schema(int ncols)
Creates a new empty schema with a starting capacity for a given number of columns.

Parameters:
ncols - the number of columns in this schema

Schema

public Schema(java.lang.String[] names,
              java.lang.Class[] types)
Create a new schema consisting of the given column names and types.

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

Schema

public Schema(java.lang.String[] names,
              java.lang.Class[] types,
              java.lang.Object[] defaults)
Create a new schema consisting of the given column names, types, and default column values.

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

clone

public java.lang.Object clone()
Creates a copy of this Schema. This might be useful for creating extended schemas from a shared base schema. Cloned copies of a locked Schema will not inherit the locked status.

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

initLookup

protected void initLookup()
Lazily construct the lookup table for this schema. Used to accelerate name-based lookups of schema information.


lockSchema

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

Returns:
a pointer to this schema

isLocked

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

Returns:
true if this schema is locked, false otherwise

addColumn

public void addColumn(java.lang.String name,
                      java.lang.Class type)
Add a column to this schema.

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

addColumn

public void addColumn(java.lang.String name,
                      java.lang.Class type,
                      java.lang.Object defaultValue)
Add a column to this schema.

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

addInterpolatedColumn

public void addInterpolatedColumn(java.lang.String name,
                                  java.lang.Class type,
                                  java.lang.Object dflt)

Add a new interpolated column to this data schema. This actually adds three columns to the schema: a column for the current value of the field, and columns for starting and ending values. During animation or operations spread over a time span, the current value can be interpolated between the start and end values.

The name for the current value column is the name parameter provided to the method. The name for the start and end columns will be determined by the return value of PrefuseLib.getStartField(String) and PrefuseLib.getEndField(String). The default behavior for these methods is to append ":start" to the name of a stating value column and append ":end" to the the name of an ending value column.

Parameters:
name - the name of the interpolated column to add
type - the data type the columns will contain
dflt - the default value for each of the columns

addInterpolatedColumn

public void addInterpolatedColumn(java.lang.String name,
                                  java.lang.Class type)
Add an interpolated column with a null default value.

Parameters:
name - the name of the interpolated column to add
type - the data type the columns will contain
See Also:
addInterpolatedColumn(String, Class, Object)

getColumnCount

public int getColumnCount()
Get the number of columns in this schema.

Returns:
the number of columns

getColumnName

public java.lang.String getColumnName(int col)
The name of the column at the given position.

Parameters:
col - the column index
Returns:
the column name

getColumnIndex

public int getColumnIndex(java.lang.String field)
The column index for the column with the given name.

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

getColumnType

public java.lang.Class getColumnType(int col)
The type of the column at the given position.

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

getColumnType

public java.lang.Class getColumnType(java.lang.String field)
The type of the column with the given name.

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

getDefault

public java.lang.Object getDefault(int col)
The default value of the column at the given position.

Parameters:
col - the column index
Returns:
the column's default value

getDefault

public java.lang.Object getDefault(java.lang.String field)
The default value of the column with the given name.

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

setDefault

public void setDefault(int col,
                       java.lang.Object val)
Set the default value for the given field.

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

setDefault

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

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

setDefault

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

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

setDefault

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

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

setDefault

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

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

setDefault

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

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

setDefault

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

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

setInterpolatedDefault

public void setInterpolatedDefault(java.lang.String field,
                                   java.lang.Object val)
Set default values for the current, start, and end columns of an interpolated column.

Parameters:
field - the field name of the interpolated column
val - the new default value for all three implicated columns

setInterpolatedDefault

public void setInterpolatedDefault(java.lang.String field,
                                   int val)
Set default values for the current, start, and end columns of an interpolated column as an int.

Parameters:
field - the field name of the interpolated column
val - the new default value for all three implicated columns

setInterpolatedDefault

public void setInterpolatedDefault(java.lang.String field,
                                   long val)
Set default values for the current, start, and end columns of an interpolated column as a long.

Parameters:
field - the field name of the interpolated column
val - the new default value for all three implicated columns

setInterpolatedDefault

public void setInterpolatedDefault(java.lang.String field,
                                   float val)
Set default values for the current, start, and end columns of an interpolated column as a float.

Parameters:
field - the field name of the interpolated column
val - the new default value for all three implicated columns

setInterpolatedDefault

public void setInterpolatedDefault(java.lang.String field,
                                   double val)
Set default values for the current, start, and end columns of an interpolated column as a double.

Parameters:
field - the field name of the interpolated column
val - the new default value for all three implicated columns

setInterpolatedDefault

public void setInterpolatedDefault(java.lang.String field,
                                   boolean val)
Set default values for the current, start, and end columns of an interpolated column as a boolean.

Parameters:
field - the field name of the interpolated column
val - the new default value for all three implicated columns

equals

public boolean equals(java.lang.Object o)
Compares this schema with another one for equality.

Overrides:
equals in class java.lang.Object

isAssignableFrom

public boolean isAssignableFrom(Schema s)
Indicates if values from a given Schema can be safely assigned to data using this Schema. The input Schema must be less than or equal in length to this Schema, and all contained columns in the given Schema must have matching names and types to this Schema. This method does not consider default values settings or the locked status of the Schemas. For example, if the given Schema has different default values than this one, this will have no impact on the assignability of the two.

Parameters:
s - the input Schema
Returns:
true if data models using this Schema could be assigned values directly from data using the input Schema, false otherwise.

hashCode

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

Overrides:
hashCode in class java.lang.Object

toString

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

Overrides:
toString in class java.lang.Object

instantiate

public Table instantiate()
Instantiate this schema as a new Table instance.

Returns:
a new Table with this schema

instantiate

public Table instantiate(int nrows)
Instantiate this schema as a new Table instance.

Parameters:
nrows - the number of starting rows in the table
Returns:
a new Table with this schema


Copyright 2007 Regents of the University of California