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. * FixedMillisecond.java
  26. * ---------------------
  27. * (C) Copyright 2002-2005 by Object Refinery Limited.
  28. *
  29. * Original Author: David Gilbert (for Object Refinery Limited);
  30. * Contributor(s): -;
  31. *
  32. * $Id: FixedMillisecond.java,v 1.2 2005/01/14 17:29:49 mungady Exp $
  33. *
  34. * Changes
  35. * -------
  36. * 19-Mar-2002 : Version 1, based on original Millisecond implementation (DG);
  37. * 24-Jun-2002 : Removed unnecessary imports (DG);
  38. * 10-Sep-2002 : Added getSerialIndex() method (DG);
  39. * 07-Oct-2002 : Fixed errors reported by Checkstyle (DG);
  40. * 13-Mar-2003 : Moved to com.jrefinery.data.time package and implemented Serializable (DG);
  41. * 21-Oct-2003 : Added hashCode() method (DG);
  42. *
  43. */
  44. package org.jfree.data.time;
  45. import java.io.Serializable;
  46. import java.util.Calendar;
  47. import java.util.Date;
  48. /**
  49. * Wrapper for a <code>java.util.Date</code> object that allows it to be used
  50. * as a {@link RegularTimePeriod}.
  51. * <P>
  52. * This class is immutable, which is a requirement for all
  53. * {@link RegularTimePeriod} subclasses.
  54. */
  55. public class FixedMillisecond extends RegularTimePeriod implements Serializable {
  56. /** The millisecond. */
  57. private Date time;
  58. /**
  59. * Constructs a millisecond based on the current system time.
  60. */
  61. public FixedMillisecond() {
  62. this(new Date());
  63. }
  64. /**
  65. * Constructs a millisecond.
  66. *
  67. * @param millisecond the millisecond (same encoding as java.util.Date).
  68. */
  69. public FixedMillisecond(long millisecond) {
  70. this(new Date(millisecond));
  71. }
  72. /**
  73. * Constructs a millisecond.
  74. *
  75. * @param time the time.
  76. */
  77. public FixedMillisecond(Date time) {
  78. this.time = time;
  79. }
  80. /**
  81. * Returns the date/time.
  82. *
  83. * @return The date/time.
  84. */
  85. public Date getTime() {
  86. return this.time;
  87. }
  88. /**
  89. * Returns the millisecond preceding this one.
  90. *
  91. * @return The millisecond preceding this one.
  92. */
  93. public RegularTimePeriod previous() {
  94. RegularTimePeriod result = null;
  95. long t = this.time.getTime();
  96. if (t != Long.MIN_VALUE) {
  97. result = new FixedMillisecond(t - 1);
  98. }
  99. return result;
  100. }
  101. /**
  102. * Returns the millisecond following this one.
  103. *
  104. * @return The millisecond following this one.
  105. */
  106. public RegularTimePeriod next() {
  107. RegularTimePeriod result = null;
  108. long t = this.time.getTime();
  109. if (t != Long.MAX_VALUE) {
  110. result = new FixedMillisecond(t + 1);
  111. }
  112. return result;
  113. }
  114. /**
  115. * Tests the equality of this object against an arbitrary Object.
  116. *
  117. * @param object the object to compare
  118. *
  119. * @return A boolean.
  120. */
  121. public boolean equals(Object object) {
  122. if (object instanceof FixedMillisecond) {
  123. FixedMillisecond m = (FixedMillisecond) object;
  124. return this.time.equals(m.getTime());
  125. }
  126. else {
  127. return false;
  128. }
  129. }
  130. /**
  131. * Returns a hash code for this object instance.
  132. *
  133. * @return A hash code.
  134. */
  135. public int hashCode() {
  136. return this.time.hashCode();
  137. }
  138. /**
  139. * Returns an integer indicating the order of this Millisecond object
  140. * relative to the specified
  141. * object: negative == before, zero == same, positive == after.
  142. *
  143. * @param o1 the object to compare.
  144. *
  145. * @return negative == before, zero == same, positive == after.
  146. */
  147. public int compareTo(Object o1) {
  148. int result;
  149. long difference;
  150. // CASE 1 : Comparing to another Second object
  151. // -------------------------------------------
  152. if (o1 instanceof FixedMillisecond) {
  153. FixedMillisecond t1 = (FixedMillisecond) o1;
  154. difference = this.time.getTime() - t1.time.getTime();
  155. if (difference > 0) {
  156. result = 1;
  157. }
  158. else {
  159. if (difference < 0) {
  160. result = -1;
  161. }
  162. else {
  163. result = 0;
  164. }
  165. }
  166. }
  167. // CASE 2 : Comparing to another TimePeriod object
  168. // -----------------------------------------------
  169. else if (o1 instanceof RegularTimePeriod) {
  170. // more difficult case - evaluate later...
  171. result = 0;
  172. }
  173. // CASE 3 : Comparing to a non-TimePeriod object
  174. // ---------------------------------------------
  175. else {
  176. // consider time periods to be ordered after general objects
  177. result = 1;
  178. }
  179. return result;
  180. }
  181. /**
  182. * Returns the first millisecond of the time period.
  183. *
  184. * @return The first millisecond of the time period.
  185. */
  186. public long getFirstMillisecond() {
  187. return this.time.getTime();
  188. }
  189. /**
  190. * Returns the first millisecond of the time period.
  191. *
  192. * @param calendar the calendar.
  193. *
  194. * @return The first millisecond of the time period.
  195. */
  196. public long getFirstMillisecond(Calendar calendar) {
  197. return this.time.getTime();
  198. }
  199. /**
  200. * Returns the last millisecond of the time period.
  201. *
  202. * @return The last millisecond of the time period.
  203. */
  204. public long getLastMillisecond() {
  205. return this.time.getTime();
  206. }
  207. /**
  208. * Returns the last millisecond of the time period.
  209. *
  210. * @param calendar the calendar.
  211. *
  212. * @return The last millisecond of the time period.
  213. */
  214. public long getLastMillisecond(Calendar calendar) {
  215. return this.time.getTime();
  216. }
  217. /**
  218. * Returns the millisecond closest to the middle of the time period.
  219. *
  220. * @return The millisecond closest to the middle of the time period.
  221. */
  222. public long getMiddleMillisecond() {
  223. return this.time.getTime();
  224. }
  225. /**
  226. * Returns the millisecond closest to the middle of the time period.
  227. *
  228. * @param calendar the calendar.
  229. *
  230. * @return the millisecond closest to the middle of the time period.
  231. */
  232. public long getMiddleMillisecond(Calendar calendar) {
  233. return this.time.getTime();
  234. }
  235. /**
  236. * Returns a serial index number for the millisecond.
  237. *
  238. * @return The serial index number.
  239. */
  240. public long getSerialIndex() {
  241. return this.time.getTime();
  242. }
  243. }