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. * TimeSeriesDataItem.java
  28. * -----------------------
  29. * (C) Copyright 2001-2005, by Object Refinery Limited.
  30. *
  31. * Original Author: David Gilbert (for Object Refinery Limited);
  32. * Contributor(s): -;
  33. *
  34. * $Id: TimeSeriesDataItem.java,v 1.4 2005/03/09 22:00:49 mungady Exp $
  35. *
  36. * Changes
  37. * -------
  38. * 11-Oct-2001 : Version 1 (DG);
  39. * 15-Nov-2001 : Updated Javadoc comments (DG);
  40. * 29-Nov-2001 : Added cloning (DG);
  41. * 24-Jun-2002 : Removed unnecessary import (DG);
  42. * 07-Oct-2002 : Fixed errors reported by Checkstyle (DG);
  43. * 13-Mar-2003 : Renamed TimeSeriesDataPair --> TimeSeriesDataItem, moved to
  44. * com.jrefinery.data.time package, implemented Serializable (DG)
  45. */
  46. package org.jfree.data.time;
  47. import java.io.Serializable;
  48. /**
  49. * Represents one data item in a time series.
  50. * <P>
  51. * The time period can be any of the following:
  52. * <ul>
  53. * <li>{@link Year}</li>
  54. * <li>{@link Quarter}</li>
  55. * <li>{@link Month}</li>
  56. * <li>{@link Week}</li>
  57. * <li>{@link Day}</li>
  58. * <li>{@link Hour}</li>
  59. * <li>{@link Minute}</li>
  60. * <li>{@link Second}</li>
  61. * <li>{@link Millisecond}</li>
  62. * <li>{@link FixedMillisecond}</li>
  63. * </ul>
  64. *
  65. * The time period is an immutable property of the data item. Data items will
  66. * often be sorted within a list, and allowing the time period to be changed
  67. * could destroy the sort order.
  68. * <P>
  69. * Implements the <code>Comparable</code> interface so that standard Java
  70. * sorting can be used to keep the data items in order.
  71. *
  72. */
  73. public class TimeSeriesDataItem implements Cloneable, Comparable, Serializable {
  74. /** The time period. */
  75. private RegularTimePeriod period;
  76. /** The value associated with the time period. */
  77. private Number value;
  78. /**
  79. * Constructs a new data item that associates a value with a time period.
  80. *
  81. * @param period the time period (<code>null</code> not permitted).
  82. * @param value the value (<code>null</code> permitted).
  83. */
  84. public TimeSeriesDataItem(RegularTimePeriod period, Number value) {
  85. if (period == null) {
  86. throw new IllegalArgumentException("Null 'period' argument.");
  87. }
  88. this.period = period;
  89. this.value = value;
  90. }
  91. /**
  92. * Constructs a new data item that associates a value with a time period.
  93. *
  94. * @param period the time period (<code>null</code> not permitted).
  95. * @param value the value associated with the time period.
  96. */
  97. public TimeSeriesDataItem(RegularTimePeriod period, double value) {
  98. this(period, new Double(value));
  99. }
  100. /**
  101. * Returns the time period.
  102. *
  103. * @return The time period (never <code>null</code>).
  104. */
  105. public RegularTimePeriod getPeriod() {
  106. return this.period;
  107. }
  108. /**
  109. * Returns the value.
  110. *
  111. * @return The value (<code>null</code> possible).
  112. */
  113. public Number getValue() {
  114. return this.value;
  115. }
  116. /**
  117. * Sets the value for this data item.
  118. *
  119. * @param value the value (<code>null</code> permitted).
  120. */
  121. public void setValue(Number value) {
  122. this.value = value;
  123. }
  124. /**
  125. * Tests this object for equality with an arbitrary object.
  126. *
  127. * @param o the other object.
  128. *
  129. * @return A boolean.
  130. */
  131. public boolean equals(Object o) {
  132. if (this == o) {
  133. return true;
  134. }
  135. if (!(o instanceof TimeSeriesDataItem)) {
  136. return false;
  137. }
  138. TimeSeriesDataItem timeSeriesDataItem = (TimeSeriesDataItem) o;
  139. if (this.period != null) {
  140. if (!this.period.equals(timeSeriesDataItem.period)) {
  141. return false;
  142. }
  143. }
  144. else if (timeSeriesDataItem.period != null) {
  145. return false;
  146. }
  147. if (this.value != null) {
  148. if (!this.value.equals(timeSeriesDataItem.value)) {
  149. return false;
  150. }
  151. }
  152. else if (timeSeriesDataItem.value != null) {
  153. return false;
  154. }
  155. return true;
  156. }
  157. /**
  158. * Returns a hash code.
  159. *
  160. * @return A hash code.
  161. */
  162. public int hashCode() {
  163. int result;
  164. result = (this.period != null ? this.period.hashCode() : 0);
  165. result = 29 * result + (this.value != null ? this.value.hashCode() : 0);
  166. return result;
  167. }
  168. /**
  169. * Returns an integer indicating the order of this data pair object
  170. * relative to another object.
  171. * <P>
  172. * For the order we consider only the timing:
  173. * negative == before, zero == same, positive == after.
  174. *
  175. * @param o1 The object being compared to.
  176. *
  177. * @return An integer indicating the order of the data item object
  178. * relative to another object.
  179. */
  180. public int compareTo(Object o1) {
  181. int result;
  182. // CASE 1 : Comparing to another TimeSeriesDataItem object
  183. // -------------------------------------------------------
  184. if (o1 instanceof TimeSeriesDataItem) {
  185. TimeSeriesDataItem datapair = (TimeSeriesDataItem) o1;
  186. result = getPeriod().compareTo(datapair.getPeriod());
  187. }
  188. // CASE 2 : Comparing to a general object
  189. // ---------------------------------------------
  190. else {
  191. // consider time periods to be ordered after general objects
  192. result = 1;
  193. }
  194. return result;
  195. }
  196. /**
  197. * Clones the data item. Note: there is no need to clone the period or
  198. * value since they are immutable classes.
  199. *
  200. * @return A clone of the data item.
  201. */
  202. public Object clone() {
  203. Object clone = null;
  204. try {
  205. clone = super.clone();
  206. }
  207. catch (CloneNotSupportedException e) { // won't get here...
  208. e.printStackTrace();
  209. }
  210. return clone;
  211. }
  212. }