001/* =========================================================== 002 * JFreeChart : a free chart library for the Java(tm) platform 003 * =========================================================== 004 * 005 * (C) Copyright 2000-2013, by Object Refinery Limited and Contributors. 006 * 007 * Project Info: http://www.jfree.org/jfreechart/index.html 008 * 009 * This library is free software; you can redistribute it and/or modify it 010 * under the terms of the GNU Lesser General Public License as published by 011 * the Free Software Foundation; either version 2.1 of the License, or 012 * (at your option) any later version. 013 * 014 * This library is distributed in the hope that it will be useful, but 015 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 016 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 017 * License for more details. 018 * 019 * You should have received a copy of the GNU Lesser General Public 020 * License along with this library; if not, write to the Free Software 021 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 022 * USA. 023 * 024 * [Oracle and Java are registered trademarks of Oracle and/or its affiliates. 025 * Other names may be trademarks of their respective owners.] 026 * 027 * ---------------- 028 * Millisecond.java 029 * ---------------- 030 * (C) Copyright 2001-2012, by Object Refinery Limited. 031 * 032 * Original Author: David Gilbert (for Object Refinery Limited); 033 * Contributor(s): -; 034 * 035 * Changes 036 * ------- 037 * 11-Oct-2001 : Version 1 (DG); 038 * 19-Dec-2001 : Added new constructors as suggested by Paul English (DG); 039 * 26-Feb-2002 : Added new getStart() and getEnd() methods (DG); 040 * 29-Mar-2002 : Fixed bug in getStart(), getEnd() and compareTo() methods (DG); 041 * 10-Sep-2002 : Added getSerialIndex() method (DG); 042 * 07-Oct-2002 : Fixed errors reported by Checkstyle (DG); 043 * 10-Jan-2003 : Changed base class and method names (DG); 044 * 13-Mar-2003 : Moved to com.jrefinery.data.time package and implemented 045 * Serializable (DG); 046 * 21-Oct-2003 : Added hashCode() method (DG); 047 * ------------- JFREECHART 1.0.x --------------------------------------------- 048 * 05-Oct-2006 : Updated API docs (DG); 049 * 06-Oct-2006 : Refactored to cache first and last millisecond values (DG); 050 * 04-Apr-2007 : In Millisecond(Date, TimeZone), peg milliseconds to the 051 * specified zone (DG); 052 * 06-Jun-2008 : Added handling for general RegularTimePeriod in compareTo() 053 * method: 054 * see http://www.jfree.org/phpBB2/viewtopic.php?t=24805 (DG); 055 * 16-Sep-2008 : Deprecated DEFAULT_TIME_ZONE (DG); 056 * 02-Mar-2009 : Added new constructor with Locale (DG); 057 * 05-Jul-2012 : Replaced getTime().getTime() with getTimeInMillis() (DG); 058 * 059 */ 060 061package org.jfree.data.time; 062 063import java.io.Serializable; 064import java.util.Calendar; 065import java.util.Date; 066import java.util.Locale; 067import java.util.TimeZone; 068 069/** 070 * Represents a millisecond. This class is immutable, which is a requirement 071 * for all {@link RegularTimePeriod} subclasses. 072 */ 073public class Millisecond extends RegularTimePeriod implements Serializable { 074 075 /** For serialization. */ 076 static final long serialVersionUID = -5316836467277638485L; 077 078 /** A constant for the first millisecond in a second. */ 079 public static final int FIRST_MILLISECOND_IN_SECOND = 0; 080 081 /** A constant for the last millisecond in a second. */ 082 public static final int LAST_MILLISECOND_IN_SECOND = 999; 083 084 /** The day. */ 085 private Day day; 086 087 /** The hour in the day. */ 088 private byte hour; 089 090 /** The minute. */ 091 private byte minute; 092 093 /** The second. */ 094 private byte second; 095 096 /** The millisecond. */ 097 private int millisecond; 098 099 /** 100 * The pegged millisecond. 101 */ 102 private long firstMillisecond; 103 104 /** 105 * Constructs a millisecond based on the current system time. 106 */ 107 public Millisecond() { 108 this(new Date()); 109 } 110 111 /** 112 * Constructs a millisecond. 113 * 114 * @param millisecond the millisecond (0-999). 115 * @param second the second. 116 */ 117 public Millisecond(int millisecond, Second second) { 118 this.millisecond = millisecond; 119 this.second = (byte) second.getSecond(); 120 this.minute = (byte) second.getMinute().getMinute(); 121 this.hour = (byte) second.getMinute().getHourValue(); 122 this.day = second.getMinute().getDay(); 123 peg(Calendar.getInstance()); 124 } 125 126 /** 127 * Creates a new millisecond. 128 * 129 * @param millisecond the millisecond (0-999). 130 * @param second the second (0-59). 131 * @param minute the minute (0-59). 132 * @param hour the hour (0-23). 133 * @param day the day (1-31). 134 * @param month the month (1-12). 135 * @param year the year (1900-9999). 136 */ 137 public Millisecond(int millisecond, int second, int minute, int hour, 138 int day, int month, int year) { 139 140 this(millisecond, new Second(second, minute, hour, day, month, year)); 141 142 } 143 144 /** 145 * Constructs a new millisecond using the default time zone. 146 * 147 * @param time the time. 148 * 149 * @see #Millisecond(Date, TimeZone) 150 */ 151 public Millisecond(Date time) { 152 this(time, TimeZone.getDefault(), Locale.getDefault()); 153 } 154 155 /** 156 * Creates a millisecond. 157 * 158 * @param time the instant in time. 159 * @param zone the time zone. 160 * 161 * @deprecated As of 1.0.13, use the constructor that specifies the locale 162 * also. 163 */ 164 public Millisecond(Date time, TimeZone zone) { 165 this(time, zone, Locale.getDefault()); 166 } 167 168 /** 169 * Creates a millisecond. 170 * 171 * @param time the date-time (<code>null</code> not permitted). 172 * @param zone the time zone (<code>null</code> not permitted). 173 * @param locale the locale (<code>null</code> not permitted). 174 * 175 * @since 1.0.13 176 */ 177 public Millisecond(Date time, TimeZone zone, Locale locale) { 178 Calendar calendar = Calendar.getInstance(zone, locale); 179 calendar.setTime(time); 180 this.millisecond = calendar.get(Calendar.MILLISECOND); 181 this.second = (byte) calendar.get(Calendar.SECOND); 182 this.minute = (byte) calendar.get(Calendar.MINUTE); 183 this.hour = (byte) calendar.get(Calendar.HOUR_OF_DAY); 184 this.day = new Day(time, zone, locale); 185 peg(calendar); 186 } 187 188 /** 189 * Returns the second. 190 * 191 * @return The second. 192 */ 193 public Second getSecond() { 194 return new Second(this.second, this.minute, this.hour, 195 this.day.getDayOfMonth(), this.day.getMonth(), 196 this.day.getYear()); 197 } 198 199 /** 200 * Returns the millisecond. 201 * 202 * @return The millisecond. 203 */ 204 public long getMillisecond() { 205 return this.millisecond; 206 } 207 208 /** 209 * Returns the first millisecond of the second. This will be determined 210 * relative to the time zone specified in the constructor, or in the 211 * calendar instance passed in the most recent call to the 212 * {@link #peg(Calendar)} method. 213 * 214 * @return The first millisecond of the second. 215 * 216 * @see #getLastMillisecond() 217 */ 218 @Override 219 public long getFirstMillisecond() { 220 return this.firstMillisecond; 221 } 222 223 /** 224 * Returns the last millisecond of the second. This will be 225 * determined relative to the time zone specified in the constructor, or 226 * in the calendar instance passed in the most recent call to the 227 * {@link #peg(Calendar)} method. 228 * 229 * @return The last millisecond of the second. 230 * 231 * @see #getFirstMillisecond() 232 */ 233 @Override 234 public long getLastMillisecond() { 235 return this.firstMillisecond; 236 } 237 238 /** 239 * Recalculates the start date/time and end date/time for this time period 240 * relative to the supplied calendar (which incorporates a time zone). 241 * 242 * @param calendar the calendar (<code>null</code> not permitted). 243 * 244 * @since 1.0.3 245 */ 246 @Override 247 public void peg(Calendar calendar) { 248 this.firstMillisecond = getFirstMillisecond(calendar); 249 } 250 251 /** 252 * Returns the millisecond preceding this one. 253 * 254 * @return The millisecond preceding this one. 255 */ 256 @Override 257 public RegularTimePeriod previous() { 258 RegularTimePeriod result = null; 259 if (this.millisecond != FIRST_MILLISECOND_IN_SECOND) { 260 result = new Millisecond(this.millisecond - 1, getSecond()); 261 } 262 else { 263 Second previous = (Second) getSecond().previous(); 264 if (previous != null) { 265 result = new Millisecond(LAST_MILLISECOND_IN_SECOND, previous); 266 } 267 } 268 return result; 269 } 270 271 /** 272 * Returns the millisecond following this one. 273 * 274 * @return The millisecond following this one. 275 */ 276 @Override 277 public RegularTimePeriod next() { 278 RegularTimePeriod result = null; 279 if (this.millisecond != LAST_MILLISECOND_IN_SECOND) { 280 result = new Millisecond(this.millisecond + 1, getSecond()); 281 } 282 else { 283 Second next = (Second) getSecond().next(); 284 if (next != null) { 285 result = new Millisecond(FIRST_MILLISECOND_IN_SECOND, next); 286 } 287 } 288 return result; 289 } 290 291 /** 292 * Returns a serial index number for the millisecond. 293 * 294 * @return The serial index number. 295 */ 296 @Override 297 public long getSerialIndex() { 298 long hourIndex = this.day.getSerialIndex() * 24L + this.hour; 299 long minuteIndex = hourIndex * 60L + this.minute; 300 long secondIndex = minuteIndex * 60L + this.second; 301 return secondIndex * 1000L + this.millisecond; 302 } 303 304 /** 305 * Tests the equality of this object against an arbitrary Object. 306 * <P> 307 * This method will return true ONLY if the object is a Millisecond object 308 * representing the same millisecond as this instance. 309 * 310 * @param obj the object to compare 311 * 312 * @return <code>true</code> if milliseconds and seconds of this and object 313 * are the same. 314 */ 315 @Override 316 public boolean equals(Object obj) { 317 if (obj == this) { 318 return true; 319 } 320 if (!(obj instanceof Millisecond)) { 321 return false; 322 } 323 Millisecond that = (Millisecond) obj; 324 if (this.millisecond != that.millisecond) { 325 return false; 326 } 327 if (this.second != that.second) { 328 return false; 329 } 330 if (this.minute != that.minute) { 331 return false; 332 } 333 if (this.hour != that.hour) { 334 return false; 335 } 336 if (!this.day.equals(that.day)) { 337 return false; 338 } 339 return true; 340 } 341 342 /** 343 * Returns a hash code for this object instance. The approach described by 344 * Joshua Bloch in "Effective Java" has been used here: 345 * <p> 346 * <code>http://developer.java.sun.com/developer/Books/effectivejava 347 * /Chapter3.pdf</code> 348 * 349 * @return A hashcode. 350 */ 351 @Override 352 public int hashCode() { 353 int result = 17; 354 result = 37 * result + this.millisecond; 355 result = 37 * result + getSecond().hashCode(); 356 return result; 357 } 358 359 /** 360 * Returns an integer indicating the order of this Millisecond object 361 * relative to the specified object: 362 * 363 * negative == before, zero == same, positive == after. 364 * 365 * @param obj the object to compare 366 * 367 * @return negative == before, zero == same, positive == after. 368 */ 369 @Override 370 public int compareTo(Object obj) { 371 int result; 372 long difference; 373 374 // CASE 1 : Comparing to another Second object 375 // ------------------------------------------- 376 if (obj instanceof Millisecond) { 377 Millisecond ms = (Millisecond) obj; 378 difference = getFirstMillisecond() - ms.getFirstMillisecond(); 379 if (difference > 0) { 380 result = 1; 381 } 382 else { 383 if (difference < 0) { 384 result = -1; 385 } 386 else { 387 result = 0; 388 } 389 } 390 } 391 392 // CASE 2 : Comparing to another TimePeriod object 393 // ----------------------------------------------- 394 else if (obj instanceof RegularTimePeriod) { 395 RegularTimePeriod rtp = (RegularTimePeriod) obj; 396 final long thisVal = this.getFirstMillisecond(); 397 final long anotherVal = rtp.getFirstMillisecond(); 398 result = (thisVal < anotherVal ? -1 399 : (thisVal == anotherVal ? 0 : 1)); 400 } 401 402 // CASE 3 : Comparing to a non-TimePeriod object 403 // --------------------------------------------- 404 else { 405 // consider time periods to be ordered after general objects 406 result = 1; 407 } 408 409 return result; 410 } 411 412 /** 413 * Returns the first millisecond of the time period. 414 * 415 * @param calendar the calendar (<code>null</code> not permitted). 416 * 417 * @return The first millisecond of the time period. 418 * 419 * @throws NullPointerException if <code>calendar</code> is 420 * <code>null</code>. 421 */ 422 @Override 423 public long getFirstMillisecond(Calendar calendar) { 424 int year = this.day.getYear(); 425 int month = this.day.getMonth() - 1; 426 int d = this.day.getDayOfMonth(); 427 calendar.clear(); 428 calendar.set(year, month, d, this.hour, this.minute, this.second); 429 calendar.set(Calendar.MILLISECOND, this.millisecond); 430 return calendar.getTimeInMillis(); 431 } 432 433 /** 434 * Returns the last millisecond of the time period. 435 * 436 * @param calendar the calendar (<code>null</code> not permitted). 437 * 438 * @return The last millisecond of the time period. 439 * 440 * @throws NullPointerException if <code>calendar</code> is 441 * <code>null</code>. 442 */ 443 @Override 444 public long getLastMillisecond(Calendar calendar) { 445 return getFirstMillisecond(calendar); 446 } 447 448}