source: timeline/src/main/java/geniusweb/progress/ProgressTime.java@ 42

Last change on this file since 42 was 42, checked in by bart, 3 years ago

Fix for IssueValue hashcode.

File size: 2.9 KB
Line 
1package geniusweb.progress;
2
3import java.util.Date;
4
5import com.fasterxml.jackson.annotation.JsonCreator;
6import com.fasterxml.jackson.annotation.JsonProperty;
7
8import geniusweb.deadline.DeadlineTime;
9
10/**
11 * keeps track of progress towards the Deadline and the time/rounds left.
12 * immutable. However, the time progresses nevertheless since this refers to the
13 * system clocks.
14 *
15 */
16public class ProgressTime implements Progress {
17
18 /**
19 * time in millis as returned from {@link System#currentTimeMillis()}. Note,
20 * we do not use RFC 3339 because we need millisecond accuracy.
21 */
22 private final Date start;
23
24 /**
25 * duration in millis. we do not use ISO 8601 because we need millisecond
26 * accuracy.
27 */
28 private final Long duration;
29
30 /**
31 *
32 * @param d the duration, eg {@link DeadlineTime#getDuration()}. Must be
33 * > 0.
34 * @param start the start Date (unix time in millisecs since 1970). See
35 * {@link System#currentTimeMillis()}.
36 */
37 @JsonCreator
38 public ProgressTime(@JsonProperty("duration") Long d,
39 @JsonProperty("start") Date start) {
40 this.start = start;
41 duration = (d == null || d <= 0) ? 1 : d;
42 }
43
44 @Override
45 public Double get(Long currentTimeMs) {
46 long delta = currentTimeMs - start.getTime();
47 // double should have ~53 digits of precision, and we're computing
48 // deltas here.
49 // 2^53 millis seems plenty for our purposes so no need to use
50 // BigDecimal here.
51 Double ratio = delta / (double) duration;
52 if (ratio > 1d)
53 ratio = 1d;
54 else if (ratio < 0d)
55 ratio = 0d;
56 return ratio;
57 }
58
59 @Override
60 public boolean isPastDeadline(Long currentTimeMs) {
61 return currentTimeMs > start.getTime() + duration;
62 }
63
64 /**
65 *
66 * @return time measured in milliseconds, between the start time and
67 * midnight, January 1, 1970 UTCas returned from
68 * {@link System#currentTimeMillis()}
69 *
70 */
71 public Date getStart() {
72 return start;
73 }
74
75 /**
76 *
77 * @return duration in milliseconds.
78 */
79 public Long getDuration() {
80 return duration;
81 }
82
83 @Override
84 public Date getTerminationTime() {
85 return new Date(start.getTime() + duration);
86 }
87
88 @Override
89 public String toString() {
90 return "ProgressTime[" + start.getTime() + " , " + duration + "ms]";
91 }
92
93 @Override
94 public int hashCode() {
95 final int prime = 31;
96 int result = 1;
97 result = prime * result
98 + ((duration == null) ? 0 : duration.hashCode());
99 result = prime * result + ((start == null) ? 0 : start.hashCode());
100 return result;
101 }
102
103 @Override
104 public boolean equals(Object obj) {
105 if (this == obj)
106 return true;
107 if (obj == null)
108 return false;
109 if (getClass() != obj.getClass())
110 return false;
111 ProgressTime other = (ProgressTime) obj;
112 if (duration == null) {
113 if (other.duration != null)
114 return false;
115 } else if (!duration.equals(other.duration))
116 return false;
117 if (start == null) {
118 if (other.start != null)
119 return false;
120 } else if (!start.equals(other.start))
121 return false;
122 return true;
123 }
124
125}
Note: See TracBrowser for help on using the repository browser.