001/* =========================================================== 002 * JFreeChart : a free chart library for the Java(tm) platform 003 * =========================================================== 004 * 005 * (C) Copyright 2000-2020, 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 * FixedMillisecond.java 029 * --------------------- 030 * (C) Copyright 2002-2016, by Object Refinery Limited. 031 * 032 * Original Author: David Gilbert (for Object Refinery Limited); 033 * Contributor(s): Ulrich Voigt; 034 * 035 * Changes 036 * ------- 037 * 19-Mar-2002 : Version 1, based on original Millisecond implementation (DG); 038 * 24-Jun-2002 : Removed unnecessary imports (DG); 039 * 10-Sep-2002 : Added getSerialIndex() method (DG); 040 * 07-Oct-2002 : Fixed errors reported by Checkstyle (DG); 041 * 13-Mar-2003 : Moved to com.jrefinery.data.time package and implemented 042 * Serializable (DG); 043 * 21-Oct-2003 : Added hashCode() method (DG); 044 * ------------- JFREECHART 1.0.x --------------------------------------------- 045 * 06-Oct-2006 : Added peg() method (DG); 046 * 28-May-2008 : Fixed immutability problem (DG); 047 * 20-Aug-2014 : Remove unnecessary Date object creation in constructors (UV); 048 * 049 */ 050 051package org.jfree.data.time; 052 053import java.io.Serializable; 054import java.util.Calendar; 055import java.util.Date; 056 057/** 058 * Wrapper for a {@code java.util.Date} object that allows it to be used 059 * as a {@link RegularTimePeriod}. This class is immutable, which is a 060 * requirement for all {@link RegularTimePeriod} subclasses. 061 */ 062public class FixedMillisecond extends RegularTimePeriod 063 implements Serializable { 064 065 /** For serialization. */ 066 private static final long serialVersionUID = 7867521484545646931L; 067 068 /** The millisecond. */ 069 private final long time; 070 071 /** 072 * Constructs a millisecond based on the current system time. 073 */ 074 public FixedMillisecond() { 075 this(System.currentTimeMillis()); 076 } 077 078 /** 079 * Constructs a millisecond. 080 * 081 * @param millisecond the millisecond (same encoding as java.util.Date). 082 */ 083 public FixedMillisecond(long millisecond) { 084 this.time = millisecond; 085 } 086 087 /** 088 * Constructs a millisecond. 089 * 090 * @param time the time ({@code null} not permitted). 091 */ 092 public FixedMillisecond(Date time) { 093 this(time.getTime()); 094 } 095 096 /** 097 * Returns the date/time (creates a new {@code Date} instance each time 098 * this method is called). 099 * 100 * @return The date/time. 101 */ 102 public Date getTime() { 103 return new Date(this.time); 104 } 105 106 /** 107 * This method is overridden to do nothing. 108 * 109 * @param calendar ignored 110 * 111 * @since 1.0.3 112 */ 113 @Override 114 public void peg(Calendar calendar) { 115 // nothing to do 116 } 117 118 /** 119 * Returns the millisecond preceding this one. 120 * 121 * @return The millisecond preceding this one. 122 */ 123 @Override 124 public RegularTimePeriod previous() { 125 RegularTimePeriod result = null; 126 long t = this.time; 127 if (t != Long.MIN_VALUE) { 128 result = new FixedMillisecond(t - 1); 129 } 130 return result; 131 } 132 133 /** 134 * Returns the millisecond following this one. 135 * 136 * @return The millisecond following this one. 137 */ 138 @Override 139 public RegularTimePeriod next() { 140 RegularTimePeriod result = null; 141 long t = this.time; 142 if (t != Long.MAX_VALUE) { 143 result = new FixedMillisecond(t + 1); 144 } 145 return result; 146 } 147 148 /** 149 * Tests the equality of this object against an arbitrary Object. 150 * 151 * @param object the object to compare 152 * 153 * @return A boolean. 154 */ 155 @Override 156 public boolean equals(Object object) { 157 if (object instanceof FixedMillisecond) { 158 FixedMillisecond m = (FixedMillisecond) object; 159 return this.time == m.getFirstMillisecond(); 160 } 161 else { 162 return false; 163 } 164 165 } 166 167 /** 168 * Returns a hash code for this object instance. 169 * 170 * @return A hash code. 171 */ 172 @Override 173 public int hashCode() { 174 return (int) this.time; 175 } 176 177 /** 178 * Returns an integer indicating the order of this Millisecond object 179 * relative to the specified 180 * object: negative == before, zero == same, positive == after. 181 * 182 * @param o1 the object to compare. 183 * 184 * @return negative == before, zero == same, positive == after. 185 */ 186 @Override 187 public int compareTo(Object o1) { 188 189 int result; 190 long difference; 191 192 // CASE 1 : Comparing to another Second object 193 // ------------------------------------------- 194 if (o1 instanceof FixedMillisecond) { 195 FixedMillisecond t1 = (FixedMillisecond) o1; 196 difference = this.time - t1.time; 197 if (difference > 0) { 198 result = 1; 199 } 200 else { 201 if (difference < 0) { 202 result = -1; 203 } 204 else { 205 result = 0; 206 } 207 } 208 } 209 210 // CASE 2 : Comparing to another TimePeriod object 211 // ----------------------------------------------- 212 else if (o1 instanceof RegularTimePeriod) { 213 // more difficult case - evaluate later... 214 result = 0; 215 } 216 217 // CASE 3 : Comparing to a non-TimePeriod object 218 // --------------------------------------------- 219 else { 220 // consider time periods to be ordered after general objects 221 result = 1; 222 } 223 224 return result; 225 226 } 227 228 /** 229 * Returns the first millisecond of the time period. 230 * 231 * @return The first millisecond of the time period. 232 */ 233 @Override 234 public long getFirstMillisecond() { 235 return this.time; 236 } 237 238 239 /** 240 * Returns the first millisecond of the time period. 241 * 242 * @param calendar the calendar. 243 * 244 * @return The first millisecond of the time period. 245 */ 246 @Override 247 public long getFirstMillisecond(Calendar calendar) { 248 return this.time; 249 } 250 251 /** 252 * Returns the last millisecond of the time period. 253 * 254 * @return The last millisecond of the time period. 255 */ 256 @Override 257 public long getLastMillisecond() { 258 return this.time; 259 } 260 261 /** 262 * Returns the last millisecond of the time period. 263 * 264 * @param calendar the calendar. 265 * 266 * @return The last millisecond of the time period. 267 */ 268 @Override 269 public long getLastMillisecond(Calendar calendar) { 270 return this.time; 271 } 272 273 /** 274 * Returns the millisecond closest to the middle of the time period. 275 * 276 * @return The millisecond closest to the middle of the time period. 277 */ 278 @Override 279 public long getMiddleMillisecond() { 280 return this.time; 281 } 282 283 /** 284 * Returns the millisecond closest to the middle of the time period. 285 * 286 * @param calendar the calendar. 287 * 288 * @return The millisecond closest to the middle of the time period. 289 */ 290 @Override 291 public long getMiddleMillisecond(Calendar calendar) { 292 return this.time; 293 } 294 295 /** 296 * Returns a serial index number for the millisecond. 297 * 298 * @return The serial index number. 299 */ 300 @Override 301 public long getSerialIndex() { 302 return this.time; 303 } 304 305}