Source for org.jfree.data.ComparableObjectItem

   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:  * ComparableObjectItem.java
  29:  * -------------------------
  30:  * (C) Copyright 2006, 2007, by Object Refinery Limited.
  31:  *
  32:  * Original Author:  David Gilbert (for Object Refinery Limited);
  33:  * Contributor(s):   -;
  34:  *
  35:  * Changes
  36:  * -------
  37:  * 19-Oct-2006 : New class, based on XYDataItem (DG);
  38:  *
  39:  */
  40: 
  41: package org.jfree.data;
  42: 
  43: import java.io.Serializable;
  44: 
  45: import org.jfree.util.ObjectUtilities;
  46: 
  47: /**
  48:  * Represents one (Comparable, Object) data item for use in a 
  49:  * {@link ComparableObjectSeries}.
  50:  *
  51:  * @since 1.0.3
  52:  */
  53: public class ComparableObjectItem implements Cloneable, Comparable, 
  54:         Serializable {
  55: 
  56:     /** For serialization. */
  57:     private static final long serialVersionUID = 2751513470325494890L;
  58:     
  59:     /** The x-value. */
  60:     private Comparable x;
  61: 
  62:     /** The y-value. */
  63:     private Object obj;
  64: 
  65:     /**
  66:      * Constructs a new data item.
  67:      *
  68:      * @param x  the x-value (<code>null</code> NOT permitted).
  69:      * @param y  the y-value (<code>null</code> permitted).
  70:      */
  71:     public ComparableObjectItem(Comparable x, Object y) {
  72:         if (x == null) {
  73:             throw new IllegalArgumentException("Null 'x' argument.");
  74:         }
  75:         this.x = x;
  76:         this.obj = y;
  77:     }
  78: 
  79:     /**
  80:      * Returns the x-value.
  81:      *
  82:      * @return The x-value (never <code>null</code>).
  83:      */
  84:     protected Comparable getComparable() {
  85:         return this.x;
  86:     }
  87: 
  88:     /**
  89:      * Returns the y-value.
  90:      *
  91:      * @return The y-value (possibly <code>null</code>).
  92:      */
  93:     protected Object getObject() {
  94:         return this.obj;
  95:     }
  96: 
  97:     /**
  98:      * Sets the y-value for this data item.  Note that there is no 
  99:      * corresponding method to change the x-value.
 100:      *
 101:      * @param y  the new y-value (<code>null</code> permitted).
 102:      */
 103:     protected void setObject(Object y) {
 104:         this.obj = y;
 105:     }
 106: 
 107:     /**
 108:      * Returns an integer indicating the order of this object relative to 
 109:      * another object.
 110:      * <P>
 111:      * For the order we consider only the x-value:
 112:      * negative == "less-than", zero == "equal", positive == "greater-than".
 113:      *
 114:      * @param o1  the object being compared to.
 115:      *
 116:      * @return An integer indicating the order of this data pair object
 117:      *      relative to another object.
 118:      */
 119:     public int compareTo(Object o1) {
 120: 
 121:         int result;
 122: 
 123:         // CASE 1 : Comparing to another ComparableObjectItem object
 124:         // ---------------------------------------------------------
 125:         if (o1 instanceof ComparableObjectItem) {
 126:             ComparableObjectItem that = (ComparableObjectItem) o1;
 127:             return this.x.compareTo(that.x);
 128:         }
 129: 
 130:         // CASE 2 : Comparing to a general object
 131:         // ---------------------------------------------
 132:         else {
 133:             // consider these to be ordered after general objects
 134:             result = 1;
 135:         }
 136: 
 137:         return result;
 138: 
 139:     }
 140: 
 141:     /**
 142:      * Returns a clone of this object.
 143:      *
 144:      * @return A clone.
 145:      * 
 146:      * @throws CloneNotSupportedException not thrown by this class, but 
 147:      *         subclasses may differ.
 148:      */
 149:     public Object clone() throws CloneNotSupportedException {
 150:         return super.clone();
 151:     }
 152:     
 153:     /**
 154:      * Tests if this object is equal to another.
 155:      *
 156:      * @param obj  the object to test against for equality (<code>null</code>
 157:      *             permitted).
 158:      *
 159:      * @return A boolean.
 160:      */
 161:     public boolean equals(Object obj) {
 162:         if (obj == this) {
 163:             return true;
 164:         }
 165:         if (!(obj instanceof ComparableObjectItem)) {
 166:             return false;
 167:         }
 168:         ComparableObjectItem that = (ComparableObjectItem) obj;
 169:         if (!this.x.equals(that.x)) {
 170:             return false;
 171:         }
 172:         if (!ObjectUtilities.equal(this.obj, that.obj)) {
 173:             return false;
 174:         }
 175:         return true;        
 176:     }
 177: 
 178:     /**
 179:      * Returns a hash code.
 180:      * 
 181:      * @return A hash code.
 182:      */
 183:     public int hashCode() {
 184:         int result;
 185:         result = this.x.hashCode();
 186:         result = 29 * result + (this.obj != null ? this.obj.hashCode() : 0);
 187:         return result;
 188:     }
 189:     
 190: }