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 * Second.java 029 * ----------- 030 * (C) Copyright 2001-2015, 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 * 18-Dec-2001 : Changed order of parameters in constructor (DG); 039 * 19-Dec-2001 : Added a new constructor as suggested by Paul English (DG); 040 * 14-Feb-2002 : Fixed bug in Second(Date) constructor, and changed start of 041 * range to zero from one (DG); 042 * 26-Feb-2002 : Changed getStart(), getMiddle() and getEnd() methods to 043 * evaluate with reference to a particular time zone (DG); 044 * 13-Mar-2002 : Added parseSecond() method (DG); 045 * 10-Sep-2002 : Added getSerialIndex() method (DG); 046 * 07-Oct-2002 : Fixed errors reported by Checkstyle (DG); 047 * 10-Jan-2003 : Changed base class and method names (DG); 048 * 05-Mar-2003 : Fixed bug in getLastMillisecond() picked up in JUnit 049 * tests (DG); 050 * 13-Mar-2003 : Moved to com.jrefinery.data.time package and implemented 051 * Serializable (DG); 052 * 21-Oct-2003 : Added hashCode() method (DG); 053 * ------------- JFREECHART 1.0.x --------------------------------------------- 054 * 05-Oct-2006 : Updated API docs (DG); 055 * 06-Oct-2006 : Refactored to cache first and last millisecond values (DG); 056 * 16-Sep-2008 : Deprecated DEFAULT_TIME_ZONE (DG); 057 * 02-Mar-2009 : Added new constructor with Locale (DG); 058 * 05-Jul-2012 : Replaced getTime().getTime() with getTimeInMillis() (DG); 059 * 03-Jul-2013 : Use ParamChecks (DG); 060 * 061 */ 062 063package org.jfree.data.time; 064 065import java.io.Serializable; 066import java.util.Calendar; 067import java.util.Date; 068import java.util.Locale; 069import java.util.TimeZone; 070import org.jfree.chart.util.Args; 071 072/** 073 * Represents a second in a particular day. This class is immutable, which is 074 * a requirement for all {@link RegularTimePeriod} subclasses. 075 */ 076public class Second extends RegularTimePeriod implements Serializable { 077 078 /** For serialization. */ 079 private static final long serialVersionUID = -6536564190712383466L; 080 081 /** Useful constant for the first second in a minute. */ 082 public static final int FIRST_SECOND_IN_MINUTE = 0; 083 084 /** Useful constant for the last second in a minute. */ 085 public static final int LAST_SECOND_IN_MINUTE = 59; 086 087 /** The day. */ 088 private Day day; 089 090 /** The hour of the day. */ 091 private byte hour; 092 093 /** The minute. */ 094 private byte minute; 095 096 /** The second. */ 097 private byte second; 098 099 /** 100 * The first millisecond. We don't store the last millisecond, because it 101 * is always firstMillisecond + 999L. 102 */ 103 private long firstMillisecond; 104 105 /** 106 * Constructs a new Second, based on the system date/time. 107 */ 108 public Second() { 109 this(new Date()); 110 } 111 112 /** 113 * Constructs a new Second. 114 * 115 * @param second the second (0 to 59). 116 * @param minute the minute ({@code null} not permitted). 117 */ 118 public Second(int second, Minute minute) { 119 Args.requireInRange(second, "second", 120 Second.FIRST_SECOND_IN_MINUTE, Second.LAST_SECOND_IN_MINUTE); 121 Args.nullNotPermitted(minute, "minute"); 122 this.day = minute.getDay(); 123 this.hour = (byte) minute.getHourValue(); 124 this.minute = (byte) minute.getMinute(); 125 this.second = (byte) second; 126 peg(Calendar.getInstance()); 127 } 128 129 /** 130 * Creates a new second. 131 * 132 * @param second the second (0-59). 133 * @param minute the minute (0-59). 134 * @param hour the hour (0-23). 135 * @param day the day (1-31). 136 * @param month the month (1-12). 137 * @param year the year (1900-9999). 138 */ 139 public Second(int second, int minute, int hour, 140 int day, int month, int year) { 141 this(second, new Minute(minute, hour, day, month, year)); 142 } 143 144 /** 145 * Constructs a new instance from the specified date/time and the default 146 * time zone. 147 * 148 * @param time the time ({@code null} not permitted). 149 * 150 * @see #Second(Date, TimeZone, Locale) 151 */ 152 public Second(Date time) { 153 this(time, TimeZone.getDefault(), Locale.getDefault()); 154 } 155 156 /** 157 * Creates a new second based on the supplied time and time zone. 158 * 159 * @param time the time ({@code null} not permitted). 160 * @param zone the time zone ({@code null} not permitted). 161 * @param locale the locale ({@code null} not permitted). 162 * 163 * @since 1.0.13 164 */ 165 public Second(Date time, TimeZone zone, Locale locale) { 166 Calendar calendar = Calendar.getInstance(zone, locale); 167 calendar.setTime(time); 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 within the minute. 177 * 178 * @return The second (0 - 59). 179 */ 180 public int getSecond() { 181 return this.second; 182 } 183 184 /** 185 * Returns the minute. 186 * 187 * @return The minute (never {@code null}). 188 */ 189 public Minute getMinute() { 190 return new Minute(this.minute, new Hour(this.hour, this.day)); 191 } 192 193 /** 194 * Returns the first millisecond of the second. This will be determined 195 * relative to the time zone specified in the constructor, or in the 196 * calendar instance passed in the most recent call to the 197 * {@link #peg(Calendar)} method. 198 * 199 * @return The first millisecond of the second. 200 * 201 * @see #getLastMillisecond() 202 */ 203 @Override 204 public long getFirstMillisecond() { 205 return this.firstMillisecond; 206 } 207 208 /** 209 * Returns the last millisecond of the second. This will be 210 * determined relative to the time zone specified in the constructor, or 211 * in the calendar instance passed in the most recent call to the 212 * {@link #peg(Calendar)} method. 213 * 214 * @return The last millisecond of the second. 215 * 216 * @see #getFirstMillisecond() 217 */ 218 @Override 219 public long getLastMillisecond() { 220 return this.firstMillisecond + 999L; 221 } 222 223 /** 224 * Recalculates the start date/time and end date/time for this time period 225 * relative to the supplied calendar (which incorporates a time zone). 226 * 227 * @param calendar the calendar ({@code null} not permitted). 228 * 229 * @since 1.0.3 230 */ 231 @Override 232 public void peg(Calendar calendar) { 233 this.firstMillisecond = getFirstMillisecond(calendar); 234 } 235 236 /** 237 * Returns the second preceding this one. 238 * 239 * @return The second preceding this one. 240 */ 241 @Override 242 public RegularTimePeriod previous() { 243 Second result = null; 244 if (this.second != FIRST_SECOND_IN_MINUTE) { 245 result = new Second(this.second - 1, getMinute()); 246 } 247 else { 248 Minute previous = (Minute) getMinute().previous(); 249 if (previous != null) { 250 result = new Second(LAST_SECOND_IN_MINUTE, previous); 251 } 252 } 253 return result; 254 } 255 256 /** 257 * Returns the second following this one. 258 * 259 * @return The second following this one. 260 */ 261 @Override 262 public RegularTimePeriod next() { 263 Second result = null; 264 if (this.second != LAST_SECOND_IN_MINUTE) { 265 result = new Second(this.second + 1, getMinute()); 266 } 267 else { 268 Minute next = (Minute) getMinute().next(); 269 if (next != null) { 270 result = new Second(FIRST_SECOND_IN_MINUTE, next); 271 } 272 } 273 return result; 274 } 275 276 /** 277 * Returns a serial index number for the minute. 278 * 279 * @return The serial index number. 280 */ 281 @Override 282 public long getSerialIndex() { 283 long hourIndex = this.day.getSerialIndex() * 24L + this.hour; 284 long minuteIndex = hourIndex * 60L + this.minute; 285 return minuteIndex * 60L + this.second; 286 } 287 288 /** 289 * Returns the first millisecond of the minute. 290 * 291 * @param calendar the calendar/timezone ({@code null} not permitted). 292 * 293 * @return The first millisecond. 294 * 295 * @throws NullPointerException if {@code calendar} is {@code null}. 296 */ 297 @Override 298 public long getFirstMillisecond(Calendar calendar) { 299 int year = this.day.getYear(); 300 int month = this.day.getMonth() - 1; 301 int d = this.day.getDayOfMonth(); 302 calendar.clear(); 303 calendar.set(year, month, d, this.hour, this.minute, this.second); 304 calendar.set(Calendar.MILLISECOND, 0); 305 return calendar.getTimeInMillis(); 306 } 307 308 /** 309 * Returns the last millisecond of the second. 310 * 311 * @param calendar the calendar/timezone ({@code null} not permitted). 312 * 313 * @return The last millisecond. 314 * 315 * @throws NullPointerException if {@code calendar} is {@code null}. 316 */ 317 @Override 318 public long getLastMillisecond(Calendar calendar) { 319 return getFirstMillisecond(calendar) + 999L; 320 } 321 322 /** 323 * Tests the equality of this object against an arbitrary Object. 324 * <P> 325 * This method will return true ONLY if the object is a Second object 326 * representing the same second as this instance. 327 * 328 * @param obj the object to compare ({@code null} permitted). 329 * 330 * @return {@code true} if second and minute of this and the object 331 * are the same. 332 */ 333 @Override 334 public boolean equals(Object obj) { 335 if (obj == this) { 336 return true; 337 } 338 if (!(obj instanceof Second)) { 339 return false; 340 } 341 Second that = (Second) obj; 342 if (this.second != that.second) { 343 return false; 344 } 345 if (this.minute != that.minute) { 346 return false; 347 } 348 if (this.hour != that.hour) { 349 return false; 350 } 351 if (!this.day.equals(that.day)) { 352 return false; 353 } 354 return true; 355 } 356 357 /** 358 * Returns a hash code for this object instance. The approach described by 359 * Joshua Bloch in "Effective Java" has been used here: 360 * <p> 361 * {@code http://developer.java.sun.com/developer/Books/effectivejava 362 * /Chapter3.pdf} 363 * 364 * @return A hash code. 365 */ 366 @Override 367 public int hashCode() { 368 int result = 17; 369 result = 37 * result + this.second; 370 result = 37 * result + this.minute; 371 result = 37 * result + this.hour; 372 result = 37 * result + this.day.hashCode(); 373 return result; 374 } 375 376 /** 377 * Returns an integer indicating the order of this Second object relative 378 * to the specified 379 * object: negative == before, zero == same, positive == after. 380 * 381 * @param o1 the object to compare. 382 * 383 * @return negative == before, zero == same, positive == after. 384 */ 385 @Override 386 public int compareTo(Object o1) { 387 int result; 388 389 // CASE 1 : Comparing to another Second object 390 // ------------------------------------------- 391 if (o1 instanceof Second) { 392 Second s = (Second) o1; 393 if (this.firstMillisecond < s.firstMillisecond) { 394 return -1; 395 } 396 else if (this.firstMillisecond > s.firstMillisecond) { 397 return 1; 398 } 399 else { 400 return 0; 401 } 402 } 403 404 // CASE 2 : Comparing to another TimePeriod object 405 // ----------------------------------------------- 406 else if (o1 instanceof RegularTimePeriod) { 407 // more difficult case - evaluate later... 408 result = 0; 409 } 410 411 // CASE 3 : Comparing to a non-TimePeriod object 412 // --------------------------------------------- 413 else { 414 // consider time periods to be ordered after general objects 415 result = 1; 416 } 417 418 return result; 419 } 420 421 /** 422 * Creates a new instance by parsing a string. The string is assumed to 423 * be in the format "YYYY-MM-DD HH:MM:SS", perhaps with leading or trailing 424 * whitespace. 425 * 426 * @param s the string to parse. 427 * 428 * @return The second, or {@code null} if the string is not parseable. 429 */ 430 public static Second parseSecond(String s) { 431 Second result = null; 432 s = s.trim(); 433 String daystr = s.substring(0, Math.min(10, s.length())); 434 Day day = Day.parseDay(daystr); 435 if (day != null) { 436 String hmsstr = s.substring(Math.min(daystr.length() + 1, 437 s.length()), s.length()); 438 hmsstr = hmsstr.trim(); 439 440 int l = hmsstr.length(); 441 String hourstr = hmsstr.substring(0, Math.min(2, l)); 442 String minstr = hmsstr.substring(Math.min(3, l), Math.min(5, l)); 443 String secstr = hmsstr.substring(Math.min(6, l), Math.min(8, l)); 444 int hour = Integer.parseInt(hourstr); 445 446 if ((hour >= 0) && (hour <= 23)) { 447 448 int minute = Integer.parseInt(minstr); 449 if ((minute >= 0) && (minute <= 59)) { 450 451 Minute m = new Minute(minute, new Hour(hour, day)); 452 int second = Integer.parseInt(secstr); 453 if ((second >= 0) && (second <= 59)) { 454 result = new Second(second, m); 455 } 456 } 457 } 458 } 459 return result; 460 } 461 462}