001/* =========================================================== 002 * JFreeChart : a free chart library for the Java(tm) platform 003 * =========================================================== 004 * 005 * (C) Copyright 2000-2017, 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, Locale) 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 date-time ({@code null} not permitted). 159 * @param zone the time zone ({@code null} not permitted). 160 * @param locale the locale ({@code null} not permitted). 161 * 162 * @since 1.0.13 163 */ 164 public Millisecond(Date time, TimeZone zone, Locale locale) { 165 Calendar calendar = Calendar.getInstance(zone, locale); 166 calendar.setTime(time); 167 this.millisecond = calendar.get(Calendar.MILLISECOND); 168 this.second = (byte) calendar.get(Calendar.SECOND); 169 this.minute = (byte) calendar.get(Calendar.MINUTE); 170 this.hour = (byte) calendar.get(Calendar.HOUR_OF_DAY); 171 this.day = new Day(time, zone, locale); 172 peg(calendar); 173 } 174 175 /** 176 * Returns the second. 177 * 178 * @return The second. 179 */ 180 public Second getSecond() { 181 return new Second(this.second, this.minute, this.hour, 182 this.day.getDayOfMonth(), this.day.getMonth(), 183 this.day.getYear()); 184 } 185 186 /** 187 * Returns the millisecond. 188 * 189 * @return The millisecond. 190 */ 191 public long getMillisecond() { 192 return this.millisecond; 193 } 194 195 /** 196 * Returns the first millisecond of the second. This will be determined 197 * relative to the time zone specified in the constructor, or in the 198 * calendar instance passed in the most recent call to the 199 * {@link #peg(Calendar)} method. 200 * 201 * @return The first millisecond of the second. 202 * 203 * @see #getLastMillisecond() 204 */ 205 @Override 206 public long getFirstMillisecond() { 207 return this.firstMillisecond; 208 } 209 210 /** 211 * Returns the last millisecond of the second. This will be 212 * determined relative to the time zone specified in the constructor, or 213 * in the calendar instance passed in the most recent call to the 214 * {@link #peg(Calendar)} method. 215 * 216 * @return The last millisecond of the second. 217 * 218 * @see #getFirstMillisecond() 219 */ 220 @Override 221 public long getLastMillisecond() { 222 return this.firstMillisecond; 223 } 224 225 /** 226 * Recalculates the start date/time and end date/time for this time period 227 * relative to the supplied calendar (which incorporates a time zone). 228 * 229 * @param calendar the calendar ({@code null} not permitted). 230 * 231 * @since 1.0.3 232 */ 233 @Override 234 public void peg(Calendar calendar) { 235 this.firstMillisecond = getFirstMillisecond(calendar); 236 } 237 238 /** 239 * Returns the millisecond preceding this one. 240 * 241 * @return The millisecond preceding this one. 242 */ 243 @Override 244 public RegularTimePeriod previous() { 245 RegularTimePeriod result = null; 246 if (this.millisecond != FIRST_MILLISECOND_IN_SECOND) { 247 result = new Millisecond(this.millisecond - 1, getSecond()); 248 } 249 else { 250 Second previous = (Second) getSecond().previous(); 251 if (previous != null) { 252 result = new Millisecond(LAST_MILLISECOND_IN_SECOND, previous); 253 } 254 } 255 return result; 256 } 257 258 /** 259 * Returns the millisecond following this one. 260 * 261 * @return The millisecond following this one. 262 */ 263 @Override 264 public RegularTimePeriod next() { 265 RegularTimePeriod result = null; 266 if (this.millisecond != LAST_MILLISECOND_IN_SECOND) { 267 result = new Millisecond(this.millisecond + 1, getSecond()); 268 } 269 else { 270 Second next = (Second) getSecond().next(); 271 if (next != null) { 272 result = new Millisecond(FIRST_MILLISECOND_IN_SECOND, next); 273 } 274 } 275 return result; 276 } 277 278 /** 279 * Returns a serial index number for the millisecond. 280 * 281 * @return The serial index number. 282 */ 283 @Override 284 public long getSerialIndex() { 285 long hourIndex = this.day.getSerialIndex() * 24L + this.hour; 286 long minuteIndex = hourIndex * 60L + this.minute; 287 long secondIndex = minuteIndex * 60L + this.second; 288 return secondIndex * 1000L + this.millisecond; 289 } 290 291 /** 292 * Tests the equality of this object against an arbitrary Object. 293 * <P> 294 * This method will return true ONLY if the object is a Millisecond object 295 * representing the same millisecond as this instance. 296 * 297 * @param obj the object to compare 298 * 299 * @return {@code true} if milliseconds and seconds of this and object 300 * are the same. 301 */ 302 @Override 303 public boolean equals(Object obj) { 304 if (obj == this) { 305 return true; 306 } 307 if (!(obj instanceof Millisecond)) { 308 return false; 309 } 310 Millisecond that = (Millisecond) obj; 311 if (this.millisecond != that.millisecond) { 312 return false; 313 } 314 if (this.second != that.second) { 315 return false; 316 } 317 if (this.minute != that.minute) { 318 return false; 319 } 320 if (this.hour != that.hour) { 321 return false; 322 } 323 if (!this.day.equals(that.day)) { 324 return false; 325 } 326 return true; 327 } 328 329 /** 330 * Returns a hash code for this object instance. The approach described by 331 * Joshua Bloch in "Effective Java" has been used here: 332 * <p> 333 * {@code http://developer.java.sun.com/developer/Books/effectivejava 334 * /Chapter3.pdf} 335 * 336 * @return A hashcode. 337 */ 338 @Override 339 public int hashCode() { 340 int result = 17; 341 result = 37 * result + this.millisecond; 342 result = 37 * result + getSecond().hashCode(); 343 return result; 344 } 345 346 /** 347 * Returns an integer indicating the order of this Millisecond object 348 * relative to the specified object: 349 * 350 * negative == before, zero == same, positive == after. 351 * 352 * @param obj the object to compare 353 * 354 * @return negative == before, zero == same, positive == after. 355 */ 356 @Override 357 public int compareTo(Object obj) { 358 int result; 359 long difference; 360 361 // CASE 1 : Comparing to another Second object 362 // ------------------------------------------- 363 if (obj instanceof Millisecond) { 364 Millisecond ms = (Millisecond) obj; 365 difference = getFirstMillisecond() - ms.getFirstMillisecond(); 366 if (difference > 0) { 367 result = 1; 368 } 369 else { 370 if (difference < 0) { 371 result = -1; 372 } 373 else { 374 result = 0; 375 } 376 } 377 } 378 379 // CASE 2 : Comparing to another TimePeriod object 380 // ----------------------------------------------- 381 else if (obj instanceof RegularTimePeriod) { 382 RegularTimePeriod rtp = (RegularTimePeriod) obj; 383 final long thisVal = this.getFirstMillisecond(); 384 final long anotherVal = rtp.getFirstMillisecond(); 385 result = (thisVal < anotherVal ? -1 386 : (thisVal == anotherVal ? 0 : 1)); 387 } 388 389 // CASE 3 : Comparing to a non-TimePeriod object 390 // --------------------------------------------- 391 else { 392 // consider time periods to be ordered after general objects 393 result = 1; 394 } 395 396 return result; 397 } 398 399 /** 400 * Returns the first millisecond of the time period. 401 * 402 * @param calendar the calendar ({@code null} not permitted). 403 * 404 * @return The first millisecond of the time period. 405 * 406 * @throws NullPointerException if {@code calendar} is 407 * {@code null}. 408 */ 409 @Override 410 public long getFirstMillisecond(Calendar calendar) { 411 int year = this.day.getYear(); 412 int month = this.day.getMonth() - 1; 413 int d = this.day.getDayOfMonth(); 414 calendar.clear(); 415 calendar.set(year, month, d, this.hour, this.minute, this.second); 416 calendar.set(Calendar.MILLISECOND, this.millisecond); 417 return calendar.getTimeInMillis(); 418 } 419 420 /** 421 * Returns the last millisecond of the time period. 422 * 423 * @param calendar the calendar ({@code null} not permitted). 424 * 425 * @return The last millisecond of the time period. 426 * 427 * @throws NullPointerException if {@code calendar} is 428 * {@code null}. 429 */ 430 @Override 431 public long getLastMillisecond(Calendar calendar) { 432 return getFirstMillisecond(calendar); 433 } 434 435}