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 under the terms
  10. * of the GNU Lesser General Public License as published by the Free Software Foundation;
  11. * either version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
  14. * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  15. * See the GNU Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public License along with this
  18. * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
  19. * Boston, MA 02111-1307, USA.
  20. *
  21. * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
  22. * in the United States and other countries.]
  23. *
  24. * ---------------------
  25. * SimpleTimePeriod.java
  26. * ---------------------
  27. * (C) Copyright 2002-2005, by Object Refinery Limited and Contributors.
  28. *
  29. * Original Author: David Gilbert (for Object Refinery Limited);
  30. * Contributor(s): -;
  31. *
  32. * $Id: SimpleTimePeriod.java,v 1.2 2005/01/28 13:14:13 mungady Exp $
  33. *
  34. * Changes
  35. * -------
  36. * 07-Oct-2002 : Added Javadocs (DG);
  37. * 10-Jan-2003 : Renamed TimeAllocation --> SimpleTimePeriod (DG);
  38. * 13-Mar-2003 : Added equals() method, and Serializable interface (DG);
  39. * 21-Oct-2003 : Added hashCode() method (DG);
  40. * 27-Jan-2005 : Implemented Comparable, to enable this class to be used
  41. * in the TimeTableXYDataset class (DG);
  42. *
  43. */
  44. package org.jfree.data.time;
  45. import java.io.Serializable;
  46. import java.util.Date;
  47. /**
  48. * An arbitrary period of time, measured to millisecond precision using
  49. * <code>java.util.Date</code>.
  50. * <p>
  51. * This class is intentionally immutable (that is, once constructed, you cannot
  52. * alter the start and end attributes).
  53. */
  54. public class SimpleTimePeriod implements TimePeriod, Comparable, Serializable {
  55. /** The start date/time. */
  56. private Date start;
  57. /** The end date/time. */
  58. private Date end;
  59. /**
  60. * Creates a new time allocation.
  61. *
  62. * @param start the start date/time in milliseconds.
  63. * @param end the end date/time in milliseconds.
  64. */
  65. public SimpleTimePeriod(long start, long end) {
  66. this(new Date(start), new Date(end));
  67. }
  68. /**
  69. * Creates a new time allocation.
  70. *
  71. * @param start the start date/time (<code>null</code> not permitted).
  72. * @param end the end date/time (<code>null</code> not permitted).
  73. */
  74. public SimpleTimePeriod(Date start, Date end) {
  75. if (start.getTime() > end.getTime()) {
  76. throw new IllegalArgumentException("Requires end >= start.");
  77. }
  78. this.start = start;
  79. this.end = end;
  80. }
  81. /**
  82. * Returns the start date/time.
  83. *
  84. * @return The start date/time (never <code>null</code>).
  85. */
  86. public Date getStart() {
  87. return this.start;
  88. }
  89. /**
  90. * Returns the end date/time.
  91. *
  92. * @return The end date/time (never <code>null</code>).
  93. */
  94. public Date getEnd() {
  95. return this.end;
  96. }
  97. /**
  98. * Tests this time period instance for equality with an arbitrary object.
  99. * The object is considered equal if it is an instance of {@link TimePeriod}
  100. * and it has the same start and end dates.
  101. *
  102. * @param obj the other object (<code>null</code> permitted).
  103. *
  104. * @return A boolean.
  105. */
  106. public boolean equals(Object obj) {
  107. if (obj == this) {
  108. return true;
  109. }
  110. if (!(obj instanceof TimePeriod)) {
  111. return false;
  112. }
  113. TimePeriod that = (TimePeriod) obj;
  114. if (!this.start.equals(that.getStart())) {
  115. return false;
  116. }
  117. if (!this.end.equals(that.getEnd())) {
  118. return false;
  119. }
  120. return true;
  121. }
  122. /**
  123. * Returns an integer that indicates the relative ordering of two
  124. * time periods.
  125. *
  126. * @param obj the object (<code>null</code> not permitted).
  127. *
  128. * @return An integer.
  129. *
  130. * @throws ClassCastException if <code>obj</code> is not an instance of
  131. * {@link TimePeriod}.
  132. */
  133. public int compareTo(Object obj) {
  134. TimePeriod that = (TimePeriod) obj;
  135. long t0 = this.getStart().getTime();
  136. long t1 = this.getEnd().getTime();
  137. long m0 = t0 + (t1 - t0) / 2L;
  138. long t2 = that.getStart().getTime();
  139. long t3 = that.getEnd().getTime();
  140. long m1 = t2 + (t3 - t2) / 2L;
  141. if (m0 < m1) {
  142. return -1;
  143. }
  144. else if (m0 > m1) {
  145. return 1;
  146. }
  147. else {
  148. if (t0 < t2) {
  149. return -1;
  150. }
  151. else if (t0 > t2) {
  152. return 1;
  153. }
  154. else {
  155. if (t1 < t3) {
  156. return -1;
  157. }
  158. else if (t1 > t3) {
  159. return 1;
  160. }
  161. else {
  162. return 0;
  163. }
  164. }
  165. }
  166. }
  167. /**
  168. * Returns a hash code for this object instance. The approach described by
  169. * Joshua Bloch in "Effective Java" has been used here - see:
  170. * <p>
  171. * <code>http://developer.java.sun.com/
  172. * developer/Books/effectivejava/Chapter3.pdf</code>
  173. *
  174. * @return A hash code.
  175. */
  176. public int hashCode() {
  177. int result = 17;
  178. result = 37 * result + this.start.hashCode();
  179. result = 37 * result + this.end.hashCode();
  180. return result;
  181. }
  182. }