[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 | /**
|
---|
| 10 | * progress in terms of number of rounds. The round has to be updated by the
|
---|
| 11 | * user of this class, calling {@link #advance()}.
|
---|
| 12 | */
|
---|
| 13 | @JsonTypeName("rounds")
|
---|
| 14 | public class ProgressRounds implements Progress {
|
---|
| 15 |
|
---|
| 16 | private final Integer duration;
|
---|
| 17 | private final Integer currentRound;
|
---|
| 18 | private final Date endtime;
|
---|
| 19 |
|
---|
| 20 | /**
|
---|
| 21 | *
|
---|
| 22 | * @param deadline length max number of rounds, must be positive (not 0)
|
---|
| 23 | * @param currentRound the current round number (can be from 0 to deadlne).
|
---|
| 24 | * When = deadline, it means the progress has gone past
|
---|
| 25 | * the deadline.
|
---|
| 26 | * @param end the termination time of this session.
|
---|
| 27 | */
|
---|
| 28 | @JsonCreator
|
---|
| 29 | public ProgressRounds(@JsonProperty("duration") Integer deadline,
|
---|
| 30 | @JsonProperty("currentRound") Integer currentRound,
|
---|
| 31 | @JsonProperty("endtime") Date end) {
|
---|
| 32 | if (deadline <= 0)
|
---|
| 33 | throw new IllegalArgumentException(
|
---|
| 34 | "deadline must be positive but is " + deadline);
|
---|
| 35 | if (currentRound < 0 || currentRound > deadline) {
|
---|
| 36 | throw new IllegalArgumentException(
|
---|
| 37 | "current round must be inside [0," + deadline + "]");
|
---|
| 38 | }
|
---|
| 39 | this.duration = deadline;
|
---|
| 40 | this.currentRound = currentRound;
|
---|
| 41 | this.endtime = end;
|
---|
| 42 | }
|
---|
| 43 |
|
---|
| 44 | @Override
|
---|
| 45 | public Date getTerminationTime() {
|
---|
| 46 | return endtime;
|
---|
| 47 | }
|
---|
| 48 |
|
---|
| 49 | @Override
|
---|
| 50 | public Double get(long currentTimeMs) {
|
---|
| 51 | // deadline and current both are limited to MAXINT is 32 bits; double
|
---|
| 52 | // fits 52
|
---|
| 53 | // bits so this should not result in accuracy issues
|
---|
| 54 | double ratio = (double) currentRound / (double) duration;
|
---|
| 55 | if (ratio > 1d)
|
---|
| 56 | ratio = 1d;
|
---|
| 57 | else if (ratio < 0d)
|
---|
| 58 | ratio = 0d;
|
---|
| 59 | return ratio;
|
---|
| 60 | }
|
---|
| 61 |
|
---|
| 62 | @Override
|
---|
| 63 | public boolean isPastDeadline(long currentTimeMs) {
|
---|
| 64 | return currentRound >= duration || currentTimeMs > endtime.getTime();
|
---|
| 65 | }
|
---|
| 66 |
|
---|
| 67 | /**
|
---|
| 68 | *
|
---|
| 69 | * @return new ProgressRounds with round 1 advanced (or this, if
|
---|
| 70 | * currentRound= duration). This is up to the user, as it is up to
|
---|
| 71 | * the used protocol what exactly is a round.
|
---|
| 72 | */
|
---|
| 73 | public ProgressRounds advance() {
|
---|
| 74 | if (duration == currentRound)
|
---|
| 75 | return this;
|
---|
| 76 | return new ProgressRounds(duration, currentRound + 1, endtime);
|
---|
| 77 | }
|
---|
| 78 |
|
---|
| 79 | @Override
|
---|
| 80 | public int hashCode() {
|
---|
| 81 | final int prime = 31;
|
---|
| 82 | int result = 1;
|
---|
| 83 | result = prime * result
|
---|
| 84 | + ((currentRound == null) ? 0 : currentRound.hashCode());
|
---|
| 85 | result = prime * result
|
---|
| 86 | + ((duration == null) ? 0 : duration.hashCode());
|
---|
| 87 | return result;
|
---|
| 88 | }
|
---|
| 89 |
|
---|
| 90 | @Override
|
---|
| 91 | public boolean equals(Object obj) {
|
---|
| 92 | if (this == obj)
|
---|
| 93 | return true;
|
---|
| 94 | if (obj == null)
|
---|
| 95 | return false;
|
---|
| 96 | if (getClass() != obj.getClass())
|
---|
| 97 | return false;
|
---|
| 98 | ProgressRounds other = (ProgressRounds) obj;
|
---|
| 99 | if (currentRound == null) {
|
---|
| 100 | if (other.currentRound != null)
|
---|
| 101 | return false;
|
---|
| 102 | } else if (!currentRound.equals(other.currentRound))
|
---|
| 103 | return false;
|
---|
| 104 | if (duration == null) {
|
---|
| 105 | if (other.duration != null)
|
---|
| 106 | return false;
|
---|
| 107 | } else if (!duration.equals(other.duration))
|
---|
| 108 | return false;
|
---|
| 109 | return true;
|
---|
| 110 | }
|
---|
| 111 |
|
---|
| 112 | @Override
|
---|
| 113 | public String toString() {
|
---|
| 114 | return "ProgressRounds[" + currentRound + " of " + duration + "]";
|
---|
| 115 | }
|
---|
| 116 |
|
---|
| 117 | }
|
---|