Source for org.jfree.data.KeyedObject

   1: /* ===========================================================
   2:  * JFreeChart : a free chart library for the Java(tm) platform
   3:  * ===========================================================
   4:  *
   5:  * (C) Copyright 2000-2007, 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:  * KeyedObject.java
  29:  * ----------------
  30:  * (C) Copyright 2003-2007, by Object Refinery Limited.
  31:  *
  32:  * Original Author:  David Gilbert (for Object Refinery Limited);
  33:  * Contributor(s):   -;
  34:  *
  35:  * Changes:
  36:  * --------
  37:  * 05-Feb-2003 : Version 1 (DG);
  38:  * 27-Jan-2003 : Implemented Cloneable and Serializable, and added an equals()
  39:  *               method (DG);
  40:  *
  41:  */
  42: 
  43: package org.jfree.data;
  44: 
  45: import java.io.Serializable;
  46: 
  47: import org.jfree.util.ObjectUtilities;
  48: import org.jfree.util.PublicCloneable;
  49: 
  50: /**
  51:  * A (key, object) pair.
  52:  */
  53: public class KeyedObject implements Cloneable, PublicCloneable, Serializable {
  54: 
  55:     /** For serialization. */
  56:     private static final long serialVersionUID = 2677930479256885863L;
  57:     
  58:     /** The key. */
  59:     private Comparable key;
  60: 
  61:     /** The object. */
  62:     private Object object;
  63: 
  64:     /**
  65:      * Creates a new (key, object) pair.
  66:      *
  67:      * @param key  the key.
  68:      * @param object  the object (<code>null</code> permitted).
  69:      */
  70:     public KeyedObject(Comparable key, Object object) {
  71:         this.key = key;
  72:         this.object = object;
  73:     }
  74: 
  75:     /**
  76:      * Returns the key.
  77:      *
  78:      * @return The key.
  79:      */
  80:     public Comparable getKey() {
  81:         return this.key;
  82:     }
  83: 
  84:     /**
  85:      * Returns the object.
  86:      *
  87:      * @return The object (possibly <code>null</code>).
  88:      */
  89:     public Object getObject() {
  90:         return this.object;
  91:     }
  92: 
  93:     /**
  94:      * Sets the object.
  95:      *
  96:      * @param object  the object (<code>null</code> permitted).
  97:      */
  98:     public void setObject(Object object) {
  99:         this.object = object;
 100:     }
 101:     
 102:     /**
 103:      * Returns a clone of this object.  It is assumed that the key is an 
 104:      * immutable object, so it is not deep-cloned.  The object is deep-cloned 
 105:      * if it implements {@link PublicCloneable}, otherwise a shallow clone is 
 106:      * made.
 107:      * 
 108:      * @return A clone.
 109:      * 
 110:      * @throws CloneNotSupportedException if there is a problem cloning.
 111:      */
 112:     public Object clone() throws CloneNotSupportedException {
 113:         KeyedObject clone = (KeyedObject) super.clone();
 114:         if (this.object instanceof PublicCloneable) {
 115:             PublicCloneable pc = (PublicCloneable) this.object;
 116:             clone.object = pc.clone();
 117:         }
 118:         return clone;      
 119:     }
 120:     
 121:     /**
 122:      * Tests if this object is equal to another.
 123:      *
 124:      * @param obj  the other object.
 125:      *
 126:      * @return A boolean.
 127:      */
 128:     public boolean equals(Object obj) {
 129: 
 130:         if (obj == this) {
 131:             return true;
 132:         }
 133: 
 134:         if (!(obj instanceof KeyedObject)) {
 135:             return false;
 136:         }
 137:         KeyedObject that = (KeyedObject) obj;
 138:         if (!ObjectUtilities.equal(this.key, that.key)) {
 139:             return false;
 140:         }
 141: 
 142:         if (!ObjectUtilities.equal(this.object, that.object)) {
 143:             return false;
 144:         }
 145: 
 146:         return true;
 147:     }
 148:     
 149: }