📄 defaultkeyedvalues2d.java
字号:
/* ===========================================================
* JFreeChart : a free chart library for the Java(tm) platform
* ===========================================================
*
* (C) Copyright 2000-2007, by Object Refinery Limited and Contributors.
*
* Project Info: http://www.jfree.org/jfreechart/index.html
*
* This library is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* This library is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
* License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
* USA.
*
* [Java is a trademark or registered trademark of Sun Microsystems, Inc.
* in the United States and other countries.]
*
* -------------------------
* DefaultKeyedValues2D.java
* -------------------------
* (C) Copyright 2002-2007, by Object Refinery Limited.
*
* Original Author: David Gilbert (for Object Refinery Limited);
* Contributor(s): Andreas Schroeder;
*
* $Id: DefaultKeyedValues2D.java,v 1.7.2.5 2007/03/30 09:53:10 mungady Exp $
*
* Changes
* -------
* 28-Oct-2002 : Version 1 (DG);
* 21-Jan-2003 : Updated Javadocs (DG);
* 13-Mar-2003 : Implemented Serializable (DG);
* 18-Aug-2003 : Implemented Cloneable (DG);
* 31-Mar-2004 : Made the rows optionally sortable by a flag (AS);
* 01-Apr-2004 : Implemented remove method (AS);
* 05-Apr-2004 : Added clear() method (DG);
* 15-Sep-2004 : Fixed clone() method (DG);
* 12-Jan-2005 : Fixed bug in getValue() method (DG);
* 23-Mar-2005 : Implemented PublicCloneable (DG);
* 09-Jun-2005 : Modified getValue() method to throw exception for unknown
* keys (DG);
* ------------- JFREECHART 1.0.x ---------------------------------------------
* 18-Jan-2007 : Fixed bug in getValue() method (DG);
* 30-Mar-2007 : Fixed bug 1690654, problem with removeValue() (DG);
*
*/
package org.jfree.data;
import java.io.Serializable;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import org.jfree.util.ObjectUtilities;
import org.jfree.util.PublicCloneable;
/**
* A data structure that stores zero, one or many values, where each value
* is associated with two keys (a 'row' key and a 'column' key). The keys
* should be (a) instances of {@link Comparable} and (b) immutable.
*/
public class DefaultKeyedValues2D implements KeyedValues2D,
PublicCloneable, Cloneable,
Serializable {
/** For serialization. */
private static final long serialVersionUID = -5514169970951994748L;
/** The row keys. */
private List rowKeys;
/** The column keys. */
private List columnKeys;
/** The row data. */
private List rows;
/** If the row keys should be sorted by their comparable order. */
private boolean sortRowKeys;
/**
* Creates a new instance (initially empty).
*/
public DefaultKeyedValues2D() {
this(false);
}
/**
* Creates a new instance (initially empty).
*
* @param sortRowKeys if the row keys should be sorted.
*/
public DefaultKeyedValues2D(boolean sortRowKeys) {
this.rowKeys = new java.util.ArrayList();
this.columnKeys = new java.util.ArrayList();
this.rows = new java.util.ArrayList();
this.sortRowKeys = sortRowKeys;
}
/**
* Returns the row count.
*
* @return The row count.
*
* @see #getColumnCount()
*/
public int getRowCount() {
return this.rowKeys.size();
}
/**
* Returns the column count.
*
* @return The column count.
*
* @see #getRowCount()
*/
public int getColumnCount() {
return this.columnKeys.size();
}
/**
* Returns the value for a given row and column.
*
* @param row the row index.
* @param column the column index.
*
* @return The value.
*
* @see #getValue(Comparable, Comparable)
*/
public Number getValue(int row, int column) {
Number result = null;
DefaultKeyedValues rowData = (DefaultKeyedValues) this.rows.get(row);
if (rowData != null) {
Comparable columnKey = (Comparable) this.columnKeys.get(column);
// the row may not have an entry for this key, in which case the
// return value is null
int index = rowData.getIndex(columnKey);
if (index >= 0) {
result = rowData.getValue(index);
}
}
return result;
}
/**
* Returns the key for a given row.
*
* @param row the row index (in the range 0 to {@link #getRowCount()} - 1).
*
* @return The row key.
*
* @see #getRowIndex(Comparable)
* @see #getColumnKey(int)
*/
public Comparable getRowKey(int row) {
return (Comparable) this.rowKeys.get(row);
}
/**
* Returns the row index for a given key.
*
* @param key the key (<code>null</code> not permitted).
*
* @return The row index.
*
* @see #getRowKey(int)
* @see #getColumnIndex(Comparable)
*/
public int getRowIndex(Comparable key) {
if (key == null) {
throw new IllegalArgumentException("Null 'key' argument.");
}
if (this.sortRowKeys) {
return Collections.binarySearch(this.rowKeys, key);
}
else {
return this.rowKeys.indexOf(key);
}
}
/**
* Returns the row keys in an unmodifiable list.
*
* @return The row keys.
*
* @see #getColumnKeys()
*/
public List getRowKeys() {
return Collections.unmodifiableList(this.rowKeys);
}
/**
* Returns the key for a given column.
*
* @param column the column (in the range 0 to {@link #getColumnCount()}
* - 1).
*
* @return The key.
*
* @see #getColumnIndex(Comparable)
* @see #getRowKey(int)
*/
public Comparable getColumnKey(int column) {
return (Comparable) this.columnKeys.get(column);
}
/**
* Returns the column index for a given key.
*
* @param key the key (<code>null</code> not permitted).
*
* @return The column index.
*
* @see #getColumnKey(int)
* @see #getRowIndex(Comparable)
*/
public int getColumnIndex(Comparable key) {
if (key == null) {
throw new IllegalArgumentException("Null 'key' argument.");
}
return this.columnKeys.indexOf(key);
}
/**
* Returns the column keys in an unmodifiable list.
*
* @return The column keys.
*
* @see #getRowKeys()
*/
public List getColumnKeys() {
return Collections.unmodifiableList(this.columnKeys);
}
/**
* Returns the value for the given row and column keys. This method will
* throw an {@link UnknownKeyException} if either key is not defined in the
* data structure.
*
* @param rowKey the row key (<code>null</code> not permitted).
* @param columnKey the column key (<code>null</code> not permitted).
*
* @return The value (possibly <code>null</code>).
*
* @see #addValue(Number, Comparable, Comparable)
* @see #removeValue(Comparable, Comparable)
*/
public Number getValue(Comparable rowKey, Comparable columnKey) {
if (rowKey == null) {
throw new IllegalArgumentException("Null 'rowKey' argument.");
}
if (columnKey == null) {
throw new IllegalArgumentException("Null 'columnKey' argument.");
}
// check that the column key is defined in the 2D structure
if (!(this.columnKeys.contains(columnKey))) {
throw new UnknownKeyException("Unrecognised columnKey: "
+ columnKey);
}
// now fetch the row data - need to bear in mind that the row
// structure may not have an entry for the column key, but that we
// have already checked that the key is valid for the 2D structure
int row = getRowIndex(rowKey);
if (row >= 0) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -