From 90e3fa5e310f73249890ca1b5a5a72e646e9b081 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Helge=20Stenstr=C3=B6m?= Date: Tue, 14 Jun 2022 13:14:11 +0200 Subject: [PATCH 1/2] Remove a dependency cycle involving StreamPlayer, StreamPlayerEvent and StreamPlayerEventLauncher. Plus fixed a spelling error. The cycle is removed by - Make use of StreamPlayerInterface instead of StreamPlayer in StreamPlayerEvent - Stop sending the source as a part of the event. The source was one link in the cycle. --- .../streamplayer/stream/StreamPlayer.java | 48 +++++-------------- .../stream/StreamPlayerEvent.java | 21 ++------ .../stream/StreamPlayerEventLauncher.java | 31 ++++++------ .../stream/StreamPlayerEventTest.java | 26 +++------- 4 files changed, 35 insertions(+), 91 deletions(-) diff --git a/src/main/java/com/goxr3plus/streamplayer/stream/StreamPlayer.java b/src/main/java/com/goxr3plus/streamplayer/stream/StreamPlayer.java index de5f041..c3035f1 100644 --- a/src/main/java/com/goxr3plus/streamplayer/stream/StreamPlayer.java +++ b/src/main/java/com/goxr3plus/streamplayer/stream/StreamPlayer.java @@ -10,6 +10,14 @@ package com.goxr3plus.streamplayer.stream; +import com.goxr3plus.streamplayer.enums.Status; +import com.goxr3plus.streamplayer.stream.StreamPlayerException.PlayerException; +import javazoom.spi.PropertiesContainer; +import org.tritonus.share.sampled.TAudioFormat; +import org.tritonus.share.sampled.file.TAudioFileFormat; + +import javax.naming.OperationNotSupportedException; +import javax.sound.sampled.*; import java.io.File; import java.io.IOException; import java.io.InputStream; @@ -17,41 +25,11 @@ import java.nio.ByteBuffer; import java.nio.ByteOrder; import java.time.Duration; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.concurrent.Callable; -import java.util.concurrent.ExecutionException; -import java.util.concurrent.ExecutorService; -import java.util.concurrent.Executors; -import java.util.concurrent.Future; +import java.util.*; +import java.util.concurrent.*; import java.util.logging.Level; import java.util.logging.Logger; -import javax.naming.OperationNotSupportedException; -import javax.sound.sampled.AudioFileFormat; -import javax.sound.sampled.AudioFormat; -import javax.sound.sampled.AudioInputStream; -import javax.sound.sampled.AudioSystem; -import javax.sound.sampled.BooleanControl; -import javax.sound.sampled.DataLine; -import javax.sound.sampled.FloatControl; -import javax.sound.sampled.Line; -import javax.sound.sampled.LineUnavailableException; -import javax.sound.sampled.Mixer; -import javax.sound.sampled.SourceDataLine; -import javax.sound.sampled.UnsupportedAudioFileException; - -import org.tritonus.share.sampled.TAudioFormat; -import org.tritonus.share.sampled.file.TAudioFileFormat; - -import com.goxr3plus.streamplayer.enums.Status; -import com.goxr3plus.streamplayer.stream.StreamPlayerException.PlayerException; - -import javazoom.spi.PropertiesContainer; - /** * StreamPlayer is a class based on JavaSound API. It has been successfully tested under Java 10 * @@ -222,7 +200,7 @@ public void reset() { private String generateEvent(final Status status, final int encodedStreamPosition, final Object description) { try { return eventsExecutorService - .submit(new StreamPlayerEventLauncher(this, status, encodedStreamPosition, description, listeners)) + .submit(new StreamPlayerEventLauncher(this, status, encodedStreamPosition, description, listeners, logger)) .get(); } catch (InterruptedException | ExecutionException ex) { logger.log(Level.WARNING, "Problem in StreamPlayer generateEvent() method", ex); @@ -1407,10 +1385,6 @@ public boolean isSeeking() { return status == Status.SEEKING; } - Logger getLogger() { - return logger; - } - @Override public SourceDataLine getSourceDataLine() { return outlet.getSourceDataLine(); diff --git a/src/main/java/com/goxr3plus/streamplayer/stream/StreamPlayerEvent.java b/src/main/java/com/goxr3plus/streamplayer/stream/StreamPlayerEvent.java index 23cc13a..7f8af9c 100644 --- a/src/main/java/com/goxr3plus/streamplayer/stream/StreamPlayerEvent.java +++ b/src/main/java/com/goxr3plus/streamplayer/stream/StreamPlayerEvent.java @@ -37,17 +37,12 @@ public class StreamPlayerEvent { /** The stream position. */ private int encodedStreamPosition = -1; - /** The source. */ - private StreamPlayer source = null; - /** The description. */ private Object description = null; /** * Constructor. * - * @param source - * the source * @param status * the status * @param encodededStreamPosition @@ -55,9 +50,8 @@ public class StreamPlayerEvent { * @param description * the description */ - public StreamPlayerEvent(StreamPlayer source, Status status, int encodededStreamPosition, Object description) { - this.source = source; - this.playerStatus = status; + public StreamPlayerEvent(Status status, int encodededStreamPosition, Object description) { + this.playerStatus = status; this.encodedStreamPosition = encodededStreamPosition; this.description = description; } @@ -91,18 +85,9 @@ public Object getDescription() { return description; } - /** - * Gets the source. - * - * @return the source - */ - public Object getSource() { - return source; - } - @Override public String toString() { - return "Source :=" + source + " , Player Status := " + playerStatus + " , EncodedStreamPosition :=" + return "Player Status := " + playerStatus + " , EncodedStreamPosition :=" + encodedStreamPosition + " , Description :=" + description; } diff --git a/src/main/java/com/goxr3plus/streamplayer/stream/StreamPlayerEventLauncher.java b/src/main/java/com/goxr3plus/streamplayer/stream/StreamPlayerEventLauncher.java index 34d32f0..5dbc5f0 100644 --- a/src/main/java/com/goxr3plus/streamplayer/stream/StreamPlayerEventLauncher.java +++ b/src/main/java/com/goxr3plus/streamplayer/stream/StreamPlayerEventLauncher.java @@ -22,13 +22,13 @@ */ package com.goxr3plus.streamplayer.stream; +import com.goxr3plus.streamplayer.enums.Status; + import java.util.List; import java.util.concurrent.Callable; import java.util.logging.Level; import java.util.logging.Logger; -import com.goxr3plus.streamplayer.enums.Status; - /** * The Class StreamPlayerEventLauncher. * @@ -50,29 +50,26 @@ public class StreamPlayerEventLauncher implements Callable { private List listeners = null; /** The source. */ - private StreamPlayer source = null; + private StreamPlayerInterface source = null; /** * Instantiates a new stream player event launcher. - * - * @param source - * the source - * @param playerStatus - * the play state - * @param encodedStreamPosition - * the stream position - * @param description - * the description - * @param listeners + * + * @param source the source + * @param playerStatus the play state + * @param encodedStreamPosition the stream position + * @param description the description + * @param listeners will be called when events happens + * @param logger Logger to use for logging */ - public StreamPlayerEventLauncher(StreamPlayer source, Status playerStatus, int encodedStreamPosition, Object description, - List listeners) { + public StreamPlayerEventLauncher(StreamPlayerInterface source, Status playerStatus, int encodedStreamPosition, Object description, + List listeners, Logger logger) { this.source = source; this.playerState = playerStatus; this.encodedStreamPosition = encodedStreamPosition; this.description = description; this.listeners = listeners; - this.logger = source.getLogger(); + this.logger = logger; } @Override @@ -80,7 +77,7 @@ public String call() { // Notify all the listeners that the state has been updated if (listeners != null) { listeners.forEach(listener -> listener - .statusUpdated(new StreamPlayerEvent(source, playerState, encodedStreamPosition, description))); + .statusUpdated(new StreamPlayerEvent(playerState, encodedStreamPosition, description))); } logger.log(Level.INFO, "Stream player Status -> " + playerState); return "OK"; diff --git a/src/test/java/com/goxr3plus/streamplayer/stream/StreamPlayerEventTest.java b/src/test/java/com/goxr3plus/streamplayer/stream/StreamPlayerEventTest.java index d8ed334..5e7ebb3 100644 --- a/src/test/java/com/goxr3plus/streamplayer/stream/StreamPlayerEventTest.java +++ b/src/test/java/com/goxr3plus/streamplayer/stream/StreamPlayerEventTest.java @@ -3,26 +3,23 @@ import com.goxr3plus.streamplayer.enums.Status; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -import org.mockito.configuration.IMockitoConfiguration; -import static org.junit.jupiter.api.Assertions.*; -import static org.mockito.Mockito.mock; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertSame; class StreamPlayerEventTest { - private StreamPlayer source; private Object description; private Status status; - private int encodededStreamPosition; + private int encodedStreamPosition; private StreamPlayerEvent event; @BeforeEach void setUp() { description = new Object(); - source = mock(StreamPlayer.class); status = Status.RESUMED; - encodededStreamPosition = 12345; - event = new StreamPlayerEvent(source, status, encodededStreamPosition, description); + encodedStreamPosition = 12345; + event = new StreamPlayerEvent(status, encodedStreamPosition, description); } @Test @@ -32,12 +29,7 @@ void itReturnsTheStatus() { @Test void itReturnsTheEncodedStreamPosition() { - assertEquals(encodededStreamPosition, event.getEncodedStreamPosition()); - } - - @Test - void itReturnsTheSource() { - assertSame(source, event.getSource()); + assertEquals(encodedStreamPosition, event.getEncodedStreamPosition()); } @Test @@ -48,12 +40,8 @@ void itReturnsTheDescription() { @Test void itReturnsAString() { final String actual = event.toString(); - final String expected = "Source :=" - + source.toString() - + " , Player Status := RESUMED , EncodedStreamPosition :=12345 , Description :=" + final String expected = "Player Status := RESUMED , EncodedStreamPosition :=12345 , Description :=" + description.toString(); assertEquals(expected, actual); } - - } \ No newline at end of file From a294850d145e05027e3107bd8250a9caf1940602 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Helge=20Stenstr=C3=B6m?= Date: Tue, 14 Jun 2022 13:14:11 +0200 Subject: [PATCH 2/2] Remove a dependency cycle involving StreamPlayer, StreamPlayerEvent and StreamPlayerEventLauncher. Plus fixed a spelling error. The cycle is removed by - Make use of StreamPlayerInterface instead of StreamPlayer in StreamPlayerEvent - Stop sending the source as a part of the event. The source was one link in the cycle. --- .../streamplayer/stream/StreamPlayer.java | 6 +--- .../stream/StreamPlayerEvent.java | 21 ++----------- .../stream/StreamPlayerEventLauncher.java | 31 +++++++++---------- .../stream/StreamPlayerEventTest.java | 26 +++++----------- 4 files changed, 25 insertions(+), 59 deletions(-) diff --git a/src/main/java/com/goxr3plus/streamplayer/stream/StreamPlayer.java b/src/main/java/com/goxr3plus/streamplayer/stream/StreamPlayer.java index fbe5835..c0fc02a 100644 --- a/src/main/java/com/goxr3plus/streamplayer/stream/StreamPlayer.java +++ b/src/main/java/com/goxr3plus/streamplayer/stream/StreamPlayer.java @@ -223,7 +223,7 @@ public void reset() { private String generateEvent(final Status status, final int encodedStreamPosition, final Object description) { try { return eventsExecutorService - .submit(new StreamPlayerEventLauncher(this, status, encodedStreamPosition, description, listeners)) + .submit(new StreamPlayerEventLauncher(this, status, encodedStreamPosition, description, listeners, logger)) .get(); } catch (InterruptedException | ExecutionException ex) { logger.log(Level.WARNING, "Problem in StreamPlayer generateEvent() method", ex); @@ -1412,10 +1412,6 @@ public boolean isSeeking() { return status == Status.SEEKING; } - Logger getLogger() { - return logger; - } - @Override public SourceDataLine getSourceDataLine() { return outlet.getSourceDataLine(); diff --git a/src/main/java/com/goxr3plus/streamplayer/stream/StreamPlayerEvent.java b/src/main/java/com/goxr3plus/streamplayer/stream/StreamPlayerEvent.java index 23cc13a..7f8af9c 100644 --- a/src/main/java/com/goxr3plus/streamplayer/stream/StreamPlayerEvent.java +++ b/src/main/java/com/goxr3plus/streamplayer/stream/StreamPlayerEvent.java @@ -37,17 +37,12 @@ public class StreamPlayerEvent { /** The stream position. */ private int encodedStreamPosition = -1; - /** The source. */ - private StreamPlayer source = null; - /** The description. */ private Object description = null; /** * Constructor. * - * @param source - * the source * @param status * the status * @param encodededStreamPosition @@ -55,9 +50,8 @@ public class StreamPlayerEvent { * @param description * the description */ - public StreamPlayerEvent(StreamPlayer source, Status status, int encodededStreamPosition, Object description) { - this.source = source; - this.playerStatus = status; + public StreamPlayerEvent(Status status, int encodededStreamPosition, Object description) { + this.playerStatus = status; this.encodedStreamPosition = encodededStreamPosition; this.description = description; } @@ -91,18 +85,9 @@ public Object getDescription() { return description; } - /** - * Gets the source. - * - * @return the source - */ - public Object getSource() { - return source; - } - @Override public String toString() { - return "Source :=" + source + " , Player Status := " + playerStatus + " , EncodedStreamPosition :=" + return "Player Status := " + playerStatus + " , EncodedStreamPosition :=" + encodedStreamPosition + " , Description :=" + description; } diff --git a/src/main/java/com/goxr3plus/streamplayer/stream/StreamPlayerEventLauncher.java b/src/main/java/com/goxr3plus/streamplayer/stream/StreamPlayerEventLauncher.java index 34d32f0..5dbc5f0 100644 --- a/src/main/java/com/goxr3plus/streamplayer/stream/StreamPlayerEventLauncher.java +++ b/src/main/java/com/goxr3plus/streamplayer/stream/StreamPlayerEventLauncher.java @@ -22,13 +22,13 @@ */ package com.goxr3plus.streamplayer.stream; +import com.goxr3plus.streamplayer.enums.Status; + import java.util.List; import java.util.concurrent.Callable; import java.util.logging.Level; import java.util.logging.Logger; -import com.goxr3plus.streamplayer.enums.Status; - /** * The Class StreamPlayerEventLauncher. * @@ -50,29 +50,26 @@ public class StreamPlayerEventLauncher implements Callable { private List listeners = null; /** The source. */ - private StreamPlayer source = null; + private StreamPlayerInterface source = null; /** * Instantiates a new stream player event launcher. - * - * @param source - * the source - * @param playerStatus - * the play state - * @param encodedStreamPosition - * the stream position - * @param description - * the description - * @param listeners + * + * @param source the source + * @param playerStatus the play state + * @param encodedStreamPosition the stream position + * @param description the description + * @param listeners will be called when events happens + * @param logger Logger to use for logging */ - public StreamPlayerEventLauncher(StreamPlayer source, Status playerStatus, int encodedStreamPosition, Object description, - List listeners) { + public StreamPlayerEventLauncher(StreamPlayerInterface source, Status playerStatus, int encodedStreamPosition, Object description, + List listeners, Logger logger) { this.source = source; this.playerState = playerStatus; this.encodedStreamPosition = encodedStreamPosition; this.description = description; this.listeners = listeners; - this.logger = source.getLogger(); + this.logger = logger; } @Override @@ -80,7 +77,7 @@ public String call() { // Notify all the listeners that the state has been updated if (listeners != null) { listeners.forEach(listener -> listener - .statusUpdated(new StreamPlayerEvent(source, playerState, encodedStreamPosition, description))); + .statusUpdated(new StreamPlayerEvent(playerState, encodedStreamPosition, description))); } logger.log(Level.INFO, "Stream player Status -> " + playerState); return "OK"; diff --git a/src/test/java/com/goxr3plus/streamplayer/stream/StreamPlayerEventTest.java b/src/test/java/com/goxr3plus/streamplayer/stream/StreamPlayerEventTest.java index d8ed334..5e7ebb3 100644 --- a/src/test/java/com/goxr3plus/streamplayer/stream/StreamPlayerEventTest.java +++ b/src/test/java/com/goxr3plus/streamplayer/stream/StreamPlayerEventTest.java @@ -3,26 +3,23 @@ import com.goxr3plus.streamplayer.enums.Status; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -import org.mockito.configuration.IMockitoConfiguration; -import static org.junit.jupiter.api.Assertions.*; -import static org.mockito.Mockito.mock; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertSame; class StreamPlayerEventTest { - private StreamPlayer source; private Object description; private Status status; - private int encodededStreamPosition; + private int encodedStreamPosition; private StreamPlayerEvent event; @BeforeEach void setUp() { description = new Object(); - source = mock(StreamPlayer.class); status = Status.RESUMED; - encodededStreamPosition = 12345; - event = new StreamPlayerEvent(source, status, encodededStreamPosition, description); + encodedStreamPosition = 12345; + event = new StreamPlayerEvent(status, encodedStreamPosition, description); } @Test @@ -32,12 +29,7 @@ void itReturnsTheStatus() { @Test void itReturnsTheEncodedStreamPosition() { - assertEquals(encodededStreamPosition, event.getEncodedStreamPosition()); - } - - @Test - void itReturnsTheSource() { - assertSame(source, event.getSource()); + assertEquals(encodedStreamPosition, event.getEncodedStreamPosition()); } @Test @@ -48,12 +40,8 @@ void itReturnsTheDescription() { @Test void itReturnsAString() { final String actual = event.toString(); - final String expected = "Source :=" - + source.toString() - + " , Player Status := RESUMED , EncodedStreamPosition :=12345 , Description :=" + final String expected = "Player Status := RESUMED , EncodedStreamPosition :=12345 , Description :=" + description.toString(); assertEquals(expected, actual); } - - } \ No newline at end of file