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. * Millisecond.java
  26. * ----------------
  27. * (C) Copyright 2001-2005, by Object Refinery Limited.
  28. *
  29. * Original Author: David Gilbert (for Object Refinery Limited);
  30. * Contributor(s): -;
  31. *
  32. * $Id: Millisecond.java,v 1.3 2005/01/14 17:29:49 mungady Exp $
  33. *
  34. * Changes
  35. * -------
  36. * 11-Oct-2001 : Version 1 (DG);
  37. * 19-Dec-2001 : Added new constructors as suggested by Paul English (DG);
  38. * 26-Feb-2002 : Added new getStart(...) and getEnd(...) methods (DG);
  39. * 29-Mar-2002 : Fixed bug in getStart(...), getEnd(...) and compareTo(...) methods (DG);
  40. * 10-Sep-2002 : Added getSerialIndex() method (DG);
  41. * 07-Oct-2002 : Fixed errors reported by Checkstyle (DG);
  42. * 10-Jan-2003 : Changed base class and method names (DG);
  43. * 13-Mar-2003 : Moved to com.jrefinery.data.time package and implemented Serializable (DG);
  44. * 21-Oct-2003 : Added hashCode() method (DG);
  45. *
  46. */
  47. package org.jfree.data.time;
  48. import java.io.Serializable;
  49. import java.util.Calendar;
  50. import java.util.Date;
  51. import java.util.TimeZone;
  52. /**
  53. * Represents a millisecond.
  54. * <P>
  55. * This class is immutable, which is a requirement for all {@link RegularTimePeriod} subclasses.
  56. */
  57. public class Millisecond extends RegularTimePeriod implements Serializable {
  58. /** A constant for the first millisecond in a second. */
  59. public static final int FIRST_MILLISECOND_IN_SECOND = 0;
  60. /** A constant for the last millisecond in a second. */
  61. public static final int LAST_MILLISECOND_IN_SECOND = 999;
  62. /** The millisecond. */
  63. private int millisecond;
  64. /** The second. */
  65. private Second second;
  66. /**
  67. * Constructs a millisecond based on the current system time.
  68. */
  69. public Millisecond() {
  70. this(new Date());
  71. }
  72. /**
  73. * Constructs a millisecond.
  74. *
  75. * @param millisecond the millisecond (0-999).
  76. * @param second the second.
  77. */
  78. public Millisecond(int millisecond, Second second) {
  79. this.millisecond = millisecond;
  80. this.second = second;
  81. }
  82. /**
  83. * Creates a new millisecond.
  84. *
  85. * @param millisecond the millisecond (0-999).
  86. * @param second the second (0-59).
  87. * @param minute the minute (0-59).
  88. * @param hour the hour (0-23).
  89. * @param day the day (1-31).
  90. * @param month the month (1-12).
  91. * @param year the year (1900-9999).
  92. */
  93. public Millisecond(int millisecond, int second, int minute, int hour,
  94. int day, int month, int year) {
  95. this(millisecond, new Second(second, minute, hour, day, month, year));
  96. }
  97. /**
  98. * Constructs a millisecond.
  99. *
  100. * @param time the time.
  101. */
  102. public Millisecond(Date time) {
  103. this(time, RegularTimePeriod.DEFAULT_TIME_ZONE);
  104. }
  105. /**
  106. * Creates a millisecond.
  107. *
  108. * @param time the instant in time.
  109. * @param zone the time zone.
  110. */
  111. public Millisecond(Date time, TimeZone zone) {
  112. this.second = new Second(time, zone);
  113. Calendar calendar = Calendar.getInstance(zone);
  114. calendar.setTime(time);
  115. this.millisecond = calendar.get(Calendar.MILLISECOND);
  116. }
  117. /**
  118. * Returns the second.
  119. *
  120. * @return The second.
  121. */
  122. public Second getSecond() {
  123. return this.second;
  124. }
  125. /**
  126. * Returns the millisecond.
  127. *
  128. * @return The millisecond.
  129. */
  130. public long getMillisecond() {
  131. return this.millisecond;
  132. }
  133. /**
  134. * Returns the millisecond preceding this one.
  135. *
  136. * @return The millisecond preceding this one.
  137. */
  138. public RegularTimePeriod previous() {
  139. RegularTimePeriod result = null;
  140. if (this.millisecond != FIRST_MILLISECOND_IN_SECOND) {
  141. result = new Millisecond(this.millisecond - 1, this.second);
  142. }
  143. else {
  144. Second previous = (Second) this.second.previous();
  145. if (previous != null) {
  146. result = new Millisecond(LAST_MILLISECOND_IN_SECOND, previous);
  147. }
  148. }
  149. return result;
  150. }
  151. /**
  152. * Returns the millisecond following this one.
  153. *
  154. * @return The millisecond following this one.
  155. */
  156. public RegularTimePeriod next() {
  157. RegularTimePeriod result = null;
  158. if (this.millisecond != LAST_MILLISECOND_IN_SECOND) {
  159. result = new Millisecond(this.millisecond + 1, this.second);
  160. }
  161. else {
  162. Second next = (Second) this.second.next();
  163. if (next != null) {
  164. result = new Millisecond(FIRST_MILLISECOND_IN_SECOND, next);
  165. }
  166. }
  167. return result;
  168. }
  169. /**
  170. * Returns a serial index number for the millisecond.
  171. *
  172. * @return The serial index number.
  173. */
  174. public long getSerialIndex() {
  175. return this.second.getSerialIndex() * 1000L + this.millisecond;
  176. }
  177. /**
  178. * Tests the equality of this object against an arbitrary Object.
  179. * <P>
  180. * This method will return true ONLY if the object is a Millisecond object
  181. * representing the same millisecond as this instance.
  182. *
  183. * @param obj the object to compare
  184. *
  185. * @return <code>true</code> if milliseconds and seconds of this and object
  186. * are the same.
  187. */
  188. public boolean equals(Object obj) {
  189. if (obj instanceof Millisecond) {
  190. Millisecond m = (Millisecond) obj;
  191. return ((this.millisecond == m.getMillisecond())
  192. && (this.second.equals(m.getSecond())));
  193. }
  194. else {
  195. return false;
  196. }
  197. }
  198. /**
  199. * Returns a hash code for this object instance.
  200. * <p>
  201. * The approach described by Joshua Bloch in "Effective Java" has been used here:
  202. * <p>
  203. * <code>http://developer.java.sun.com/developer/Books/effectivejava/Chapter3.pdf</code>
  204. *
  205. * @return A hashcode.
  206. */
  207. public int hashCode() {
  208. int result = 17;
  209. result = 37 * result + this.millisecond;
  210. result = 37 * result + this.second.hashCode();
  211. return result;
  212. }
  213. /**
  214. * Returns an integer indicating the order of this Millisecond object
  215. * relative to the specified object:
  216. *
  217. * negative == before, zero == same, positive == after.
  218. *
  219. * @param obj the object to compare
  220. *
  221. * @return negative == before, zero == same, positive == after.
  222. */
  223. public int compareTo(Object obj) {
  224. int result;
  225. long difference;
  226. // CASE 1 : Comparing to another Second object
  227. // -------------------------------------------
  228. if (obj instanceof Millisecond) {
  229. Millisecond ms = (Millisecond) obj;
  230. difference = getFirstMillisecond() - ms.getFirstMillisecond();
  231. if (difference > 0) {
  232. result = 1;
  233. }
  234. else {
  235. if (difference < 0) {
  236. result = -1;
  237. }
  238. else {
  239. result = 0;
  240. }
  241. }
  242. }
  243. // CASE 2 : Comparing to another TimePeriod object
  244. // -----------------------------------------------
  245. else if (obj instanceof RegularTimePeriod) {
  246. // more difficult case - evaluate later...
  247. result = 0;
  248. }
  249. // CASE 3 : Comparing to a non-TimePeriod object
  250. // ---------------------------------------------
  251. else {
  252. // consider time periods to be ordered after general objects
  253. result = 1;
  254. }
  255. return result;
  256. }
  257. /**
  258. * Returns the first millisecond of the time period.
  259. *
  260. * @return The first millisecond of the time period.
  261. */
  262. public long getFirstMillisecond() {
  263. return this.second.getFirstMillisecond() + this.millisecond;
  264. }
  265. /**
  266. * Returns the first millisecond of the time period.
  267. *
  268. * @param calendar the calendar.
  269. *
  270. * @return The first millisecond of the time period.
  271. */
  272. public long getFirstMillisecond(Calendar calendar) {
  273. return this.second.getFirstMillisecond(calendar) + this.millisecond;
  274. }
  275. /**
  276. * Returns the last millisecond of the time period.
  277. *
  278. * @return The last millisecond of the time period.
  279. */
  280. public long getLastMillisecond() {
  281. return this.second.getFirstMillisecond() + this.millisecond;
  282. }
  283. /**
  284. * Returns the last millisecond of the time period.
  285. *
  286. * @param calendar the calendar.
  287. *
  288. * @return The last millisecond of the time period.
  289. */
  290. public long getLastMillisecond(Calendar calendar) {
  291. return this.second.getFirstMillisecond(calendar) + this.millisecond;
  292. }
  293. }