📄 keyedobjects2d.java
字号:
* @param object the object.
* @param rowKey the row key (<code>null</code> not permitted).
* @param columnKey the column key (<code>null</code> not permitted).
*/
public void setObject(Object object, Comparable rowKey,
Comparable columnKey) {
if (rowKey == null) {
throw new IllegalArgumentException("Null 'rowKey' argument.");
}
if (columnKey == null) {
throw new IllegalArgumentException("Null 'columnKey' argument.");
}
KeyedObjects row;
int rowIndex = this.rowKeys.indexOf(rowKey);
if (rowIndex >= 0) {
row = (KeyedObjects) this.rows.get(rowIndex);
}
else {
this.rowKeys.add(rowKey);
row = new KeyedObjects();
this.rows.add(row);
}
row.setObject(columnKey, object);
int columnIndex = this.columnKeys.indexOf(columnKey);
if (columnIndex < 0) {
this.columnKeys.add(columnKey);
}
}
/**
* Removes an object from the table by setting it to <code>null</code>. If
* all the objects 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 #addObject(Object, Comparable, Comparable)
*/
public void removeObject(Comparable rowKey, Comparable columnKey) {
int rowIndex = getRowIndex(rowKey);
if (rowIndex < 0) {
throw new UnknownKeyException("Row key (" + rowKey
+ ") not recognised.");
}
int columnIndex = getColumnIndex(columnKey);
if (columnIndex < 0) {
throw new UnknownKeyException("Column key (" + columnKey
+ ") not recognised.");
}
setObject(null, rowKey, columnKey);
// 1. check whether the row is now empty.
boolean allNull = true;
KeyedObjects row = (KeyedObjects) this.rows.get(rowIndex);
for (int item = 0, itemCount = row.getItemCount(); item < itemCount;
item++) {
if (row.getObject(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;
for (int item = 0, itemCount = this.rows.size(); item < itemCount;
item++) {
row = (KeyedObjects) this.rows.get(item);
int colIndex = row.getIndex(columnKey);
if (colIndex >= 0 && row.getObject(colIndex) != null) {
allNull = false;
break;
}
}
if (allNull) {
for (int item = 0, itemCount = this.rows.size(); item < itemCount;
item++) {
row = (KeyedObjects) this.rows.get(item);
int colIndex = row.getIndex(columnKey);
if (colIndex >= 0) {
row.removeValue(colIndex);
}
}
this.columnKeys.remove(columnKey);
}
}
/**
* Removes an entire row from the table.
*
* @param rowIndex the row index.
*
* @see #removeColumn(int)
*/
public void removeRow(int rowIndex) {
this.rowKeys.remove(rowIndex);
this.rows.remove(rowIndex);
}
/**
* Removes an entire row from the table.
*
* @param rowKey the row key (<code>null</code> not permitted).
*
* @throws UnknownKeyException if <code>rowKey</code> is not recognised.
*
* @see #removeColumn(Comparable)
*/
public void removeRow(Comparable rowKey) {
int index = getRowIndex(rowKey);
if (index < 0) {
throw new UnknownKeyException("Row key (" + rowKey
+ ") not recognised.");
}
removeRow(index);
}
/**
* Removes an entire column from the table.
*
* @param columnIndex the column index.
*
* @see #removeRow(int)
*/
public void removeColumn(int columnIndex) {
Comparable columnKey = getColumnKey(columnIndex);
removeColumn(columnKey);
}
/**
* Removes an entire column from the table.
*
* @param columnKey the column key (<code>null</code> not permitted).
*
* @throws UnknownKeyException if <code>rowKey</code> is not recognised.
*
* @see #removeRow(Comparable)
*/
public void removeColumn(Comparable columnKey) {
int index = getColumnIndex(columnKey);
if (index < 0) {
throw new UnknownKeyException("Column key (" + columnKey
+ ") not recognised.");
}
Iterator iterator = this.rows.iterator();
while (iterator.hasNext()) {
KeyedObjects rowData = (KeyedObjects) iterator.next();
int i = rowData.getIndex(columnKey);
if (i >= 0) {
rowData.removeValue(i);
}
}
this.columnKeys.remove(columnKey);
}
/**
* Clears all the data and associated keys.
*
* @since 1.0.7
*/
public void clear() {
this.rowKeys.clear();
this.columnKeys.clear();
this.rows.clear();
}
/**
* Tests this object for equality with an arbitrary object.
*
* @param obj the object to test (<code>null</code> permitted).
*
* @return A boolean.
*/
public boolean equals(Object obj) {
if (obj == this) {
return true;
}
if (!(obj instanceof KeyedObjects2D)) {
return false;
}
KeyedObjects2D that = (KeyedObjects2D) obj;
if (!getRowKeys().equals(that.getRowKeys())) {
return false;
}
if (!getColumnKeys().equals(that.getColumnKeys())) {
return false;
}
int rowCount = getRowCount();
if (rowCount != that.getRowCount()) {
return false;
}
int colCount = getColumnCount();
if (colCount != that.getColumnCount()) {
return false;
}
for (int r = 0; r < rowCount; r++) {
for (int c = 0; c < colCount; c++) {
Object v1 = getObject(r, c);
Object v2 = that.getObject(r, c);
if (v1 == null) {
if (v2 != null) {
return false;
}
}
else {
if (!v1.equals(v2)) {
return false;
}
}
}
}
return true;
}
/**
* Returns a hashcode for this object.
*
* @return A hashcode.
*/
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 {
KeyedObjects2D clone = (KeyedObjects2D) super.clone();
clone.columnKeys = new java.util.ArrayList(this.columnKeys);
clone.rowKeys = new java.util.ArrayList(this.rowKeys);
clone.rows = new java.util.ArrayList(this.rows.size());
Iterator iterator = this.rows.iterator();
while (iterator.hasNext()) {
KeyedObjects row = (KeyedObjects) iterator.next();
clone.rows.add(row.clone());
}
return clone;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -