001: /* ===========================================================
002: * JFreeChart : a free chart library for the Java(tm) platform
003: * ===========================================================
004: *
005: * (C) Copyright 2000-2007, by Object Refinery Limited and Contributors.
006: *
007: * Project Info: http://www.jfree.org/jfreechart/index.html
008: *
009: * This library is free software; you can redistribute it and/or modify it
010: * under the terms of the GNU Lesser General Public License as published by
011: * the Free Software Foundation; either version 2.1 of the License, or
012: * (at your option) any later version.
013: *
014: * This library is distributed in the hope that it will be useful, but
015: * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
016: * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
017: * License for more details.
018: *
019: * You should have received a copy of the GNU Lesser General Public
020: * License along with this library; if not, write to the Free Software
021: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
022: * USA.
023: *
024: * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
025: * in the United States and other countries.]
026: *
027: * -----------------------
028: * DefaultKeyedValues.java
029: * -----------------------
030: * (C) Copyright 2002-2007, by Object Refinery Limited.
031: *
032: * Original Author: David Gilbert (for Object Refinery Limited);
033: * Contributor(s): -;
034: *
035: * $Id: DefaultKeyedValues.java,v 1.8.2.6 2007/04/30 15:28:04 mungady Exp $
036: *
037: * Changes:
038: * --------
039: * 31-Oct-2002 : Version 1 (DG);
040: * 11-Feb-2003 : Fixed bug in getValue(key) method for unrecognised key (DG);
041: * 05-Mar-2003 : Added methods to sort stored data 'by key' or 'by value' (DG);
042: * 13-Mar-2003 : Implemented Serializable (DG);
043: * 08-Apr-2003 : Modified removeValue(Comparable) method to fix bug 717049 (DG);
044: * 18-Aug-2003 : Implemented Cloneable (DG);
045: * 27-Aug-2003 : Moved SortOrder from org.jfree.data --> org.jfree.util (DG);
046: * 09-Feb-2004 : Modified getIndex() method - see bug report 893256 (DG);
047: * 15-Sep-2004 : Updated clone() method and added PublicCloneable
048: * interface (DG);
049: * 25-Nov-2004 : Small update to the clone() implementation (DG);
050: * 24-Feb-2005 : Added methods addValue(Comparable, double) and
051: * setValue(Comparable, double) for convenience (DG);
052: * ------------- JFREECHART 1.0.x -----------------------------------------------
053: * 31-Jul-2006 : Added a clear() method (DG);
054: * 01-Aug-2006 : Added argument check to getIndex() method (DG);
055: * 30-Apr-2007 : Added insertValue() methods (DG);
056: *
057: */
058:
059: package org.jfree.data;
060:
061: import java.io.Serializable;
062: import java.util.Collections;
063: import java.util.Comparator;
064: import java.util.Iterator;
065: import java.util.List;
066:
067: import org.jfree.util.ObjectUtilities;
068: import org.jfree.util.PublicCloneable;
069: import org.jfree.util.SortOrder;
070:
071: /**
072: * An ordered list of (key, value) items. This class provides a default
073: * implementation of the {@link KeyedValues} interface.
074: */
075: public class DefaultKeyedValues implements KeyedValues, Cloneable,
076: PublicCloneable, Serializable {
077:
078: /** For serialization. */
079: private static final long serialVersionUID = 8468154364608194797L;
080:
081: /** Storage for the data. */
082: private List data;
083:
084: /**
085: * Creates a new collection (initially empty).
086: */
087: public DefaultKeyedValues() {
088: this .data = new java.util.ArrayList();
089: }
090:
091: /**
092: * Returns the number of items (values) in the collection.
093: *
094: * @return The item count.
095: */
096: public int getItemCount() {
097: return this .data.size();
098: }
099:
100: /**
101: * Returns a value.
102: *
103: * @param item the item of interest (zero-based index).
104: *
105: * @return The value.
106: *
107: * @throws IndexOutOfBoundsException if <code>item</code> is out of bounds.
108: */
109: public Number getValue(int item) {
110: Number result = null;
111: KeyedValue kval = (KeyedValue) this .data.get(item);
112: if (kval != null) {
113: result = kval.getValue();
114: }
115: return result;
116: }
117:
118: /**
119: * Returns a key.
120: *
121: * @param index the item index (zero-based).
122: *
123: * @return The row key.
124: *
125: * @throws IndexOutOfBoundsException if <code>item</code> is out of bounds.
126: */
127: public Comparable getKey(int index) {
128: Comparable result = null;
129: KeyedValue item = (KeyedValue) this .data.get(index);
130: if (item != null) {
131: result = item.getKey();
132: }
133: return result;
134: }
135:
136: /**
137: * Returns the index for a given key.
138: *
139: * @param key the key (<code>null</code> not permitted).
140: *
141: * @return The index, or <code>-1</code> if the key is not recognised.
142: *
143: * @throws IllegalArgumentException if <code>key</code> is
144: * <code>null</code>.
145: */
146: public int getIndex(Comparable key) {
147: if (key == null) {
148: throw new IllegalArgumentException("Null 'key' argument.");
149: }
150: int i = 0;
151: Iterator iterator = this .data.iterator();
152: while (iterator.hasNext()) {
153: KeyedValue kv = (KeyedValue) iterator.next();
154: if (kv.getKey().equals(key)) {
155: return i;
156: }
157: i++;
158: }
159: return -1; // key not found
160: }
161:
162: /**
163: * Returns the keys for the values in the collection.
164: *
165: * @return The keys (never <code>null</code>).
166: */
167: public List getKeys() {
168: List result = new java.util.ArrayList();
169: Iterator iterator = this .data.iterator();
170: while (iterator.hasNext()) {
171: KeyedValue kv = (KeyedValue) iterator.next();
172: result.add(kv.getKey());
173: }
174: return result;
175: }
176:
177: /**
178: * Returns the value for a given key.
179: *
180: * @param key the key (<code>null</code> not permitted).
181: *
182: * @return The value (possibly <code>null</code>).
183: *
184: * @throws UnknownKeyException if the key is not recognised.
185: *
186: * @see #getValue(int)
187: */
188: public Number getValue(Comparable key) {
189: int index = getIndex(key);
190: if (index < 0) {
191: throw new UnknownKeyException("Key not found: " + key);
192: }
193: return getValue(index);
194: }
195:
196: /**
197: * Updates an existing value, or adds a new value to the collection.
198: *
199: * @param key the key (<code>null</code> not permitted).
200: * @param value the value.
201: *
202: * @see #addValue(Comparable, Number)
203: */
204: public void addValue(Comparable key, double value) {
205: addValue(key, new Double(value));
206: }
207:
208: /**
209: * Adds a new value to the collection, or updates an existing value.
210: * This method passes control directly to the
211: * {@link #setValue(Comparable, Number)} method.
212: *
213: * @param key the key (<code>null</code> not permitted).
214: * @param value the value (<code>null</code> permitted).
215: */
216: public void addValue(Comparable key, Number value) {
217: setValue(key, value);
218: }
219:
220: /**
221: * Updates an existing value, or adds a new value to the collection.
222: *
223: * @param key the key (<code>null</code> not permitted).
224: * @param value the value.
225: */
226: public void setValue(Comparable key, double value) {
227: setValue(key, new Double(value));
228: }
229:
230: /**
231: * Updates an existing value, or adds a new value to the collection.
232: *
233: * @param key the key (<code>null</code> not permitted).
234: * @param value the value (<code>null</code> permitted).
235: */
236: public void setValue(Comparable key, Number value) {
237: if (key == null) {
238: throw new IllegalArgumentException("Null 'key' argument.");
239: }
240: int keyIndex = getIndex(key);
241: if (keyIndex >= 0) {
242: DefaultKeyedValue kv = (DefaultKeyedValue) this .data
243: .get(keyIndex);
244: kv.setValue(value);
245: } else {
246: KeyedValue kv = new DefaultKeyedValue(key, value);
247: this .data.add(kv);
248: }
249: }
250:
251: /**
252: * Inserts a new value at the specified position in the dataset or, if
253: * there is an existing item with the specified key, updates the value
254: * for that item and moves it to the specified position.
255: *
256: * @param position the position (in the range 0 to getItemCount()).
257: * @param key the key (<code>null</code> not permitted).
258: * @param value the value.
259: *
260: * @since 1.0.6
261: */
262: public void insertValue(int position, Comparable key, double value) {
263: insertValue(position, key, new Double(value));
264: }
265:
266: /**
267: * Inserts a new value at the specified position in the dataset or, if
268: * there is an existing item with the specified key, updates the value
269: * for that item and moves it to the specified position.
270: *
271: * @param position the position (in the range 0 to getItemCount()).
272: * @param key the key (<code>null</code> not permitted).
273: * @param value the value (<code>null</code> permitted).
274: *
275: * @since 1.0.6
276: */
277: public void insertValue(int position, Comparable key, Number value) {
278: if (position < 0 || position > this .data.size()) {
279: throw new IllegalArgumentException(
280: "'position' out of bounds.");
281: }
282: if (key == null) {
283: throw new IllegalArgumentException("Null 'key' argument.");
284: }
285: int pos = this .getIndex(key);
286: if (pos >= 0) {
287: this .data.remove(pos);
288: }
289: KeyedValue kv = new DefaultKeyedValue(key, value);
290: if (position <= this .data.size()) {
291: this .data.add(position, kv);
292: } else {
293: this .data.add(kv);
294: }
295: }
296:
297: /**
298: * Removes a value from the collection.
299: *
300: * @param index the index of the item to remove (in the range
301: * <code>0</code> to <code>getItemCount() - 1</code>).
302: *
303: * @throws IndexOutOfBoundsException if <code>index</code> is not within
304: * the specified range.
305: */
306: public void removeValue(int index) {
307: this .data.remove(index);
308: }
309:
310: /**
311: * Removes a value from the collection. If there is no item with the
312: * specified key, this method does nothing.
313: *
314: * @param key the item key (<code>null</code> not permitted).
315: *
316: * @throws IllegalArgumentException if <code>key</code> is
317: * <code>null</code>.
318: */
319: public void removeValue(Comparable key) {
320: int index = getIndex(key);
321: if (index >= 0) {
322: removeValue(index);
323: }
324: }
325:
326: /**
327: * Clears all values from the collection.
328: *
329: * @since 1.0.2
330: */
331: public void clear() {
332: this .data.clear();
333: }
334:
335: /**
336: * Sorts the items in the list by key.
337: *
338: * @param order the sort order (<code>null</code> not permitted).
339: */
340: public void sortByKeys(SortOrder order) {
341: Comparator comparator = new KeyedValueComparator(
342: KeyedValueComparatorType.BY_KEY, order);
343: Collections.sort(this .data, comparator);
344: }
345:
346: /**
347: * Sorts the items in the list by value. If the list contains
348: * <code>null</code> values, they will sort to the end of the list,
349: * irrespective of the sort order.
350: *
351: * @param order the sort order (<code>null</code> not permitted).
352: */
353: public void sortByValues(SortOrder order) {
354: Comparator comparator = new KeyedValueComparator(
355: KeyedValueComparatorType.BY_VALUE, order);
356: Collections.sort(this .data, comparator);
357: }
358:
359: /**
360: * Tests if this object is equal to another.
361: *
362: * @param obj the object (<code>null</code> permitted).
363: *
364: * @return A boolean.
365: */
366: public boolean equals(Object obj) {
367: if (obj == this ) {
368: return true;
369: }
370:
371: if (!(obj instanceof KeyedValues)) {
372: return false;
373: }
374:
375: KeyedValues that = (KeyedValues) obj;
376: int count = getItemCount();
377: if (count != that.getItemCount()) {
378: return false;
379: }
380:
381: for (int i = 0; i < count; i++) {
382: Comparable k1 = getKey(i);
383: Comparable k2 = that.getKey(i);
384: if (!k1.equals(k2)) {
385: return false;
386: }
387: Number v1 = getValue(i);
388: Number v2 = that.getValue(i);
389: if (v1 == null) {
390: if (v2 != null) {
391: return false;
392: }
393: } else {
394: if (!v1.equals(v2)) {
395: return false;
396: }
397: }
398: }
399: return true;
400: }
401:
402: /**
403: * Returns a hash code.
404: *
405: * @return A hash code.
406: */
407: public int hashCode() {
408: return (this .data != null ? this .data.hashCode() : 0);
409: }
410:
411: /**
412: * Returns a clone.
413: *
414: * @return A clone.
415: *
416: * @throws CloneNotSupportedException this class will not throw this
417: * exception, but subclasses might.
418: */
419: public Object clone() throws CloneNotSupportedException {
420: DefaultKeyedValues clone = (DefaultKeyedValues) super .clone();
421: clone.data = (List) ObjectUtilities.deepClone(this.data);
422: return clone;
423: }
424:
425: }
|