Source for org.jfree.data.xy.XYDataItem

   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:  * XYDataItem.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-Aug-2003 : Renamed XYDataPair --> XYDataItem (DG);
  38:  * 03-Feb-2004 : Fixed bug in equals() method (DG);
  39:  * 21-Feb-2005 : Added setY(double) method (DG);
  40:  *
  41:  */
  42: 
  43: package org.jfree.data.xy;
  44: 
  45: import java.io.Serializable;
  46: 
  47: import org.jfree.util.ObjectUtilities;
  48: 
  49: /**
  50:  * Represents one (x, y) data item for an {@link XYSeries}.
  51:  */
  52: public class XYDataItem implements Cloneable, Comparable, Serializable {
  53: 
  54:     /** For serialization. */
  55:     private static final long serialVersionUID = 2751513470325494890L;
  56:     
  57:     /** The x-value. */
  58:     private Number x;
  59: 
  60:     /** The y-value. */
  61:     private Number y;
  62: 
  63:     /**
  64:      * Constructs a new data item.
  65:      *
  66:      * @param x  the x-value (<code>null</code> NOT permitted).
  67:      * @param y  the y-value (<code>null</code> permitted).
  68:      */
  69:     public XYDataItem(Number x, Number y) {
  70:         if (x == null) {
  71:             throw new IllegalArgumentException("Null 'x' argument.");
  72:         }
  73:         this.x = x;
  74:         this.y = y;
  75:     }
  76: 
  77:     /**
  78:      * Constructs a new data item.
  79:      *
  80:      * @param x  the x-value.
  81:      * @param y  the y-value.
  82:      */
  83:     public XYDataItem(double x, double y) {
  84:         this(new Double(x), new Double(y));
  85:     }
  86: 
  87:     /**
  88:      * Returns the x-value.
  89:      *
  90:      * @return The x-value (never <code>null</code>).
  91:      */
  92:     public Number getX() {
  93:         return this.x;
  94:     }
  95: 
  96:     /**
  97:      * Returns the y-value.
  98:      *
  99:      * @return The y-value (possibly <code>null</code>).
 100:      */
 101:     public Number getY() {
 102:         return this.y;
 103:     }
 104: 
 105:     /**
 106:      * Sets the y-value for this data item.  Note that there is no 
 107:      * corresponding method to change the x-value.
 108:      *
 109:      * @param y  the new y-value.
 110:      */
 111:     public void setY(double y) {
 112:         setY(new Double(y));   
 113:     }
 114:     
 115:     /**
 116:      * Sets the y-value for this data item.  Note that there is no 
 117:      * corresponding method to change the x-value.
 118:      *
 119:      * @param y  the new y-value (<code>null</code> permitted).
 120:      */
 121:     public void setY(Number y) {
 122:         this.y = y;
 123:     }
 124: 
 125:     /**
 126:      * Returns an integer indicating the order of this object relative to 
 127:      * another object.
 128:      * <P>
 129:      * For the order we consider only the x-value:
 130:      * negative == "less-than", zero == "equal", positive == "greater-than".
 131:      *
 132:      * @param o1  the object being compared to.
 133:      *
 134:      * @return An integer indicating the order of this data pair object
 135:      *      relative to another object.
 136:      */
 137:     public int compareTo(Object o1) {
 138: 
 139:         int result;
 140: 
 141:         // CASE 1 : Comparing to another TimeSeriesDataPair object
 142:         // -------------------------------------------------------
 143:         if (o1 instanceof XYDataItem) {
 144:             XYDataItem dataItem = (XYDataItem) o1;
 145:             double compare = this.x.doubleValue() 
 146:                              - dataItem.getX().doubleValue();
 147:             if (compare > 0.0) {
 148:                 result = 1;
 149:             }
 150:             else {
 151:                 if (compare < 0.0) {
 152:                     result = -1;
 153:                 }
 154:                 else {
 155:                     result = 0;
 156:                 }
 157:             }
 158:         }
 159: 
 160:         // CASE 2 : Comparing to a general object
 161:         // ---------------------------------------------
 162:         else {
 163:             // consider time periods to be ordered after general objects
 164:             result = 1;
 165:         }
 166: 
 167:         return result;
 168: 
 169:     }
 170: 
 171:     /**
 172:      * Returns a clone of this object.
 173:      *
 174:      * @return A clone.
 175:      * 
 176:      * @throws CloneNotSupportedException not thrown by this class, but 
 177:      *         subclasses may differ.
 178:      */
 179:     public Object clone() throws CloneNotSupportedException {
 180:         return super.clone();
 181:     }
 182:     
 183:     /**
 184:      * Tests if this object is equal to another.
 185:      *
 186:      * @param obj  the object to test against for equality (<code>null</code>
 187:      *             permitted).
 188:      *
 189:      * @return A boolean.
 190:      */
 191:     public boolean equals(Object obj) {
 192:         if (obj == this) {
 193:             return true;
 194:         }
 195:         if (!(obj instanceof XYDataItem)) {
 196:             return false;
 197:         }
 198:         XYDataItem that = (XYDataItem) obj;
 199:         if (!this.x.equals(that.x)) {
 200:             return false;
 201:         }
 202:         if (!ObjectUtilities.equal(this.y, that.y)) {
 203:             return false;
 204:         }
 205:         return true;        
 206:     }
 207: 
 208:     /**
 209:      * Returns a hash code.
 210:      * 
 211:      * @return A hash code.
 212:      */
 213:     public int hashCode() {
 214:         int result;
 215:         result = this.x.hashCode();
 216:         result = 29 * result + (this.y != null ? this.y.hashCode() : 0);
 217:         return result;
 218:     }
 219:     
 220: }