Frames | No Frames |
1: /* =========================================================== 2: * JFreeChart : a free chart library for the Java(tm) platform 3: * =========================================================== 4: * 5: * (C) Copyright 2000-2007, by Object Refinery Limited and Contributors. 6: * 7: * Project Info: http://www.jfree.org/jfreechart/index.html 8: * 9: * This library is free software; you can redistribute it and/or modify it 10: * under the terms of the GNU Lesser General Public License as published by 11: * the Free Software Foundation; either version 2.1 of the License, or 12: * (at your option) any later version. 13: * 14: * This library is distributed in the hope that it will be useful, but 15: * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 16: * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 17: * License for more details. 18: * 19: * You should have received a copy of the GNU Lesser General Public 20: * License along with this library; if not, write to the Free Software 21: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 22: * USA. 23: * 24: * [Java is a trademark or registered trademark of Sun Microsystems, Inc. 25: * in the United States and other countries.] 26: * 27: * ----------------------------- 28: * DefaultKeyedValueDataset.java 29: * ----------------------------- 30: * (C) Copyright 2003-2007, by Object Refinery Limited. 31: * 32: * Original Author: David Gilbert (for Object Refinery Limited); 33: * Contributor(s): -; 34: * 35: * Changes 36: * ------- 37: * 27-Mar-2003 : Version 1 (DG); 38: * 18-Aug-2003 : Implemented Cloneable (DG); 39: * 40: */ 41: 42: package org.jfree.data.general; 43: 44: import java.io.Serializable; 45: 46: import org.jfree.data.DefaultKeyedValue; 47: import org.jfree.data.KeyedValue; 48: import org.jfree.util.ObjectUtilities; 49: 50: /** 51: * A default implementation of the {@link KeyedValueDataset} interface. 52: */ 53: public class DefaultKeyedValueDataset extends AbstractDataset 54: implements KeyedValueDataset, 55: Serializable { 56: 57: /** For serialization. */ 58: private static final long serialVersionUID = -8149484339560406750L; 59: 60: /** Storage for the data. */ 61: private KeyedValue data; 62: 63: /** 64: * Constructs a new dataset, initially empty. 65: */ 66: public DefaultKeyedValueDataset() { 67: this(null); 68: } 69: 70: /** 71: * Creates a new dataset with the specified initial value. 72: * 73: * @param key the key. 74: * @param value the value (<code>null</code> permitted). 75: */ 76: public DefaultKeyedValueDataset(Comparable key, Number value) { 77: this(new DefaultKeyedValue(key, value)); 78: } 79: 80: /** 81: * Creates a new dataset that uses the data from a {@link KeyedValue} 82: * instance. 83: * 84: * @param data the data (<code>null</code> permitted). 85: */ 86: public DefaultKeyedValueDataset(KeyedValue data) { 87: this.data = data; 88: } 89: 90: /** 91: * Returns the key associated with the value, or <code>null</code> if the 92: * dataset has no data item. 93: * 94: * @return The key. 95: */ 96: public Comparable getKey() { 97: Comparable result = null; 98: if (this.data != null) { 99: result = this.data.getKey(); 100: } 101: return result; 102: } 103: 104: /** 105: * Returns the value. 106: * 107: * @return The value (possibly <code>null</code>). 108: */ 109: public Number getValue() { 110: Number result = null; 111: if (this.data != null) { 112: result = this.data.getValue(); 113: } 114: return result; 115: } 116: 117: /** 118: * Updates the value. 119: * 120: * @param value the new value (<code>null</code> permitted). 121: */ 122: public void updateValue(Number value) { 123: if (this.data == null) { 124: throw new RuntimeException("updateValue: can't update null."); 125: } 126: setValue(this.data.getKey(), value); 127: } 128: 129: /** 130: * Sets the value for the dataset and sends a {@link DatasetChangeEvent} to 131: * all registered listeners. 132: * 133: * @param key the key. 134: * @param value the value (<code>null</code> permitted). 135: */ 136: public void setValue(Comparable key, Number value) { 137: this.data = new DefaultKeyedValue(key, value); 138: notifyListeners(new DatasetChangeEvent(this, this)); 139: } 140: 141: /** 142: * Tests this dataset for equality with an arbitrary object. 143: * 144: * @param obj the object. 145: * 146: * @return A boolean. 147: */ 148: public boolean equals(Object obj) { 149: 150: if (obj == this) { 151: return true; 152: } 153: if (!(obj instanceof KeyedValueDataset)) { 154: return false; 155: } 156: KeyedValueDataset that = (KeyedValueDataset) obj; 157: if (this.data == null) { 158: if (that.getKey() != null || that.getValue() != null) { 159: return false; 160: } 161: return true; 162: } 163: if (!ObjectUtilities.equal(this.data.getKey(), that.getKey())) { 164: return false; 165: } 166: if (!ObjectUtilities.equal(this.data.getValue(), that.getValue())) { 167: return false; 168: } 169: return true; 170: } 171: 172: /** 173: * Returns a hash code. 174: * 175: * @return A hash code. 176: */ 177: public int hashCode() { 178: return (this.data != null ? this.data.hashCode() : 0); 179: } 180: 181: /** 182: * Creates a clone of the dataset. 183: * 184: * @return A clone. 185: * 186: * @throws CloneNotSupportedException This class will not throw this 187: * exception, but subclasses (if any) might. 188: */ 189: public Object clone() throws CloneNotSupportedException { 190: DefaultKeyedValueDataset clone 191: = (DefaultKeyedValueDataset) super.clone(); 192: return clone; 193: } 194: 195: }