Add name to all controlled components.
[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
30 /**
31  * {@link Filter} implementation that uses a {@link Predicate} to determine
32  * whether a filter will be used or the data will only be passed through.
33  *
34  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
35  */
36 public class PredicateFilter extends DummyFilter {
37
38         /** The predicate. */
39         private final Predicate<Metadata> metadataPredicate;
40
41         /** The filter to use if the predicate matches. */
42         private final Filter filter;
43
44         /** Whether the predicate currently matches. */
45         private final AtomicBoolean metadataMatches = new AtomicBoolean(false);
46
47         /**
48          * Creates a new predicate filter.
49          *
50          * @param metadataPredicate
51          *              The predicate to evaluate every time the metadata changes
52          * @param filter
53          *              The filter to use if the predicate matches the metadata
54          */
55         public PredicateFilter(Predicate<Metadata> metadataPredicate, Filter filter) {
56                 super(String.format("%s (maybe)", filter.name()));
57                 this.metadataPredicate = metadataPredicate;
58                 this.filter = filter;
59         }
60
61         //
62         // FILTER METHODS
63         //
64
65         @Override
66         public void open(Metadata metadata) throws IOException {
67                 checkNotNull(metadata, "metadata must not be null");
68
69                 metadataMatches.set(metadataPredicate.apply(metadata));
70                 if (metadataMatches.get()) {
71                         filter.open(metadata);
72                 } else {
73                         super.open(metadata);
74                 }
75         }
76
77         @Override
78         public void close() {
79                 if (metadataMatches.get()) {
80                         filter.close();
81                 } else {
82                         super.close();
83                 }
84         }
85
86         @Override
87         public void metadataUpdated(Metadata metadata) {
88                 metadataMatches.set(metadataPredicate.apply(metadata));
89                 if (metadataMatches.get()) {
90                         filter.metadataUpdated(metadata);
91                 } else {
92                         super.metadataUpdated(metadata);
93                 }
94         }
95
96         @Override
97         public void process(byte[] buffer) throws IOException {
98                 if (metadataMatches.get()) {
99                         filter.process(buffer);
100                 } else {
101                         super.process(buffer);
102                 }
103         }
104
105         @Override
106         public Metadata metadata() {
107                 if (metadataMatches.get()) {
108                         return filter.metadata();
109                 } else {
110                         return super.metadata();
111                 }
112         }
113
114         @Override
115         public byte[] get(int bufferSize) throws IOException {
116                 if (metadataMatches.get()) {
117                         return filter.get(bufferSize);
118                 } else {
119                         return super.get(bufferSize);
120                 }
121         }
122
123 }