Skip to content

Commit

Permalink
[TH2-5204] added DropDuplicatesPredicate.
Browse files Browse the repository at this point in the history
  • Loading branch information
Nikita-Smirnov-Exactpro committed Jun 26, 2024
1 parent 12d496e commit 0c3d951
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion cradle-core/src/main/java/com/exactpro/cradle/BookInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ public Iterator<PageInfo> getPages(@Nullable Instant leftBoundTimestamp,
}
return builder.build()
.flatMap(epochDay -> getPageInterval(epochDay).stream(leftTimestamp, rightTimestamp, order))
.distinct()
.filter(new DropDuplicatesPredicate<>())
.iterator();
}

Expand Down Expand Up @@ -472,6 +472,22 @@ private interface IPageInterval {
Stream<PageInfo> stream(Instant leftBoundTimestamp, Instant rightBoundTimestamp, Order order);
}

/**
* This is stateful predicate because it holds previous checked element in class variable.
* You use it to drop duplicated elements in ordered stream without parallel mode.
*/
private static final class DropDuplicatesPredicate<T> implements Predicate<T> {
private T previous = null;
@Override
public boolean test(T t) {
if (Objects.equals(previous, t)) {
return false;
}
previous = t;
return true;
}
}

private static final class EmptyPageInterval implements IPageInterval {

@Override
Expand Down

0 comments on commit 0c3d951

Please sign in to comment.