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-2020, by Object Refinery Limited. 031 * 032 * Original Author: David Gilbert (for Object Refinery Limited); 033 * Contributor(s): -; 034 * 035 */ 036 037package org.jfree.data.xy; 038 039import java.io.Serializable; 040import org.jfree.chart.util.ObjectUtils; 041import org.jfree.chart.util.Args; 042 043/** 044 * Represents one (x, y) data item for an {@link XYSeries}. Note that 045 * subclasses are REQUIRED to support cloning. 046 */ 047public class XYDataItem implements Cloneable, Comparable, Serializable { 048 049 /** For serialization. */ 050 private static final long serialVersionUID = 2751513470325494890L; 051 052 /** The x-value ({@code null} not permitted). */ 053 private Number x; 054 055 /** The y-value. */ 056 private Number y; 057 058 /** 059 * Constructs a new data item. 060 * 061 * @param x the x-value ({@code null} NOT permitted). 062 * @param y the y-value ({@code null} permitted). 063 */ 064 public XYDataItem(Number x, Number y) { 065 Args.nullNotPermitted(x, "x"); 066 this.x = x; 067 this.y = y; 068 } 069 070 /** 071 * Constructs a new data item. 072 * 073 * @param x the x-value. 074 * @param y the y-value. 075 */ 076 public XYDataItem(double x, double y) { 077 this(Double.valueOf(x), Double.valueOf(y)); 078 } 079 080 /** 081 * Returns the x-value. 082 * 083 * @return The x-value (never {@code null}). 084 */ 085 public Number getX() { 086 return this.x; 087 } 088 089 /** 090 * Returns the x-value as a double primitive. 091 * 092 * @return The x-value. 093 * 094 * @see #getX() 095 * @see #getYValue() 096 * 097 * @since 1.0.9 098 */ 099 public double getXValue() { 100 // this.x is not allowed to be null... 101 return this.x.doubleValue(); 102 } 103 104 /** 105 * Returns the y-value. 106 * 107 * @return The y-value (possibly {@code null}). 108 */ 109 public Number getY() { 110 return this.y; 111 } 112 113 /** 114 * Returns the y-value as a double primitive. 115 * 116 * @return The y-value. 117 * 118 * @see #getY() 119 * @see #getXValue() 120 * 121 * @since 1.0.9 122 */ 123 public double getYValue() { 124 double result = Double.NaN; 125 if (this.y != null) { 126 result = this.y.doubleValue(); 127 } 128 return result; 129 } 130 131 /** 132 * Sets the y-value for this data item. Note that there is no 133 * corresponding method to change the x-value. 134 * 135 * @param y the new y-value. 136 */ 137 public void setY(double y) { 138 setY(Double.valueOf(y)); 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 ({@code null} permitted). 146 */ 147 public void setY(Number y) { 148 this.y = y; 149 } 150 151 /** 152 * Returns an integer indicating the order of this object relative to 153 * another object. 154 * <P> 155 * For the order we consider only the x-value: 156 * negative == "less-than", zero == "equal", positive == "greater-than". 157 * 158 * @param o1 the object being compared to. 159 * 160 * @return An integer indicating the order of this data pair object 161 * relative to another object. 162 */ 163 @Override 164 public int compareTo(Object o1) { 165 166 int result; 167 168 // CASE 1 : Comparing to another TimeSeriesDataPair object 169 // ------------------------------------------------------- 170 if (o1 instanceof XYDataItem) { 171 XYDataItem dataItem = (XYDataItem) o1; 172 double compare = this.x.doubleValue() 173 - dataItem.getX().doubleValue(); 174 if (compare > 0.0) { 175 result = 1; 176 } 177 else { 178 if (compare < 0.0) { 179 result = -1; 180 } 181 else { 182 result = 0; 183 } 184 } 185 } 186 187 // CASE 2 : Comparing to a general object 188 // --------------------------------------------- 189 else { 190 // consider time periods to be ordered after general objects 191 result = 1; 192 } 193 194 return result; 195 196 } 197 198 /** 199 * Returns a clone of this object. 200 * 201 * @return A clone. 202 */ 203 @Override 204 public Object clone() { 205 Object clone = null; 206 try { 207 clone = super.clone(); 208 } 209 catch (CloneNotSupportedException e) { // won't get here... 210 e.printStackTrace(); 211 } 212 return clone; 213 } 214 215 /** 216 * Tests if this object is equal to another. 217 * 218 * @param obj the object to test against for equality ({@code null} 219 * permitted). 220 * 221 * @return A boolean. 222 */ 223 @Override 224 public boolean equals(Object obj) { 225 if (obj == this) { 226 return true; 227 } 228 if (!(obj instanceof XYDataItem)) { 229 return false; 230 } 231 XYDataItem that = (XYDataItem) obj; 232 if (!this.x.equals(that.x)) { 233 return false; 234 } 235 if (!ObjectUtils.equal(this.y, that.y)) { 236 return false; 237 } 238 return true; 239 } 240 241 /** 242 * Returns a hash code. 243 * 244 * @return A hash code. 245 */ 246 @Override 247 public int hashCode() { 248 int result; 249 result = this.x.hashCode(); 250 result = 29 * result + (this.y != null ? this.y.hashCode() : 0); 251 return result; 252 } 253 254 /** 255 * Returns a string representing this instance, primarily for debugging 256 * use. 257 * 258 * @return A string. 259 */ 260 @Override 261 public String toString() { 262 return "[" + getXValue() + ", " + getYValue() + "]"; 263 } 264 265}