d62ba973b743e67803c44debcef504fde7970921
[sonitus.git] / src / main / java / net / pterodactylus / sonitus / data / filter / MultiSourceFilter.java
1 /*
2  * Sonitus - MultiSource.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.*;
21
22 import java.io.EOFException;
23 import java.io.IOException;
24 import java.util.concurrent.atomic.AtomicReference;
25 import java.util.logging.Logger;
26
27 import net.pterodactylus.sonitus.data.ConnectException;
28 import net.pterodactylus.sonitus.data.Filter;
29 import net.pterodactylus.sonitus.data.Metadata;
30 import net.pterodactylus.sonitus.data.ReusableSink;
31 import net.pterodactylus.sonitus.data.Source;
32 import net.pterodactylus.sonitus.data.event.SourceFinishedEvent;
33
34 import com.google.common.eventbus.EventBus;
35 import com.google.inject.Inject;
36
37 /**
38  * {@link ReusableSink} implementation that supports changing the source without
39  * letting the {@link net.pterodactylus.sonitus.data.Sink} know.
40  *
41  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
42  */
43 public class MultiSourceFilter implements Filter, ReusableSink {
44
45         /** The logger. */
46         private static final Logger logger = Logger.getLogger(MultiSourceFilter.class.getName());
47
48         /** The event bus. */
49         private final EventBus eventBus;
50
51         /** The current source. */
52         private final AtomicReference<Source> source = new AtomicReference<Source>();
53
54         @Inject
55         public MultiSourceFilter(EventBus eventBus) {
56                 this.eventBus = eventBus;
57         }
58
59         @Override
60         public Metadata metadata() {
61                 return source.get().metadata();
62         }
63
64         @Override
65         public byte[] get(int bufferSize) throws EOFException, IOException {
66                 try {
67                         return source.get().get(bufferSize);
68                 } catch (EOFException eofe1) {
69                         eventBus.post(new SourceFinishedEvent(source.get()));
70                         throw eofe1;
71                 }
72         }
73
74         @Override
75         public void connect(Source source) throws ConnectException {
76                 checkNotNull(source, "source must not be null");
77
78                 Source oldSource = this.source.getAndSet(source);
79                 if (oldSource != null) {
80                         checkArgument(oldSource.metadata().channels() == source.metadata().channels(), "source’s channel count must equal existing source’s channel count");
81                         checkArgument(oldSource.metadata().frequency() == source.metadata().frequency(), "source’s frequency must equal existing source’s frequency");
82                         checkArgument(oldSource.metadata().encoding().equalsIgnoreCase(source.metadata().encoding()), "source’s encoding must equal existing source’s encoding");
83                 }
84         }
85
86         @Override
87         public void metadataUpdated() {
88                 /* ignore. */
89         }
90
91 }