source: timeline/src/main/java/geniusweb/progress/ProgressRounds.java@ 49

Last change on this file since 49 was 49, checked in by bart, 2 years ago

Domaineditor released.

File size: 3.7 KB
RevLine 
[49]1package geniusweb.progress;
2
3import java.util.Date;
4
5import com.fasterxml.jackson.annotation.JsonCreator;
6import com.fasterxml.jackson.annotation.JsonProperty;
7
8/**
9 * progress in terms of number of rounds. The round has to be updated by the
10 * user of this class, calling {@link #advance()}. immutable.
11 */
12public class ProgressRounds implements Progress {
13
14 private final Integer duration;
15 private final Integer currentRound;
16 private final Date endtime;
17
18 /**
19 *
20 * @param deadline length max number of rounds, must be positive (not 0)
21 * @param currentRound the current round number (can be from 0 to deadlne).
22 * When = deadline, it means the progress has gone past
23 * the deadline.
24 * @param end the termination time of this session.
25 */
26 @JsonCreator
27 public ProgressRounds(@JsonProperty("duration") Integer deadline,
28 @JsonProperty("currentRound") Integer currentRound,
29 @JsonProperty("endtime") Date end) {
30 if (deadline <= 0)
31 throw new IllegalArgumentException(
32 "deadline must be positive but is " + deadline);
33 if (currentRound < 0 || currentRound > deadline) {
34 throw new IllegalArgumentException(
35 "current round must be inside [0," + deadline + "]");
36 }
37 this.duration = deadline;
38 this.currentRound = currentRound;
39 this.endtime = end;
40 }
41
42 @Override
43 public Date getTerminationTime() {
44 return endtime;
45 }
46
47 /**
48 *
49 * @return the current round. First round is 0. It is recommended that you
50 * use the functions in {@link Progress} instead of this, to ensure
51 * your code works with all implementations of Progress including
52 * future developments.
53 */
54 public Integer getCurrentRound() {
55 return currentRound;
56 }
57
58 /**
59 *
60 * @return total number of rounds. It is recommended that you use the
61 * functions in {@link Progress} instead of this, to ensure your
62 * code works with all implementations of Progress including future
63 * developments.
64 */
65 public Integer getTotalRounds() {
66 return duration;
67 }
68
69 @Override
70 public Double get(Long currentTimeMs) {
71 // deadline and current both are limited to MAXINT is 32 bits; double
72 // fits 52
73 // bits so this should not result in accuracy issues
74 double ratio = (double) currentRound / (double) duration;
75 if (ratio > 1d)
76 ratio = 1d;
77 else if (ratio < 0d)
78 ratio = 0d;
79 return ratio;
80 }
81
82 @Override
83 public boolean isPastDeadline(Long currentTimeMs) {
84 return currentRound >= duration || currentTimeMs > endtime.getTime();
85 }
86
87 /**
88 *
89 * @return new ProgressRounds with round 1 advanced (or this, if
90 * currentRound= duration). This is up to the user, as it is up to
91 * the used protocol what exactly is a round.
92 */
93 public ProgressRounds advance() {
94 if (duration == currentRound)
95 return this;
96 return new ProgressRounds(duration, currentRound + 1, endtime);
97 }
98
99 @Override
100 public int hashCode() {
101 final int prime = 31;
102 int result = 1;
103 result = prime * result
104 + ((currentRound == null) ? 0 : currentRound.hashCode());
105 result = prime * result
106 + ((duration == null) ? 0 : duration.hashCode());
107 return result;
108 }
109
110 @Override
111 public boolean equals(Object obj) {
112 if (this == obj)
113 return true;
114 if (obj == null)
115 return false;
116 if (getClass() != obj.getClass())
117 return false;
118 ProgressRounds other = (ProgressRounds) obj;
119 if (currentRound == null) {
120 if (other.currentRound != null)
121 return false;
122 } else if (!currentRound.equals(other.currentRound))
123 return false;
124 if (duration == null) {
125 if (other.duration != null)
126 return false;
127 } else if (!duration.equals(other.duration))
128 return false;
129 return true;
130 }
131
132 @Override
133 public String toString() {
134 return "ProgressRounds[" + currentRound + " of " + duration + "]";
135 }
136
137}
Note: See TracBrowser for help on using the repository browser.