Skip to content

Commit

Permalink
add snapshot filter
Browse files Browse the repository at this point in the history
  • Loading branch information
HSGamer committed May 4, 2024
1 parent dddd4aa commit 6d89ad1
Showing 1 changed file with 4 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class SnapshotAgent<T> extends TaskAgent {
private final AtomicReference<List<DataSnapshot<T>>> topSnapshot = new AtomicReference<>(Collections.emptyList());
private final AtomicReference<Map<UUID, Integer>> indexMap = new AtomicReference<>(Collections.emptyMap());
private final DataHolder<T> holder;
private final List<Predicate<UUID>> filters = new ArrayList<>();
private final List<Predicate<DataSnapshot<T>>> filters = new ArrayList<>();
private Comparator<T> comparator;

public SnapshotAgent(DataHolder<T> holder) {
Expand All @@ -26,8 +26,8 @@ public SnapshotAgent(DataHolder<T> holder) {
protected Runnable getRunnable() {
return () -> {
Stream<DataSnapshot<T>> stream = holder.getEntryMap().entrySet().stream()
.filter(entry -> filters.parallelStream().allMatch(filter -> filter.test(entry.getKey())))
.map(entry -> new DataSnapshot<>(entry.getKey(), entry.getValue().getValue()));
.map(entry -> new DataSnapshot<>(entry.getKey(), entry.getValue().getValue()))
.filter(snapshot -> filters.stream().allMatch(filter -> filter.test(snapshot)));
if (comparator != null) {
stream = stream.sorted(Comparator.<DataSnapshot<T>, T>comparing(snapshot -> snapshot.value, comparator).reversed());
}
Expand Down Expand Up @@ -67,7 +67,7 @@ public void setComparator(Comparator<T> comparator) {
this.comparator = comparator;
}

public void addFilter(Predicate<UUID> filter) {
public void addFilter(Predicate<DataSnapshot<T>> filter) {
filters.add(filter);
}
}

0 comments on commit 6d89ad1

Please sign in to comment.