Frames | No Frames |
1: /* =========================================================== 2: * JFreeChart : a free chart library for the Java(tm) platform 3: * =========================================================== 4: * 5: * (C) Copyright 2000-2008, by Object Refinery Limited and Contributors. 6: * 7: * Project Info: http://www.jfree.org/jfreechart/index.html 8: * 9: * This library is free software; you can redistribute it and/or modify it 10: * under the terms of the GNU Lesser General Public License as published by 11: * the Free Software Foundation; either version 2.1 of the License, or 12: * (at your option) any later version. 13: * 14: * This library is distributed in the hope that it will be useful, but 15: * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 16: * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 17: * License for more details. 18: * 19: * You should have received a copy of the GNU Lesser General Public 20: * License along with this library; if not, write to the Free Software 21: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 22: * USA. 23: * 24: * [Java is a trademark or registered trademark of Sun Microsystems, Inc. 25: * in the United States and other countries.] 26: * 27: * ---------------------- 28: * DefaultKeyedValue.java 29: * ---------------------- 30: * (C) Copyright 2002-2008, by Object Refinery Limited. 31: * 32: * Original Author: David Gilbert (for Object Refinery Limited); 33: * Contributor(s): -; 34: * 35: * Changes: 36: * -------- 37: * 31-Oct-2002 : Version 1 (DG); 38: * 13-Mar-2003 : Added equals() method, and implemented Serializable (DG); 39: * 18-Aug-2003 : Implemented Cloneable (DG); 40: * 18-Aug-2004 : Moved from org.jfree.data --> org.jfree.data.base (DG); 41: * 15-Sep-2004 : Added PublicCloneable interface (DG); 42: * ------------- JFREECHART 1.0.x --------------------------------------------- 43: * 11-Jun-2007 : Added toString() method to help with debugging (DG); 44: * 15-Feb-2008 : Prevent null key (DG); 45: * 07-Apr-2008 : Removed to-do item (DG); 46: * 47: */ 48: 49: package org.jfree.data; 50: 51: import java.io.Serializable; 52: 53: import org.jfree.util.PublicCloneable; 54: 55: /** 56: * A (key, value) pair. This class provides a default implementation 57: * of the {@link KeyedValue} interface. 58: */ 59: public class DefaultKeyedValue implements KeyedValue, 60: Cloneable, PublicCloneable, 61: Serializable { 62: 63: /** For serialization. */ 64: private static final long serialVersionUID = -7388924517460437712L; 65: 66: /** The key. */ 67: private Comparable key; 68: 69: /** The value. */ 70: private Number value; 71: 72: /** 73: * Creates a new (key, value) item. 74: * 75: * @param key the key (should be immutable, <code>null</code> not 76: * permitted). 77: * @param value the value (<code>null</code> permitted). 78: */ 79: public DefaultKeyedValue(Comparable key, Number value) { 80: if (key == null) { 81: throw new IllegalArgumentException("Null 'key' argument."); 82: } 83: this.key = key; 84: this.value = value; 85: } 86: 87: /** 88: * Returns the key. 89: * 90: * @return The key (never <code>null</code>). 91: */ 92: public Comparable getKey() { 93: return this.key; 94: } 95: 96: /** 97: * Returns the value. 98: * 99: * @return The value (possibly <code>null</code>). 100: */ 101: public Number getValue() { 102: return this.value; 103: } 104: 105: /** 106: * Sets the value. 107: * 108: * @param value the value (<code>null</code> permitted). 109: */ 110: public synchronized void setValue(Number value) { 111: this.value = value; 112: } 113: 114: /** 115: * Tests this key-value pair for equality with an arbitrary object. 116: * 117: * @param obj the object (<code>null</code> permitted). 118: * 119: * @return A boolean. 120: */ 121: public boolean equals(Object obj) { 122: if (obj == this) { 123: return true; 124: } 125: if (!(obj instanceof DefaultKeyedValue)) { 126: return false; 127: } 128: DefaultKeyedValue that = (DefaultKeyedValue) obj; 129: 130: if (!this.key.equals(that.key)) { 131: return false; 132: } 133: if (this.value != null 134: ? !this.value.equals(that.value) : 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 + (this.value != null ? this.value.hashCode() : 0); 149: return result; 150: } 151: 152: /** 153: * Returns a clone. It is assumed that both the key and value are 154: * immutable objects, so only the references are cloned, not the objects 155: * themselves. 156: * 157: * @return A clone. 158: * 159: * @throws CloneNotSupportedException Not thrown by this class, but 160: * subclasses (if any) might. 161: */ 162: public Object clone() throws CloneNotSupportedException { 163: DefaultKeyedValue clone = (DefaultKeyedValue) super.clone(); 164: return clone; 165: } 166: 167: /** 168: * Returns a string representing this instance, primarily useful for 169: * debugging. 170: * 171: * @return A string. 172: */ 173: public String toString() { 174: return "(" + this.key.toString() + ", " + this.value.toString() + ")"; 175: } 176: 177: }