Source for org.jfree.data.DefaultKeyedValue

   1: /* ===========================================================
   2:  * JFreeChart : a free chart library for the Java(tm) platform
   3:  * ===========================================================
   4:  *
   5:  * (C) Copyright 2000-2005, 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-2005, by Object Refinery Limited.
  31:  *
  32:  * Original Author:  David Gilbert (for Object Refinery Limited);
  33:  * Contributor(s):   -;
  34:  *
  35:  * $Id: DefaultKeyedValue.java,v 1.6.2.1 2005/10/25 21:29:13 mungady Exp $
  36:  *
  37:  * Changes:
  38:  * --------
  39:  * 31-Oct-2002 : Version 1 (DG);
  40:  * 13-Mar-2003 : Added equals() method, and implemented Serializable (DG);
  41:  * 18-Aug-2003 : Implemented Cloneable (DG);
  42:  * 18-Aug-2004 : Moved from org.jfree.data --> org.jfree.data.base (DG);
  43:  * 15-Sep-2004 : Added PublicCloneable interface (DG);
  44:  *
  45:  */
  46: 
  47: package org.jfree.data;
  48: 
  49: import java.io.Serializable;
  50: 
  51: import org.jfree.util.PublicCloneable;
  52: 
  53: /**
  54:  * A (key, value) pair.  This class provides a default implementation 
  55:  * of the {@link KeyedValue} interface.
  56:  */
  57: public class DefaultKeyedValue implements KeyedValue, 
  58:                                           Cloneable, PublicCloneable, 
  59:                                           Serializable {
  60: 
  61:     /** For serialization. */
  62:     private static final long serialVersionUID = -7388924517460437712L;
  63:     
  64:     /** The key. */
  65:     private Comparable key;
  66: 
  67:     /** The value. */
  68:     private Number value;
  69: 
  70:     /**
  71:      * Creates a new (key, value) item.
  72:      *
  73:      * @param key  the key (should be immutable).
  74:      * @param value  the value (<code>null</code> permitted).
  75:      */
  76:     public DefaultKeyedValue(Comparable key, Number value) {
  77:         this.key = key;
  78:         this.value = value;
  79:     }
  80: 
  81:     /**
  82:      * Returns the key.
  83:      *
  84:      * @return The key.
  85:      */
  86:     public Comparable getKey() {
  87:         return this.key;
  88:     }
  89: 
  90:     /**
  91:      * Returns the value.
  92:      *
  93:      * @return The value (possibly <code>null</code>).
  94:      */
  95:     public Number getValue() {
  96:         return this.value;
  97:     }
  98: 
  99:     /**
 100:      * Sets the value.
 101:      *
 102:      * @param value  the value (<code>null</code> permitted).
 103:      */
 104:     public synchronized void setValue(Number value) {
 105:         this.value = value;
 106:     }
 107: 
 108:     /**
 109:      * Tests this key-value pair for equality with an arbitrary object.
 110:      *
 111:      * @param obj  the object (<code>null</code> permitted).
 112:      *
 113:      * @return A boolean.
 114:      */
 115:     public boolean equals(Object obj) {
 116:         if (obj == this) {
 117:             return true;
 118:         }
 119:         if (!(obj instanceof DefaultKeyedValue)) {
 120:             return false;
 121:         }
 122:         // TODO: modify this so that we check for equality with any KeyedValue
 123:         // rather than specifically a DefaultKeyedValue
 124:         DefaultKeyedValue that = (DefaultKeyedValue) obj;
 125:         
 126:         // TODO: the following checks for null should be handled in a utility 
 127:         // method
 128:         if (this.key != null ? !this.key.equals(that.key) : that.key != null) {
 129:             return false;
 130:         }
 131:         if (this.value != null 
 132:                 ? !this.value.equals(that.value) : that.value != null) {
 133:             return false;
 134:         }
 135:         return true;
 136:     }
 137: 
 138:     /**
 139:      * Returns a hash code.
 140:      * 
 141:      * @return A hash code.
 142:      */
 143:     public int hashCode() {
 144:         int result;
 145:         result = (this.key != null ? this.key.hashCode() : 0);
 146:         result = 29 * result + (this.value != null ? this.value.hashCode() : 0);
 147:         return result;
 148:     }
 149: 
 150:     /**
 151:      * Returns a clone.  It is assumed that both the key and value are 
 152:      * immutable objects, so only the references are cloned, not the objects 
 153:      * themselves.
 154:      * 
 155:      * @return A clone.
 156:      * 
 157:      * @throws CloneNotSupportedException Not thrown by this class, but 
 158:      *         subclasses (if any) might.
 159:      */
 160:     public Object clone() throws CloneNotSupportedException {
 161:         DefaultKeyedValue clone = (DefaultKeyedValue) super.clone();
 162:         return clone;
 163:     }
 164: 
 165: }