From d07ec839a266057079d5a065176d46a0cc567b5d Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Mon, 27 May 2013 09:42:35 +0200 Subject: [PATCH] Send metadata updates events when the metadata changes. --- .../sonitus/data/event/MetadataUpdated.java | 72 ++++++++++++++++++++++ .../sonitus/data/filter/DummyFilter.java | 4 +- .../pterodactylus/sonitus/data/sink/AudioSink.java | 3 + .../pterodactylus/sonitus/data/sink/FileSink.java | 2 + .../sonitus/data/sink/Icecast2Sink.java | 2 + .../sonitus/data/source/MultiSource.java | 2 + .../sonitus/data/source/StreamSource.java | 2 + 7 files changed, 86 insertions(+), 1 deletion(-) create mode 100644 src/main/java/net/pterodactylus/sonitus/data/event/MetadataUpdated.java diff --git a/src/main/java/net/pterodactylus/sonitus/data/event/MetadataUpdated.java b/src/main/java/net/pterodactylus/sonitus/data/event/MetadataUpdated.java new file mode 100644 index 0000000..deb1ec6 --- /dev/null +++ b/src/main/java/net/pterodactylus/sonitus/data/event/MetadataUpdated.java @@ -0,0 +1,72 @@ +/* + * Sonitus - MetadataUpdated.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.event; + +import net.pterodactylus.sonitus.data.Controlled; +import net.pterodactylus.sonitus.data.Metadata; + +/** + * Event that notifies all listeners that the {@link Metadata} of a {@link + * Controlled} component was changed. + * + * @author David ‘Bombe’ Roden + */ +public class MetadataUpdated { + + /** The controlled component. */ + private final Controlled controlled; + + /** The new metadata. */ + private final Metadata metadata; + + /** + * Creates a new metadata updated event. + * + * @param controlled + * The controlled component + * @param metadata + * The new metadata + */ + public MetadataUpdated(Controlled controlled, Metadata metadata) { + this.controlled = controlled; + this.metadata = metadata; + } + + // + // ACCESSORS + // + + /** + * Returns the controlled component. + * + * @return The controlled component + */ + public Controlled controlled() { + return controlled; + } + + /** + * Returns the new metadata. + * + * @return The new metadata + */ + public Metadata metadata() { + return 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 4e19683..ea874d4 100644 --- a/src/main/java/net/pterodactylus/sonitus/data/filter/DummyFilter.java +++ b/src/main/java/net/pterodactylus/sonitus/data/filter/DummyFilter.java @@ -30,6 +30,7 @@ import java.util.List; import net.pterodactylus.sonitus.data.Controller; import net.pterodactylus.sonitus.data.Filter; import net.pterodactylus.sonitus.data.Metadata; +import net.pterodactylus.sonitus.data.event.MetadataUpdated; import com.google.common.eventbus.EventBus; import com.google.common.io.Closeables; @@ -89,7 +90,7 @@ public class DummyFilter implements Filter { @Override public void open(Metadata metadata) throws IOException { - this.metadata = metadata; + metadataUpdated(metadata); inputStream = createInputStream(); outputStream = createOutputStream(); } @@ -112,6 +113,7 @@ public class DummyFilter implements Filter { @Override public void metadataUpdated(Metadata metadata) { this.metadata = metadata; + eventBus.post(new MetadataUpdated(this, metadata)); } @Override 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 a0a4c6a..b0f0067 100644 --- a/src/main/java/net/pterodactylus/sonitus/data/sink/AudioSink.java +++ b/src/main/java/net/pterodactylus/sonitus/data/sink/AudioSink.java @@ -36,6 +36,7 @@ import net.pterodactylus.sonitus.data.Sink; import net.pterodactylus.sonitus.data.Source; import net.pterodactylus.sonitus.data.controller.Fader; import net.pterodactylus.sonitus.data.controller.Switch; +import net.pterodactylus.sonitus.data.event.MetadataUpdated; import net.pterodactylus.sonitus.io.IntegralWriteOutputStream; import com.google.common.base.Preconditions; @@ -155,6 +156,7 @@ public class AudioSink implements Sink { sourceDataLine = AudioSystem.getSourceDataLine(audioFormat); sourceDataLine.open(audioFormat); sourceDataLine.start(); + metadataUpdated(metadata); } catch (LineUnavailableException e) { /* TODO */ throw new IOException(e); @@ -171,6 +173,7 @@ public class AudioSink implements Sink { public void metadataUpdated(Metadata metadata) { logger.info(String.format("Now playing %s.", metadata)); this.metadata = metadata; + eventBus.post(new MetadataUpdated(this, metadata)); } @Override 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 769ac65..9738c50 100644 --- a/src/main/java/net/pterodactylus/sonitus/data/sink/FileSink.java +++ b/src/main/java/net/pterodactylus/sonitus/data/sink/FileSink.java @@ -26,6 +26,7 @@ import java.util.logging.Logger; import net.pterodactylus.sonitus.data.Controller; import net.pterodactylus.sonitus.data.Metadata; import net.pterodactylus.sonitus.data.Sink; +import net.pterodactylus.sonitus.data.event.MetadataUpdated; import com.google.common.eventbus.EventBus; @@ -105,6 +106,7 @@ public class FileSink implements Sink { @Override public void metadataUpdated(Metadata metadata) { this.metadata = metadata; + eventBus.post(new MetadataUpdated(this, metadata)); } @Override 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 4d492fd..b924cc5 100644 --- a/src/main/java/net/pterodactylus/sonitus/data/sink/Icecast2Sink.java +++ b/src/main/java/net/pterodactylus/sonitus/data/sink/Icecast2Sink.java @@ -32,6 +32,7 @@ import java.util.logging.Logger; import net.pterodactylus.sonitus.data.Controller; import net.pterodactylus.sonitus.data.Metadata; import net.pterodactylus.sonitus.data.Sink; +import net.pterodactylus.sonitus.data.event.MetadataUpdated; import net.pterodactylus.sonitus.io.InputStreamDrainer; import com.google.common.base.Function; @@ -218,6 +219,7 @@ public class Icecast2Sink implements Sink { } } }).start(); + eventBus.post(new MetadataUpdated(this, metadata)); } @Override 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 a1d0202..81f4f7c 100644 --- a/src/main/java/net/pterodactylus/sonitus/data/source/MultiSource.java +++ b/src/main/java/net/pterodactylus/sonitus/data/source/MultiSource.java @@ -29,6 +29,7 @@ import java.util.logging.Logger; import net.pterodactylus.sonitus.data.Controller; import net.pterodactylus.sonitus.data.Metadata; import net.pterodactylus.sonitus.data.Source; +import net.pterodactylus.sonitus.data.event.MetadataUpdated; import net.pterodactylus.sonitus.data.event.SourceFinishedEvent; import com.google.common.eventbus.EventBus; @@ -79,6 +80,7 @@ public class MultiSource implements Source { sourceChanged = true; this.source.notifyAll(); } + 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 a116d80..604a868 100644 --- a/src/main/java/net/pterodactylus/sonitus/data/source/StreamSource.java +++ b/src/main/java/net/pterodactylus/sonitus/data/source/StreamSource.java @@ -31,6 +31,7 @@ import net.pterodactylus.sonitus.data.Controller; import net.pterodactylus.sonitus.data.FormatMetadata; import net.pterodactylus.sonitus.data.Metadata; import net.pterodactylus.sonitus.data.Source; +import net.pterodactylus.sonitus.data.event.MetadataUpdated; import net.pterodactylus.sonitus.io.MetadataStream; import com.google.common.base.Optional; @@ -152,6 +153,7 @@ public class StreamSource implements Source { return metadata; } metadata = metadata.title(streamMetadata.get().title()); + eventBus.post(new MetadataUpdated(this, metadata)); return metadata; } -- 2.7.4