X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Fsonitus%2Fdata%2Ffilter%2FPredicateFilter.java;fp=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Fsonitus%2Fdata%2Ffilter%2FPredicateFilter.java;h=1e8c7fa9385a6d20603f6c249fe474689db1284e;hb=7188da95cfb6dc2bf140eb8ac7e4dc99a0761a97;hp=0000000000000000000000000000000000000000;hpb=3d073631242b121378ff9c60104170d13cab52d7;p=sonitus.git
diff --git a/src/main/java/net/pterodactylus/sonitus/data/filter/PredicateFilter.java b/src/main/java/net/pterodactylus/sonitus/data/filter/PredicateFilter.java
new file mode 100644
index 0000000..1e8c7fa
--- /dev/null
+++ b/src/main/java/net/pterodactylus/sonitus/data/filter/PredicateFilter.java
@@ -0,0 +1,122 @@
+/*
+ * Sonitus - PredicateFilter.java - Copyright © 2013 David Roden
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see .
+ */
+
+package net.pterodactylus.sonitus.data.filter;
+
+import static com.google.common.base.Preconditions.checkNotNull;
+
+import java.io.IOException;
+import java.util.concurrent.atomic.AtomicBoolean;
+
+import net.pterodactylus.sonitus.data.Filter;
+import net.pterodactylus.sonitus.data.Metadata;
+
+import com.google.common.base.Predicate;
+
+/**
+ * {@link Filter} implementation that uses a {@link Predicate} to determine
+ * whether a filter will be used or the data will only be passed through.
+ *
+ * @author David âBombeâ Roden
+ */
+public class PredicateFilter extends DummyFilter {
+
+ /** The predicate. */
+ private final Predicate metadataPredicate;
+
+ /** The filter to use if the predicate matches. */
+ private final Filter filter;
+
+ /** Whether the predicate currently matches. */
+ private final AtomicBoolean metadataMatches = new AtomicBoolean(false);
+
+ /**
+ * Creates a new predicate filter.
+ *
+ * @param metadataPredicate
+ * The predicate to evaluate every time the metadata changes
+ * @param filter
+ * The filter to use if the predicate matches the metadata
+ */
+ public PredicateFilter(Predicate metadataPredicate, Filter filter) {
+ this.metadataPredicate = metadataPredicate;
+ this.filter = filter;
+ }
+
+ //
+ // FILTER METHODS
+ //
+
+ @Override
+ public void open(Metadata metadata) throws IOException {
+ checkNotNull(metadata, "metadata must not be null");
+
+ metadataMatches.set(metadataPredicate.apply(metadata));
+ if (metadataMatches.get()) {
+ filter.open(metadata);
+ } else {
+ super.open(metadata);
+ }
+ }
+
+ @Override
+ public void close() {
+ if (metadataMatches.get()) {
+ filter.close();
+ } else {
+ super.close();
+ }
+ }
+
+ @Override
+ public void metadataUpdated(Metadata metadata) {
+ metadataMatches.set(metadataPredicate.apply(metadata));
+ if (metadataMatches.get()) {
+ filter.metadataUpdated(metadata);
+ } else {
+ super.metadataUpdated(metadata);
+ }
+ }
+
+ @Override
+ public void process(byte[] buffer) throws IOException {
+ if (metadataMatches.get()) {
+ filter.process(buffer);
+ } else {
+ super.process(buffer);
+ }
+ }
+
+ @Override
+ public Metadata metadata() {
+ if (metadataMatches.get()) {
+ return filter.metadata();
+ } else {
+ return super.metadata();
+ }
+ }
+
+ @Override
+ public byte[] get(int bufferSize) throws IOException {
+ if (metadataMatches.get()) {
+ return filter.get(bufferSize);
+ } else {
+ return super.get(bufferSize);
+ }
+ }
+
+}