📄 defaultkeyedvalues2d.java
字号:
DefaultKeyedValues rowData
= (DefaultKeyedValues) this.rows.get(row);
int col = rowData.getIndex(columnKey);
return (col >= 0 ? rowData.getValue(col) : null);
}
else {
throw new UnknownKeyException("Unrecognised rowKey: " + rowKey);
}
}
/**
* Adds a value to the table. Performs the same function as
* #setValue(Number, Comparable, Comparable).
*
* @param value the value (<code>null</code> permitted).
* @param rowKey the row key (<code>null</code> not permitted).
* @param columnKey the column key (<code>null</code> not permitted).
*
* @see #setValue(Number, Comparable, Comparable)
* @see #removeValue(Comparable, Comparable)
*/
public void addValue(Number value, Comparable rowKey,
Comparable columnKey) {
// defer argument checking
setValue(value, rowKey, columnKey);
}
/**
* Adds or updates a value.
*
* @param value the value (<code>null</code> permitted).
* @param rowKey the row key (<code>null</code> not permitted).
* @param columnKey the column key (<code>null</code> not permitted).
*
* @see #addValue(Number, Comparable, Comparable)
* @see #removeValue(Comparable, Comparable)
*/
public void setValue(Number value, Comparable rowKey,
Comparable columnKey) {
DefaultKeyedValues row;
int rowIndex = getRowIndex(rowKey);
if (rowIndex >= 0) {
row = (DefaultKeyedValues) this.rows.get(rowIndex);
}
else {
row = new DefaultKeyedValues();
if (this.sortRowKeys) {
rowIndex = -rowIndex - 1;
this.rowKeys.add(rowIndex, rowKey);
this.rows.add(rowIndex, row);
}
else {
this.rowKeys.add(rowKey);
this.rows.add(row);
}
}
row.setValue(columnKey, value);
int columnIndex = this.columnKeys.indexOf(columnKey);
if (columnIndex < 0) {
this.columnKeys.add(columnKey);
}
}
/**
* Removes a value from the table by setting it to <code>null</code>. If
* all the values in the specified row and/or column are now
* <code>null</code>, the row and/or column is removed from the table.
*
* @param rowKey the row key (<code>null</code> not permitted).
* @param columnKey the column key (<code>null</code> not permitted).
*
* @see #addValue(Number, Comparable, Comparable)
*/
public void removeValue(Comparable rowKey, Comparable columnKey) {
setValue(null, rowKey, columnKey);
// 1. check whether the row is now empty.
boolean allNull = true;
int rowIndex = getRowIndex(rowKey);
DefaultKeyedValues row = (DefaultKeyedValues) this.rows.get(rowIndex);
for (int item = 0, itemCount = row.getItemCount(); item < itemCount;
item++) {
if (row.getValue(item) != null) {
allNull = false;
break;
}
}
if (allNull) {
this.rowKeys.remove(rowIndex);
this.rows.remove(rowIndex);
}
// 2. check whether the column is now empty.
allNull = true;
//int columnIndex = getColumnIndex(columnKey);
for (int item = 0, itemCount = this.rows.size(); item < itemCount;
item++) {
row = (DefaultKeyedValues) this.rows.get(item);
int columnIndex = row.getIndex(columnKey);
if (columnIndex >= 0 && row.getValue(columnIndex) != null) {
allNull = false;
break;
}
}
if (allNull) {
for (int item = 0, itemCount = this.rows.size(); item < itemCount;
item++) {
row = (DefaultKeyedValues) this.rows.get(item);
int columnIndex = row.getIndex(columnKey);
if (columnIndex >= 0) {
row.removeValue(columnIndex);
}
}
this.columnKeys.remove(columnKey);
}
}
/**
* Removes a row.
*
* @param rowIndex the row index.
*
* @see #removeRow(Comparable)
* @see #removeColumn(int)
*/
public void removeRow(int rowIndex) {
this.rowKeys.remove(rowIndex);
this.rows.remove(rowIndex);
}
/**
* Removes a row.
*
* @param rowKey the row key (<code>null</code> not permitted).
*
* @see #removeRow(int)
* @see #removeColumn(Comparable)
*/
public void removeRow(Comparable rowKey) {
removeRow(getRowIndex(rowKey));
}
/**
* Removes a column.
*
* @param columnIndex the column index.
*
* @see #removeColumn(Comparable)
* @see #removeRow(int)
*/
public void removeColumn(int columnIndex) {
Comparable columnKey = getColumnKey(columnIndex);
removeColumn(columnKey);
}
/**
* Removes a column.
*
* @param columnKey the column key (<code>null</code> not permitted).
*
* @see #removeColumn(int)
* @see #removeRow(Comparable)
*/
public void removeColumn(Comparable columnKey) {
Iterator iterator = this.rows.iterator();
while (iterator.hasNext()) {
DefaultKeyedValues rowData = (DefaultKeyedValues) iterator.next();
rowData.removeValue(columnKey);
}
this.columnKeys.remove(columnKey);
}
/**
* Clears all the data and associated keys.
*/
public void clear() {
this.rowKeys.clear();
this.columnKeys.clear();
this.rows.clear();
}
/**
* Tests if this object is equal to another.
*
* @param o the other object (<code>null</code> permitted).
*
* @return A boolean.
*/
public boolean equals(Object o) {
if (o == null) {
return false;
}
if (o == this) {
return true;
}
if (!(o instanceof KeyedValues2D)) {
return false;
}
KeyedValues2D kv2D = (KeyedValues2D) o;
if (!getRowKeys().equals(kv2D.getRowKeys())) {
return false;
}
if (!getColumnKeys().equals(kv2D.getColumnKeys())) {
return false;
}
int rowCount = getRowCount();
if (rowCount != kv2D.getRowCount()) {
return false;
}
int colCount = getColumnCount();
if (colCount != kv2D.getColumnCount()) {
return false;
}
for (int r = 0; r < rowCount; r++) {
for (int c = 0; c < colCount; c++) {
Number v1 = getValue(r, c);
Number v2 = kv2D.getValue(r, c);
if (v1 == null) {
if (v2 != null) {
return false;
}
}
else {
if (!v1.equals(v2)) {
return false;
}
}
}
}
return true;
}
/**
* Returns a hash code.
*
* @return A hash code.
*/
public int hashCode() {
int result;
result = this.rowKeys.hashCode();
result = 29 * result + this.columnKeys.hashCode();
result = 29 * result + this.rows.hashCode();
return result;
}
/**
* Returns a clone.
*
* @return A clone.
*
* @throws CloneNotSupportedException this class will not throw this
* exception, but subclasses (if any) might.
*/
public Object clone() throws CloneNotSupportedException {
DefaultKeyedValues2D clone = (DefaultKeyedValues2D) super.clone();
// for the keys, a shallow copy should be fine because keys
// should be immutable...
clone.columnKeys = new java.util.ArrayList(this.columnKeys);
clone.rowKeys = new java.util.ArrayList(this.rowKeys);
// but the row data requires a deep copy
clone.rows = (List) ObjectUtilities.deepClone(this.rows);
return clone;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -