2 * Sonitus - PipelineFilter.java - Copyright © 2013 David Roden
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.
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.
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/>.
18 package net.pterodactylus.sonitus.data.filter;
20 import java.io.IOException;
21 import java.util.Arrays;
22 import java.util.Collection;
23 import java.util.List;
26 import net.pterodactylus.sonitus.data.AbstractFilter;
27 import net.pterodactylus.sonitus.data.DataPacket;
28 import net.pterodactylus.sonitus.data.Filter;
29 import net.pterodactylus.sonitus.data.Metadata;
30 import net.pterodactylus.sonitus.data.Pipeline.Connection;
32 import com.google.common.collect.Lists;
33 import com.google.common.collect.Maps;
36 * {@link Filter} that combines several filters into one.
38 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
40 public class PipelineFilter extends AbstractFilter implements Filter {
42 /** The first filter. */
43 private final Filter source;
45 /** All following filters. */
46 private final List<Filter> filters = Lists.newArrayList();
48 /** The last filter (for convenience). */
49 private final Filter lastFilter;
51 /** The connections for each filter. */
52 private final Map<Filter, Connection> filterConnections = Maps.newHashMap();
55 * Creates a new pipeline filter.
58 * The name of the filter
60 * The first source of the filter
62 * All other filters in correct order
64 private PipelineFilter(String name, Filter source, Collection<Filter> filters) {
67 this.filters.addAll(filters);
68 this.lastFilter = this.filters.get(filters.size() - 1);
76 public Metadata metadata() {
77 return lastFilter.metadata();
81 public void open(Metadata metadata) throws IOException {
82 /* open the source and all filters in the correct order. */
83 source.open(metadata);
84 Metadata currentMetadata = source.metadata();
85 Filter currentSource = source;
86 for (Filter filter : filters) {
87 filter.open(currentMetadata);
88 currentMetadata = filter.metadata();
89 Connection connection = new Connection(currentSource, Arrays.asList(filter));
90 filterConnections.put(filter, connection);
91 String threadName = String.format("%s → %s", connection.source().name(), filter.name());
92 new Thread(connection, threadName).start();
93 currentSource = filter;
95 metadataUpdated(currentMetadata);
99 public DataPacket get(int bufferSize) throws IOException {
100 return lastFilter.get(bufferSize);
104 public void process(DataPacket dataPacket) throws IOException {
105 source.process(dataPacket);
113 * Returns a builder that can create pipeline filters.
116 * The source filter of the pipeline
117 * @return The pipeline filter builder
119 public static Builder builder(Filter source) {
120 return new Builder(source);
124 * Builder for a {@link PipelineFilter}.
126 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
128 public static class Builder {
130 /** The source of the pipeline. */
131 private final Filter source;
133 /** All other filters of the pipeline. */
134 private final List<Filter> filters = Lists.newArrayList();
137 * Creates a new builder with the given source.
140 * The source of the pipeline filter
142 private Builder(Filter source) {
143 this.source = source;
147 * Connects the given filter at the end of the pipeline being build.
151 * @return This builder
153 public Builder to(Filter filter) {
159 * Builds a filter using the given name. If no filters other than the source
160 * have been added, only the source filter is being returned.
163 * The name of the pipeline filter to build
164 * @return The created filter, or the source filter
166 public Filter build(String name) {
167 if (filters.isEmpty()) {
170 return new PipelineFilter(name, source, filters);