From c0949ae75da11f60138bae30756e6513face469c Mon Sep 17 00:00:00 2001 From: Joris Guffens Date: Wed, 7 Dec 2022 21:40:20 +0100 Subject: [PATCH] fixed adding more filters to a subscription --- .../com/guflimc/brick/stats/api/event/Filter.java | 11 +++++++++++ .../guflimc/brick/stats/common/BrickStatsManager.java | 9 +++++---- .../common/event/AbstractSubscriptionBuilder.java | 7 +++++-- .../event/subscriptions/AbstractSubscription.java | 4 ++-- 4 files changed, 23 insertions(+), 8 deletions(-) diff --git a/api/src/main/java/com/guflimc/brick/stats/api/event/Filter.java b/api/src/main/java/com/guflimc/brick/stats/api/event/Filter.java index d8d02da..0e9e37f 100644 --- a/api/src/main/java/com/guflimc/brick/stats/api/event/Filter.java +++ b/api/src/main/java/com/guflimc/brick/stats/api/event/Filter.java @@ -2,7 +2,18 @@ import org.jetbrains.annotations.NotNull; +import java.util.Collection; +import java.util.List; + @FunctionalInterface public interface Filter { boolean test(@NotNull Event event); + + static Filter allOf(Collection filters) { + return event -> filters.stream().allMatch(f -> f.test(event)); + } + + static Filter allOf(Filter... filters) { + return allOf(List.of(filters)); + } } diff --git a/common/src/main/java/com/guflimc/brick/stats/common/BrickStatsManager.java b/common/src/main/java/com/guflimc/brick/stats/common/BrickStatsManager.java index 54ac8ef..f560dac 100644 --- a/common/src/main/java/com/guflimc/brick/stats/common/BrickStatsManager.java +++ b/common/src/main/java/com/guflimc/brick/stats/common/BrickStatsManager.java @@ -198,8 +198,9 @@ public void unsubscribe() { private class BrickSubscriptionBuilder extends AbstractSubscriptionBuilder { private Subscription subscribe(@NotNull Filter extraFilter) { - Filter combined = event -> filter.test(event) && extraFilter.test(event); - return subscribe(new BrickSubscription(handler, combined)); + List filters = new ArrayList<>(this.filters); + filters.add(extraFilter); + return subscribe(new BrickSubscription(handler, Filter.allOf(filters))); } private Subscription subscribe(@NotNull BrickSubscription sub) { @@ -222,10 +223,10 @@ public Subscription change() { @Override public Subscription interval(int interval) { - return subscribe(new BrickSubscription(handler, filter) { + return subscribe(new BrickSubscription(handler, Filter.allOf(filters)) { @Override public void execute(Event event) { - if (!filter.test(event)) { + if ( !filter.test(event) ) { return; } diff --git a/common/src/main/java/com/guflimc/brick/stats/common/event/AbstractSubscriptionBuilder.java b/common/src/main/java/com/guflimc/brick/stats/common/event/AbstractSubscriptionBuilder.java index 5ab964e..ba511c1 100644 --- a/common/src/main/java/com/guflimc/brick/stats/common/event/AbstractSubscriptionBuilder.java +++ b/common/src/main/java/com/guflimc/brick/stats/common/event/AbstractSubscriptionBuilder.java @@ -5,14 +5,17 @@ import com.guflimc.brick.stats.api.event.SubscriptionBuilder; import org.jetbrains.annotations.NotNull; +import java.util.ArrayList; +import java.util.List; + public abstract class AbstractSubscriptionBuilder implements SubscriptionBuilder { - protected Filter filter = event -> true; + protected List filters = new ArrayList<>(); protected EventHandler handler; @Override public SubscriptionBuilder filter(@NotNull Filter filter) { - this.filter = filter; + this.filters.add(filter); return this; } diff --git a/common/src/main/java/com/guflimc/brick/stats/common/event/subscriptions/AbstractSubscription.java b/common/src/main/java/com/guflimc/brick/stats/common/event/subscriptions/AbstractSubscription.java index 05af461..d30c54f 100644 --- a/common/src/main/java/com/guflimc/brick/stats/common/event/subscriptions/AbstractSubscription.java +++ b/common/src/main/java/com/guflimc/brick/stats/common/event/subscriptions/AbstractSubscription.java @@ -8,8 +8,8 @@ public abstract class AbstractSubscription implements Subscription { - private final EventHandler handler; - private final Filter filter; + protected final EventHandler handler; + protected final Filter filter; public AbstractSubscription(@NotNull EventHandler handler, @NotNull Filter filter) { this.handler = handler;