|
jfreechart example source code file (DefaultKeyedValues.java)
The jfreechart DefaultKeyedValues.java source code
/* ===========================================================
* JFreeChart : a free chart library for the Java(tm) platform
* ===========================================================
*
* (C) Copyright 2000-2008, 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.]
*
* -----------------------
* DefaultKeyedValues.java
* -----------------------
* (C) Copyright 2002-2008, by Object Refinery Limited.
*
* Original Author: David Gilbert (for Object Refinery Limited);
* Contributor(s): Thomas Morgner;
*
* Changes:
* --------
* 31-Oct-2002 : Version 1 (DG);
* 11-Feb-2003 : Fixed bug in getValue(key) method for unrecognised key (DG);
* 05-Mar-2003 : Added methods to sort stored data 'by key' or 'by value' (DG);
* 13-Mar-2003 : Implemented Serializable (DG);
* 08-Apr-2003 : Modified removeValue(Comparable) method to fix bug 717049 (DG);
* 18-Aug-2003 : Implemented Cloneable (DG);
* 27-Aug-2003 : Moved SortOrder from org.jfree.data --> org.jfree.util (DG);
* 09-Feb-2004 : Modified getIndex() method - see bug report 893256 (DG);
* 15-Sep-2004 : Updated clone() method and added PublicCloneable
* interface (DG);
* 25-Nov-2004 : Small update to the clone() implementation (DG);
* 24-Feb-2005 : Added methods addValue(Comparable, double) and
* setValue(Comparable, double) for convenience (DG);
* ------------- JFREECHART 1.0.x ---------------------------------------------
* 31-Jul-2006 : Added a clear() method (DG);
* 01-Aug-2006 : Added argument check to getIndex() method (DG);
* 30-Apr-2007 : Added insertValue() methods (DG);
* 31-Oct-2007 : Performance improvements by using separate lists for keys and
* values (TM);
* 21-Nov-2007 : Fixed bug in removeValue() method from previous patch (DG);
*
*/
package org.jfree.data;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import org.jfree.util.PublicCloneable;
import org.jfree.util.SortOrder;
/**
* An ordered list of (key, value) items. This class provides a default
* implementation of the {@link KeyedValues} interface.
*/
public class DefaultKeyedValues implements KeyedValues, Cloneable,
PublicCloneable, Serializable {
/** For serialization. */
private static final long serialVersionUID = 8468154364608194797L;
/** Storage for the keys. */
private ArrayList keys;
/** Storage for the values. */
private ArrayList values;
/**
* Contains (key, Integer) mappings, where the Integer is the index for
* the key in the list.
*/
private HashMap indexMap;
/**
* Creates a new collection (initially empty).
*/
public DefaultKeyedValues() {
this.keys = new ArrayList();
this.values = new ArrayList();
this.indexMap = new HashMap();
}
/**
* Returns the number of items (values) in the collection.
*
* @return The item count.
*/
public int getItemCount() {
return this.indexMap.size();
}
/**
* Returns a value.
*
* @param item the item of interest (zero-based index).
*
* @return The value (possibly <code>null).
*
* @throws IndexOutOfBoundsException if <code>item is out of bounds.
*/
public Number getValue(int item) {
return (Number) this.values.get(item);
}
/**
* Returns a key.
*
* @param index the item index (zero-based).
*
* @return The row key.
*
* @throws IndexOutOfBoundsException if <code>item is out of bounds.
*/
public Comparable getKey(int index) {
return (Comparable) this.keys.get(index);
}
/**
* Returns the index for a given key.
*
* @param key the key (<code>null not permitted).
*
* @return The index, or <code>-1 if the key is not recognised.
*
* @throws IllegalArgumentException if <code>key is
* <code>null.
*/
public int getIndex(Comparable key) {
if (key == null) {
throw new IllegalArgumentException("Null 'key' argument.");
}
final Integer i = (Integer) this.indexMap.get(key);
if (i == null) {
return -1; // key not found
}
return i.intValue();
}
/**
* Returns the keys for the values in the collection.
*
* @return The keys (never <code>null).
*/
public List getKeys() {
return (List) this.keys.clone();
}
/**
* Returns the value for a given key.
*
* @param key the key (<code>null not permitted).
*
* @return The value (possibly <code>null).
*
* @throws UnknownKeyException if the key is not recognised.
*
* @see #getValue(int)
*/
public Number getValue(Comparable key) {
int index = getIndex(key);
if (index < 0) {
throw new UnknownKeyException("Key not found: " + key);
}
return getValue(index);
}
/**
* Updates an existing value, or adds a new value to the collection.
*
* @param key the key (<code>null not permitted).
* @param value the value.
*
* @see #addValue(Comparable, Number)
*/
public void addValue(Comparable key, double value) {
addValue(key, new Double(value));
}
/**
* Adds a new value to the collection, or updates an existing value.
* This method passes control directly to the
* {@link #setValue(Comparable, Number)} method.
*
* @param key the key (<code>null not permitted).
* @param value the value (<code>null permitted).
*/
public void addValue(Comparable key, Number value) {
setValue(key, value);
}
/**
* Updates an existing value, or adds a new value to the collection.
*
* @param key the key (<code>null not permitted).
* @param value the value.
*/
public void setValue(Comparable key, double value) {
setValue(key, new Double(value));
}
/**
* Updates an existing value, or adds a new value to the collection.
*
* @param key the key (<code>null not permitted).
* @param value the value (<code>null permitted).
*/
public void setValue(Comparable key, Number value) {
if (key == null) {
throw new IllegalArgumentException("Null 'key' argument.");
}
int keyIndex = getIndex(key);
if (keyIndex >= 0) {
this.keys.set(keyIndex, key);
this.values.set(keyIndex, value);
}
else {
this.keys.add(key);
this.values.add(value);
this.indexMap.put(key, new Integer(this.keys.size() - 1));
}
}
/**
* Inserts a new value at the specified position in the dataset or, if
* there is an existing item with the specified key, updates the value
* for that item and moves it to the specified position.
*
* @param position the position (in the range 0 to getItemCount()).
* @param key the key (<code>null not permitted).
* @param value the value.
*
* @since 1.0.6
*/
public void insertValue(int position, Comparable key, double value) {
insertValue(position, key, new Double(value));
}
/**
* Inserts a new value at the specified position in the dataset or, if
* there is an existing item with the specified key, updates the value
* for that item and moves it to the specified position.
*
* @param position the position (in the range 0 to getItemCount()).
* @param key the key (<code>null not permitted).
* @param value the value (<code>null permitted).
*
* @since 1.0.6
*/
public void insertValue(int position, Comparable key, Number value) {
if (position < 0 || position > getItemCount()) {
throw new IllegalArgumentException("'position' out of bounds.");
}
if (key == null) {
throw new IllegalArgumentException("Null 'key' argument.");
}
int pos = getIndex(key);
if (pos == position) {
this.keys.set(pos, key);
this.values.set(pos, value);
}
else {
if (pos >= 0) {
this.keys.remove(pos);
this.values.remove(pos);
}
this.keys.add(position, key);
this.values.add(position, value);
rebuildIndex();
}
}
/**
* Rebuilds the key to indexed-position mapping after an positioned insert
* or a remove operation.
*/
private void rebuildIndex () {
this.indexMap.clear();
for (int i = 0; i < this.keys.size(); i++) {
final Object key = this.keys.get(i);
this.indexMap.put(key, new Integer(i));
}
}
/**
* Removes a value from the collection.
*
* @param index the index of the item to remove (in the range
* <code>0 to
Other jfreechart examples (source code examples)Here is a short list of links related to this jfreechart DefaultKeyedValues.java source code file: |
... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2024 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.