1. /* ===========================================================
  2. * JFreeChart : a free chart library for the Java(tm) platform
  3. * ===========================================================
  4. *
  5. * (C) Copyright 2000-2005, 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 License
  20. * along with this library; if not, write to the Free Software Foundation,
  21. * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
  22. *
  23. * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
  24. * in the United States and other countries.]
  25. *
  26. * ---------------
  27. * XYDataItem.java
  28. * ---------------
  29. * (C) Copyright 2003-2005, by Object Refinery Limited.
  30. *
  31. * Original Author: David Gilbert (for Object Refinery Limited);
  32. * Contributor(s): -;
  33. *
  34. * $Id: XYDataItem.java,v 1.4 2005/02/22 08:20:08 mungady Exp $
  35. *
  36. * Changes
  37. * -------
  38. * 05-Aug-2003 : Renamed XYDataPair --> XYDataItem (DG);
  39. * 03-Feb-2004 : Fixed bug in equals() method (DG);
  40. * 21-Feb-2005 : Added setY(double) method (DG);
  41. *
  42. */
  43. package org.jfree.data.xy;
  44. import java.io.Serializable;
  45. import org.jfree.util.ObjectUtilities;
  46. /**
  47. * Represents one (x, y) data item for an {@link XYSeries}.
  48. */
  49. public class XYDataItem implements Cloneable, Comparable, Serializable {
  50. /** The x-value. */
  51. private Number x;
  52. /** The y-value. */
  53. private Number y;
  54. /**
  55. * Constructs a new data item.
  56. *
  57. * @param x the x-value (<code>null</code> NOT permitted).
  58. * @param y the y-value (<code>null</code> permitted).
  59. */
  60. public XYDataItem(Number x, Number y) {
  61. if (x == null) {
  62. throw new IllegalArgumentException("Null 'x' argument.");
  63. }
  64. this.x = x;
  65. this.y = y;
  66. }
  67. /**
  68. * Constructs a new data item.
  69. *
  70. * @param x the x-value.
  71. * @param y the y-value.
  72. */
  73. public XYDataItem(double x, double y) {
  74. this(new Double(x), new Double(y));
  75. }
  76. /**
  77. * Returns the x-value.
  78. *
  79. * @return The x-value (never <code>null</code>).
  80. */
  81. public Number getX() {
  82. return this.x;
  83. }
  84. /**
  85. * Returns the y-value.
  86. *
  87. * @return The y-value (possibly <code>null</code>).
  88. */
  89. public Number getY() {
  90. return this.y;
  91. }
  92. /**
  93. * Sets the y-value for this data item. Note that there is no
  94. * corresponding method to change the x-value.
  95. *
  96. * @param y the new y-value.
  97. */
  98. public void setY(double y) {
  99. setY(new Double(y));
  100. }
  101. /**
  102. * Sets the y-value for this data item. Note that there is no
  103. * corresponding method to change the x-value.
  104. *
  105. * @param y the new y-value (<code>null</code> permitted).
  106. */
  107. public void setY(Number y) {
  108. this.y = y;
  109. }
  110. /**
  111. * Returns an integer indicating the order of this object relative to
  112. * another object.
  113. * <P>
  114. * For the order we consider only the x-value:
  115. * negative == "less-than", zero == "equal", positive == "greater-than".
  116. *
  117. * @param o1 the object being compared to.
  118. *
  119. * @return an integer indicating the order of this data pair object
  120. * relative to another object.
  121. */
  122. public int compareTo(Object o1) {
  123. int result;
  124. // CASE 1 : Comparing to another TimeSeriesDataPair object
  125. // -------------------------------------------------------
  126. if (o1 instanceof XYDataItem) {
  127. XYDataItem dataItem = (XYDataItem) o1;
  128. double compare = this.x.doubleValue()
  129. - dataItem.getX().doubleValue();
  130. if (compare > 0.0) {
  131. result = 1;
  132. }
  133. else {
  134. if (compare < 0.0) {
  135. result = -1;
  136. }
  137. else {
  138. result = 0;
  139. }
  140. }
  141. }
  142. // CASE 2 : Comparing to a general object
  143. // ---------------------------------------------
  144. else {
  145. // consider time periods to be ordered after general objects
  146. result = 1;
  147. }
  148. return result;
  149. }
  150. /**
  151. * Returns a clone of this object.
  152. *
  153. * @return A clone.
  154. *
  155. * @throws CloneNotSupportedException not thrown by this class, but
  156. * subclasses may differ.
  157. */
  158. public Object clone() throws CloneNotSupportedException {
  159. return super.clone();
  160. }
  161. /**
  162. * Tests if this object is equal to another.
  163. *
  164. * @param obj the object to test against for equality (<code>null</code>
  165. * permitted).
  166. *
  167. * @return A boolean.
  168. */
  169. public boolean equals(Object obj) {
  170. if (obj == this) {
  171. return true;
  172. }
  173. if (!(obj instanceof XYDataItem)) {
  174. return false;
  175. }
  176. XYDataItem that = (XYDataItem) obj;
  177. if (!this.x.equals(that.x)) {
  178. return false;
  179. }
  180. if (!ObjectUtilities.equal(this.y, that.y)) {
  181. return false;
  182. }
  183. return true;
  184. }
  185. /**
  186. * Returns a hash code.
  187. *
  188. * @return A hash code.
  189. */
  190. public int hashCode() {
  191. int result;
  192. result = this.x.hashCode();
  193. result = 29 * result + (this.y != null ? this.y.hashCode() : 0);
  194. return result;
  195. }
  196. }