--- /dev/null
+/*
+ * Sonitus - AbstractControlledComponent.java - Copyright © 2013 David Roden
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+package net.pterodactylus.sonitus.data;
+
+import java.util.List;
+
+import com.google.common.collect.Lists;
+
+/**
+ * Abstract {@link ControlledComponent} implementation that takes care of
+ * managing {@link MetadataListener}s.
+ *
+ * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
+ */
+public abstract class AbstractControlledComponent implements ControlledComponent {
+
+ /** The list of metadata listeners. */
+ private final List<MetadataListener> metadataListeners = Lists.newArrayList();
+
+ //
+ // LISTENER MANAGEMENT
+ //
+
+ @Override
+ public void addMetadataListener(MetadataListener metadataListener) {
+ metadataListeners.add(metadataListener);
+ }
+
+ @Override
+ public void removeMetadataListener(MetadataListener metadataListener) {
+ metadataListeners.remove(metadataListener);
+ }
+
+ //
+ // EVENT METHODS
+ //
+
+ /**
+ * Notifies all registered metadata listeners that the metadata has changed.
+ *
+ * @param metadata
+ * The new metadata
+ */
+ protected void fireMetadataUpdated(Metadata metadata) {
+ for (MetadataListener metadataListener : metadataListeners) {
+ metadataListener.metadataUpdated(this, metadata);
+ }
+ }
+
+}
public interface ControlledComponent {
/**
+ * Adds the given listener to the list of registered listeners.
+ *
+ * @param metadataListener
+ * The metadata listener to add
+ */
+ void addMetadataListener(MetadataListener metadataListener);
+
+ /**
+ * Removes the given listener from the list of registered listeners.
+ *
+ * @param metadataListener
+ * The metadata listener to remove
+ */
+ void removeMetadataListener(MetadataListener metadataListener);
+
+ /**
* Returns the name of this controlled component.
*
* @return The name of this controlled component
--- /dev/null
+/*
+ * Sonitus - MetadataListener.java - Copyright © 2013 David Roden
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+package net.pterodactylus.sonitus.data;
+
+/**
+ * Interface for metadata listeners.
+ *
+ * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
+ */
+public interface MetadataListener {
+
+ /**
+ * Notifies a listener when the metadata of the given component was updated.
+ *
+ * @param component
+ * The component whose metadata was updated
+ * @param metadata
+ * The new metadata
+ */
+ void metadataUpdated(ControlledComponent component, Metadata metadata);
+
+}
import java.util.Collections;
import java.util.List;
+import net.pterodactylus.sonitus.data.AbstractControlledComponent;
import net.pterodactylus.sonitus.data.Controller;
import net.pterodactylus.sonitus.data.Filter;
import net.pterodactylus.sonitus.data.Metadata;
*
* @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
*/
-public class DummyFilter implements Filter {
+public class DummyFilter extends AbstractControlledComponent implements Filter {
/** The name of this filter. */
private final String name;
@Override
public void metadataUpdated(Metadata metadata) {
this.metadata = metadata;
+ fireMetadataUpdated(metadata);
eventBus.post(new MetadataUpdated(this, metadata));
}
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.SourceDataLine;
+import net.pterodactylus.sonitus.data.AbstractControlledComponent;
import net.pterodactylus.sonitus.data.Controller;
import net.pterodactylus.sonitus.data.Metadata;
import net.pterodactylus.sonitus.data.Sink;
*
* @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
*/
-public class AudioSink implements Sink {
+public class AudioSink extends AbstractControlledComponent implements Sink {
/** The logger. */
private static final Logger logger = Logger.getLogger(AudioSink.class.getName());
public void metadataUpdated(Metadata metadata) {
logger.info(String.format("Now playing %s.", metadata));
this.metadata = metadata;
+ fireMetadataUpdated(metadata);
eventBus.post(new MetadataUpdated(this, metadata));
}
import java.util.List;
import java.util.logging.Logger;
+import net.pterodactylus.sonitus.data.AbstractControlledComponent;
import net.pterodactylus.sonitus.data.Controller;
import net.pterodactylus.sonitus.data.Metadata;
import net.pterodactylus.sonitus.data.Sink;
*
* @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
*/
-public class FileSink implements Sink {
+public class FileSink extends AbstractControlledComponent implements Sink {
/** The logger. */
private static final Logger logger = Logger.getLogger(FileSink.class.getName());
@Override
public void metadataUpdated(Metadata metadata) {
this.metadata = metadata;
+ fireMetadataUpdated(metadata);
eventBus.post(new MetadataUpdated(this, metadata));
}
import java.util.logging.Level;
import java.util.logging.Logger;
+import net.pterodactylus.sonitus.data.AbstractControlledComponent;
import net.pterodactylus.sonitus.data.Controller;
import net.pterodactylus.sonitus.data.Metadata;
import net.pterodactylus.sonitus.data.Sink;
*
* @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
*/
-public class Icecast2Sink implements Sink {
+public class Icecast2Sink extends AbstractControlledComponent implements Sink {
/** The logger. */
private static final Logger logger = Logger.getLogger(Icecast2Sink.class.getName());
}
}
}).start();
+ fireMetadataUpdated(metadata);
eventBus.post(new MetadataUpdated(this, metadata));
}
import java.util.Collections;
import java.util.List;
+import net.pterodactylus.sonitus.data.AbstractControlledComponent;
import net.pterodactylus.sonitus.data.Controller;
import net.pterodactylus.sonitus.data.Metadata;
import net.pterodactylus.sonitus.data.Source;
*
* @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
*/
-public class FileSource implements Source {
+public class FileSource extends AbstractControlledComponent implements Source {
/** The path of the file. */
private final String path;
import java.util.concurrent.atomic.AtomicReference;
import java.util.logging.Logger;
+import net.pterodactylus.sonitus.data.AbstractControlledComponent;
import net.pterodactylus.sonitus.data.Controller;
import net.pterodactylus.sonitus.data.Metadata;
import net.pterodactylus.sonitus.data.Source;
*
* @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
*/
-public class MultiSource implements Source {
+public class MultiSource extends AbstractControlledComponent implements Source {
/** The logger. */
private static final Logger logger = Logger.getLogger(MultiSource.class.getName());
sourceChanged = true;
this.source.notifyAll();
}
+ fireMetadataUpdated(source.metadata());
eventBus.post(new MetadataUpdated(this, source.metadata()));
logger.info(String.format("Next Source set: %s", source));
}
import java.util.List;
import java.util.Map;
+import net.pterodactylus.sonitus.data.AbstractControlledComponent;
import net.pterodactylus.sonitus.data.ContentMetadata;
import net.pterodactylus.sonitus.data.Controller;
import net.pterodactylus.sonitus.data.FormatMetadata;
*
* @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
*/
-public class StreamSource implements Source {
+public class StreamSource extends AbstractControlledComponent implements Source {
/** The event bus. */
private final EventBus eventBus;
return metadata;
}
metadata = metadata.title(streamMetadata.get().title());
+ fireMetadataUpdated(metadata);
eventBus.post(new MetadataUpdated(this, metadata));
return metadata;
}