import java.io.EOFException;
import java.io.IOException;
+import java.util.Collections;
+import java.util.List;
import java.util.concurrent.atomic.AtomicReference;
import java.util.logging.Logger;
+import javax.swing.event.EventListenerList;
+import net.pterodactylus.sonitus.data.AbstractFilter;
+import net.pterodactylus.sonitus.data.Controller;
+import net.pterodactylus.sonitus.data.Filter;
import net.pterodactylus.sonitus.data.Metadata;
-import net.pterodactylus.sonitus.data.Source;
-import net.pterodactylus.sonitus.data.event.SourceFinishedEvent;
-import com.google.common.eventbus.EventBus;
import com.google.inject.Inject;
/**
- * {@link Source} implementation that simply forwards another source and
- * supports changing the source without letting the {@link
- * net.pterodactylus.sonitus.data.Sink} know.
+ * {@link Filter} implementation that simply forwards data from another filter
+ * and supports changing the source without letting downstream filters know.
*
* @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
*/
-public class MultiSource implements Source {
+public class MultiSource extends AbstractFilter {
/** The logger. */
private static final Logger logger = Logger.getLogger(MultiSource.class.getName());
- /** The event bus. */
- private final EventBus eventBus;
+ /** The source finished listeners. */
+ private final EventListenerList sourceFinishedListeners = new EventListenerList();
/** The current source. */
- private final AtomicReference<Source> source = new AtomicReference<Source>();
+ private final AtomicReference<Filter> source = new AtomicReference<Filter>();
/** Whether the source was changed. */
private boolean sourceChanged;
+ /** Creates a new multi source. */
@Inject
- public MultiSource(EventBus eventBus) {
- this.eventBus = eventBus;
+ public MultiSource() {
+ super("Multisource");
+ }
+
+ //
+ // LISTENER MANAGEMENT
+ //
+
+ /**
+ * Adds a source finished listener to the list of registered listeners.
+ *
+ * @param sourceFinishedListener
+ * The source finished listener to add
+ */
+ public void addSourceFinishedListener(SourceFinishedListener sourceFinishedListener) {
+ sourceFinishedListeners.add(SourceFinishedListener.class, sourceFinishedListener);
+ }
+
+ /**
+ * Removes a source finished listener from the list of registered listeners.
+ *
+ * @param sourceFinishedListener
+ * The source finished listener to remove
+ */
+ public void removeSourceFinishedListener(SourceFinishedListener sourceFinishedListener) {
+ sourceFinishedListeners.remove(SourceFinishedListener.class, sourceFinishedListener);
}
//
* @param source
* The new source to use
*/
- public void setSource(Source source) {
+ public void setSource(Filter source) {
checkNotNull(source, "source must not be null");
- Source oldSource = this.source.getAndSet(source);
- if (oldSource != null) {
+ Filter oldSource = this.source.getAndSet(source);
+ if (!source.equals(oldSource)) {
synchronized (this.source) {
sourceChanged = true;
this.source.notifyAll();
}
+ metadataUpdated(source.metadata());
logger.info(String.format("Next Source set: %s", source));
}
}
//
- // SOURCE METHODS
+ // EVENT METHODS
+ //
+
+ /**
+ * Notifies all registered listeners that the current source finished playing
+ * and that a new source should be {@link #setSource(Filter) set}.
+ *
+ * @see SourceFinishedListener
+ */
+ private void fireSourceFinished() {
+ for (SourceFinishedListener sourceFinishedListener : sourceFinishedListeners.getListeners(SourceFinishedListener.class)) {
+ sourceFinishedListener.sourceFinished(this);
+ }
+ }
+
+ //
+ // FILTER METHODS
//
@Override
+ public List<Controller<?>> controllers() {
+ return Collections.emptyList();
+ }
+
+ @Override
public Metadata metadata() {
- return source.get().metadata();
+ if (super.metadata() == null) {
+ /* no metadata yet, wait for it. */
+ waitForNewSource();
+ sourceChanged = false;
+ }
+ return super.metadata();
}
@Override
try {
return source.get().get(bufferSize);
} catch (EOFException eofe1) {
- eventBus.post(new SourceFinishedEvent(source.get()));
- synchronized (source) {
- while (!sourceChanged) {
- try {
- logger.info("Waiting for next Source...");
- source.wait();
- logger.info("Was notified.");
- } catch (InterruptedException ioe1) {
- /* ignore: we’ll end up here again if we were interrupted. */
- }
- }
- }
+ waitForNewSource();
} finally {
synchronized (source) {
sourceChanged = false;
}
}
+ //
+ // PRIVATE METHODS
+ //
+
+ /** Waits for a new source to be {@link #setSource(Filter) set}. */
+ private void waitForNewSource() {
+ fireSourceFinished();
+ synchronized (source) {
+ while (!sourceChanged) {
+ try {
+ logger.info("Waiting for next Source...");
+ source.wait();
+ logger.info("Was notified.");
+ } catch (InterruptedException ioe1) {
+ /* ignore: we’ll end up here again if we were interrupted. */
+ }
+ }
+ }
+ }
+
}