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 * DefaultKeyedValueDataset.java
029 * -----------------------------
030 * (C) Copyright 2003-2007, by Object Refinery Limited.
031 *
032 * Original Author: David Gilbert (for Object Refinery Limited);
033 * Contributor(s): -;
034 *
035 * Changes
036 * -------
037 * 27-Mar-2003 : Version 1 (DG);
038 * 18-Aug-2003 : Implemented Cloneable (DG);
039 *
040 */
041
042 package org.jfree.data.general;
043
044 import java.io.Serializable;
045
046 import org.jfree.data.DefaultKeyedValue;
047 import org.jfree.data.KeyedValue;
048 import org.jfree.util.ObjectUtilities;
049
050 /**
051 * A default implementation of the {@link KeyedValueDataset} interface.
052 */
053 public class DefaultKeyedValueDataset extends AbstractDataset
054 implements KeyedValueDataset,
055 Serializable {
056
057 /** For serialization. */
058 private static final long serialVersionUID = -8149484339560406750L;
059
060 /** Storage for the data. */
061 private KeyedValue data;
062
063 /**
064 * Constructs a new dataset, initially empty.
065 */
066 public DefaultKeyedValueDataset() {
067 this(null);
068 }
069
070 /**
071 * Creates a new dataset with the specified initial value.
072 *
073 * @param key the key.
074 * @param value the value (<code>null</code> permitted).
075 */
076 public DefaultKeyedValueDataset(Comparable key, Number value) {
077 this(new DefaultKeyedValue(key, value));
078 }
079
080 /**
081 * Creates a new dataset that uses the data from a {@link KeyedValue}
082 * instance.
083 *
084 * @param data the data (<code>null</code> permitted).
085 */
086 public DefaultKeyedValueDataset(KeyedValue data) {
087 this.data = data;
088 }
089
090 /**
091 * Returns the key associated with the value, or <code>null</code> if the
092 * dataset has no data item.
093 *
094 * @return The key.
095 */
096 public Comparable getKey() {
097 Comparable result = null;
098 if (this.data != null) {
099 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 }