📄 table.java
字号:
* @param field the data field name of the column
* @return the stored index for the column, or null if no index has
* been created
*/
public Index getIndex(String field) {
ColumnEntry e = (ColumnEntry)m_entries.get(field);
if ( e == null ) {
throw new IllegalArgumentException("Unknown column name: "+field);
}
return e.index;
}
/**
* Internal method for index creation and retrieval.
* @param field the data field name of the column
* @param expType the expected data type of the index
* @param create indicates whether or not a new index should be created
* if none currently exists for the given data field
* @return the Index for the given data field
*/
protected Index getIndex(String field, Class expType, boolean create) {
if ( !expType.equals(getColumnType(field)) ) {
// TODO: need more nuanced type checking here?
throw new IllegalArgumentException("Column type does not match.");
}
if ( getIndex(field)==null && create) {
index(field);
}
return getIndex(field);
}
/**
* Remove the Index associated with the given data field / column name.
* @param field the name of the column for which to remove the index
* @return true if an index was successfully removed, false if no
* such index was found
*/
public boolean removeIndex(String field) {
ColumnEntry e = (ColumnEntry)m_entries.get(field);
if ( e == null ) {
throw new IllegalArgumentException("Unknown column name: "+field);
}
if ( e.index == null ) {
return false;
} else {
e.index.dispose();
e.index = null;
return true;
}
}
// ------------------------------------------------------------------------
// Tuple Methods
/**
* Get the Tuple instance providing object-oriented access to the given
* table row.
* @param row the table row
* @return the Tuple for the given table row
*/
public Tuple getTuple(int row) {
return m_tuples.getTuple(row);
}
/**
* Add a Tuple to this table. If the Tuple is already a member of this
* table, nothing is done and null is returned. If the Tuple is not
* a member of this Table but has a compatible data schema, as
* determined by {@link Schema#isAssignableFrom(Schema)}, a new row
* is created, the Tuple's values are copied, and the new Tuple that
* is a member of this Table is returned. If the data schemas are not
* compatible, nothing is done and null is returned.
* @param t the Tuple to "add" to this table
* @return the actual Tuple instance added to this table, or null if
* no new Tuple has been added
* @see prefuse.data.tuple.TupleSet#addTuple(prefuse.data.Tuple)
*/
public Tuple addTuple(Tuple t) {
if ( t.getTable() == this ) {
return null;
} else {
Schema s = t.getSchema();
if ( getSchema().isAssignableFrom(s) ) {
int r = addRow();
for ( int i=0; i<s.getColumnCount(); ++i ) {
String field = s.getColumnName(i);
this.set(r, field, t.get(i));
}
return getTuple(r);
} else {
return null;
}
}
}
/**
* Clears the contents of this table and then attempts to add the given
* Tuple instance.
* @param t the Tuple to make the sole tuple in thie table
* @return the actual Tuple instance added to this table, or null if
* no new Tuple has been added
* @see prefuse.data.tuple.TupleSet#setTuple(prefuse.data.Tuple)
*/
public Tuple setTuple(Tuple t) {
clear();
return addTuple(t);
}
/**
* Remove a tuple from this table. If the Tuple is a member of this table,
* its row is deleted from the table. Otherwise, nothing is done.
* @param t the Tuple to remove from the table
* @return true if the Tuple row was successfully deleted, false if the
* Tuple is invalid or not a member of this table
* @see prefuse.data.tuple.TupleSet#removeTuple(prefuse.data.Tuple)
*/
public boolean removeTuple(Tuple t) {
if ( containsTuple(t) ) {
removeRow(t.getRow());
return true;
} else {
return false;
}
}
/**
* Indicates if this table contains the given Tuple instance.
* @param t the Tuple to check for containment
* @return true if the Tuple represents a row of this table, false if
* it does not
* @see prefuse.data.tuple.TupleSet#containsTuple(prefuse.data.Tuple)
*/
public boolean containsTuple(Tuple t) {
return (t.getTable()==this && isValidRow(t.getRow()));
}
/**
* Get the number of tuples in this table. This is the same as the
* value returned by {@link #getRowCount()}.
* @return the number of tuples, which is the same as the number of rows
* @see prefuse.data.tuple.TupleSet#getTupleCount()
*/
public int getTupleCount() {
return getRowCount();
}
/**
* Returns true, as this table supports the addition of new data fields.
* @see prefuse.data.tuple.TupleSet#isAddColumnSupported()
*/
public boolean isAddColumnSupported() {
return true;
}
// ------------------------------------------------------------------------
// Data Access Methods
/**
* Check if the <code>get</code> method for the given data field returns
* values that are compatible with a given target type.
* @param field the data field to check
* @param type a Class instance to check for compatibility with the
* data field values.
* @return true if the data field is compatible with provided type,
* false otherwise. If the value is true, objects returned by
* the {@link #get(int, String)} can be cast to the given type.
* @see #get(int, String)
*/
public boolean canGet(String field, Class type) {
Column c = getColumn(field);
return ( c==null ? false : c.canGet(type) );
}
/**
* Check if the <code>set</code> method for the given data field can
* accept values of a given target type.
* @param field the data field to check
* @param type a Class instance to check for compatibility with the
* data field values.
* @return true if the data field is compatible with provided type,
* false otherwise. If the value is true, objects of the given type
* can be used as parameters of the {@link #set(int, String, Object)}
* method.
* @see #set(int, String, Object)
*/
public boolean canSet(String field, Class type) {
Column c = getColumn(field);
return ( c==null ? false : c.canSet(type) );
}
/**
* Get the data value at the given row and field as an Object.
* @param row the table row to get
* @param field the data field to retrieve
* @return the data value as an Object. The concrete type of this
* Object is dependent on the underlying data column used.
* @see #canGet(String, Class)
* @see #getColumnType(String)
*/
public Object get(int row, String field) {
int col = getColumnNumber(field);
row = getColumnRow(row, col);
return getColumn(col).get(row);
}
/**
* Set the value of a given row and data field.
* @param row the table row to set
* @param field the data field to set
* @param val the value for the field. If the concrete type of this
* Object is not compatible with the underlying data model, an
* Exception will be thrown. Use the {@link #canSet(String, Class)}
* method to check the type-safety ahead of time.
* @see #canSet(String, Class)
* @see #getColumnType(String)
*/
public void set(int row, String field, Object val) {
int col = getColumnNumber(field);
row = getColumnRow(row, col);
getColumn(col).set(val, row);
// we don't fire a notification here, as we catch the
// notification from the column itself and then dispatch
}
/**
* Get the data value at the given row and column numbers as an Object.
* @param row the row number
* @param col the column number
* @return the data value as an Object. The concrete type of this
* Object is dependent on the underlying data column used.
* @see #canGet(String, Class)
* @see #getColumnType(int)
*/
public Object get(int row, int col) {
row = getColumnRow(row, col);
return getColumn(col).get(row);
}
/**
* Set the value of at the given row and column numbers.
* @param row the row number
* @param col the column number
* @param val the value for the field. If the concrete type of this
* Object is not compatible with the underlying data model, an
* Exception will be thrown. Use the {@link #canSet(String, Class)}
* method to check the type-safety ahead of time.
* @see #canSet(String, Class)
* @see #getColumnType(String)
*/
public void set(int row, int col, Object val) {
row = getColumnRow(row, col);
getColumn(col).set(val, row);
// we don't fire a notification here, as we catch the
// notification from the column itself and then dispatch
}
/**
* Get the default value for the given data field.
* @param field the data field
* @return the default value, as an Object, used to populate rows
* of the data field.
*/
public Object getDefault(String field) {
int col = getColumnNumber(field);
return getColumn(col).getDefaultValue();
}
/**
* Revert this tuple's value for the given field to the default value
* for the field.
* @param field the data field
* @see #getDefault(String)
*/
public void revertToDefault(int row, String field) {
int col = getColumnNumber(field);
row = getColumnRow(row, col);
getColumn(col).revertToDefault(row);
}
// ------------------------------------------------------------------------
// Convenience Data Access Methods
/**
* Check if the given data field can return primitive <code>int</code>
* values.
* @param field the data field to check
* @return true if the data field can return primitive <code>int</code>
* values, false otherwise. If true, the {@link #getInt(int, String)}
* method can be used safely.
*/
public final boolean canGetInt(String field) {
Column col = getColumn(field);
return ( col==null ? false : col.canGetInt() );
}
/**
* Check if the <code>setInt</code> method can safely be used for the
* given data field.
* @param field the data field to check
* @return true if the {@link #setInt(int, String, int)} method can safely
* be used for the given field, false otherwise.
*/
public final boolean canSetInt(String field) {
Column col = getColumn(field);
return ( col==null ? false : col.canSetInt() );
}
/**
* Get the data value at the given row and field as an
* <code>int</code>.
* @param row the table row to retrieve
* @param field the data field to retrieve
* @see #canGetInt(String)
*/
public final int getInt(int row, String field) {
int col = getColumnNumber(field);
row = getColumnRow(row, col);
return getColumn(col).getInt(row);
}
/**
* Set the data value of the given row and field as an
* <code>int</code>.
* @param row the table row to set
* @param field the data field to set
* @param val the value to set
* @see #canSetInt(String)
*/
public final void setInt(int row, String field, int val) {
int col = getColumnNumber(field);
row = getColumnRow(row, col);
getColumn(col).setInt(val, row);
}
/**
* Get the data value at the given row and field as an
* <code>int</code>.
* @param row the table row to retrieve
* @param col the column number of the data field to retrieve
* @see #canGetInt(String)
*/
public final int getInt(int row, int col) {
row = getColumnRow(row, col);
return getColumn(col).getInt(row);
}
/**
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -