Skip to content

Commit 4bb8876

Browse files
zutshiympkorstanje
authored andcommitted
[Core] Replace TimeService with Java Time API (#1620)
## Summary Migrating the current code to use Java 8 Time API fields instead of the currently used primitive fields. ## Details The long fields `time`, `timeStampMillis` and `duration` have been replaced by `Instant` and `Duration` respectively. These values can now be used instead of older primitives to allow for better representation of time and duration in cucumber without having to worry about precision or granularity. ## Motivation and Context To allow for more consistent time measurements and reduce custom code.
1 parent d785bc4 commit 4bb8876

File tree

71 files changed

+583
-614
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

71 files changed

+583
-614
lines changed

Diff for: core/src/main/java/io/cucumber/core/api/event/CanonicalEventOrder.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public int compare(TestCaseEvent a, TestCaseEvent b) {
7272
return line;
7373
}
7474

75-
return Long.compare(a.getTimeStamp(), b.getTimeStamp());
75+
return a.getInstant().compareTo(b.getInstant());
7676
}
7777
}
7878
}

Diff for: core/src/main/java/io/cucumber/core/api/event/EmbedEvent.java

+4-7
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,13 @@
11
package io.cucumber.core.api.event;
22

3+
import java.time.Instant;
4+
35
public final class EmbedEvent extends TestCaseEvent {
46
public final byte[] data;
57
public final String mimeType;
68

7-
@Deprecated
8-
public EmbedEvent(Long timeStamp, TestCase testCase, byte[] data, String mimeType) {
9-
this(timeStamp, 0, testCase, data, mimeType);
10-
}
11-
12-
public EmbedEvent(Long timeStamp, long timeStampMillis, TestCase testCase, byte[] data, String mimeType) {
13-
super(timeStamp, timeStampMillis, testCase);
9+
public EmbedEvent(Instant timeInstant, TestCase testCase, byte[] data, String mimeType) {
10+
super(timeInstant, testCase);
1411
this.data = data;
1512
this.mimeType = mimeType;
1613
}

Diff for: core/src/main/java/io/cucumber/core/api/event/Event.java

+5-7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package io.cucumber.core.api.event;
22

3+
import java.time.Instant;
34
import java.util.Comparator;
45

56
public interface Event {
@@ -31,13 +32,10 @@ public interface Event {
3132
Comparator<Event> CANONICAL_ORDER = new CanonicalEventOrder();
3233

3334
/**
34-
* Returns timestamp in nano seconds since an arbitrary start time.
35+
* Returns instant from epoch.
3536
*
36-
* @return timestamp in nano seconds
37-
* @see System#nanoTime()
38-
* @deprecated prefer {@link TimeStampedEvent#getTimeStampMillis()}
37+
* @return time instant in Instant
38+
* @see Instant#now()
3939
*/
40-
@Deprecated
41-
Long getTimeStamp();
42-
40+
Instant getInstant();
4341
}

Diff for: core/src/main/java/io/cucumber/core/api/event/Result.java

+9-7
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22

33
import java.io.PrintWriter;
44
import java.io.StringWriter;
5+
import java.time.Duration;
56
import java.util.Comparator;
67
import java.util.Objects;
78

9+
import static java.time.Duration.ZERO;
810
import static java.util.Locale.ROOT;
911
import static java.util.Objects.requireNonNull;
1012

@@ -19,9 +21,9 @@ public int compare(Result a, Result b) {
1921
};
2022

2123
private final Type status;
22-
private final Long duration;
24+
private final Duration duration;
2325
private final Throwable error;
24-
public static final Result UNDEFINED = new Result(Result.Type.UNDEFINED, 0L, null);
26+
public static final Result UNDEFINED = new Result(Result.Type.UNDEFINED, ZERO, null);
2527
public enum Type {
2628
PASSED,
2729
SKIPPED,
@@ -49,10 +51,10 @@ public String firstLetterCapitalizedName() {
4951
* The result of a step or scenario
5052
*
5153
* @param status status of the step or scenario
52-
* @param duration the duration in nanoseconds
54+
* @param duration the duration
5355
* @param error the error that caused the failure if any
5456
*/
55-
public Result(Result.Type status, Long duration, Throwable error) {
57+
public Result(Result.Type status, Duration duration, Throwable error) {
5658
this.status = requireNonNull(status);
5759
this.duration = requireNonNull(duration);
5860
this.error = error;
@@ -62,10 +64,10 @@ public Result.Type getStatus() {
6264
return status;
6365
}
6466

65-
public Long getDuration() {
67+
public Duration getDuration() {
6668
return duration;
6769
}
68-
70+
6971
public String getErrorMessage() {
7072
return error != null ? getErrorMessage(error) : null;
7173
}
@@ -101,7 +103,7 @@ private String getErrorMessage(Throwable error) {
101103
public String toString() {
102104
return "Result{" +
103105
"status=" + status +
104-
", duration=" + duration +
106+
", duration=" + duration.getSeconds() +
105107
", error=" + error +
106108
'}';
107109
}

Diff for: core/src/main/java/io/cucumber/core/api/event/SnippetsSuggestedEvent.java

+3-7
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import gherkin.pickles.PickleLocation;
44

5+
import java.time.Instant;
56
import java.util.Collections;
67
import java.util.List;
78

@@ -10,13 +11,8 @@ public class SnippetsSuggestedEvent extends TimeStampedEvent {
1011
public final List<PickleLocation> stepLocations;
1112
public final List<String> snippets;
1213

13-
@Deprecated
14-
public SnippetsSuggestedEvent(Long timeStamp, String uri, List<PickleLocation> stepLocations, List<String> snippets) {
15-
this(timeStamp, 0, uri, stepLocations, snippets);
16-
}
17-
18-
public SnippetsSuggestedEvent(Long timeStamp, long timeStampMillis, String uri, List<PickleLocation> stepLocations, List<String> snippets) {
19-
super(timeStamp, timeStampMillis);
14+
public SnippetsSuggestedEvent(Instant timeInstant, String uri, List<PickleLocation> stepLocations, List<String> snippets) {
15+
super(timeInstant);
2016
this.uri = uri;
2117
this.stepLocations = stepLocations;
2218
this.snippets = Collections.unmodifiableList(snippets);

Diff for: core/src/main/java/io/cucumber/core/api/event/TestCaseEvent.java

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
package io.cucumber.core.api.event;
22

3+
import java.time.Instant;
4+
35
public abstract class TestCaseEvent extends TimeStampedEvent {
46

57
private final TestCase testCase;
68

7-
TestCaseEvent(Long timeStamp, long timeStampMillis, TestCase testCase) {
8-
super(timeStamp, timeStampMillis);
9+
TestCaseEvent(Instant timeInstant, TestCase testCase) {
10+
super(timeInstant);
911
this.testCase = testCase;
1012
}
1113

Original file line numberDiff line numberDiff line change
@@ -1,18 +1,14 @@
11
package io.cucumber.core.api.event;
22

3+
import java.time.Instant;
4+
35
public final class TestCaseFinished extends TestCaseEvent {
46
public final Result result;
57
public final TestCase testCase;
68

7-
@Deprecated
8-
public TestCaseFinished(Long timeStamp, TestCase testCase, Result result) {
9-
this(timeStamp, 0, testCase, result);
10-
}
11-
12-
public TestCaseFinished(Long timeStamp, long timeStampMillis, TestCase testCase, Result result) {
13-
super(timeStamp, timeStampMillis, testCase);
14-
this.testCase = testCase;
15-
this.result = result;
16-
}
17-
9+
public TestCaseFinished(Instant timeInstant, TestCase testCase, Result result) {
10+
super(timeInstant, testCase);
11+
this.testCase = testCase;
12+
this.result = result;
13+
}
1814
}
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,13 @@
11
package io.cucumber.core.api.event;
22

3+
import java.time.Instant;
4+
35
public final class TestCaseStarted extends TestCaseEvent {
46
public final TestCase testCase;
57

6-
@Deprecated
7-
public TestCaseStarted(Long timeStamp, TestCase testCase) {
8-
this(timeStamp, 0L, testCase);
9-
}
10-
11-
public TestCaseStarted(Long timeStamp, long timeStampMillis, TestCase testCase) {
12-
super(timeStamp, timeStampMillis, testCase);
13-
this.testCase = testCase;
14-
}
8+
public TestCaseStarted(Instant timeInstant, TestCase testCase) {
9+
super(timeInstant, testCase);
10+
this.testCase = testCase;
11+
}
1512

1613
}
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
11
package io.cucumber.core.api.event;
22

3-
public final class TestRunFinished extends TimeStampedEvent {
3+
import java.time.Instant;
44

5-
@Deprecated
6-
public TestRunFinished(Long timeStamp) {
7-
this(timeStamp, 0);
8-
}
5+
public final class TestRunFinished extends TimeStampedEvent {
96

10-
public TestRunFinished(Long timeStamp, long timeStampMillis) {
11-
super(timeStamp, timeStampMillis);
7+
public TestRunFinished(Instant timeInstant) {
8+
super(timeInstant);
129
}
1310
}
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
11
package io.cucumber.core.api.event;
22

3-
public final class TestRunStarted extends TimeStampedEvent {
3+
import java.time.Instant;
44

5-
@Deprecated
6-
public TestRunStarted(Long timeStamp) {
7-
this(timeStamp, 0);
8-
}
5+
public final class TestRunStarted extends TimeStampedEvent {
96

10-
public TestRunStarted(Long timeStamp, long timeStampMillis) {
11-
super(timeStamp, timeStampMillis);
7+
public TestRunStarted(Instant timeInstant) {
8+
super(timeInstant);
129
}
1310
}

Diff for: core/src/main/java/io/cucumber/core/api/event/TestSourceRead.java

+4-7
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,13 @@
11
package io.cucumber.core.api.event;
22

3+
import java.time.Instant;
4+
35
public final class TestSourceRead extends TimeStampedEvent {
46
public final String uri;
57
public final String source;
68

7-
@Deprecated
8-
public TestSourceRead(Long timeStamp, String uri, String source) {
9-
this(timeStamp, 0, uri, source);
10-
}
11-
12-
public TestSourceRead(Long timeStamp, long timeStampMillis, String uri, String source) {
13-
super(timeStamp, timeStampMillis);
9+
public TestSourceRead(Instant timeInstant, String uri, String source) {
10+
super(timeInstant);
1411
this.uri = uri;
1512
this.source = source;
1613
}

Diff for: core/src/main/java/io/cucumber/core/api/event/TestStepFinished.java

+4-7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package io.cucumber.core.api.event;
22

3+
import java.time.Instant;
4+
35
/**
46
* A test step finished event is broadcast when ever a step finishes.
57
* <p>
@@ -20,13 +22,8 @@ public final class TestStepFinished extends TestCaseEvent {
2022
public final TestStep testStep;
2123
public final Result result;
2224

23-
@Deprecated
24-
public TestStepFinished(Long timeStamp, TestCase testCase, TestStep testStep, Result result) {
25-
this(timeStamp, 0, testCase, testStep, result);
26-
}
27-
28-
public TestStepFinished(Long timeStamp, long timeStampMillis, TestCase testCase, TestStep testStep, Result result) {
29-
super(timeStamp, timeStampMillis, testCase);
25+
public TestStepFinished(Instant timeInstant, TestCase testCase, TestStep testStep, Result result) {
26+
super(timeInstant, testCase);
3027
this.testStep = testStep;
3128
this.result = result;
3229
}

Diff for: core/src/main/java/io/cucumber/core/api/event/TestStepStarted.java

+4-7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package io.cucumber.core.api.event;
22

3+
import java.time.Instant;
4+
35
/**
46
* A test step started event is broadcast when ever a step starts.
57
* <p>
@@ -20,13 +22,8 @@
2022
public final class TestStepStarted extends TestCaseEvent {
2123
public final TestStep testStep;
2224

23-
@Deprecated
24-
public TestStepStarted(Long timeStamp, TestCase testCase, TestStep testStep) {
25-
this(timeStamp, 0, testCase, testStep);
26-
}
27-
28-
public TestStepStarted(Long timeStamp, long timeStampMillis, TestCase testCase, TestStep testStep) {
29-
super(timeStamp, timeStampMillis, testCase);
25+
public TestStepStarted(Instant timeInstant, TestCase testCase, TestStep testStep) {
26+
super(timeInstant, testCase);
3027
this.testStep = testStep;
3128
}
3229

Original file line numberDiff line numberDiff line change
@@ -1,30 +1,24 @@
11
package io.cucumber.core.api.event;
22

3+
import java.time.Instant;
4+
35
abstract class TimeStampedEvent implements Event {
46

5-
private final Long timeStamp;
6-
private final long timeStampMillis;
7+
private final Instant instant;
78

8-
TimeStampedEvent(Long timeStamp, Long timeStampMillis) {
9-
this.timeStamp = timeStamp;
10-
this.timeStampMillis = timeStampMillis;
9+
TimeStampedEvent(Instant timeInstant) {
10+
this.instant = timeInstant;
1111
}
12-
12+
1313
/**
1414
* {@inheritDoc}
1515
*/
16-
@Override
17-
public Long getTimeStamp() {
18-
return timeStamp;
19-
}
2016

2117
/**
22-
* Returns timestamp in milliseconds of the epoch.
23-
*
24-
* @return timestamp in milli seconds
25-
* @see System#currentTimeMillis()
18+
* {@inheritDoc}
2619
*/
27-
public long getTimeStampMillis() {
28-
return timeStampMillis;
20+
@Override
21+
public Instant getInstant() {
22+
return instant;
2923
}
3024
}
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
11
package io.cucumber.core.api.event;
22

3+
import java.time.Instant;
4+
35
public final class WriteEvent extends TestCaseEvent {
46
public final String text;
57

6-
@Deprecated
7-
public WriteEvent(Long timeStamp, TestCase testCase, String text) {
8-
this(timeStamp, 0, testCase, text);
9-
}
10-
11-
public WriteEvent(Long timeStamp, long timeStampMillis, TestCase testCase, String text) {
12-
super(timeStamp, timeStampMillis, testCase);
8+
public WriteEvent(Instant timeInstant, TestCase testCase, String text) {
9+
super(timeInstant, testCase);
1310
this.text = text;
1411
}
1512
}

Diff for: core/src/main/java/io/cucumber/core/event/EventBus.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
package io.cucumber.core.event;
22

3+
import java.time.Instant;
4+
35
import io.cucumber.core.api.event.Event;
46
import io.cucumber.core.api.event.EventPublisher;
57

68
public interface EventBus extends EventPublisher {
79

8-
Long getTime();
9-
10-
Long getTimeMillis();
10+
Instant getInstant();
1111

1212
void send(Event event);
1313

0 commit comments

Comments
 (0)