[1] | 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 | @SuppressWarnings("unused") // for deserializer
|
---|
| 33 | private ProgressTime() {
|
---|
| 34 | start = null;
|
---|
| 35 | duration = null;
|
---|
| 36 | }
|
---|
| 37 |
|
---|
| 38 | /**
|
---|
| 39 | *
|
---|
| 40 | * @param duration the duration, eg {@link DeadlineTime#getDuration()}. Must
|
---|
| 41 | * be >0.
|
---|
| 42 | * @param start the start Date (unix time in millisecs since 1970). See
|
---|
| 43 | * {@link System#currentTimeMillis()}.
|
---|
| 44 | */
|
---|
| 45 | @JsonCreator
|
---|
| 46 | public ProgressTime(@JsonProperty("duration") Long d,
|
---|
| 47 | @JsonProperty("start") Date start) {
|
---|
| 48 | this.start = start;
|
---|
| 49 | duration = (d == null || d <= 0) ? 1 : d;
|
---|
| 50 | }
|
---|
| 51 |
|
---|
| 52 | @Override
|
---|
| 53 | public Double get(long currentTimeMs) {
|
---|
| 54 | long delta = currentTimeMs - start.getTime();
|
---|
| 55 | // double should have ~53 digits of precision, and we're computing
|
---|
| 56 | // deltas here.
|
---|
| 57 | // 2^53 millis seems plenty for our purposes so no need to use
|
---|
| 58 | // BigDecimal here.
|
---|
| 59 | Double ratio = delta / (double) duration;
|
---|
| 60 | if (ratio > 1d)
|
---|
| 61 | ratio = 1d;
|
---|
| 62 | else if (ratio < 0d)
|
---|
| 63 | ratio = 0d;
|
---|
| 64 | return ratio;
|
---|
| 65 | }
|
---|
| 66 |
|
---|
| 67 | @Override
|
---|
| 68 | public boolean isPastDeadline(long currentTimeMs) {
|
---|
| 69 | return currentTimeMs > start.getTime() + duration;
|
---|
| 70 | }
|
---|
| 71 |
|
---|
| 72 | /**
|
---|
| 73 | *
|
---|
| 74 | * @return time measured in milliseconds, between the start time and
|
---|
| 75 | * midnight, January 1, 1970 UTCas returned from
|
---|
| 76 | * {@link System#currentTimeMillis()}
|
---|
| 77 | *
|
---|
| 78 | */
|
---|
| 79 | public Date getStart() {
|
---|
| 80 | return start;
|
---|
| 81 | }
|
---|
| 82 |
|
---|
| 83 | /**
|
---|
| 84 | *
|
---|
| 85 | * @return duration in milliseconds.
|
---|
| 86 | */
|
---|
| 87 | public Long getDuration() {
|
---|
| 88 | return duration;
|
---|
| 89 | }
|
---|
| 90 |
|
---|
| 91 | @Override
|
---|
| 92 | public Date getTerminationTime() {
|
---|
| 93 | return new Date(start.getTime() + duration);
|
---|
| 94 | }
|
---|
| 95 |
|
---|
| 96 | @Override
|
---|
| 97 | public String toString() {
|
---|
| 98 | return "ProgressTime[" + start.getTime() + " , " + duration + "ms]";
|
---|
| 99 | }
|
---|
| 100 |
|
---|
| 101 | @Override
|
---|
| 102 | public int hashCode() {
|
---|
| 103 | final int prime = 31;
|
---|
| 104 | int result = 1;
|
---|
| 105 | result = prime * result
|
---|
| 106 | + ((duration == null) ? 0 : duration.hashCode());
|
---|
| 107 | result = prime * result + ((start == null) ? 0 : start.hashCode());
|
---|
| 108 | return result;
|
---|
| 109 | }
|
---|
| 110 |
|
---|
| 111 | @Override
|
---|
| 112 | public boolean equals(Object obj) {
|
---|
| 113 | if (this == obj)
|
---|
| 114 | return true;
|
---|
| 115 | if (obj == null)
|
---|
| 116 | return false;
|
---|
| 117 | if (getClass() != obj.getClass())
|
---|
| 118 | return false;
|
---|
| 119 | ProgressTime other = (ProgressTime) obj;
|
---|
| 120 | if (duration == null) {
|
---|
| 121 | if (other.duration != null)
|
---|
| 122 | return false;
|
---|
| 123 | } else if (!duration.equals(other.duration))
|
---|
| 124 | return false;
|
---|
| 125 | if (start == null) {
|
---|
| 126 | if (other.start != null)
|
---|
| 127 | return false;
|
---|
| 128 | } else if (!start.equals(other.start))
|
---|
| 129 | return false;
|
---|
| 130 | return true;
|
---|
| 131 | }
|
---|
| 132 |
|
---|
| 133 | }
|
---|