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