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 * DefaultKeyedValue.java
029 * ----------------------
030 * (C) Copyright 2002-2013, by Object Refinery Limited.
031 *
032 * Original Author:  David Gilbert (for Object Refinery Limited);
033 * Contributor(s):   -;
034 *
035 * Changes:
036 * --------
037 * 31-Oct-2002 : Version 1 (DG);
038 * 13-Mar-2003 : Added equals() method, and implemented Serializable (DG);
039 * 18-Aug-2003 : Implemented Cloneable (DG);
040 * 18-Aug-2004 : Moved from org.jfree.data --> org.jfree.data.base (DG);
041 * 15-Sep-2004 : Added PublicCloneable interface (DG);
042 * ------------- JFREECHART 1.0.x ---------------------------------------------
043 * 11-Jun-2007 : Added toString() method to help with debugging (DG);
044 * 15-Feb-2008 : Prevent null key (DG);
045 * 07-Apr-2008 : Removed to-do item (DG);
046 * 03-Jul-2013 : Use ParamChecks (DG);
047 *
048 */
049
050package org.jfree.data;
051
052import java.io.Serializable;
053import org.jfree.chart.util.ParamChecks;
054
055import org.jfree.util.PublicCloneable;
056
057/**
058 * A (key, value) pair.  This class provides a default implementation
059 * of the {@link KeyedValue} interface.
060 */
061public class DefaultKeyedValue implements KeyedValue, Cloneable,
062        PublicCloneable, Serializable {
063
064    /** For serialization. */
065    private static final long serialVersionUID = -7388924517460437712L;
066
067    /** The key. */
068    private Comparable key;
069
070    /** The value. */
071    private Number value;
072
073    /**
074     * Creates a new (key, value) item.
075     *
076     * @param key  the key (should be immutable, <code>null</code> not
077     *         permitted).
078     * @param value  the value (<code>null</code> permitted).
079     */
080    public DefaultKeyedValue(Comparable key, Number value) {
081        ParamChecks.nullNotPermitted(key, "key");
082        this.key = key;
083        this.value = value;
084    }
085
086    /**
087     * Returns the key.
088     *
089     * @return The key (never <code>null</code>).
090     */
091    @Override
092    public Comparable getKey() {
093        return this.key;
094    }
095
096    /**
097     * Returns the value.
098     *
099     * @return The value (possibly <code>null</code>).
100     */
101    @Override
102    public Number getValue() {
103        return this.value;
104    }
105
106    /**
107     * Sets the value.
108     *
109     * @param value  the value (<code>null</code> permitted).
110     */
111    public synchronized void setValue(Number value) {
112        this.value = value;
113    }
114
115    /**
116     * Tests this key-value pair for equality with an arbitrary object.
117     *
118     * @param obj  the object (<code>null</code> permitted).
119     *
120     * @return A boolean.
121     */
122    @Override
123    public boolean equals(Object obj) {
124        if (obj == this) {
125            return true;
126        }
127        if (!(obj instanceof DefaultKeyedValue)) {
128            return false;
129        }
130        DefaultKeyedValue that = (DefaultKeyedValue) obj;
131
132        if (!this.key.equals(that.key)) {
133            return false;
134        }
135        if (this.value != null
136                ? !this.value.equals(that.value) : that.value != null) {
137            return false;
138        }
139        return true;
140    }
141
142    /**
143     * Returns a hash code.
144     *
145     * @return A hash code.
146     */
147    @Override
148    public int hashCode() {
149        int result;
150        result = (this.key != null ? this.key.hashCode() : 0);
151        result = 29 * result + (this.value != null ? this.value.hashCode() : 0);
152        return result;
153    }
154
155    /**
156     * Returns a clone.  It is assumed that both the key and value are
157     * immutable objects, so only the references are cloned, not the objects
158     * themselves.
159     *
160     * @return A clone.
161     *
162     * @throws CloneNotSupportedException Not thrown by this class, but
163     *         subclasses (if any) might.
164     */
165    @Override
166    public Object clone() throws CloneNotSupportedException {
167        return (DefaultKeyedValue) super.clone();
168    }
169
170    /**
171     * Returns a string representing this instance, primarily useful for
172     * debugging.
173     *
174     * @return A string.
175     */
176    @Override
177    public String toString() {
178        return "(" + this.key.toString() + ", " + this.value.toString() + ")";
179    }
180
181}