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