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

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

Multiple learns with repeated tournament, maven use https.

File size: 3.8 KB
Line 
1package geniusweb.progress;
2
3import java.util.Date;
4
5import com.fasterxml.jackson.annotation.JsonCreator;
6import com.fasterxml.jackson.annotation.JsonProperty;
7import 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()}. immutable.
12 */
13@JsonTypeName("rounds")
14public 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 /**
50 *
51 * @return the current round. First round is 0. It is recommended that you
52 * use the functions in {@link Progress} instead of this, to ensure
53 * your code works with all implementations of Progress including
54 * future developments.
55 */
56 public Integer getCurrentRound() {
57 return currentRound;
58 }
59
60 /**
61 *
62 * @return total number of rounds. It is recommended that you use the
63 * functions in {@link Progress} instead of this, to ensure your
64 * code works with all implementations of Progress including future
65 * developments.
66 */
67 public Integer getTotalRounds() {
68 return duration;
69 }
70
71 @Override
72 public Double get(Long currentTimeMs) {
73 // deadline and current both are limited to MAXINT is 32 bits; double
74 // fits 52
75 // bits so this should not result in accuracy issues
76 double ratio = (double) currentRound / (double) duration;
77 if (ratio > 1d)
78 ratio = 1d;
79 else if (ratio < 0d)
80 ratio = 0d;
81 return ratio;
82 }
83
84 @Override
85 public boolean isPastDeadline(Long currentTimeMs) {
86 return currentRound >= duration || currentTimeMs > endtime.getTime();
87 }
88
89 /**
90 *
91 * @return new ProgressRounds with round 1 advanced (or this, if
92 * currentRound= duration). This is up to the user, as it is up to
93 * the used protocol what exactly is a round.
94 */
95 public ProgressRounds advance() {
96 if (duration == currentRound)
97 return this;
98 return new ProgressRounds(duration, currentRound + 1, endtime);
99 }
100
101 @Override
102 public int hashCode() {
103 final int prime = 31;
104 int result = 1;
105 result = prime * result
106 + ((currentRound == null) ? 0 : currentRound.hashCode());
107 result = prime * result
108 + ((duration == null) ? 0 : duration.hashCode());
109 return result;
110 }
111
112 @Override
113 public boolean equals(Object obj) {
114 if (this == obj)
115 return true;
116 if (obj == null)
117 return false;
118 if (getClass() != obj.getClass())
119 return false;
120 ProgressRounds other = (ProgressRounds) obj;
121 if (currentRound == null) {
122 if (other.currentRound != null)
123 return false;
124 } else if (!currentRound.equals(other.currentRound))
125 return false;
126 if (duration == null) {
127 if (other.duration != null)
128 return false;
129 } else if (!duration.equals(other.duration))
130 return false;
131 return true;
132 }
133
134 @Override
135 public String toString() {
136 return "ProgressRounds[" + currentRound + " of " + duration + "]";
137 }
138
139}
Note: See TracBrowser for help on using the repository browser.