Source for org.jfree.data.KeyedObject

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