001 /*
002 * Apache License
003 * Version 2.0, January 2004
004 * http://www.apache.org/licenses/
005 *
006 * Copyright 2008 by chenillekit.org
007 *
008 * Licensed under the Apache License, Version 2.0 (the "License");
009 * you may not use this file except in compliance with the License.
010 * You may obtain a copy of the License at
011 *
012 * http://www.apache.org/licenses/LICENSE-2.0
013 */
014
015 package org.chenillekit.tapestry.core.utils;
016
017 import java.io.Serializable;
018
019 /**
020 * @version $Id: XYDataItem.java 670 2010-07-19 09:22:02Z mlusetti $
021 */
022 public class XYDataItem implements Serializable, Comparable
023 {
024 private static final long serialVersionUID = 2751513470325494890L;
025
026 /**
027 * The x-value.
028 */
029 private Number _xValue;
030
031 /**
032 * The y-value.
033 */
034 private Number _yValue;
035
036 /**
037 * Constructs a new data item.
038 *
039 * @param xValue the x-value.
040 * @param yValue the y-value.
041 */
042 public XYDataItem(Number xValue, Number yValue)
043 {
044 assert xValue != null;
045 assert yValue != null;
046
047 _xValue = xValue;
048 _yValue = yValue;
049 }
050
051 /**
052 * Returns the x-value.
053 *
054 * @return The x-value.
055 */
056 public Number getXValue()
057 {
058 return _xValue;
059 }
060
061 /**
062 * Returns the y-value.
063 *
064 * @return The y-value.
065 */
066 public Number getYValue()
067 {
068 return _yValue;
069 }
070
071 /**
072 * Sets the y-value for this data item.
073 *
074 * @param yValue the new y-value.
075 */
076 public void setYValue(Number yValue)
077 {
078 assert yValue != null;
079 _yValue = yValue;
080 }
081
082 /**
083 * Sets the x-value for this data item.
084 *
085 * @param xValue the new x-value.
086 */
087 public void setXValue(Number xValue)
088 {
089 assert xValue != null;
090 _xValue = xValue;
091 }
092
093 /**
094 * Tests if this object is equal to another.
095 *
096 * @param obj the object to test against for equality (<code>null</code>
097 * permitted).
098 *
099 * @return A boolean.
100 */
101 public boolean equals(Object obj)
102 {
103 if (this == obj) return true;
104 if (!(obj instanceof XYDataItem)) return false;
105
106 XYDataItem that = (XYDataItem) obj;
107
108 if (_xValue != null ? !_xValue.equals(that._xValue) : that._xValue != null) return false;
109 if (_yValue != null ? !_yValue.equals(that._yValue) : that._yValue != null) return false;
110
111 return true;
112 }
113
114 /**
115 * Returns a hash code.
116 *
117 * @return A hash code.
118 */
119 public int hashCode()
120 {
121 int result;
122 result = (_xValue != null ? _xValue.hashCode() : 0);
123 result = 31 * result + (_yValue != null ? _yValue.hashCode() : 0);
124 return result;
125 }
126
127 /**
128 * Returns an integer indicating the order of this object relative to
129 * another object.
130 * <p/>
131 * For the order we consider only the x-value:
132 * negative == "less-than", zero == "equal", positive == "greater-than".
133 *
134 * @param o1 the object being compared to.
135 *
136 * @return An integer indicating the order of this data pair object
137 * relative to another object.
138 */
139 public int compareTo(Object o1)
140 {
141
142 int result;
143
144 // CASE 1 : Comparing to another TimeSeriesDataPair object
145 // -------------------------------------------------------
146 if (o1 instanceof XYDataItem)
147 {
148 XYDataItem dataItem = (XYDataItem) o1;
149 double compare = _xValue.doubleValue() - dataItem.getXValue().doubleValue();
150 if (compare > 0.0)
151 result = 1;
152 else
153 {
154 if (compare < 0.0)
155 result = -1;
156 else
157 result = 0;
158 }
159 }
160 // CASE 2 : Comparing to a general object
161 // ---------------------------------------------
162 // consider time periods to be ordered after general objects
163 else
164 result = 1;
165
166 return result;
167 }
168
169
170 public String toString()
171 {
172 return String.format("[%s,%s]", _xValue.toString(), _yValue.toString());
173 }
174 }