4edc661b3d7411f4b34094bd2e90cb5292419d35
[sonitus.git] / src / main / java / net / pterodactylus / sonitus / data / filter / PredicateFilter.java
1 /*
2  * Sonitus - PredicateFilter.java - Copyright © 2013 David Roden
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17
18 package net.pterodactylus.sonitus.data.filter;
19
20 import static com.google.common.base.Preconditions.checkNotNull;
21
22 import java.io.IOException;
23 import java.util.concurrent.atomic.AtomicBoolean;
24
25 import net.pterodactylus.sonitus.data.Filter;
26 import net.pterodactylus.sonitus.data.Metadata;
27
28 import com.google.common.base.Predicate;
29 import com.google.common.eventbus.EventBus;
30
31 /**
32  * {@link Filter} implementation that uses a {@link Predicate} to determine
33  * whether a filter will be used or the data will only be passed through.
34  *
35  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
36  */
37 public class PredicateFilter extends DummyFilter {
38
39         /** The predicate. */
40         private final Predicate<Metadata> metadataPredicate;
41
42         /** The filter to use if the predicate matches. */
43         private final Filter filter;
44
45         /** Whether the predicate currently matches. */
46         private final AtomicBoolean metadataMatches = new AtomicBoolean(false);
47
48         /**
49          * Creates a new predicate filter.
50          *
51          * @param eventBus
52          *              The event bus
53          * @param metadataPredicate
54          *              The predicate to evaluate every time the metadata changes
55          * @param filter
56          *              The filter to use if the predicate matches the metadata
57          */
58         public PredicateFilter(EventBus eventBus, Predicate<Metadata> metadataPredicate, Filter filter) {
59                 super(eventBus, String.format("%s (maybe)", filter.name()));
60                 this.metadataPredicate = metadataPredicate;
61                 this.filter = filter;
62         }
63
64         //
65         // FILTER METHODS
66         //
67
68         @Override
69         public void open(Metadata metadata) throws IOException {
70                 checkNotNull(metadata, "metadata must not be null");
71
72                 metadataMatches.set(metadataPredicate.apply(metadata));
73                 if (metadataMatches.get()) {
74                         filter.open(metadata);
75                 } else {
76                         super.open(metadata);
77                 }
78         }
79
80         @Override
81         public void close() {
82                 if (metadataMatches.get()) {
83                         filter.close();
84                 } else {
85                         super.close();
86                 }
87         }
88
89         @Override
90         public void metadataUpdated(Metadata metadata) {
91                 metadataMatches.set(metadataPredicate.apply(metadata));
92                 if (metadataMatches.get()) {
93                         filter.metadataUpdated(metadata);
94                 } else {
95                         super.metadataUpdated(metadata);
96                 }
97         }
98
99         @Override
100         public void process(byte[] buffer) throws IOException {
101                 if (metadataMatches.get()) {
102                         filter.process(buffer);
103                 } else {
104                         super.process(buffer);
105                 }
106         }
107
108         @Override
109         public Metadata metadata() {
110                 if (metadataMatches.get()) {
111                         return filter.metadata();
112                 } else {
113                         return super.metadata();
114                 }
115         }
116
117         @Override
118         public byte[] get(int bufferSize) throws IOException {
119                 if (metadataMatches.get()) {
120                         return filter.get(bufferSize);
121                 } else {
122                         return super.get(bufferSize);
123                 }
124         }
125
126 }