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;
25 import net.pterodactylus.sonitus.data.AbstractFilter;
26 import net.pterodactylus.sonitus.data.DataPacket;
27 import net.pterodactylus.sonitus.data.Filter;
28 import net.pterodactylus.sonitus.data.Metadata;
29 import net.pterodactylus.sonitus.data.Pipeline.Connection;
31 import com.google.common.collect.Lists;
34 * {@link Filter} that combines several filters into one.
36 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
38 public class PipelineFilter extends AbstractFilter implements Filter {
40 /** The first filter. */
41 private final Filter source;
43 /** All following filters. */
44 private final List<Filter> filters = Lists.newArrayList();
46 /** The last filter (for convenience). */
47 private final Filter lastFilter;
50 * Creates a new pipeline filter.
53 * The name of the filter
55 * The first source of the filter
57 * All other filters in correct order
59 private PipelineFilter(String name, Filter source, Collection<Filter> filters) {
62 this.filters.addAll(filters);
63 this.lastFilter = this.filters.get(filters.size() - 1);
71 public Metadata metadata() {
72 return lastFilter.metadata();
76 public void open(Metadata metadata) throws IOException {
77 /* open the source and all filters in the correct order. */
78 source.open(metadata);
79 Metadata currentMetadata = source.metadata();
80 Filter currentSource = source;
81 for (Filter filter : filters) {
82 filter.open(currentMetadata);
83 currentMetadata = filter.metadata();
84 Connection connection = new Connection(currentSource, Arrays.asList(filter));
85 String threadName = String.format("%s → %s", connection.source().name(), filter.name());
86 new Thread(connection, threadName).start();
87 currentSource = filter;
92 public DataPacket get(int bufferSize) throws IOException {
93 return lastFilter.get(bufferSize);
97 public void process(DataPacket dataPacket) throws IOException {
98 source.process(dataPacket);
106 * Returns a builder that can create pipeline filters.
109 * The source filter of the pipeline
110 * @return The pipeline filter builder
112 public static Builder builder(Filter source) {
113 return new Builder(source);
117 * Builder for a {@link PipelineFilter}.
119 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
121 public static class Builder {
123 /** The source of the pipeline. */
124 private final Filter source;
126 /** All other filters of the pipeline. */
127 private final List<Filter> filters = Lists.newArrayList();
130 * Creates a new builder with the given source.
133 * The source of the pipeline filter
135 private Builder(Filter source) {
136 this.source = source;
140 * Connects the given filter at the end of the pipeline being build.
144 * @return This builder
146 public Builder to(Filter filter) {
152 * Builds a filter using the given name. If no filters other than the source
153 * have been added, only the source filter is being returned.
156 * The name of the pipeline filter to build
157 * @return The created filter, or the source filter
159 public Filter build(String name) {
160 if (filters.isEmpty()) {
163 return new PipelineFilter(name, source, filters);