From 87436ac0b103a112722c1df835e11ec928e57d38 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Tue, 28 May 2013 07:57:41 +0200 Subject: [PATCH] Add metadata listeners to components. --- .../sonitus/data/AbstractControlledComponent.java | 65 ++++++++++++++++++++++ .../sonitus/data/ControlledComponent.java | 16 ++++++ .../sonitus/data/MetadataListener.java | 37 ++++++++++++ .../sonitus/data/filter/DummyFilter.java | 4 +- .../pterodactylus/sonitus/data/sink/AudioSink.java | 4 +- .../pterodactylus/sonitus/data/sink/FileSink.java | 4 +- .../sonitus/data/sink/Icecast2Sink.java | 4 +- .../sonitus/data/source/FileSource.java | 3 +- .../sonitus/data/source/MultiSource.java | 4 +- .../sonitus/data/source/StreamSource.java | 4 +- 10 files changed, 138 insertions(+), 7 deletions(-) create mode 100644 src/main/java/net/pterodactylus/sonitus/data/AbstractControlledComponent.java create mode 100644 src/main/java/net/pterodactylus/sonitus/data/MetadataListener.java diff --git a/src/main/java/net/pterodactylus/sonitus/data/AbstractControlledComponent.java b/src/main/java/net/pterodactylus/sonitus/data/AbstractControlledComponent.java new file mode 100644 index 0000000..2c63a08 --- /dev/null +++ b/src/main/java/net/pterodactylus/sonitus/data/AbstractControlledComponent.java @@ -0,0 +1,65 @@ +/* + * 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 . + */ + +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 David ‘Bombe’ Roden + */ +public abstract class AbstractControlledComponent implements ControlledComponent { + + /** The list of metadata listeners. */ + private final List 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); + } + } + +} diff --git a/src/main/java/net/pterodactylus/sonitus/data/ControlledComponent.java b/src/main/java/net/pterodactylus/sonitus/data/ControlledComponent.java index 29c7828..ee50620 100644 --- a/src/main/java/net/pterodactylus/sonitus/data/ControlledComponent.java +++ b/src/main/java/net/pterodactylus/sonitus/data/ControlledComponent.java @@ -27,6 +27,22 @@ import java.util.List; 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 diff --git a/src/main/java/net/pterodactylus/sonitus/data/MetadataListener.java b/src/main/java/net/pterodactylus/sonitus/data/MetadataListener.java new file mode 100644 index 0000000..01efe78 --- /dev/null +++ b/src/main/java/net/pterodactylus/sonitus/data/MetadataListener.java @@ -0,0 +1,37 @@ +/* + * 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 . + */ + +package net.pterodactylus.sonitus.data; + +/** + * Interface for metadata listeners. + * + * @author David ‘Bombe’ Roden + */ +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); + +} diff --git a/src/main/java/net/pterodactylus/sonitus/data/filter/DummyFilter.java b/src/main/java/net/pterodactylus/sonitus/data/filter/DummyFilter.java index ea874d4..4b23c06 100644 --- a/src/main/java/net/pterodactylus/sonitus/data/filter/DummyFilter.java +++ b/src/main/java/net/pterodactylus/sonitus/data/filter/DummyFilter.java @@ -27,6 +27,7 @@ import java.util.Arrays; 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; @@ -40,7 +41,7 @@ import com.google.common.io.Closeables; * * @author David ‘Bombe’ Roden */ -public class DummyFilter implements Filter { +public class DummyFilter extends AbstractControlledComponent implements Filter { /** The name of this filter. */ private final String name; @@ -113,6 +114,7 @@ public class DummyFilter implements Filter { @Override public void metadataUpdated(Metadata metadata) { this.metadata = metadata; + fireMetadataUpdated(metadata); eventBus.post(new MetadataUpdated(this, metadata)); } diff --git a/src/main/java/net/pterodactylus/sonitus/data/sink/AudioSink.java b/src/main/java/net/pterodactylus/sonitus/data/sink/AudioSink.java index 066588d..7c28f2c 100644 --- a/src/main/java/net/pterodactylus/sonitus/data/sink/AudioSink.java +++ b/src/main/java/net/pterodactylus/sonitus/data/sink/AudioSink.java @@ -34,6 +34,7 @@ import javax.sound.sampled.FloatControl; 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; @@ -52,7 +53,7 @@ import com.google.common.eventbus.EventBus; * * @author David ‘Bombe’ Roden */ -public class AudioSink implements Sink { +public class AudioSink extends AbstractControlledComponent implements Sink { /** The logger. */ private static final Logger logger = Logger.getLogger(AudioSink.class.getName()); @@ -195,6 +196,7 @@ public class AudioSink implements Sink { public void metadataUpdated(Metadata metadata) { logger.info(String.format("Now playing %s.", metadata)); this.metadata = metadata; + fireMetadataUpdated(metadata); eventBus.post(new MetadataUpdated(this, metadata)); } diff --git a/src/main/java/net/pterodactylus/sonitus/data/sink/FileSink.java b/src/main/java/net/pterodactylus/sonitus/data/sink/FileSink.java index 48476b3..b3b1ff0 100644 --- a/src/main/java/net/pterodactylus/sonitus/data/sink/FileSink.java +++ b/src/main/java/net/pterodactylus/sonitus/data/sink/FileSink.java @@ -23,6 +23,7 @@ import java.util.Collections; 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; @@ -36,7 +37,7 @@ import com.google.common.eventbus.EventBus; * * @author David ‘Bombe’ Roden */ -public class FileSink implements Sink { +public class FileSink extends AbstractControlledComponent implements Sink { /** The logger. */ private static final Logger logger = Logger.getLogger(FileSink.class.getName()); @@ -107,6 +108,7 @@ public class FileSink implements Sink { @Override public void metadataUpdated(Metadata metadata) { this.metadata = metadata; + fireMetadataUpdated(metadata); eventBus.post(new MetadataUpdated(this, metadata)); } diff --git a/src/main/java/net/pterodactylus/sonitus/data/sink/Icecast2Sink.java b/src/main/java/net/pterodactylus/sonitus/data/sink/Icecast2Sink.java index 87d9496..515aece 100644 --- a/src/main/java/net/pterodactylus/sonitus/data/sink/Icecast2Sink.java +++ b/src/main/java/net/pterodactylus/sonitus/data/sink/Icecast2Sink.java @@ -28,6 +28,7 @@ import java.util.List; 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; @@ -44,7 +45,7 @@ import com.google.common.io.Closeables; * * @author David ‘Bombe’ Roden */ -public class Icecast2Sink implements Sink { +public class Icecast2Sink extends AbstractControlledComponent implements Sink { /** The logger. */ private static final Logger logger = Logger.getLogger(Icecast2Sink.class.getName()); @@ -209,6 +210,7 @@ public class Icecast2Sink implements Sink { } } }).start(); + fireMetadataUpdated(metadata); eventBus.post(new MetadataUpdated(this, metadata)); } diff --git a/src/main/java/net/pterodactylus/sonitus/data/source/FileSource.java b/src/main/java/net/pterodactylus/sonitus/data/source/FileSource.java index 766d7de..2829a42 100644 --- a/src/main/java/net/pterodactylus/sonitus/data/source/FileSource.java +++ b/src/main/java/net/pterodactylus/sonitus/data/source/FileSource.java @@ -27,6 +27,7 @@ import java.util.Arrays; 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; @@ -40,7 +41,7 @@ import com.google.common.base.Optional; * * @author David ‘Bombe’ Roden */ -public class FileSource implements Source { +public class FileSource extends AbstractControlledComponent implements Source { /** The path of the file. */ private final String path; diff --git a/src/main/java/net/pterodactylus/sonitus/data/source/MultiSource.java b/src/main/java/net/pterodactylus/sonitus/data/source/MultiSource.java index 13cf030..1d8f289 100644 --- a/src/main/java/net/pterodactylus/sonitus/data/source/MultiSource.java +++ b/src/main/java/net/pterodactylus/sonitus/data/source/MultiSource.java @@ -26,6 +26,7 @@ import java.util.List; 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; @@ -42,7 +43,7 @@ import com.google.inject.Inject; * * @author David ‘Bombe’ Roden */ -public class MultiSource implements Source { +public class MultiSource extends AbstractControlledComponent implements Source { /** The logger. */ private static final Logger logger = Logger.getLogger(MultiSource.class.getName()); @@ -86,6 +87,7 @@ public class MultiSource implements Source { sourceChanged = true; this.source.notifyAll(); } + fireMetadataUpdated(source.metadata()); eventBus.post(new MetadataUpdated(this, source.metadata())); logger.info(String.format("Next Source set: %s", source)); } diff --git a/src/main/java/net/pterodactylus/sonitus/data/source/StreamSource.java b/src/main/java/net/pterodactylus/sonitus/data/source/StreamSource.java index 604a868..4cb47e1 100644 --- a/src/main/java/net/pterodactylus/sonitus/data/source/StreamSource.java +++ b/src/main/java/net/pterodactylus/sonitus/data/source/StreamSource.java @@ -26,6 +26,7 @@ import java.util.Collections; 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; @@ -47,7 +48,7 @@ import com.google.common.primitives.Ints; * * @author David ‘Bombe’ Roden */ -public class StreamSource implements Source { +public class StreamSource extends AbstractControlledComponent implements Source { /** The event bus. */ private final EventBus eventBus; @@ -153,6 +154,7 @@ public class StreamSource implements Source { return metadata; } metadata = metadata.title(streamMetadata.get().title()); + fireMetadataUpdated(metadata); eventBus.post(new MetadataUpdated(this, metadata)); return metadata; } -- 2.7.4