Changeset 54 for timeline/src


Ignore:
Timestamp:
Jul 23, 2025, 10:16:55 AM (2 months ago)
Author:
ruud
Message:

Modifications for automatic java to python conversion. Overloaded methods now have different names.

Location:
timeline/src
Files:
10 edited

Legend:

Unmodified
Added
Removed
  • timeline/src/main/java/geniusweb/deadline/Deadline.java

    r52 r54  
    11package geniusweb.deadline;
     2
     3import org.eclipse.jdt.annotation.NonNull;
    24
    35import com.fasterxml.jackson.annotation.JsonAutoDetect;
     
    2123         * @return the duration of this deadline, measured in milliseconds
    2224         */
    23         public Long getDuration();
     25        public @NonNull Long getDurationMs();
    2426
    2527}
  • timeline/src/main/java/geniusweb/deadline/DeadlineRounds.java

    r52 r54  
    11package geniusweb.deadline;
     2
     3import org.eclipse.jdt.annotation.NonNull;
    24
    35import com.fasterxml.jackson.annotation.JsonCreator;
     
    1113 */
    1214public class DeadlineRounds extends DeadlineTime {
    13         private final Integer rounds;
     15        private final @NonNull Integer rounds;
    1416
    1517        /**
     
    2022         */
    2123        @JsonCreator
    22         public DeadlineRounds(@JsonProperty("rounds") Integer rounds,
     24        public DeadlineRounds(@JsonProperty("rounds") @NonNull Integer rounds,
    2325                        @JsonProperty("durationms") long durationms) {
    2426                super(durationms);
     
    3032        }
    3133
    32         public Integer getRounds() {
     34        public @NonNull Integer getRounds() {
    3335                return rounds;
    3436        }
     
    6062
    6163        @Override
     64        @NonNull
    6265        public String toString() {
    63                 return "DeadlineRounds[" + rounds + "," + getDuration() + "]";
     66                return "DeadlineRounds[" + getRounds().toString() + ","
     67                                + getDurationMs().toString() + "]";
    6468        }
    6569}
  • timeline/src/main/java/geniusweb/deadline/DeadlineTime.java

    r52 r54  
    11package geniusweb.deadline;
     2
     3import org.eclipse.jdt.annotation.NonNull;
    24
    35import com.fasterxml.jackson.annotation.JsonCreator;
     
    1315         * accuracy.
    1416         */
    15         private final Long durationms;
     17        private final @NonNull Long durationms;
    1618
    1719        /**
    1820         *
    19          * @param millis number of milliseconds the session will be allowed to run
     21         * @param durationms number of milliseconds the session will be allowed to run
    2022         */
    2123        @JsonCreator
    22         public DeadlineTime(@JsonProperty("durationms") long millis) {
    23                 if (millis <= 0) {
     24        public DeadlineTime(@JsonProperty("durationms") long durationms) {
     25                if (durationms <= 0) {
    2426                        throw new IllegalArgumentException(
    2527                                        "deadline must be positive time");
    2628                }
    27                 this.durationms = millis;
     29                this.durationms = durationms;
    2830        }
    2931
     
    3335         */
    3436        @Override
    35         public Long getDuration() {
     37        @NonNull
     38        public Long getDurationMs() {
    3639                return durationms;
    3740        }
    3841
    3942        @Override
     43        @NonNull
    4044        public String toString() {
    41                 return "DeadlineTime[" + durationms + "]";
     45                return "DeadlineTime[" + durationms.toString() + "]";
    4246        }
    4347
  • timeline/src/main/java/geniusweb/progress/Progress.java

    r52 r54  
    22
    33import java.util.Date;
     4
     5import org.eclipse.jdt.annotation.NonNull;
    46
    57import com.fasterxml.jackson.annotation.JsonAutoDetect;
     
    3436         *         eye on {@link #getTerminationTime()}.
    3537         */
    36         Double get(Long currentTimeMs);
     38        @NonNull
     39        Double get(@NonNull Long currentTimeMs);
    3740
    3841        /**
     
    4447         *         whether a party is out of time.
    4548         */
     49        @NonNull
    4650        Date getTerminationTime();
    4751
     
    5155         * @return true iff the progress has passed the deadline.
    5256         */
    53         boolean isPastDeadline(Long currentTimeMs);
     57        boolean isPastDeadline(@NonNull Long currentTimeMs);
    5458}
  • timeline/src/main/java/geniusweb/progress/ProgressFactory.java

    r52 r54  
    22
    33import java.util.Date;
     4
     5import org.eclipse.jdt.annotation.NonNull;
    46
    57import geniusweb.deadline.Deadline;
     
    1517         * @return new Progress matching the deadline type.
    1618         */
    17         public static Progress create(Deadline deadline, Long nowms) {
     19        public static Progress create(@NonNull Deadline deadline,
     20                        @NonNull Long nowms) {
    1821                if (deadline instanceof DeadlineRounds) {
    1922                        return new ProgressRounds(((DeadlineRounds) deadline).getRounds(),
    2023                                        0, new Date(
    21                                                         nowms + ((DeadlineRounds) deadline).getDuration()));
     24                                                        nowms + ((DeadlineRounds) deadline).getDurationMs()));
    2225                } else {
    23                         return new ProgressTime(((DeadlineTime) deadline).getDuration(),
     26                        return new ProgressTime(((DeadlineTime) deadline).getDurationMs(),
    2427                                        new Date(nowms));
    2528                }
  • timeline/src/main/java/geniusweb/progress/ProgressRounds.java

    r52 r54  
    22
    33import java.util.Date;
     4
     5import org.eclipse.jdt.annotation.NonNull;
    46
    57import com.fasterxml.jackson.annotation.JsonCreator;
     
    1214public class ProgressRounds implements Progress {
    1315
    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;
    1719
    1820        /**
    1921         *
    20          * @param deadline     length max number of rounds, must be positive (not 0)
     22         * @param duration     length max number of rounds, must be positive (not 0)
    2123         * @param currentRound the current round number (can be from 0 to deadlne).
    2224         *                     When = deadline, it means the progress has gone past
    2325         *                     the deadline.
    24          * @param end          the termination time of this session.
     26         * @param endtime      the termination time of this session.
    2527         */
    2628        @JsonCreator
    27         public ProgressRounds(@JsonProperty("duration") Integer deadline,
    28                         @JsonProperty("currentRound") Integer currentRound,
    29                         @JsonProperty("endtime") Date end) {
    30                 if (deadline <= 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)
    3133                        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) {
    3437                        throw new IllegalArgumentException(
    35                                         "current round must be inside [0," + deadline + "]");
     38                                        "current round must be inside [0," + duration.toString()
     39                                                        + "]");
    3640                }
    37                 this.duration = deadline;
     41                this.duration = duration;
    3842                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;
    4052        }
    4153
    4254        @Override
     55        @NonNull
    4356        public Date getTerminationTime() {
    4457                return endtime;
     
    5265         *         future developments.
    5366         */
    54         public Integer getCurrentRound() {
     67        public @NonNull Integer getCurrentRound() {
    5568                return currentRound;
    5669        }
     
    6376         *         developments.
    6477         */
    65         public Integer getTotalRounds() {
     78        public @NonNull Integer getTotalRounds() {
    6679                return duration;
    6780        }
    6881
    6982        @Override
    70         public Double get(Long currentTimeMs) {
     83        public @NonNull Double get(@NonNull Long currentTimeMs) {
    7184                // 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);
    7587                if (ratio > 1d)
    7688                        ratio = 1d;
     
    8193
    8294        @Override
    83         public boolean isPastDeadline(Long currentTimeMs) {
     95        public boolean isPastDeadline(@NonNull Long currentTimeMs) {
    8496                return currentRound >= duration || currentTimeMs > endtime.getTime();
    8597        }
     
    91103         *         the used protocol what exactly is a round.
    92104         */
    93         public ProgressRounds advance() {
     105        public @NonNull ProgressRounds advance() {
    94106                if (duration == currentRound)
    95107                        return this;
     
    131143
    132144        @Override
     145        @NonNull
    133146        public String toString() {
    134                 return "ProgressRounds[" + currentRound + " of " + duration + "]";
     147                return "ProgressRounds[" + currentRound.toString() + " of "
     148                                + duration.toString() + "]";
    135149        }
    136150
  • timeline/src/main/java/geniusweb/progress/ProgressTime.java

    r52 r54  
    22
    33import java.util.Date;
     4
     5import org.eclipse.jdt.annotation.NonNull;
    46
    57import com.fasterxml.jackson.annotation.JsonCreator;
     
    2022         * we do not use RFC 3339 because we need millisecond accuracy.
    2123         */
    22         private final Date start;
     24        private final @NonNull Date start;
    2325
    2426        /**
     
    2628         * accuracy.
    2729         */
    28         private final Long duration;
     30        private final @NonNull Long duration;
    2931
    3032        /**
    3133         *
    32          * @param d     the duration, eg {@link DeadlineTime#getDuration()}. Must be
    33          *              &gt; 0.
    34          * @param start the start Date (unix time in millisecs since 1970). See
    35          *              {@link System#currentTimeMillis()}.
     34         * @param duration the duration, eg {@link DeadlineTime#getDurationMs()}.
     35         *                 Must be &gt; 0.
     36         * @param start    the start Date (unix time in millisecs since 1970). See
     37         *                 {@link System#currentTimeMillis()}.
    3638         */
    3739        @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) {
    4042                this.start = start;
    41                 duration = (d == null || d <= 0) ? 1 : d;
     43                this.duration = (duration == null || duration <= 0) ? 1 : duration;
    4244        }
    4345
    4446        @Override
    45         public Double get(Long currentTimeMs) {
     47        public @NonNull Double get(@NonNull Long currentTimeMs) {
    4648                long delta = currentTimeMs - start.getTime();
    4749                // double should have ~53 digits of precision, and we're computing
     
    4951                // 2^53 millis seems plenty for our purposes so no need to use
    5052                // BigDecimal here.
    51                 Double ratio = delta / (double) duration;
     53                @NonNull
     54                Double ratio = delta / Double.valueOf(duration);
    5255                if (ratio > 1d)
    5356                        ratio = 1d;
     
    5861
    5962        @Override
    60         public boolean isPastDeadline(Long currentTimeMs) {
     63        public boolean isPastDeadline(@NonNull Long currentTimeMs) {
    6164                return currentTimeMs > start.getTime() + duration;
    6265        }
     
    6972         *
    7073         */
    71         public Date getStart() {
     74        public @NonNull Date getStart() {
    7275                return start;
    7376        }
     
    8285
    8386        @Override
     87        @NonNull
    8488        public Date getTerminationTime() {
    8589                return new Date(start.getTime() + duration);
     
    8791
    8892        @Override
     93        @NonNull
    8994        public String toString() {
    90                 return "ProgressTime[" + start.getTime() + " , " + duration + "ms]";
     95                return "ProgressTime[" + String.valueOf(start.getTime()) + " , "
     96                                + duration.toString() + "ms]";
    9197        }
    9298
  • timeline/src/test/java/geniusweb/deadline/DeadlineTest.java

    r52 r54  
    77import java.util.List;
    88
     9import org.eclipse.jdt.annotation.NonNull;
    910import org.junit.Test;
     11import org.junit.internal.runners.JUnit4ClassRunner;
     12import org.junit.runner.RunWith;
    1013
    1114import com.fasterxml.jackson.core.JsonParseException;
     
    1619import tudelft.utilities.junit.GeneralTests;
    1720
     21@RunWith(JUnit4ClassRunner.class)
    1822public 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);
    2733
    2834        @Override
     
    4046        @Test
    4147        public void serializeRoundsTest() throws JsonProcessingException {
    42                 String text = jackson.writeValueAsString(deadlineRounds);
     48                final @NonNull String text = jackson.writeValueAsString(deadlineRounds);
    4349                assertEquals(deadlinestring, text);
    4450
     
    5561        @Test
    5662        public void serializeTimeTest() throws JsonProcessingException {
    57                 String text = jackson.writeValueAsString(deadlineTime);
     63                final @NonNull String text = jackson.writeValueAsString(deadlineTime);
    5864                assertEquals(deadlinetimestring, text);
    5965
  • timeline/src/test/java/geniusweb/progress/ProgressRoundsTest.java

    r52 r54  
    1313import java.util.List;
    1414
    15 import org.junit.Before;
     15import org.eclipse.jdt.annotation.NonNull;
    1616import org.junit.Test;
     17import org.junit.internal.runners.JUnit4ClassRunner;
     18import org.junit.runner.RunWith;
    1719
    1820import com.fasterxml.jackson.core.JsonParseException;
     
    2426import tudelft.utilities.junit.GeneralTests;
    2527
     28@RunWith(JUnit4ClassRunner.class)
    2629public 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;
    2932        private final static long TESTTIME = 10; // milliseconds!
    3033
    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);
    3336
    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}}";
    4245
    4346        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);
    4650
    47         @Before
    48         public void before() {
     51        public ProgressRoundsTest() {
    4952                when(deadline.getRounds()).thenReturn(TESTROUNDS);
    5053                progress = new ProgressRounds(deadline.getRounds(), 0, date);
     
    5457        @Override
    5558        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;
    6266        }
    6367
     
    8185        @Test
    8286        public void testAdvance() {
     87                @NonNull
    8388                ProgressRounds p = progress;
    8489                int n = 0;
    8590                while (n <= TESTROUNDS) {
    8691                        assertEquals((Double) ((double) n / TESTROUNDS), p.get(1l));
    87                         n++;
     92                        n += 1;
    8893                        p = p.advance();
    8994                }
     
    9297        @Test
    9398        public void testAdvanceWhenDeadlineReached() {
     99                @NonNull
    94100                ProgressRounds p = new ProgressRounds(TESTROUNDS, 9, date);
    95101                p = p.advance();
     
    99105        @Test
    100106        public void testSerialize() throws JsonProcessingException {
    101                 System.out.println(jackson.writeValueAsString(progress));
     107                //System.out.println(jackson.writeValueAsString(progress));
    102108                assertEquals(progressstring, jackson.writeValueAsString(progress));
    103109
  • timeline/src/test/java/geniusweb/progress/ProgressTimeTest.java

    r52 r54  
    1111import java.util.List;
    1212
     13import org.eclipse.jdt.annotation.NonNull;
    1314import org.junit.Test;
     15import org.junit.internal.runners.JUnit4ClassRunner;
     16import org.junit.runner.RunWith;
    1417
    1518import com.fasterxml.jackson.core.JsonParseException;
     
    2023import tudelft.utilities.junit.GeneralTests;
    2124
     25@RunWith(JUnit4ClassRunner.class)
    2226public class ProgressTimeTest extends GeneralTests<Progress> {
    2327        // we set T0 quite high because Python has trouble handling small datetime
    2428        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);
    2630        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,
    2932                        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);
    3137
    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}}";
    3440
    3541        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));
    3744
    3845        @Override
    3946        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;
    4451        }
    4552
     
    5764        @Test
    5865        public void testAdvancingTime() throws InterruptedException {
    59                 for (long n = 0; n <= TESTTIME; n++) {
     66                for (long n = 0; n <= TESTTIME; n += 1) {
    6067                        assertEquals((Double) ((double) n / TESTTIME),
    6168                                        progress1.get(T0 + n));
     
    6572        @Test
    6673        public void testAdvanceWhenDeadlineReached() throws InterruptedException {
    67                 ProgressTime progress = new ProgressTime(TESTTIME, TIMEZERO);
     74                final @NonNull ProgressTime progress = new ProgressTime(TESTTIME,
     75                                TIMEZERO);
    6876                assertEquals((Double) 1d, progress1.get(T0 + TESTTIME));
    6977        }
     
    7179        @Test
    7280        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);
    7583                assertEquals(progressstring, actual);
    7684
     
    8088        public void testDeserialize()
    8189                        throws JsonParseException, JsonMappingException, IOException {
    82                 ProgressTime newprog = jackson.readValue(progressstring,
     90                final @NonNull ProgressTime newprog = jackson.readValue(progressstring,
    8391                                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.
    8693                assertEquals(TESTTIME, newprog.getDuration().intValue());
    8794                assertEquals(T0, newprog.getStart().getTime());
Note: See TracChangeset for help on using the changeset viewer.