Changeset 54 for timeline/src
- Timestamp:
- Jul 23, 2025, 10:16:55 AM (2 months ago)
- Location:
- timeline/src
- Files:
-
- 10 edited
Legend:
- Unmodified
- Added
- Removed
-
timeline/src/main/java/geniusweb/deadline/Deadline.java
r52 r54 1 1 package geniusweb.deadline; 2 3 import org.eclipse.jdt.annotation.NonNull; 2 4 3 5 import com.fasterxml.jackson.annotation.JsonAutoDetect; … … 21 23 * @return the duration of this deadline, measured in milliseconds 22 24 */ 23 public Long getDuration();25 public @NonNull Long getDurationMs(); 24 26 25 27 } -
timeline/src/main/java/geniusweb/deadline/DeadlineRounds.java
r52 r54 1 1 package geniusweb.deadline; 2 3 import org.eclipse.jdt.annotation.NonNull; 2 4 3 5 import com.fasterxml.jackson.annotation.JsonCreator; … … 11 13 */ 12 14 public class DeadlineRounds extends DeadlineTime { 13 private final Integer rounds;15 private final @NonNull Integer rounds; 14 16 15 17 /** … … 20 22 */ 21 23 @JsonCreator 22 public DeadlineRounds(@JsonProperty("rounds") Integer rounds,24 public DeadlineRounds(@JsonProperty("rounds") @NonNull Integer rounds, 23 25 @JsonProperty("durationms") long durationms) { 24 26 super(durationms); … … 30 32 } 31 33 32 public Integer getRounds() {34 public @NonNull Integer getRounds() { 33 35 return rounds; 34 36 } … … 60 62 61 63 @Override 64 @NonNull 62 65 public String toString() { 63 return "DeadlineRounds[" + rounds + "," + getDuration() + "]"; 66 return "DeadlineRounds[" + getRounds().toString() + "," 67 + getDurationMs().toString() + "]"; 64 68 } 65 69 } -
timeline/src/main/java/geniusweb/deadline/DeadlineTime.java
r52 r54 1 1 package geniusweb.deadline; 2 3 import org.eclipse.jdt.annotation.NonNull; 2 4 3 5 import com.fasterxml.jackson.annotation.JsonCreator; … … 13 15 * accuracy. 14 16 */ 15 private final Long durationms;17 private final @NonNull Long durationms; 16 18 17 19 /** 18 20 * 19 * @param millis number of milliseconds the session will be allowed to run21 * @param durationms number of milliseconds the session will be allowed to run 20 22 */ 21 23 @JsonCreator 22 public DeadlineTime(@JsonProperty("durationms") long millis) {23 if ( millis <= 0) {24 public DeadlineTime(@JsonProperty("durationms") long durationms) { 25 if (durationms <= 0) { 24 26 throw new IllegalArgumentException( 25 27 "deadline must be positive time"); 26 28 } 27 this.durationms = millis;29 this.durationms = durationms; 28 30 } 29 31 … … 33 35 */ 34 36 @Override 35 public Long getDuration() { 37 @NonNull 38 public Long getDurationMs() { 36 39 return durationms; 37 40 } 38 41 39 42 @Override 43 @NonNull 40 44 public String toString() { 41 return "DeadlineTime[" + durationms + "]";45 return "DeadlineTime[" + durationms.toString() + "]"; 42 46 } 43 47 -
timeline/src/main/java/geniusweb/progress/Progress.java
r52 r54 2 2 3 3 import java.util.Date; 4 5 import org.eclipse.jdt.annotation.NonNull; 4 6 5 7 import com.fasterxml.jackson.annotation.JsonAutoDetect; … … 34 36 * eye on {@link #getTerminationTime()}. 35 37 */ 36 Double get(Long currentTimeMs); 38 @NonNull 39 Double get(@NonNull Long currentTimeMs); 37 40 38 41 /** … … 44 47 * whether a party is out of time. 45 48 */ 49 @NonNull 46 50 Date getTerminationTime(); 47 51 … … 51 55 * @return true iff the progress has passed the deadline. 52 56 */ 53 boolean isPastDeadline( Long currentTimeMs);57 boolean isPastDeadline(@NonNull Long currentTimeMs); 54 58 } -
timeline/src/main/java/geniusweb/progress/ProgressFactory.java
r52 r54 2 2 3 3 import java.util.Date; 4 5 import org.eclipse.jdt.annotation.NonNull; 4 6 5 7 import geniusweb.deadline.Deadline; … … 15 17 * @return new Progress matching the deadline type. 16 18 */ 17 public static Progress create(Deadline deadline, Long nowms) { 19 public static Progress create(@NonNull Deadline deadline, 20 @NonNull Long nowms) { 18 21 if (deadline instanceof DeadlineRounds) { 19 22 return new ProgressRounds(((DeadlineRounds) deadline).getRounds(), 20 23 0, new Date( 21 nowms + ((DeadlineRounds) deadline).getDuration ()));24 nowms + ((DeadlineRounds) deadline).getDurationMs())); 22 25 } else { 23 return new ProgressTime(((DeadlineTime) deadline).getDuration (),26 return new ProgressTime(((DeadlineTime) deadline).getDurationMs(), 24 27 new Date(nowms)); 25 28 } -
timeline/src/main/java/geniusweb/progress/ProgressRounds.java
r52 r54 2 2 3 3 import java.util.Date; 4 5 import org.eclipse.jdt.annotation.NonNull; 4 6 5 7 import com.fasterxml.jackson.annotation.JsonCreator; … … 12 14 public class ProgressRounds implements Progress { 13 15 14 private final Integer duration;15 private final Integer currentRound;16 private final Date endtime;16 private final @NonNull Integer duration; 17 private final @NonNull Integer currentRound; 18 private final @NonNull Date endtime; 17 19 18 20 /** 19 21 * 20 * @param d eadlinelength max number of rounds, must be positive (not 0)22 * @param duration length max number of rounds, must be positive (not 0) 21 23 * @param currentRound the current round number (can be from 0 to deadlne). 22 24 * When = deadline, it means the progress has gone past 23 25 * the deadline. 24 * @param end 26 * @param endtime the termination time of this session. 25 27 */ 26 28 @JsonCreator 27 public ProgressRounds(@ JsonProperty("duration") Integer deadline,28 @ JsonProperty("currentRound") Integer currentRound,29 @ JsonProperty("endtime") Date end) {30 if (d eadline<= 0)29 public ProgressRounds(@NonNull @JsonProperty("duration") Integer duration, 30 @NonNull @JsonProperty("currentRound") Integer currentRound, 31 @NonNull @JsonProperty("endtime") Date endtime) { 32 if (duration == null || duration <= 0) 31 33 throw new IllegalArgumentException( 32 "deadline must be positive but is " + deadline); 33 if (currentRound < 0 || currentRound > deadline) { 34 "deadline must be positive but is " + duration.toString()); 35 if (currentRound == null || currentRound < 0 36 || currentRound > duration) { 34 37 throw new IllegalArgumentException( 35 "current round must be inside [0," + deadline + "]"); 38 "current round must be inside [0," + duration.toString() 39 + "]"); 36 40 } 37 this.duration = d eadline;41 this.duration = duration; 38 42 this.currentRound = currentRound; 39 this.endtime = end; 43 this.endtime = endtime; 44 } 45 46 public Integer getDuration() { 47 return duration; 48 } 49 50 public Date getEndTime() { 51 return endtime; 40 52 } 41 53 42 54 @Override 55 @NonNull 43 56 public Date getTerminationTime() { 44 57 return endtime; … … 52 65 * future developments. 53 66 */ 54 public Integer getCurrentRound() {67 public @NonNull Integer getCurrentRound() { 55 68 return currentRound; 56 69 } … … 63 76 * developments. 64 77 */ 65 public Integer getTotalRounds() {78 public @NonNull Integer getTotalRounds() { 66 79 return duration; 67 80 } 68 81 69 82 @Override 70 public Double get(Long currentTimeMs) {83 public @NonNull Double get(@NonNull Long currentTimeMs) { 71 84 // 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; 85 // fits 52 bits so this should not result in accuracy issues 86 double ratio = (double) currentRound / (double) (duration); 75 87 if (ratio > 1d) 76 88 ratio = 1d; … … 81 93 82 94 @Override 83 public boolean isPastDeadline( Long currentTimeMs) {95 public boolean isPastDeadline(@NonNull Long currentTimeMs) { 84 96 return currentRound >= duration || currentTimeMs > endtime.getTime(); 85 97 } … … 91 103 * the used protocol what exactly is a round. 92 104 */ 93 public ProgressRounds advance() {105 public @NonNull ProgressRounds advance() { 94 106 if (duration == currentRound) 95 107 return this; … … 131 143 132 144 @Override 145 @NonNull 133 146 public String toString() { 134 return "ProgressRounds[" + currentRound + " of " + duration + "]"; 147 return "ProgressRounds[" + currentRound.toString() + " of " 148 + duration.toString() + "]"; 135 149 } 136 150 -
timeline/src/main/java/geniusweb/progress/ProgressTime.java
r52 r54 2 2 3 3 import java.util.Date; 4 5 import org.eclipse.jdt.annotation.NonNull; 4 6 5 7 import com.fasterxml.jackson.annotation.JsonCreator; … … 20 22 * we do not use RFC 3339 because we need millisecond accuracy. 21 23 */ 22 private final Date start;24 private final @NonNull Date start; 23 25 24 26 /** … … 26 28 * accuracy. 27 29 */ 28 private final Long duration;30 private final @NonNull Long duration; 29 31 30 32 /** 31 33 * 32 * @param d the duration, eg {@link DeadlineTime#getDuration()}. Must be33 * > 0.34 * @param start the start Date (unix time in millisecs since 1970). See35 * {@link System#currentTimeMillis()}.34 * @param duration the duration, eg {@link DeadlineTime#getDurationMs()}. 35 * Must be > 0. 36 * @param start the start Date (unix time in millisecs since 1970). See 37 * {@link System#currentTimeMillis()}. 36 38 */ 37 39 @JsonCreator 38 public ProgressTime(@ JsonProperty("duration") Long d,39 @ JsonProperty("start") Date start) {40 public ProgressTime(@NonNull @JsonProperty("duration") Long duration, 41 @NonNull @JsonProperty("start") Date start) { 40 42 this.start = start; 41 duration = (d == null || d <= 0) ? 1 : d;43 this.duration = (duration == null || duration <= 0) ? 1 : duration; 42 44 } 43 45 44 46 @Override 45 public Double get(Long currentTimeMs) {47 public @NonNull Double get(@NonNull Long currentTimeMs) { 46 48 long delta = currentTimeMs - start.getTime(); 47 49 // double should have ~53 digits of precision, and we're computing … … 49 51 // 2^53 millis seems plenty for our purposes so no need to use 50 52 // BigDecimal here. 51 Double ratio = delta / (double) duration; 53 @NonNull 54 Double ratio = delta / Double.valueOf(duration); 52 55 if (ratio > 1d) 53 56 ratio = 1d; … … 58 61 59 62 @Override 60 public boolean isPastDeadline( Long currentTimeMs) {63 public boolean isPastDeadline(@NonNull Long currentTimeMs) { 61 64 return currentTimeMs > start.getTime() + duration; 62 65 } … … 69 72 * 70 73 */ 71 public Date getStart() {74 public @NonNull Date getStart() { 72 75 return start; 73 76 } … … 82 85 83 86 @Override 87 @NonNull 84 88 public Date getTerminationTime() { 85 89 return new Date(start.getTime() + duration); … … 87 91 88 92 @Override 93 @NonNull 89 94 public String toString() { 90 return "ProgressTime[" + start.getTime() + " , " + duration + "ms]"; 95 return "ProgressTime[" + String.valueOf(start.getTime()) + " , " 96 + duration.toString() + "ms]"; 91 97 } 92 98 -
timeline/src/test/java/geniusweb/deadline/DeadlineTest.java
r52 r54 7 7 import java.util.List; 8 8 9 import org.eclipse.jdt.annotation.NonNull; 9 10 import org.junit.Test; 11 import org.junit.internal.runners.JUnit4ClassRunner; 12 import org.junit.runner.RunWith; 10 13 11 14 import com.fasterxml.jackson.core.JsonParseException; … … 16 19 import tudelft.utilities.junit.GeneralTests; 17 20 21 @RunWith(JUnit4ClassRunner.class) 18 22 public class DeadlineTest extends GeneralTests<Deadline> { 19 private final ObjectMapper jackson = new ObjectMapper(); 20 private final String deadlinestring = "{\"DeadlineRounds\":{\"rounds\":100,\"durationms\":999}}"; 21 private final DeadlineRounds deadlineRounds = new DeadlineRounds(100, 999); 22 private final DeadlineRounds deadlineRounds1 = new DeadlineRounds(100, 999); 23 private final DeadlineRounds deadlineRounds2 = new DeadlineRounds(100, 24 9999); 25 private final String deadlinetimestring = "{\"DeadlineTime\":{\"durationms\":2000}}"; 26 private final DeadlineTime deadlineTime = new DeadlineTime(2000); 23 private final @NonNull ObjectMapper jackson = new ObjectMapper(); 24 private final @NonNull String deadlinestring = "{\"DeadlineRounds\":{\"rounds\":100,\"durationms\":999}}"; 25 private final @NonNull DeadlineRounds deadlineRounds = new DeadlineRounds( 26 100, 999); 27 private final @NonNull DeadlineRounds deadlineRounds1 = new DeadlineRounds( 28 100, 999); 29 private final @NonNull DeadlineRounds deadlineRounds2 = new DeadlineRounds( 30 100, 9999); 31 private final @NonNull String deadlinetimestring = "{\"DeadlineTime\":{\"durationms\":2000}}"; 32 private final @NonNull DeadlineTime deadlineTime = new DeadlineTime(2000); 27 33 28 34 @Override … … 40 46 @Test 41 47 public void serializeRoundsTest() throws JsonProcessingException { 42 String text = jackson.writeValueAsString(deadlineRounds);48 final @NonNull String text = jackson.writeValueAsString(deadlineRounds); 43 49 assertEquals(deadlinestring, text); 44 50 … … 55 61 @Test 56 62 public void serializeTimeTest() throws JsonProcessingException { 57 String text = jackson.writeValueAsString(deadlineTime);63 final @NonNull String text = jackson.writeValueAsString(deadlineTime); 58 64 assertEquals(deadlinetimestring, text); 59 65 -
timeline/src/test/java/geniusweb/progress/ProgressRoundsTest.java
r52 r54 13 13 import java.util.List; 14 14 15 import org. junit.Before;15 import org.eclipse.jdt.annotation.NonNull; 16 16 import org.junit.Test; 17 import org.junit.internal.runners.JUnit4ClassRunner; 18 import org.junit.runner.RunWith; 17 19 18 20 import com.fasterxml.jackson.core.JsonParseException; … … 24 26 import tudelft.utilities.junit.GeneralTests; 25 27 28 @RunWith(JUnit4ClassRunner.class) 26 29 public class ProgressRoundsTest extends GeneralTests<Progress> { 27 private DeadlineRounds deadline = mock(DeadlineRounds.class);28 private final static Integer TESTROUNDS = 10;30 private @NonNull DeadlineRounds deadline = mock(DeadlineRounds.class); 31 private final static @NonNull Integer TESTROUNDS = 10; 29 32 private final static long TESTTIME = 10; // milliseconds! 30 33 31 private final Date date = new Date(98765l);32 private final Date date0 = new Date(100000l);34 private final @NonNull Date date = new Date(98765l); 35 private final @NonNull Date date0 = new Date(100000l); 33 36 34 private final Progress deadline1 = new ProgressRounds(20, 0, date);35 private final Progress deadline2 = new ProgressRounds(20, 0, date);36 private final Progress deadline3 = new ProgressRounds(30, 0, date);37 private final Progress deadline4 = new ProgressTime(20l, date0);38 private final Progress deadline5 = new ProgressTime(20l, date);39 private ProgressRounds progress;40 private final ObjectMapper jackson = new ObjectMapper();41 private final String progressstring = "{\"ProgressRounds\":{\"duration\":10,\"currentRound\":0,\"endtime\":98765}}";37 private final @NonNull Progress deadline1 = new ProgressRounds(20, 0, date); 38 private final @NonNull Progress deadline2 = new ProgressRounds(20, 0, date); 39 private final @NonNull Progress deadline3 = new ProgressRounds(30, 0, date); 40 private final @NonNull Progress deadline4 = new ProgressTime(20l, date0); 41 private final @NonNull Progress deadline5 = new ProgressTime(20l, date); 42 private @NonNull ProgressRounds progress; 43 private final static @NonNull ObjectMapper jackson = new ObjectMapper(); 44 private final static @NonNull String progressstring = "{\"ProgressRounds\":{\"duration\":10,\"currentRound\":0,\"endtime\":98765}}"; 42 45 43 46 private final long start = 1234; 44 private final Date end = new Date(start + TESTTIME); 45 private final ProgressRounds pr = new ProgressRounds(TESTROUNDS, 4, end); 47 private final @NonNull Date end = new Date(start + TESTTIME); 48 private final @NonNull ProgressRounds pr = new ProgressRounds(TESTROUNDS, 4, 49 end); 46 50 47 @Before 48 public void before() { 51 public ProgressRoundsTest() { 49 52 when(deadline.getRounds()).thenReturn(TESTROUNDS); 50 53 progress = new ProgressRounds(deadline.getRounds(), 0, date); … … 54 57 @Override 55 58 public List<List<Progress>> getGeneralTestData() { 56 List<List<Progress>> list = new LinkedList<>(); 57 list.add(Arrays.asList(deadline1, deadline2)); 58 list.add(Arrays.asList(deadline3)); 59 list.add(Arrays.asList(deadline4)); 60 list.add(Arrays.asList(deadline5)); 61 return list; 59 @NonNull 60 List<@NonNull List<@NonNull Progress>> alist = new LinkedList<>(); 61 alist.add(Arrays.asList(deadline1, deadline2)); 62 alist.add(Arrays.asList(deadline3)); 63 alist.add(Arrays.asList(deadline4)); 64 alist.add(Arrays.asList(deadline5)); 65 return alist; 62 66 } 63 67 … … 81 85 @Test 82 86 public void testAdvance() { 87 @NonNull 83 88 ProgressRounds p = progress; 84 89 int n = 0; 85 90 while (n <= TESTROUNDS) { 86 91 assertEquals((Double) ((double) n / TESTROUNDS), p.get(1l)); 87 n ++;92 n += 1; 88 93 p = p.advance(); 89 94 } … … 92 97 @Test 93 98 public void testAdvanceWhenDeadlineReached() { 99 @NonNull 94 100 ProgressRounds p = new ProgressRounds(TESTROUNDS, 9, date); 95 101 p = p.advance(); … … 99 105 @Test 100 106 public void testSerialize() throws JsonProcessingException { 101 System.out.println(jackson.writeValueAsString(progress));107 //System.out.println(jackson.writeValueAsString(progress)); 102 108 assertEquals(progressstring, jackson.writeValueAsString(progress)); 103 109 -
timeline/src/test/java/geniusweb/progress/ProgressTimeTest.java
r52 r54 11 11 import java.util.List; 12 12 13 import org.eclipse.jdt.annotation.NonNull; 13 14 import org.junit.Test; 15 import org.junit.internal.runners.JUnit4ClassRunner; 16 import org.junit.runner.RunWith; 14 17 15 18 import com.fasterxml.jackson.core.JsonParseException; … … 20 23 import tudelft.utilities.junit.GeneralTests; 21 24 25 @RunWith(JUnit4ClassRunner.class) 22 26 public class ProgressTimeTest extends GeneralTests<Progress> { 23 27 // we set T0 quite high because Python has trouble handling small datetime 24 28 private static final long T0 = 100000l; 25 private static final Date TIMEZERO = new Date(T0);29 private static final @NonNull Date TIMEZERO = new Date(T0); 26 30 private final static long TESTTIME = 10; // milliseconds! 27 private final ProgressTime progress1 = new ProgressTime(TESTTIME, TIMEZERO); 28 private final ProgressTime progress1a = new ProgressTime(TESTTIME, 31 private final @NonNull ProgressTime progress1 = new ProgressTime(TESTTIME, 29 32 TIMEZERO); 30 private final ProgressTime progress2 = new ProgressTime(200l, TIMEZERO); 33 private final @NonNull ProgressTime progress1a = new ProgressTime(TESTTIME, 34 TIMEZERO); 35 private final @NonNull ProgressTime progress2 = new ProgressTime(200l, 36 TIMEZERO); 31 37 32 private final ObjectMapper jackson = new ObjectMapper();33 private final String progressstring = "{\"ProgressTime\":{\"duration\":10,\"start\":100000}}";38 private final static ObjectMapper jackson = new ObjectMapper(); 39 private final @NonNull String progressstring = "{\"ProgressTime\":{\"duration\":10,\"start\":100000}}"; 34 40 35 41 private final long start = 1234; 36 private final ProgressTime pr = new ProgressTime(TESTTIME, new Date(start)); 42 private final @NonNull ProgressTime pr = new ProgressTime(TESTTIME, 43 new Date(start)); 37 44 38 45 @Override 39 46 public List<List<Progress>> getGeneralTestData() { 40 List<List<Progress>> list = new LinkedList<>();41 list.add(Arrays.asList(progress1, progress1a));42 list.add(Arrays.asList(progress2));43 return list;47 List<List<Progress>> alist = new LinkedList<>(); 48 alist.add(Arrays.asList(progress1, progress1a)); 49 alist.add(Arrays.asList(progress2)); 50 return alist; 44 51 } 45 52 … … 57 64 @Test 58 65 public void testAdvancingTime() throws InterruptedException { 59 for (long n = 0; n <= TESTTIME; n ++) {66 for (long n = 0; n <= TESTTIME; n += 1) { 60 67 assertEquals((Double) ((double) n / TESTTIME), 61 68 progress1.get(T0 + n)); … … 65 72 @Test 66 73 public void testAdvanceWhenDeadlineReached() throws InterruptedException { 67 ProgressTime progress = new ProgressTime(TESTTIME, TIMEZERO); 74 final @NonNull ProgressTime progress = new ProgressTime(TESTTIME, 75 TIMEZERO); 68 76 assertEquals((Double) 1d, progress1.get(T0 + TESTTIME)); 69 77 } … … 71 79 @Test 72 80 public void testSerialize() throws JsonProcessingException { 73 String actual = jackson.writeValueAsString(progress1);74 System.out.println(actual);81 final @NonNull String actual = jackson.writeValueAsString(progress1); 82 //System.out.println(actual); 75 83 assertEquals(progressstring, actual); 76 84 … … 80 88 public void testDeserialize() 81 89 throws JsonParseException, JsonMappingException, IOException { 82 ProgressTime newprog = jackson.readValue(progressstring,90 final @NonNull ProgressTime newprog = jackson.readValue(progressstring, 83 91 ProgressTime.class); 84 // we can't directly compare with progress since that's a hacked 85 // object... 92 // we can't directly compare with progress : that's a hacked object. 86 93 assertEquals(TESTTIME, newprog.getDuration().intValue()); 87 94 assertEquals(T0, newprog.getStart().getTime());
Note:
See TracChangeset
for help on using the changeset viewer.