1 | package desmoj.core.simulator; |
2 | |
3 | import static java.util.concurrent.TimeUnit.*; |
4 | import java.util.Calendar; |
5 | import java.util.Date; |
6 | import java.util.GregorianCalendar; |
7 | import java.util.TimeZone; |
8 | import java.util.concurrent.TimeUnit; |
9 | |
10 | /** |
11 | * Represents points in simulation time. Is used to indicate points in |
12 | * simulation time at which the state of the model changes. Each point in |
13 | * simulation time is represented by an individual object of this class and |
14 | * offers its own methods for arithmetic operations. |
15 | * |
16 | * @version DESMO-J, Ver. 2.3.3 copyright (c) 2011 |
17 | * @author Felix Klueckmann |
18 | * |
19 | * Licensed under the Apache License, Version 2.0 (the "License"); you |
20 | * may not use this file except in compliance with the License. You may |
21 | * obtain a copy of the License at |
22 | * http://www.apache.org/licenses/LICENSE-2.0 |
23 | * |
24 | * Unless required by applicable law or agreed to in writing, software |
25 | * distributed under the License is distributed on an "AS IS" BASIS, |
26 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or |
27 | * implied. See the License for the specific language governing |
28 | * permissions and limitations under the License. |
29 | * |
30 | */ |
31 | |
32 | public final class TimeInstant implements Comparable<TimeInstant> { |
33 | |
34 | /** |
35 | * The number of epsilons since the start of the epoch (January 1, 1970, 00:00:00 GMT). |
36 | */ |
37 | private final long _timeInEpsilon; |
38 | |
39 | /** |
40 | * The preferred time zone for printing this time instant. |
41 | */ |
42 | private TimeZone _preferredTimeZone; |
43 | |
44 | /** |
45 | * Constructs a TimeInstant object with the given time value in the time |
46 | * unit of the given parameter. It represents a time instant in simulation |
47 | * time. Note that trying to create a TimeInstant object The simulation will |
48 | * also stop immediately if the TimeSpan is larger than Long.MAX_VALUE-1 (in |
49 | * the unit of epsilon). |
50 | * |
51 | * @param time |
52 | * long : The time value of this TimeInstant in the time unit of |
53 | * unit. |
54 | * @param unit |
55 | * TimeUnit: the TimeUnit |
56 | */ |
57 | public TimeInstant(long time, TimeUnit unit) { |
58 | if (unit == null) { // no time unit given |
59 | throw (new desmoj.core.exception.SimAbortedException( |
60 | new desmoj.core.report.ErrorMessage( |
61 | null, |
62 | "Can't create TimeInstant object! Simulation aborted.", |
63 | "Class : TimeInstant Constructor : TimeInstant(long, TimeUnit)", |
64 | "Time unit passed is null", |
65 | "Make sure to pass a non-null time unit. \nNote that before " + |
66 | "connecting model and experiment, TimeInstants must explicitly\n" + |
67 | "refer to a time unit as the reference unit is not yet defined," + |
68 | "e.g. use \nTimeInstant(long time, TimeUnit unit) instead of" + |
69 | "TimeInstant(long time).", |
70 | null))); |
71 | } |
72 | // System.out.println(time + " " + unit); |
73 | // System.out.println("Format: " |
74 | // + new SimpleDateFormat().format(new Date(time))); |
75 | if (unit.compareTo(TimeOperations.getEpsilon()) < 0) { |
76 | // unit is a finer granularity than epsilon |
77 | if (TimeOperations.getStartTime() != null) {// Start time has been |
78 | // set |
79 | System.out.println("Starttime: " |
80 | + TimeOperations.getStartTime()); |
81 | long timeSinceStart = time // time since start...? |
82 | - unit.convert(TimeOperations.getStartTime()// Achtung-negative |
83 | // werden |
84 | // positiv |
85 | .getTimeInEpsilon(), TimeOperations |
86 | .getEpsilon()); |
87 | System.out.println("TimeSinceStart: " + timeSinceStart); |
88 | if (timeSinceStart != 0) { |
89 | time = time |
90 | - (timeSinceStart % unit.convert(1, TimeOperations |
91 | .getEpsilon())); |
92 | } |
93 | } |
94 | } |
95 | this._timeInEpsilon = TimeOperations.getEpsilon().convert(time, unit); |
96 | if (_timeInEpsilon == Long.MAX_VALUE) { |
97 | /*The timeInstant is too big. |
98 | (The method TimeUnit.convert(duration,unit)returns Long.MAX_VALUE if |
99 | the result of the conversion is to big*/ |
100 | |
101 | throw (new desmoj.core.exception.SimAbortedException( |
102 | new desmoj.core.report.ErrorMessage( |
103 | null, |
104 | "Can't create TimeInstant object! Simulation aborted.", |
105 | "Class : TimeInstant Constructor : TimeInstant(long,TimeUnit)", |
106 | "the TimeInstant is too big. ", |
107 | "Can only create TimeInstant objects which are smaller than Long.MAX_VALUE (in the TimeUnit of epsilon).", |
108 | null))); |
109 | } |
110 | |
111 | this._preferredTimeZone = TimeZone.getTimeZone("UTC"); |
112 | } |
113 | |
114 | /** |
115 | * Constructs a TimeInstant object with the given time value in the time |
116 | * unit of the reference time. It represents a time Instant in simulation |
117 | * time. |
118 | * |
119 | * @param timeInReferenceUnit |
120 | * long : The time value of this TimeInstant in the time unit of |
121 | * the reference time. |
122 | */ |
123 | public TimeInstant(long timeInReferenceUnit) { |
124 | this(timeInReferenceUnit, TimeOperations.getReferenceUnit()); |
125 | } |
126 | |
127 | /** |
128 | * Constructs a TimeInstant object with the given time value in the time |
129 | * unit given as second parameter. It represents a time Instant in simulation |
130 | * time. |
131 | * |
132 | * @param time |
133 | * double : The time value of this TimeInstant in the time unit of |
134 | * the reference time. |
135 | * |
136 | * @param unit |
137 | * TimeUnit : the time unit |
138 | */ |
139 | public TimeInstant(double time, TimeUnit unit) { |
140 | if (unit == null) { // no time unit given |
141 | throw (new desmoj.core.exception.SimAbortedException( |
142 | new desmoj.core.report.ErrorMessage( |
143 | null, |
144 | "Can't create TimeInstant object! Simulation aborted.", |
145 | "Class : TimeInstant Constructor : TimeInstant(double, TimeUnit)", |
146 | "Time unit passed is null", |
147 | "Make sure to pass a non-null time unit. \nNote that before " + |
148 | "connecting model and experiment, TimeInstants must explicitly\n" + |
149 | "refer to a time unit as the reference unit is not yet defined," + |
150 | "e.g. use \nTimeInstant(double time, TimeUnit unit) instead of" + |
151 | "TimeInstant(double time).", |
152 | null))); |
153 | } |
154 | // System.out.println(time + " " + unit); |
155 | // System.out.println("Format: " |
156 | // + new SimpleDateFormat().format(new Date(time))); |
157 | if (unit.compareTo(TimeOperations.getEpsilon()) < 0) { |
158 | // unit is a finer granularity than epsilon |
159 | if (TimeOperations.getStartTime() != null) {// Start time has been |
160 | // set |
161 | System.out.println("Starttime: " |
162 | + TimeOperations.getStartTime()); |
163 | double timeSinceStart = time // time since start...? |
164 | - unit.convert(TimeOperations.getStartTime()// Achtung-negative |
165 | // werden |
166 | // positiv |
167 | .getTimeInEpsilon(), TimeOperations |
168 | .getEpsilon()); |
169 | System.out.println("TimeSinceStart: " + timeSinceStart); |
170 | if (timeSinceStart != 0) { |
171 | time = time |
172 | - (timeSinceStart % unit.convert(1, TimeOperations |
173 | .getEpsilon())); |
174 | } |
175 | } |
176 | } |
177 | this._timeInEpsilon = (long) (TimeOperations.getEpsilon().convert(1, unit) * time); |
178 | if (_timeInEpsilon == Long.MAX_VALUE) { |
179 | /*The timeInstant is too big. |
180 | (The method TimeUnit.convert(duration,unit)returns Long.MAX_VALUE if |
181 | the result of the conversion is to big*/ |
182 | |
183 | throw (new desmoj.core.exception.SimAbortedException( |
184 | new desmoj.core.report.ErrorMessage( |
185 | null, |
186 | "Can't create TimeInstant object! Simulation aborted.", |
187 | "Class : TimeInstant Constructor : TimeInstant(long,TimeUnit)", |
188 | "the TimeInstant is too big. ", |
189 | "Can only create TimeInstant objects which are smaller than Long.MAX_VALUE (in the TimeUnit of epsilon).", |
190 | null))); |
191 | } |
192 | |
193 | this._preferredTimeZone = TimeZone.getTimeZone("UTC"); |
194 | |
195 | } |
196 | |
197 | /** |
198 | * Constructs a TimeInstant object with the given time value in the time |
199 | * unit of the reference time. It represents a time Instant in simulation |
200 | * time. |
201 | * |
202 | * @param timeInReferenceUnit |
203 | * double : The time value of this TimeInstant in the time unit of |
204 | * the reference time. |
205 | */ |
206 | public TimeInstant(double timeInReferenceUnit) { |
207 | this(timeInReferenceUnit, TimeOperations.getReferenceUnit()); |
208 | } |
209 | |
210 | /** |
211 | * Constructs a TimeInstant object that represents the given instant of time |
212 | * specified by the Calendar object. The preferred time zone for output will |
213 | * can either be adopted from the Calendar object or not be set. |
214 | * |
215 | * @param calendar |
216 | * Calendar : the instant of time that is represented by this TimeIstant |
217 | * @param applyPrefferdTimeZoneFromCalendar |
218 | * boolean : Use the time zone as set in calendar for output (true) |
219 | * or use UTC as default time zone for output (false) |
220 | * |
221 | * @see java.util.Calendar |
222 | */ |
223 | public TimeInstant(Calendar calendar, boolean applyPrefferdTimeZoneFromCalendar) { |
224 | this(calendar.getTimeInMillis(), MILLISECONDS); |
225 | if (applyPrefferdTimeZoneFromCalendar) |
226 | this._preferredTimeZone = calendar.getTimeZone(); |
227 | } |
228 | |
229 | /** |
230 | * Constructs a TimeInstant object that represents the given instant of time |
231 | * specified by the Calendar object. UTC will be used as default time zone for output. |
232 | * |
233 | * @see java.util.Calendar |
234 | */ |
235 | public TimeInstant(Calendar calendar) { |
236 | this(calendar, false); |
237 | } |
238 | |
239 | /** |
240 | * Constructs a TimeInstant object that represents the given instant of time |
241 | * specified by the Date object. |
242 | * |
243 | * @param date |
244 | * Date : the instant of time that is represented by this TimeInstant |
245 | * @see java.util.Date |
246 | */ |
247 | public TimeInstant(Date date) { |
248 | this(date.getTime(), MILLISECONDS); |
249 | } |
250 | |
251 | /** |
252 | * Returns the value of the TimeInstant object as a long type in the time |
253 | * unit of epsilon |
254 | * |
255 | * @return long: the time value of the TimeInstant object as a long type in |
256 | * the time unit of epsilon |
257 | */ |
258 | public long getTimeInEpsilon() { |
259 | return _timeInEpsilon; |
260 | } |
261 | |
262 | /** |
263 | * Returns the value of this TimeInstant object as a long type in the time |
264 | * unit given as a parameter. If the parameter has a coarser granularity |
265 | * than epsilon the returned value will be truncated, so lose precision. |
266 | * |
267 | * |
268 | * @return long: the time value of the TimeInstant object as a long type in |
269 | * the time unit given as a parameter or Long.MIN_VALUE if |
270 | * conversion would negatively overflow, or Long.MAX_VALUE if it |
271 | * would positively overflow. |
272 | */ |
273 | public long getTimeTruncated(TimeUnit unit) { |
274 | return unit.convert(_timeInEpsilon, TimeOperations.getEpsilon()); |
275 | } |
276 | |
277 | /** |
278 | * Returns the value of this TimeInstant object as a long type in the time |
279 | * unit of the reference time. If the parameter has a coarser granularity |
280 | * than epsilon the returned value will be truncated, so lose precision. |
281 | * |
282 | * |
283 | * @return long: the time value of the TimeInstant object as a long type in |
284 | * the time unit given as a parameter or Long.MIN_VALUE if |
285 | * conversion would negatively overflow, or Long.MAX_VALUE if it |
286 | * would positively overflow. |
287 | */ |
288 | public long getTimeTruncated() { |
289 | return getTimeTruncated(TimeOperations.getReferenceUnit()); |
290 | } |
291 | |
292 | /** |
293 | * Returns the value of this TimeInstant object as a long type in the time |
294 | * unit given as a parameter. If the parameter has a coarser granularity |
295 | * than epsilon the returned value will be rounded, so lose precision. |
296 | * |
297 | * @param unit |
298 | * the TimeUnit |
299 | * |
300 | * @return long: the time value of the TimeInstant object as a long type in |
301 | * the time unit given as a parameter or Long.MIN_VALUE if |
302 | * conversion would negatively overflow, or Long.MAX_VALUE if it |
303 | * would positively overflow. |
304 | */ |
305 | public long getTimeRounded(TimeUnit unit) { |
306 | if (unit.compareTo(TimeOperations.getEpsilon()) > 0) { |
307 | // unit has a coarser granularity than epsilon |
308 | long halfAUnitInEpsilon = TimeOperations.getEpsilon().convert(1, |
309 | unit) / 2; |
310 | long durationInUnitTruncated = getTimeTruncated(unit); |
311 | long difference = _timeInEpsilon |
312 | - TimeOperations.getEpsilon().convert( |
313 | durationInUnitTruncated, unit); |
314 | // if the time value in the unit Epsilon is bigger than |
315 | if (difference >= halfAUnitInEpsilon) { |
316 | return durationInUnitTruncated + 1; |
317 | } |
318 | return durationInUnitTruncated; |
319 | } else { |
320 | // unit has a finer granularity or is equal than epsilon |
321 | return getTimeTruncated(unit); |
322 | } |
323 | |
324 | } |
325 | |
326 | /** |
327 | * Returns the value of this TimeInstant object as a long type in the time |
328 | * unit of the reference time. If the parameter has a coarser granularity |
329 | * than epsilon the returned value will be rounded, so lose precision. |
330 | * |
331 | * |
332 | * @return long: the time value of the TimeInstant object as a long type in |
333 | * the time unit given as a parameter or Long.MIN_VALUE if |
334 | * conversion would negatively overflow, or Long.MAX_VALUE if it |
335 | * would positively overflow. |
336 | */ |
337 | public long getTimeRounded() { |
338 | return getTimeRounded(TimeOperations.getReferenceUnit()); |
339 | } |
340 | |
341 | /** |
342 | * Returns the value of this TimeInstant object as a Calender object. |
343 | * Note that the TimeZone of the Calender object returend is set |
344 | * to this TimeInstant's preferred TimeZone (which defaults to UTC |
345 | * unless set differently). |
346 | * |
347 | * @return Calendar: a Calendar representation of this TimeInstant |
348 | */ |
349 | public Calendar getTimeAsCalender() { |
350 | Calendar cal = GregorianCalendar.getInstance(); |
351 | cal.setTimeInMillis(this.getTimeRounded(TimeUnit.MILLISECONDS)); |
352 | cal.setTimeZone(this._preferredTimeZone); |
353 | return cal; |
354 | } |
355 | |
356 | /** |
357 | * Returns the value of this TimeInstant object as a Date object. |
358 | * |
359 | * @return Date: a Date representation of this TimeInstant |
360 | */ |
361 | public Date getTimeAsDate() { |
362 | return new Date(this.getTimeRounded(TimeUnit.MILLISECONDS)); |
363 | } |
364 | |
365 | /** |
366 | * Returns the value of this TimeInstant object as a double type in the time |
367 | * unit given as a parameter. |
368 | * |
369 | * @return double: the time value of the TimeInstant object as a double type |
370 | * in the time unit given as a parameter |
371 | */ |
372 | public double getTimeAsDouble(TimeUnit unit) { |
373 | return _timeInEpsilon |
374 | / (double) TimeOperations.getEpsilon().convert(1, unit); |
375 | } |
376 | |
377 | /** |
378 | * Returns the value of this TimeInstant object as a double type in the time |
379 | * unit of the reference time. |
380 | * |
381 | * @return double: the time value of the TimeInstant object as a double type |
382 | * in the time unit given as a parameter |
383 | */ |
384 | public double getTimeAsDouble() { |
385 | return getTimeAsDouble(TimeOperations.getReferenceUnit()); |
386 | } |
387 | |
388 | /** |
389 | * @deprecated Replaced by getTimeAsDouble(). |
390 | * The value of this TimeInstant object as a double type in the time |
391 | * unit of the reference time. |
392 | * |
393 | * @return double: the time value of the TimeInstant object as a double type |
394 | * in the time unit given as a parameter |
395 | */ |
396 | public double getTimeValue() { |
397 | return getTimeAsDouble(); |
398 | } |
399 | |
400 | /** |
401 | * The preferred time zone for printing this time instant. |
402 | * |
403 | * @return TimeZone: the time zone to use for printing this TimeInstant. |
404 | */ |
405 | public TimeZone getPreferredTimeZone() { |
406 | return this._preferredTimeZone; |
407 | } |
408 | |
409 | /** |
410 | * Sets the preferred time zone for printing this time instant. |
411 | * Note that this time zone does not affect the internal representation |
412 | * of this TimeInstant (stored as multiple of Experiment's epsilon |
413 | * since the start of the epoch, i.e. January 1, 1970, 00:00:00 GMT); |
414 | * the preferred TimeZone just servers TimeFormatters generating a |
415 | * convenient output. |
416 | * |
417 | * @param preferredTimeZone |
418 | * java.util.TimeZone : The time value of this TimeInstant in the time unit of |
419 | * the reference time. |
420 | */ |
421 | public void setPreferredTimeZone(TimeZone preferredTimeZone) { |
422 | this._preferredTimeZone = preferredTimeZone; |
423 | } |
424 | |
425 | /** |
426 | * Checks if the first of two points of simulation time is before the |
427 | * second. Before means, that the time value of TimeInstant a is smaller and |
428 | * hence "earlier" than TimeInstant b. Note that this is a static method |
429 | * available through calling the class <code>TimeInstant</code> i.e. |
430 | * <code>TimeInstant.isAfter(a,b)</code> where a and b are valid TimeInstant |
431 | * objects. |
432 | * |
433 | * @return boolean : True if a is before (earlier) than b |
434 | * @param a |
435 | * TimeInstant : first comparand |
436 | * @param b |
437 | * TimeInstant : second comparand |
438 | */ |
439 | public static boolean isBefore(TimeInstant a, TimeInstant b) { |
440 | return (a._timeInEpsilon < b._timeInEpsilon); |
441 | } |
442 | |
443 | /** |
444 | * Checks if the first of two points of simulation time is after the second. |
445 | * After means, that the time value of TimeInstant a is larger and hence |
446 | * "later" than TimeInstant b. Note that this is a static method available |
447 | * through calling the class <code>TimeInstant</code> i.e. |
448 | * <code>TimeInstant.isAfter(a,b)</code> where a and b are valid TimeInstant |
449 | * objects. |
450 | * |
451 | * @return boolean : True if a is after (later) than b |
452 | * @param a |
453 | * TimeInstant : first comparand |
454 | * @param b |
455 | * TimeInstant : second comparand |
456 | */ |
457 | public static boolean isAfter(TimeInstant a, TimeInstant b) { |
458 | return (a._timeInEpsilon > b._timeInEpsilon); |
459 | } |
460 | |
461 | /** |
462 | * Checks if the first of two points of simulation time is after the second |
463 | * or equal to the second. After means, that the time value of TimeInstant a |
464 | * is larger and hence after TimeInstant b. Equal means, that they both |
465 | * describe the same point in simulation time. Note that this is a static |
466 | * method available through calling the class <code>TimeInstant</code> i.e. |
467 | * <code>TimeInstant.isAfterOrEqual(a,b)</code> where a and b are valid |
468 | * timeInstant objects. |
469 | * |
470 | * @return boolean : True if a is after (later than )b or equal to b |
471 | * |
472 | * @param a |
473 | * TimeInstant : first comparand |
474 | * @param b |
475 | * TimeInstant : second comparand |
476 | */ |
477 | public static boolean isAfterOrEqual(TimeInstant a, TimeInstant b) { |
478 | return (isAfter(a, b) || isEqual(a, b)); |
479 | } |
480 | |
481 | /** |
482 | * Checks if the two TimeInstant parameters describe the same point of |
483 | * simulation time. Note that this is a static method available through |
484 | * calling the class <code>TimeInstant</code> i.e. |
485 | * <code>TimeInstant.isEqual(a,b)</code> where a and b are valid TimeInstant |
486 | * objects. |
487 | * |
488 | * @return boolean : True if both parameters describe same point of |
489 | * simulation time |
490 | * @param a |
491 | * TimeInstant : first comparand |
492 | * @param b |
493 | * TimeInstant : second comparand |
494 | */ |
495 | public static boolean isEqual(TimeInstant a, TimeInstant b) { |
496 | return (a._timeInEpsilon == b._timeInEpsilon); |
497 | } |
498 | |
499 | /** |
500 | * Checks if the first of two points of simulation time is before the second |
501 | * or equal to the second. Before means, that the time value of TimeInstant |
502 | * a is smaller and hence before TimeInstant b. Equal means, that they both |
503 | * describe the same point in simulation time. Note that this is a static |
504 | * method available through calling the class <code>TimeInstant</code> i.e. |
505 | * <code>TimeInstant.isBeforeOrEqual(a,b)</code> where a and b are valid |
506 | * timeInstant objects. |
507 | * |
508 | * @return boolean : True if a is before (earlier than )b or equal to b |
509 | * |
510 | * @param a |
511 | * TimeInstant : first comparand |
512 | * @param b |
513 | * TimeInstant : second comparand |
514 | */ |
515 | public static boolean isBeforeOrEqual(TimeInstant a, TimeInstant b) { |
516 | return (isBefore(a, b) || isEqual(a, b)); |
517 | } |
518 | |
519 | /** |
520 | * Determines that last instant prior to this instant at which |
521 | * a new period of the unit provided "begins", which means smaller |
522 | * units are zero. <br/> |
523 | * Examples: Assume this TimeInstant is 28.12.2011 11:23:45:678. <br\> |
524 | * Calling <code>getBeginOf(TimeUnit.TimeUnit.DAYS)</code> yields |
525 | * 28.12.2011 00:00:00:000 (begin of current hour).<br\> |
526 | * Calling <code>getBeginOf(TimeUnit.TimeUnit.MINUTES)</code> yields |
527 | * 28.12.2011 11:23:00:000 (begin of current day).<br\> |
528 | * Note that days are interpreted with respect to this instant's |
529 | * preferred time zone. |
530 | * |
531 | * @param smallestUnit |
532 | * TimeUnit : the unit to begin (i.e. smaller units set to zero); |
533 | * must be TimeUnit.DAYS, TimeUnit.HOURS, TimeUnit.MINUTES, TimeUnit.SECONDS or TimeUnit.MILLISECONDS. |
534 | * |
535 | * @return TimeInstant: The TimeInstant at which the day/hour/minute/second/millisecond |
536 | * of the this TimeInstant has begun. |
537 | */ |
538 | public TimeInstant getBeginOf(TimeUnit smallestUnit){ |
539 | |
540 | Calendar cal = GregorianCalendar.getInstance(this._preferredTimeZone); |
541 | cal.setTimeInMillis(this.getTimeTruncated(TimeUnit.MILLISECONDS)); |
542 | |
543 | switch (smallestUnit) { |
544 | case DAYS: |
545 | cal.set(Calendar.HOUR_OF_DAY, 0); |
546 | case HOURS: |
547 | cal.set(Calendar.MINUTE, 0); |
548 | case MINUTES: |
549 | cal.set(Calendar.SECOND, 0); |
550 | case SECONDS: |
551 | cal.set(Calendar.MILLISECOND, 0); |
552 | case MILLISECONDS: |
553 | default: |
554 | // nothing to as units smaller than milliseconds are truncated above |
555 | } |
556 | return new TimeInstant(cal, true); |
557 | } |
558 | |
559 | /** |
560 | * Returns a hash code value for the object. This methode overides |
561 | * java.lang.Object.hashCode().The method is supported for the benefit of |
562 | * hashtables such as those provided by java.util.Hashtable. |
563 | * |
564 | * @return: int: a hash code value for this TimeInstant. |
565 | */ |
566 | @Override |
567 | public int hashCode() { |
568 | return (int) (this._timeInEpsilon ^ (this._timeInEpsilon >>> 32)); |
569 | } |
570 | |
571 | /** |
572 | * Indicates whether this TimeInstant is equal to the given parameter. |
573 | * Returns true if the obj argument is a TimeInstant and represents the same |
574 | * point of time as this TimeInstant; false otherwise. This method overrides |
575 | * java.lang.Object.equals() |
576 | * |
577 | * @param o the reference object with which to compare. |
578 | * @return: true if the obj argument is a TimeInstant and represents the |
579 | * same point of time as this TimeInstant; false otherwise. |
580 | */ |
581 | @Override |
582 | public boolean equals(Object o) { |
583 | if (!(o instanceof TimeInstant)) |
584 | return false; |
585 | TimeInstant instant = (TimeInstant) o; |
586 | return isEqual(this, instant); |
587 | } |
588 | |
589 | /** |
590 | * Compares the given TimeInstant to this TimeInstant. This method |
591 | * implements the Comparable<TimeInstant> Interface |
592 | * |
593 | * @param anotherInstant : The TimeInstant to be compared to this TimeInstant |
594 | * |
595 | * @return: int: Returns a negative integer, zero, or a positive integer as |
596 | * this TimeInstant is before, at the same time, or after the given |
597 | * TimeInstant. |
598 | */ |
599 | public int compareTo(TimeInstant anotherInstant) { |
600 | long difference = this.getTimeInEpsilon() |
601 | - anotherInstant.getTimeInEpsilon(); |
602 | if (difference < 0) |
603 | return -1; |
604 | if (difference > 0) |
605 | return 1; |
606 | return 0; |
607 | } |
608 | |
609 | /** |
610 | * Returns the String Representation of this TimeInstant according to the |
611 | * TimeFormatter. |
612 | * |
613 | * @see java.lang.Object#toString() |
614 | * @see desmoj.core.simulator.TimeFormatter |
615 | */ |
616 | public String toString() { |
617 | return TimeOperations.formatTimeInstant(this); |
618 | } |
619 | |
620 | /** |
621 | * Returns the String Representation of this TimeInstant according to the |
622 | * TimeFormatter, truncating digits after the decimal point if necessary. |
623 | * |
624 | * @param digits Maximum number of digits after decimal point |
625 | * |
626 | * @see java.lang.Object#toString() |
627 | * @see desmoj.core.simulator.TimeFormatter |
628 | */ |
629 | public String toString(int digits) { |
630 | |
631 | String result = TimeOperations.formatTimeInstant(this); |
632 | |
633 | if (result.lastIndexOf(".") >= 0) { |
634 | result = result.substring(0, Math.max(result.length()-1, result.lastIndexOf(".") + digits)); |
635 | } |
636 | return result; |
637 | } |
638 | } |