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