1 50 51 package org.jfree.data.time; 52 53 import java.io.Serializable ; 54 import java.util.Calendar ; 55 import java.util.Date ; 56 import java.util.TimeZone ; 57 58 62 public class Millisecond extends RegularTimePeriod implements Serializable { 63 64 65 static final long serialVersionUID = -5316836467277638485L; 66 67 68 public static final int FIRST_MILLISECOND_IN_SECOND = 0; 69 70 71 public static final int LAST_MILLISECOND_IN_SECOND = 999; 72 73 74 private int millisecond; 75 76 77 private Second second; 78 79 82 public Millisecond() { 83 this(new Date ()); 84 } 85 86 92 public Millisecond(int millisecond, Second second) { 93 this.millisecond = millisecond; 94 this.second = second; 95 } 96 97 108 public Millisecond(int millisecond, int second, int minute, int hour, 109 int day, int month, int year) { 110 111 this(millisecond, new Second(second, minute, hour, day, month, year)); 112 113 } 114 115 120 public Millisecond(Date time) { 121 this(time, RegularTimePeriod.DEFAULT_TIME_ZONE); 122 } 123 124 130 public Millisecond(Date time, TimeZone zone) { 131 132 this.second = new Second(time, zone); 133 Calendar calendar = Calendar.getInstance(zone); 134 calendar.setTime(time); 135 this.millisecond = calendar.get(Calendar.MILLISECOND); 136 137 } 138 139 144 public Second getSecond() { 145 return this.second; 146 } 147 148 153 public long getMillisecond() { 154 return this.millisecond; 155 } 156 157 162 public RegularTimePeriod previous() { 163 164 RegularTimePeriod result = null; 165 166 if (this.millisecond != FIRST_MILLISECOND_IN_SECOND) { 167 result = new Millisecond(this.millisecond - 1, this.second); 168 } 169 else { 170 Second previous = (Second) this.second.previous(); 171 if (previous != null) { 172 result = new Millisecond(LAST_MILLISECOND_IN_SECOND, previous); 173 } 174 } 175 return result; 176 177 } 178 179 184 public RegularTimePeriod next() { 185 186 RegularTimePeriod result = null; 187 if (this.millisecond != LAST_MILLISECOND_IN_SECOND) { 188 result = new Millisecond(this.millisecond + 1, this.second); 189 } 190 else { 191 Second next = (Second) this.second.next(); 192 if (next != null) { 193 result = new Millisecond(FIRST_MILLISECOND_IN_SECOND, next); 194 } 195 } 196 return result; 197 198 } 199 200 205 public long getSerialIndex() { 206 return this.second.getSerialIndex() * 1000L + this.millisecond; 207 } 208 209 220 public boolean equals(Object obj) { 221 222 if (obj instanceof Millisecond) { 223 Millisecond m = (Millisecond) obj; 224 return ((this.millisecond == m.getMillisecond()) 225 && (this.second.equals(m.getSecond()))); 226 } 227 else { 228 return false; 229 } 230 231 } 232 233 242 public int hashCode() { 243 int result = 17; 244 result = 37 * result + this.millisecond; 245 result = 37 * result + this.second.hashCode(); 246 return result; 247 } 248 249 259 public int compareTo(Object obj) { 260 261 int result; 262 long difference; 263 264 if (obj instanceof Millisecond) { 267 Millisecond ms = (Millisecond) obj; 268 difference = getFirstMillisecond() - ms.getFirstMillisecond(); 269 if (difference > 0) { 270 result = 1; 271 } 272 else { 273 if (difference < 0) { 274 result = -1; 275 } 276 else { 277 result = 0; 278 } 279 } 280 } 281 282 else if (obj instanceof RegularTimePeriod) { 285 result = 0; 287 } 288 289 else { 292 result = 1; 294 } 295 296 return result; 297 298 } 299 300 305 public long getFirstMillisecond() { 306 return this.second.getFirstMillisecond() + this.millisecond; 307 } 308 309 316 public long getFirstMillisecond(Calendar calendar) { 317 return this.second.getFirstMillisecond(calendar) + this.millisecond; 318 } 319 320 325 public long getLastMillisecond() { 326 return this.second.getFirstMillisecond() + this.millisecond; 327 } 328 329 336 public long getLastMillisecond(Calendar calendar) { 337 return this.second.getFirstMillisecond(calendar) + this.millisecond; 338 } 339 340 } 341 | Popular Tags |