001/* =========================================================== 002 * JFreeChart : a free chart library for the Java(tm) platform 003 * =========================================================== 004 * 005 * (C) Copyright 2000-2020, by Object Refinery Limited and Contributors. 006 * 007 * Project Info: http://www.jfree.org/jfreechart/index.html 008 * 009 * This library is free software; you can redistribute it and/or modify it 010 * under the terms of the GNU Lesser General Public License as published by 011 * the Free Software Foundation; either version 2.1 of the License, or 012 * (at your option) any later version. 013 * 014 * This library is distributed in the hope that it will be useful, but 015 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 016 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 017 * License for more details. 018 * 019 * You should have received a copy of the GNU Lesser General Public 020 * License along with this library; if not, write to the Free Software 021 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 022 * USA. 023 * 024 * [Oracle and Java are registered trademarks of Oracle and/or its affiliates. 025 * Other names may be trademarks of their respective owners.] 026 * 027 * --------------- 028 * XYDataItem.java 029 * --------------- 030 * (C) Copyright 2003-2013, by Object Refinery Limited. 031 * 032 * Original Author: David Gilbert (for Object Refinery Limited); 033 * Contributor(s): -; 034 * 035 * Changes 036 * ------- 037 * 05-Aug-2003 : Renamed XYDataPair --> XYDataItem (DG); 038 * 03-Feb-2004 : Fixed bug in equals() method (DG); 039 * 21-Feb-2005 : Added setY(double) method (DG); 040 * ------------- JFREECHART 1.0.x --------------------------------------------- 041 * 30-Nov-2007 : Implemented getXValue() and getYValue(), plus toString() for 042 * debugging use (DG); 043 * 10-Jun-2009 : Reimplemented cloning (DG); 044 * 02-Jul-2013 : Use ParamChecks (DG); 045 * 046 */ 047 048package org.jfree.data.xy; 049 050import java.io.Serializable; 051import org.jfree.chart.util.ObjectUtils; 052import org.jfree.chart.util.Args; 053 054/** 055 * Represents one (x, y) data item for an {@link XYSeries}. Note that 056 * subclasses are REQUIRED to support cloning. 057 */ 058public class XYDataItem implements Cloneable, Comparable, Serializable { 059 060 /** For serialization. */ 061 private static final long serialVersionUID = 2751513470325494890L; 062 063 /** The x-value ({@code null} not permitted). */ 064 private Number x; 065 066 /** The y-value. */ 067 private Number y; 068 069 /** 070 * Constructs a new data item. 071 * 072 * @param x the x-value ({@code null} NOT permitted). 073 * @param y the y-value ({@code null} permitted). 074 */ 075 public XYDataItem(Number x, Number y) { 076 Args.nullNotPermitted(x, "x"); 077 this.x = x; 078 this.y = y; 079 } 080 081 /** 082 * Constructs a new data item. 083 * 084 * @param x the x-value. 085 * @param y the y-value. 086 */ 087 public XYDataItem(double x, double y) { 088 this(Double.valueOf(x), Double.valueOf(y)); 089 } 090 091 /** 092 * Returns the x-value. 093 * 094 * @return The x-value (never {@code null}). 095 */ 096 public Number getX() { 097 return this.x; 098 } 099 100 /** 101 * Returns the x-value as a double primitive. 102 * 103 * @return The x-value. 104 * 105 * @see #getX() 106 * @see #getYValue() 107 * 108 * @since 1.0.9 109 */ 110 public double getXValue() { 111 // this.x is not allowed to be null... 112 return this.x.doubleValue(); 113 } 114 115 /** 116 * Returns the y-value. 117 * 118 * @return The y-value (possibly {@code null}). 119 */ 120 public Number getY() { 121 return this.y; 122 } 123 124 /** 125 * Returns the y-value as a double primitive. 126 * 127 * @return The y-value. 128 * 129 * @see #getY() 130 * @see #getXValue() 131 * 132 * @since 1.0.9 133 */ 134 public double getYValue() { 135 double result = Double.NaN; 136 if (this.y != null) { 137 result = this.y.doubleValue(); 138 } 139 return result; 140 } 141 142 /** 143 * Sets the y-value for this data item. Note that there is no 144 * corresponding method to change the x-value. 145 * 146 * @param y the new y-value. 147 */ 148 public void setY(double y) { 149 setY(Double.valueOf(y)); 150 } 151 152 /** 153 * Sets the y-value for this data item. Note that there is no 154 * corresponding method to change the x-value. 155 * 156 * @param y the new y-value ({@code null} permitted). 157 */ 158 public void setY(Number y) { 159 this.y = y; 160 } 161 162 /** 163 * Returns an integer indicating the order of this object relative to 164 * another object. 165 * <P> 166 * For the order we consider only the x-value: 167 * negative == "less-than", zero == "equal", positive == "greater-than". 168 * 169 * @param o1 the object being compared to. 170 * 171 * @return An integer indicating the order of this data pair object 172 * relative to another object. 173 */ 174 @Override 175 public int compareTo(Object o1) { 176 177 int result; 178 179 // CASE 1 : Comparing to another TimeSeriesDataPair object 180 // ------------------------------------------------------- 181 if (o1 instanceof XYDataItem) { 182 XYDataItem dataItem = (XYDataItem) o1; 183 double compare = this.x.doubleValue() 184 - dataItem.getX().doubleValue(); 185 if (compare > 0.0) { 186 result = 1; 187 } 188 else { 189 if (compare < 0.0) { 190 result = -1; 191 } 192 else { 193 result = 0; 194 } 195 } 196 } 197 198 // CASE 2 : Comparing to a general object 199 // --------------------------------------------- 200 else { 201 // consider time periods to be ordered after general objects 202 result = 1; 203 } 204 205 return result; 206 207 } 208 209 /** 210 * Returns a clone of this object. 211 * 212 * @return A clone. 213 */ 214 @Override 215 public Object clone() { 216 Object clone = null; 217 try { 218 clone = super.clone(); 219 } 220 catch (CloneNotSupportedException e) { // won't get here... 221 e.printStackTrace(); 222 } 223 return clone; 224 } 225 226 /** 227 * Tests if this object is equal to another. 228 * 229 * @param obj the object to test against for equality ({@code null} 230 * permitted). 231 * 232 * @return A boolean. 233 */ 234 @Override 235 public boolean equals(Object obj) { 236 if (obj == this) { 237 return true; 238 } 239 if (!(obj instanceof XYDataItem)) { 240 return false; 241 } 242 XYDataItem that = (XYDataItem) obj; 243 if (!this.x.equals(that.x)) { 244 return false; 245 } 246 if (!ObjectUtils.equal(this.y, that.y)) { 247 return false; 248 } 249 return true; 250 } 251 252 /** 253 * Returns a hash code. 254 * 255 * @return A hash code. 256 */ 257 @Override 258 public int hashCode() { 259 int result; 260 result = this.x.hashCode(); 261 result = 29 * result + (this.y != null ? this.y.hashCode() : 0); 262 return result; 263 } 264 265 /** 266 * Returns a string representing this instance, primarily for debugging 267 * use. 268 * 269 * @return A string. 270 */ 271 @Override 272 public String toString() { 273 return "[" + getXValue() + ", " + getYValue() + "]"; 274 } 275 276}