Skip to content

Commit 3502809

Browse files
committed
Extract comparator
1 parent 15345c4 commit 3502809

File tree

3 files changed

+73
-21
lines changed

3 files changed

+73
-21
lines changed

java/src/main/java/io/cucumber/query/Query.java

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@
3636
import java.util.function.Supplier;
3737
import java.util.stream.Collectors;
3838

39-
import static io.cucumber.query.LineageReducer.ascending;
4039
import static java.util.Collections.emptyList;
4140
import static java.util.Comparator.comparing;
4241
import static java.util.Comparator.naturalOrder;
@@ -109,31 +108,12 @@ public List<PickleStep> findAllPickleSteps() {
109108

110109
public List<TestCaseStarted> findAllTestCaseStarted() {
111110
return this.testCaseStartedById.values().stream()
112-
.sorted(comparing(TestCaseStarted::getTimestamp, timestampComparator))
111+
.sorted(comparing(TestCaseStarted::getTimestamp, new TimestampComparator()))
113112
.filter(element -> !findTestCaseFinishedBy(element)
114113
.filter(TestCaseFinished::getWillBeRetried)
115114
.isPresent())
116115
.collect(toList());
117116
}
118-
119-
// TODO: Move to Messages, make comparable?
120-
private final Comparator<Timestamp> timestampComparator = (a, b) -> {
121-
long x = a.getSeconds();
122-
long y = b.getSeconds();
123-
int cmp;
124-
if (x < y)
125-
return -1;
126-
if(y > x)
127-
return 1;
128-
129-
long x1 = a.getNanos();
130-
long y1 = b.getNanos();
131-
if (x1 < y1)
132-
return -1;
133-
if(y1 > x1)
134-
return 1;
135-
return 0;
136-
};
137117

138118
public Map<Optional<Feature>, List<TestCaseStarted>> findAllTestCaseStartedGroupedByFeature() {
139119
return findAllTestCaseStarted()
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package io.cucumber.query;
2+
3+
import io.cucumber.messages.types.Timestamp;
4+
5+
import java.util.Comparator;
6+
7+
class TimestampComparator implements Comparator<Timestamp> {
8+
@Override
9+
public int compare(Timestamp a, Timestamp b) {
10+
long sa = a.getSeconds();
11+
long sb = b.getSeconds();
12+
13+
if (sa < sb) {
14+
return -1;
15+
} else if (sb < sa) {
16+
return 1;
17+
}
18+
19+
long na = a.getNanos();
20+
long nb = b.getNanos();
21+
22+
if (na < nb) {
23+
return -1;
24+
} else if (nb < na) {
25+
return 1;
26+
}
27+
28+
return 0;
29+
}
30+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package io.cucumber.query;
2+
3+
import io.cucumber.messages.types.Timestamp;
4+
import org.junit.jupiter.api.Test;
5+
6+
import static org.assertj.core.api.Assertions.assertThat;
7+
8+
class TimestampComparatorTest {
9+
10+
private final TimestampComparator comparator = new TimestampComparator();
11+
12+
@Test
13+
void identity(){
14+
Timestamp a = new Timestamp(1L, 1L);
15+
Timestamp b = new Timestamp(1L, 1L);
16+
17+
assertThat(comparator.compare(a, b)).isEqualTo(0);
18+
assertThat(comparator.compare(b, a)).isEqualTo(0);
19+
}
20+
21+
@Test
22+
void onSeconds(){
23+
Timestamp a = new Timestamp(1L, 1L);
24+
Timestamp b = new Timestamp(2L, 2L);
25+
assertThat(comparator.compare(a, b)).isEqualTo(-1);
26+
assertThat(comparator.compare(b, a)).isEqualTo(1);
27+
}
28+
29+
@Test
30+
void onNanoSeconds(){
31+
Timestamp a = new Timestamp(1L, 1L);
32+
Timestamp b1 = new Timestamp(1L, 0L);
33+
Timestamp b2 = new Timestamp(1L, 2L);
34+
35+
assertThat(comparator.compare(a, b1)).isEqualTo(1);
36+
assertThat(comparator.compare(b1, a)).isEqualTo(-1);
37+
38+
assertThat(comparator.compare(a, b2)).isEqualTo(-1);
39+
assertThat(comparator.compare(b2, a)).isEqualTo(1);
40+
41+
}
42+
}

0 commit comments

Comments
 (0)