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