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