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