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

Last change on this file since 17 was 12, checked in by bart, 5 years ago

Added ANAC2019 Example parties: Agentgg and WinkyAgent

File size: 3.6 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()}.
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 not recommended to use
52 * this, as this may make your code working with rounds only.
53 */
54 public Integer getCurrentRound() {
55 return currentRound;
56 }
57
58 /**
59 *
60 * @return total number of rounds. It is not recommended to use this, as
61 * this may make your code working with rounds only.
62 */
63 public Integer getTotalRounds() {
64 return duration;
65 }
66
67 @Override
68 public Double get(long currentTimeMs) {
69 // deadline and current both are limited to MAXINT is 32 bits; double
70 // fits 52
71 // bits so this should not result in accuracy issues
72 double ratio = (double) currentRound / (double) duration;
73 if (ratio > 1d)
74 ratio = 1d;
75 else if (ratio < 0d)
76 ratio = 0d;
77 return ratio;
78 }
79
80 @Override
81 public boolean isPastDeadline(long currentTimeMs) {
82 return currentRound >= duration || currentTimeMs > endtime.getTime();
83 }
84
85 /**
86 *
87 * @return new ProgressRounds with round 1 advanced (or this, if
88 * currentRound= duration). This is up to the user, as it is up to
89 * the used protocol what exactly is a round.
90 */
91 public ProgressRounds advance() {
92 if (duration == currentRound)
93 return this;
94 return new ProgressRounds(duration, currentRound + 1, endtime);
95 }
96
97 @Override
98 public int hashCode() {
99 final int prime = 31;
100 int result = 1;
101 result = prime * result
102 + ((currentRound == null) ? 0 : currentRound.hashCode());
103 result = prime * result
104 + ((duration == null) ? 0 : duration.hashCode());
105 return result;
106 }
107
108 @Override
109 public boolean equals(Object obj) {
110 if (this == obj)
111 return true;
112 if (obj == null)
113 return false;
114 if (getClass() != obj.getClass())
115 return false;
116 ProgressRounds other = (ProgressRounds) obj;
117 if (currentRound == null) {
118 if (other.currentRound != null)
119 return false;
120 } else if (!currentRound.equals(other.currentRound))
121 return false;
122 if (duration == null) {
123 if (other.duration != null)
124 return false;
125 } else if (!duration.equals(other.duration))
126 return false;
127 return true;
128 }
129
130 @Override
131 public String toString() {
132 return "ProgressRounds[" + currentRound + " of " + duration + "]";
133 }
134
135}
Note: See TracBrowser for help on using the repository browser.