📄 schema.java
字号:
* The type of the column with the given name.
* @param field the column name
* @return the column type
*/
public Class getColumnType(String field) {
int idx = getColumnIndex(field);
return ( idx<0 ? null : m_types[idx] );
}
/**
* The default value of the column at the given position.
* @param col the column index
* @return the column's default value
*/
public Object getDefault(int col) {
return m_dflts[col];
}
/**
* The default value of the column with the given name.
* @param field the column name
* @return the column's default value
*/
public Object getDefault(String field) {
int idx = getColumnIndex(field);
return ( idx<0 ? null : m_dflts[idx] );
}
/**
* Set the default value for the given field.
* @param col the column index of the field to set the default for
* @param val the new default value
*/
public void setDefault(int col, Object val) {
// check lock status
if ( m_locked ) {
throw new IllegalStateException(
"Can not update default values of a locked Schema.");
}
m_dflts[col] = val;
}
/**
* Set the default value for the given field.
* @param field the name of column to set the default for
* @param val the new default value
*/
public void setDefault(String field, Object val) {
// check lock status
if ( m_locked ) {
throw new IllegalStateException(
"Can not update default values of a locked Schema.");
}
int idx = getColumnIndex(field);
m_dflts[idx] = val;
}
/**
* Set the default value for the given field as an int.
* @param field the name of column to set the default for
* @param val the new default value
*/
public void setDefault(String field, int val) {
setDefault(field, new Integer(val));
}
/**
* Set the default value for the given field as a long.
* @param field the name of column to set the default for
* @param val the new default value
*/
public void setDefault(String field, long val) {
setDefault(field, new Long(val));
}
/**
* Set the default value for the given field as a float.
* @param field the name of column to set the default for
* @param val the new default value
*/
public void setDefault(String field, float val) {
setDefault(field, new Float(val));
}
/**
* Set the default value for the given field as a double.
* @param field the name of column to set the default for
* @param val the new default value
*/
public void setDefault(String field, double val) {
setDefault(field, new Double(val));
}
/**
* Set the default value for the given field as a boolean.
* @param field the name of column to set the default for
* @param val the new default value
*/
public void setDefault(String field, boolean val) {
setDefault(field, val ? Boolean.TRUE : Boolean.FALSE);
}
/**
* Set default values for the current, start, and end columns of an
* interpolated column.
* @param field the field name of the interpolated column
* @param val the new default value for all three implicated columns
*/
public void setInterpolatedDefault(String field, Object val) {
setDefault(field, val);
setDefault(PrefuseLib.getStartField(field), val);
setDefault(PrefuseLib.getEndField(field), val);
}
/**
* Set default values for the current, start, and end columns of an
* interpolated column as an int.
* @param field the field name of the interpolated column
* @param val the new default value for all three implicated columns
*/
public void setInterpolatedDefault(String field, int val) {
setInterpolatedDefault(field, new Integer(val));
}
/**
* Set default values for the current, start, and end columns of an
* interpolated column as a long.
* @param field the field name of the interpolated column
* @param val the new default value for all three implicated columns
*/
public void setInterpolatedDefault(String field, long val) {
setInterpolatedDefault(field, new Long(val));
}
/**
* Set default values for the current, start, and end columns of an
* interpolated column as a float.
* @param field the field name of the interpolated column
* @param val the new default value for all three implicated columns
*/
public void setInterpolatedDefault(String field, float val) {
setInterpolatedDefault(field, new Float(val));
}
/**
* Set default values for the current, start, and end columns of an
* interpolated column as a double.
* @param field the field name of the interpolated column
* @param val the new default value for all three implicated columns
*/
public void setInterpolatedDefault(String field, double val) {
setInterpolatedDefault(field, new Double(val));
}
/**
* Set default values for the current, start, and end columns of an
* interpolated column as a boolean.
* @param field the field name of the interpolated column
* @param val the new default value for all three implicated columns
*/
public void setInterpolatedDefault(String field, boolean val) {
setInterpolatedDefault(field, val ? Boolean.TRUE : Boolean.FALSE);
}
// ------------------------------------------------------------------------
// Comparison and Hashing
/**
* Compares this schema with another one for equality.
*/
public boolean equals(Object o) {
if ( !(o instanceof Schema) )
return false;
Schema s = (Schema)o;
if ( m_size != s.getColumnCount() )
return false;
for ( int i=0; i<m_size; ++i ) {
if ( !(m_names[i].equals(s.getColumnName(i)) &&
m_types[i].equals(s.getColumnType(i)) &&
m_dflts[i].equals(s.getDefault(i))) )
{
return false;
}
}
return true;
}
/**
* 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.
* @param s the input Schema
* @return true if data models using this Schema could be assigned values
* directly from data using the input Schema, false otherwise.
*/
public boolean isAssignableFrom(Schema s) {
int ssize = s.getColumnCount();
if ( ssize > m_size )
return false;
for ( int i=0; i<ssize; ++i ) {
int idx = getColumnIndex(s.getColumnName(i));
if ( idx < 0 )
return false;
if ( !m_types[idx].equals(s.getColumnType(i)) )
return false;
}
return true;
}
/**
* Computes a hashcode for this schema.
*/
public int hashCode() {
int hashcode = 0;
for ( int i=0; i<m_size; ++i ) {
int idx = i+1;
int code = idx*m_names[i].hashCode();
code ^= idx*m_types[i].hashCode();
if ( m_dflts[i] != null )
code ^= m_dflts[i].hashCode();
hashcode ^= code;
}
return hashcode;
}
/**
* Returns a descriptive String for this schema.
*/
public String toString() {
StringBuffer sbuf = new StringBuffer();
sbuf.append("Schema[");
for ( int i=0; i<m_size; ++i ) {
if ( i > 0 ) sbuf.append(' ');
sbuf.append('(').append(m_names[i]).append(", ");
sbuf.append(m_types[i].getName()).append(", ");
sbuf.append(m_dflts[i]).append(')');
}
sbuf.append(']');
return sbuf.toString();
}
// ------------------------------------------------------------------------
// Table Operations
/**
* Instantiate this schema as a new Table instance.
* @return a new Table with this schema
*/
public Table instantiate() {
return instantiate(0);
}
/**
* Instantiate this schema as a new Table instance.
* @param nrows the number of starting rows in the table
* @return a new Table with this schema
*/
public Table instantiate(int nrows) {
Table t = new Table(nrows, m_size);
for ( int i=0; i<m_size; ++i ) {
t.addColumn(m_names[i], m_types[i], m_dflts[i]);
}
return t;
}
} // end of class Schema
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -