Get rid of event bus, use a custom listener instead.
[sonitus.git] / src / main / java / net / pterodactylus / sonitus / data / source / MultiSource.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.source;
19
20 import static com.google.common.base.Preconditions.checkNotNull;
21
22 import java.io.EOFException;
23 import java.io.IOException;
24 import java.util.Collections;
25 import java.util.List;
26 import java.util.concurrent.atomic.AtomicReference;
27 import java.util.logging.Logger;
28 import javax.swing.event.EventListenerList;
29
30 import net.pterodactylus.sonitus.data.AbstractControlledComponent;
31 import net.pterodactylus.sonitus.data.Controller;
32 import net.pterodactylus.sonitus.data.Metadata;
33 import net.pterodactylus.sonitus.data.Source;
34
35 import com.google.inject.Inject;
36
37 /**
38  * {@link Source} implementation that simply forwards another source and
39  * supports changing the source without letting the {@link
40  * net.pterodactylus.sonitus.data.Sink} know.
41  *
42  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
43  */
44 public class MultiSource extends AbstractControlledComponent implements Source {
45
46         /** The logger. */
47         private static final Logger logger = Logger.getLogger(MultiSource.class.getName());
48
49         /** The source finished listeners. */
50         private final EventListenerList sourceFinishedListeners = new EventListenerList();
51
52         /** The current source. */
53         private final AtomicReference<Source> source = new AtomicReference<Source>();
54
55         /** Whether the source was changed. */
56         private boolean sourceChanged;
57
58         /** Creates a new multi source. */
59         @Inject
60         public MultiSource() {
61                 super("Multisource");
62         }
63
64         //
65         // LISTENER MANAGEMENT
66         //
67
68         /**
69          * Adds a source finished listener to the list of registered listeners.
70          *
71          * @param sourceFinishedListener
72          *              The source finished listener to add
73          */
74         public void addSourceFinishedListener(SourceFinishedListener sourceFinishedListener) {
75                 sourceFinishedListeners.add(SourceFinishedListener.class, sourceFinishedListener);
76         }
77
78         /**
79          * Removes a source finished listener from the list of registered listeners.
80          *
81          * @param sourceFinishedListener
82          *              The source finished listener to remove
83          */
84         public void removeSourceFinishedListener(SourceFinishedListener sourceFinishedListener) {
85                 sourceFinishedListeners.remove(SourceFinishedListener.class, sourceFinishedListener);
86         }
87
88         //
89         // ACTIONS
90         //
91
92         /**
93          * Sets the new source to use.
94          *
95          * @param source
96          *              The new source to use
97          */
98         public void setSource(Source source) {
99                 checkNotNull(source, "source must not be null");
100
101                 Source oldSource = this.source.getAndSet(source);
102                 if (!source.equals(oldSource)) {
103                         synchronized (this.source) {
104                                 sourceChanged = true;
105                                 this.source.notifyAll();
106                         }
107                         metadataUpdated(source.metadata());
108                         logger.info(String.format("Next Source set: %s", source));
109                 }
110         }
111
112         //
113         // EVENT METHODS
114         //
115
116         /**
117          * Notifies all registered listeners that the current source finished playing
118          * and that a new source should be {@link #setSource(Source) set}.
119          *
120          * @see SourceFinishedListener
121          */
122         private void fireSourceFinished() {
123                 for (SourceFinishedListener sourceFinishedListener : sourceFinishedListeners.getListeners(SourceFinishedListener.class)) {
124                         sourceFinishedListener.sourceFinished(this);
125                 }
126         }
127
128         //
129         // CONTROLLED METHODS
130         //
131
132         @Override
133         public List<Controller<?>> controllers() {
134                 return Collections.emptyList();
135         }
136
137         @Override
138         public Metadata metadata() {
139                 if (super.metadata() == null) {
140                         /* no metadata yet, wait for it. */
141                         waitForNewSource();
142                         sourceChanged = false;
143                 }
144                 return super.metadata();
145         }
146
147         //
148         // SOURCE METHODS
149         //
150
151         @Override
152         public byte[] get(int bufferSize) throws EOFException, IOException {
153                 while (true) {
154                         try {
155                                 return source.get().get(bufferSize);
156                         } catch (EOFException eofe1) {
157                                 waitForNewSource();
158                         } finally {
159                                 synchronized (source) {
160                                         sourceChanged = false;
161                                 }
162                         }
163                 }
164         }
165
166         //
167         // PRIVATE METHODS
168         //
169
170         /** Waits for a new source to be {@link #setSource(Source) set}. */
171         private void waitForNewSource() {
172                 fireSourceFinished();
173                 synchronized (source) {
174                         while (!sourceChanged) {
175                                 try {
176                                         logger.info("Waiting for next Source...");
177                                         source.wait();
178                                         logger.info("Was notified.");
179                                 } catch (InterruptedException ioe1) {
180                                         /* ignore: we’ll end up here again if we were interrupted. */
181                                 }
182                         }
183                 }
184         }
185
186 }