Frames | No Frames |
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: * ------------- JFREECHART 1.0.x --------------------------------------------- 41: * 30-Nov-2007 : Implemented getXValue() and getYValue(), plus toString() for 42: * debugging use (DG); 43: * 44: */ 45: 46: package org.jfree.data.xy; 47: 48: import java.io.Serializable; 49: 50: import org.jfree.util.ObjectUtilities; 51: 52: /** 53: * Represents one (x, y) data item for an {@link XYSeries}. 54: */ 55: public class XYDataItem implements Cloneable, Comparable, Serializable { 56: 57: /** For serialization. */ 58: private static final long serialVersionUID = 2751513470325494890L; 59: 60: /** The x-value (<code>null</code> not permitted). */ 61: private Number x; 62: 63: /** The y-value. */ 64: private Number y; 65: 66: /** 67: * Constructs a new data item. 68: * 69: * @param x the x-value (<code>null</code> NOT permitted). 70: * @param y the y-value (<code>null</code> permitted). 71: */ 72: public XYDataItem(Number x, Number y) { 73: if (x == null) { 74: throw new IllegalArgumentException("Null 'x' argument."); 75: } 76: this.x = x; 77: this.y = y; 78: } 79: 80: /** 81: * Constructs a new data item. 82: * 83: * @param x the x-value. 84: * @param y the y-value. 85: */ 86: public XYDataItem(double x, double y) { 87: this(new Double(x), new Double(y)); 88: } 89: 90: /** 91: * Returns the x-value. 92: * 93: * @return The x-value (never <code>null</code>). 94: */ 95: public Number getX() { 96: return this.x; 97: } 98: 99: /** 100: * Returns the x-value as a double primitive. 101: * 102: * @return The x-value. 103: * 104: * @see #getX() 105: * @see #getYValue() 106: * 107: * @since 1.0.9 108: */ 109: public double getXValue() { 110: // this.x is not allowed to be null... 111: return this.x.doubleValue(); 112: } 113: 114: /** 115: * Returns the y-value. 116: * 117: * @return The y-value (possibly <code>null</code>). 118: */ 119: public Number getY() { 120: return this.y; 121: } 122: 123: /** 124: * Returns the y-value as a double primitive. 125: * 126: * @return The y-value. 127: * 128: * @see #getY() 129: * @see #getXValue() 130: * 131: * @since 1.0.9 132: */ 133: public double getYValue() { 134: double result = Double.NaN; 135: if (this.y != null) { 136: result = this.y.doubleValue(); 137: } 138: return result; 139: } 140: 141: /** 142: * Sets the y-value for this data item. Note that there is no 143: * corresponding method to change the x-value. 144: * 145: * @param y the new y-value. 146: */ 147: public void setY(double y) { 148: setY(new Double(y)); 149: } 150: 151: /** 152: * Sets the y-value for this data item. Note that there is no 153: * corresponding method to change the x-value. 154: * 155: * @param y the new y-value (<code>null</code> permitted). 156: */ 157: public void setY(Number y) { 158: this.y = y; 159: } 160: 161: /** 162: * Returns an integer indicating the order of this object relative to 163: * another object. 164: * <P> 165: * For the order we consider only the x-value: 166: * negative == "less-than", zero == "equal", positive == "greater-than". 167: * 168: * @param o1 the object being compared to. 169: * 170: * @return An integer indicating the order of this data pair object 171: * relative to another object. 172: */ 173: public int compareTo(Object o1) { 174: 175: int result; 176: 177: // CASE 1 : Comparing to another TimeSeriesDataPair object 178: // ------------------------------------------------------- 179: if (o1 instanceof XYDataItem) { 180: XYDataItem dataItem = (XYDataItem) o1; 181: double compare = this.x.doubleValue() 182: - dataItem.getX().doubleValue(); 183: if (compare > 0.0) { 184: result = 1; 185: } 186: else { 187: if (compare < 0.0) { 188: result = -1; 189: } 190: else { 191: result = 0; 192: } 193: } 194: } 195: 196: // CASE 2 : Comparing to a general object 197: // --------------------------------------------- 198: else { 199: // consider time periods to be ordered after general objects 200: result = 1; 201: } 202: 203: return result; 204: 205: } 206: 207: /** 208: * Returns a clone of this object. 209: * 210: * @return A clone. 211: * 212: * @throws CloneNotSupportedException not thrown by this class, but 213: * subclasses may differ. 214: */ 215: public Object clone() throws CloneNotSupportedException { 216: return super.clone(); 217: } 218: 219: /** 220: * Tests if this object is equal to another. 221: * 222: * @param obj the object to test against for equality (<code>null</code> 223: * permitted). 224: * 225: * @return A boolean. 226: */ 227: public boolean equals(Object obj) { 228: if (obj == this) { 229: return true; 230: } 231: if (!(obj instanceof XYDataItem)) { 232: return false; 233: } 234: XYDataItem that = (XYDataItem) obj; 235: if (!this.x.equals(that.x)) { 236: return false; 237: } 238: if (!ObjectUtilities.equal(this.y, that.y)) { 239: return false; 240: } 241: return true; 242: } 243: 244: /** 245: * Returns a hash code. 246: * 247: * @return A hash code. 248: */ 249: public int hashCode() { 250: int result; 251: result = this.x.hashCode(); 252: result = 29 * result + (this.y != null ? this.y.hashCode() : 0); 253: return result; 254: } 255: 256: /** 257: * Returns a string representing this instance, primarily for debugging 258: * use. 259: * 260: * @return A string. 261: */ 262: public String toString() { 263: return "[" + getXValue() + ", " + getYValue() + "]"; 264: } 265: 266: }