001    /* ===========================================================
002     * JFreeChart : a free chart library for the Java(tm) platform
003     * ===========================================================
004     *
005     * (C) Copyright 2000-2008, 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-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    
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, 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        public Comparable getKey() {
096            Comparable result = null;
097            if (this.data != null) {
098                result = this.data.getKey();
099            }
100            return result;
101        }
102    
103        /**
104         * Returns the value.
105         *
106         * @return The value (possibly <code>null</code>).
107         */
108        public Number getValue() {
109            Number result = null;
110            if (this.data != null) {
111                result = this.data.getValue();
112            }
113            return result;
114        }
115    
116        /**
117         * Updates the value.
118         *
119         * @param value  the new value (<code>null</code> permitted).
120         */
121        public void updateValue(Number value) {
122            if (this.data == null) {
123                throw new RuntimeException("updateValue: can't update null.");
124            }
125            setValue(this.data.getKey(), value);
126        }
127    
128        /**
129         * Sets the value for the dataset and sends a {@link DatasetChangeEvent} to
130         * all registered listeners.
131         *
132         * @param key  the key.
133         * @param value  the value (<code>null</code> permitted).
134         */
135        public void setValue(Comparable key, Number value) {
136            this.data = new DefaultKeyedValue(key, value);
137            notifyListeners(new DatasetChangeEvent(this, this));
138        }
139    
140        /**
141         * Tests this dataset for equality with an arbitrary object.
142         *
143         * @param obj  the object (<code>null</code> permitted).
144         *
145         * @return A boolean.
146         */
147        public boolean equals(Object obj) {
148            if (obj == this) {
149                return true;
150            }
151            if (!(obj instanceof KeyedValueDataset)) {
152                return false;
153            }
154            KeyedValueDataset that = (KeyedValueDataset) obj;
155            if (this.data == null) {
156                if (that.getKey() != null || that.getValue() != null) {
157                    return false;
158                }
159                return true;
160            }
161            if (!ObjectUtilities.equal(this.data.getKey(), that.getKey())) {
162                return false;
163            }
164            if (!ObjectUtilities.equal(this.data.getValue(), that.getValue())) {
165                return false;
166            }
167            return true;
168        }
169    
170        /**
171         * Returns a hash code.
172         *
173         * @return A hash code.
174         */
175        public int hashCode() {
176            return (this.data != null ? this.data.hashCode() : 0);
177        }
178    
179        /**
180         * Creates a clone of the dataset.
181         *
182         * @return A clone.
183         *
184         * @throws CloneNotSupportedException This class will not throw this
185         *         exception, but subclasses (if any) might.
186         */
187        public Object clone() throws CloneNotSupportedException {
188            DefaultKeyedValueDataset clone
189                    = (DefaultKeyedValueDataset) super.clone();
190            return clone;
191        }
192    
193    }