001/* ===========================================================
002 * JFreeChart : a free chart library for the Java(tm) platform
003 * ===========================================================
004 *
005 * (C) Copyright 2000-2013, 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 * [Oracle and Java are registered trademarks of Oracle and/or its affiliates. 
025 * Other names may be trademarks of their respective owners.]
026 *
027 * -----------------------------
028 * DefaultKeyedValueDataset.java
029 * -----------------------------
030 * (C) Copyright 2003-2008, 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
042package org.jfree.data.general;
043
044import java.io.Serializable;
045
046import org.jfree.data.DefaultKeyedValue;
047import org.jfree.data.KeyedValue;
048import org.jfree.util.ObjectUtilities;
049
050/**
051 * A default implementation of the {@link KeyedValueDataset} interface.
052 */
053public class DefaultKeyedValueDataset extends AbstractDataset
054        implements KeyedValueDataset, Serializable {
055
056    /** For serialization. */
057    private static final long serialVersionUID = -8149484339560406750L;
058
059    /** Storage for the data. */
060    private KeyedValue data;
061
062    /**
063     * Constructs a new dataset, initially empty.
064     */
065    public DefaultKeyedValueDataset() {
066        this(null);
067    }
068
069    /**
070     * Creates a new dataset with the specified initial value.
071     *
072     * @param key  the key.
073     * @param value  the value (<code>null</code> permitted).
074     */
075    public DefaultKeyedValueDataset(Comparable key, Number value) {
076        this(new DefaultKeyedValue(key, value));
077    }
078
079    /**
080     * Creates a new dataset that uses the data from a {@link KeyedValue}
081     * instance.
082     *
083     * @param data  the data (<code>null</code> permitted).
084     */
085    public DefaultKeyedValueDataset(KeyedValue data) {
086        this.data = data;
087    }
088
089    /**
090     * Returns the key associated with the value, or <code>null</code> if the
091     * dataset has no data item.
092     *
093     * @return The key.
094     */
095    @Override
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    @Override
110    public Number getValue() {
111        Number result = null;
112        if (this.data != null) {
113            result = this.data.getValue();
114        }
115        return result;
116    }
117
118    /**
119     * Updates the value.
120     *
121     * @param value  the new value (<code>null</code> permitted).
122     */
123    public void updateValue(Number value) {
124        if (this.data == null) {
125            throw new RuntimeException("updateValue: can't update null.");
126        }
127        setValue(this.data.getKey(), value);
128    }
129
130    /**
131     * Sets the value for the dataset and sends a {@link DatasetChangeEvent} to
132     * all registered listeners.
133     *
134     * @param key  the key.
135     * @param value  the value (<code>null</code> permitted).
136     */
137    public void setValue(Comparable key, Number value) {
138        this.data = new DefaultKeyedValue(key, value);
139        notifyListeners(new DatasetChangeEvent(this, this));
140    }
141
142    /**
143     * Tests this dataset for equality with an arbitrary object.
144     *
145     * @param obj  the object (<code>null</code> permitted).
146     *
147     * @return A boolean.
148     */
149    @Override
150    public boolean equals(Object obj) {
151        if (obj == this) {
152            return true;
153        }
154        if (!(obj instanceof KeyedValueDataset)) {
155            return false;
156        }
157        KeyedValueDataset that = (KeyedValueDataset) obj;
158        if (this.data == null) {
159            if (that.getKey() != null || that.getValue() != null) {
160                return false;
161            }
162            return true;
163        }
164        if (!ObjectUtilities.equal(this.data.getKey(), that.getKey())) {
165            return false;
166        }
167        if (!ObjectUtilities.equal(this.data.getValue(), that.getValue())) {
168            return false;
169        }
170        return true;
171    }
172
173    /**
174     * Returns a hash code.
175     *
176     * @return A hash code.
177     */
178    @Override
179    public int hashCode() {
180        return (this.data != null ? this.data.hashCode() : 0);
181    }
182
183    /**
184     * Creates a clone of the dataset.
185     *
186     * @return A clone.
187     *
188     * @throws CloneNotSupportedException This class will not throw this
189     *         exception, but subclasses (if any) might.
190     */
191    @Override
192    public Object clone() throws CloneNotSupportedException {
193        DefaultKeyedValueDataset clone
194                = (DefaultKeyedValueDataset) super.clone();
195        return clone;
196    }
197
198}