diff --git a/bundles/org.openhab.core.audio/src/main/java/org/eclipse/smarthome/core/audio/AudioException.java b/bundles/org.openhab.core.audio/src/main/java/org/eclipse/smarthome/core/audio/AudioException.java index 8a634b8de03..96308788a3f 100644 --- a/bundles/org.openhab.core.audio/src/main/java/org/eclipse/smarthome/core/audio/AudioException.java +++ b/bundles/org.openhab.core.audio/src/main/java/org/eclipse/smarthome/core/audio/AudioException.java @@ -12,47 +12,51 @@ */ package org.eclipse.smarthome.core.audio; +import org.eclipse.jdt.annotation.NonNullByDefault; +import org.eclipse.jdt.annotation.Nullable; + /** * General purpose audio exception * * @author Harald Kuhn - Initial contribution * @author Kelly Davis - Modified to match discussion in #584 */ +@NonNullByDefault public class AudioException extends Exception { private static final long serialVersionUID = 1L; - /** - * Constructs a new exception with null as its detail message. - */ + /** + * Constructs a new exception with null as its detail message. + */ public AudioException() { super(); } - /** - * Constructs a new exception with the specified detail message and cause. - * - * @param message Detail message - * @param cause The cause - */ - public AudioException(String message, Throwable cause) { + /** + * Constructs a new exception with the specified detail message and cause. + * + * @param message Detail message + * @param cause The cause + */ + public AudioException(String message, @Nullable Throwable cause) { super(message, cause); } - /** - * Constructs a new exception with the specified detail message. - * - * @param message Detail message - */ + /** + * Constructs a new exception with the specified detail message. + * + * @param message Detail message + */ public AudioException(String message) { super(message); } - /** - * Constructs a new exception with the specified cause. - * - * @param cause The cause - */ + /** + * Constructs a new exception with the specified cause. + * + * @param cause The cause + */ public AudioException(Throwable cause) { super(cause); } diff --git a/bundles/org.openhab.core.audio/src/main/java/org/eclipse/smarthome/core/audio/AudioFormat.java b/bundles/org.openhab.core.audio/src/main/java/org/eclipse/smarthome/core/audio/AudioFormat.java index 773e26cc9f0..6351034ecf0 100644 --- a/bundles/org.openhab.core.audio/src/main/java/org/eclipse/smarthome/core/audio/AudioFormat.java +++ b/bundles/org.openhab.core.audio/src/main/java/org/eclipse/smarthome/core/audio/AudioFormat.java @@ -14,6 +14,9 @@ import java.util.Set; +import org.eclipse.jdt.annotation.NonNullByDefault; +import org.eclipse.jdt.annotation.Nullable; + /** * An audio format definition * @@ -21,6 +24,7 @@ * @author Kelly Davis - Modified to match discussion in #584 * @author Kai Kreuzer - Moved class, included constants, added toString */ +@NonNullByDefault public class AudioFormat { // generic mp3 format without any further constraints @@ -111,36 +115,36 @@ public class AudioFormat { /** * Codec */ - private final String codec; + private final @Nullable String codec; /** * Container */ - private final String container; + private final @Nullable String container; /** * Big endian or little endian */ - private final Boolean bigEndian; + private final @Nullable Boolean bigEndian; /** * Bit depth * * @see Bit Depth */ - private final Integer bitDepth; + private final @Nullable Integer bitDepth; /** * Bit rate * * @see Bit Rate */ - private final Integer bitRate; + private final @Nullable Integer bitRate; /** * Sample frequency */ - private final Long frequency; + private final @Nullable Long frequency; /** * Constructs an instance with the specified properties. @@ -161,8 +165,8 @@ public class AudioFormat { * @param bitRate The bit rate of the audio * @param frequency The frequency at which the audio was sampled */ - public AudioFormat(String container, String codec, Boolean bigEndian, Integer bitDepth, Integer bitRate, - Long frequency) { + public AudioFormat(@Nullable String container, @Nullable String codec, @Nullable Boolean bigEndian, + @Nullable Integer bitDepth, @Nullable Integer bitRate, @Nullable Long frequency) { this.container = container; this.codec = codec; this.bigEndian = bigEndian; @@ -176,7 +180,7 @@ public AudioFormat(String container, String codec, Boolean bigEndian, Integer bi * * @return The codec */ - public String getCodec() { + public @Nullable String getCodec() { return codec; } @@ -185,7 +189,7 @@ public String getCodec() { * * @return The container */ - public String getContainer() { + public @Nullable String getContainer() { return container; } @@ -194,7 +198,7 @@ public String getContainer() { * * @return If format is big endian */ - public Boolean isBigEndian() { + public @Nullable Boolean isBigEndian() { return bigEndian; } @@ -204,7 +208,7 @@ public Boolean isBigEndian() { * @see Bit Depth * @return Bit depth */ - public Integer getBitDepth() { + public @Nullable Integer getBitDepth() { return bitDepth; } @@ -214,7 +218,7 @@ public Integer getBitDepth() { * @see Bit Rate * @return Bit rate */ - public Integer getBitRate() { + public @Nullable Integer getBitRate() { return bitRate; } @@ -223,7 +227,7 @@ public Integer getBitRate() { * * @return The frequency */ - public Long getFrequency() { + public @Nullable Long getFrequency() { return frequency; } @@ -233,7 +237,7 @@ public Long getFrequency() { * This AudioFormat is compatible with the passed AudioFormat if both have * the same value for all non-null members of this instance. */ - public boolean isCompatible(AudioFormat audioFormat) { + public boolean isCompatible(@Nullable AudioFormat audioFormat) { if (audioFormat == null) { return false; } @@ -265,7 +269,7 @@ public boolean isCompatible(AudioFormat audioFormat) { * @param outputs the supported audio formats of an audio sink * @return the best matching format or null, if source and sink are incompatible */ - public static AudioFormat getBestMatch(Set inputs, Set outputs) { + public static @Nullable AudioFormat getBestMatch(Set inputs, Set outputs) { AudioFormat preferredFormat = getPreferredFormat(inputs); if (preferredFormat != null) { for (AudioFormat output : outputs) { @@ -291,7 +295,7 @@ public static AudioFormat getBestMatch(Set inputs, Set * @return The preferred AudioFormat or null if none could be determined. A passed concrete format is preferred * adding default values to an abstract AudioFormat in the passed set. */ - public static AudioFormat getPreferredFormat(Set audioFormats) { + public static @Nullable AudioFormat getPreferredFormat(Set audioFormats) { // Return the first concrete AudioFormat found for (AudioFormat currentAudioFormat : audioFormats) { // Check if currentAudioFormat is abstract @@ -387,7 +391,7 @@ public static AudioFormat getPreferredFormat(Set audioFormats) { } @Override - public boolean equals(Object obj) { + public boolean equals(@Nullable Object obj) { if (obj instanceof AudioFormat) { AudioFormat format = (AudioFormat) obj; if (!(null == getCodec() ? null == format.getCodec() : getCodec().equals(format.getCodec()))) { diff --git a/bundles/org.openhab.core.audio/src/main/java/org/eclipse/smarthome/core/audio/AudioHTTPServer.java b/bundles/org.openhab.core.audio/src/main/java/org/eclipse/smarthome/core/audio/AudioHTTPServer.java index 0cee8c5083e..c52ee220b24 100644 --- a/bundles/org.openhab.core.audio/src/main/java/org/eclipse/smarthome/core/audio/AudioHTTPServer.java +++ b/bundles/org.openhab.core.audio/src/main/java/org/eclipse/smarthome/core/audio/AudioHTTPServer.java @@ -12,6 +12,7 @@ */ package org.eclipse.smarthome.core.audio; +import org.eclipse.jdt.annotation.NonNullByDefault; import org.eclipse.smarthome.core.audio.internal.AudioServlet; /** @@ -21,6 +22,7 @@ * * @author Kai Kreuzer - Initial contribution */ +@NonNullByDefault public interface AudioHTTPServer { /** diff --git a/bundles/org.openhab.core.audio/src/main/java/org/eclipse/smarthome/core/audio/AudioSource.java b/bundles/org.openhab.core.audio/src/main/java/org/eclipse/smarthome/core/audio/AudioSource.java index ee7580ecd30..86cbc40be5c 100644 --- a/bundles/org.openhab.core.audio/src/main/java/org/eclipse/smarthome/core/audio/AudioSource.java +++ b/bundles/org.openhab.core.audio/src/main/java/org/eclipse/smarthome/core/audio/AudioSource.java @@ -15,6 +15,9 @@ import java.util.Locale; import java.util.Set; +import org.eclipse.jdt.annotation.NonNullByDefault; +import org.eclipse.jdt.annotation.Nullable; + /** * This is an audio source, which can provide a continuous live stream of audio. * Its main use is for microphones and other "line-in" sources and it can be registered as a service in order to make @@ -22,6 +25,7 @@ * * @author Kai Kreuzer - Initial contribution */ +@NonNullByDefault public interface AudioSource { /** @@ -37,7 +41,7 @@ public interface AudioSource { * @param locale the locale to provide the label for * @return a localized string to be used in UIs */ - String getLabel(Locale locale); + String getLabel(@Nullable Locale locale); /** * Obtain the audio formats supported by this AudioSource diff --git a/bundles/org.openhab.core.audio/src/main/java/org/eclipse/smarthome/core/audio/AudioStream.java b/bundles/org.openhab.core.audio/src/main/java/org/eclipse/smarthome/core/audio/AudioStream.java index dea6ba79c7f..eebdba64775 100644 --- a/bundles/org.openhab.core.audio/src/main/java/org/eclipse/smarthome/core/audio/AudioStream.java +++ b/bundles/org.openhab.core.audio/src/main/java/org/eclipse/smarthome/core/audio/AudioStream.java @@ -14,6 +14,8 @@ import java.io.InputStream; +import org.eclipse.jdt.annotation.NonNullByDefault; + /** * Wrapper for a source of audio data. * @@ -26,6 +28,7 @@ * @author Kelly Davis - Modified to match discussion in #584 * @author Kai Kreuzer - Refactored to be only a temporary instance for the stream */ +@NonNullByDefault public abstract class AudioStream extends InputStream { /** diff --git a/bundles/org.openhab.core.audio/src/main/java/org/eclipse/smarthome/core/audio/ByteArrayAudioStream.java b/bundles/org.openhab.core.audio/src/main/java/org/eclipse/smarthome/core/audio/ByteArrayAudioStream.java index 5a4a5dd5582..dc578682b69 100644 --- a/bundles/org.openhab.core.audio/src/main/java/org/eclipse/smarthome/core/audio/ByteArrayAudioStream.java +++ b/bundles/org.openhab.core.audio/src/main/java/org/eclipse/smarthome/core/audio/ByteArrayAudioStream.java @@ -16,16 +16,19 @@ import java.io.IOException; import java.io.InputStream; +import org.eclipse.jdt.annotation.NonNullByDefault; + /** * This is an implementation of a {@link FixedLengthAudioStream}, which is based on a simple byte array. * * @author Kai Kreuzer - Initial contribution */ +@NonNullByDefault public class ByteArrayAudioStream extends FixedLengthAudioStream { - private byte[] bytes; - private AudioFormat format; - private ByteArrayInputStream stream; + private final byte[] bytes; + private final AudioFormat format; + private final ByteArrayInputStream stream; public ByteArrayAudioStream(byte[] bytes, AudioFormat format) { this.bytes = bytes; diff --git a/bundles/org.openhab.core.audio/src/main/java/org/eclipse/smarthome/core/audio/FileAudioStream.java b/bundles/org.openhab.core.audio/src/main/java/org/eclipse/smarthome/core/audio/FileAudioStream.java index f8beea44603..015b6cba792 100644 --- a/bundles/org.openhab.core.audio/src/main/java/org/eclipse/smarthome/core/audio/FileAudioStream.java +++ b/bundles/org.openhab.core.audio/src/main/java/org/eclipse/smarthome/core/audio/FileAudioStream.java @@ -19,6 +19,7 @@ import java.io.InputStream; import org.apache.commons.io.IOUtils; +import org.eclipse.jdt.annotation.NonNullByDefault; import org.eclipse.smarthome.core.audio.utils.AudioStreamUtils; /** @@ -28,6 +29,7 @@ * @author Kai Kreuzer - Refactored to take a file as input * @author Christoph Weitkamp - Refactored use of filename extension */ +@NonNullByDefault public class FileAudioStream extends FixedLengthAudioStream { public static final String WAV_EXTENSION = "wav"; diff --git a/bundles/org.openhab.core.audio/src/main/java/org/eclipse/smarthome/core/audio/FixedLengthAudioStream.java b/bundles/org.openhab.core.audio/src/main/java/org/eclipse/smarthome/core/audio/FixedLengthAudioStream.java index 337d1527323..596d9df9ded 100644 --- a/bundles/org.openhab.core.audio/src/main/java/org/eclipse/smarthome/core/audio/FixedLengthAudioStream.java +++ b/bundles/org.openhab.core.audio/src/main/java/org/eclipse/smarthome/core/audio/FixedLengthAudioStream.java @@ -14,12 +14,15 @@ import java.io.InputStream; +import org.eclipse.jdt.annotation.NonNullByDefault; + /** * This is an {@link AudioStream}, which can provide information about its absolute length and is able to provide * cloned streams. * * @author Kai Kreuzer - Initial contribution */ +@NonNullByDefault public abstract class FixedLengthAudioStream extends AudioStream { /** diff --git a/bundles/org.openhab.core.audio/src/main/java/org/eclipse/smarthome/core/audio/URLAudioStream.java b/bundles/org.openhab.core.audio/src/main/java/org/eclipse/smarthome/core/audio/URLAudioStream.java index c0eaf64a3ae..5c823780036 100644 --- a/bundles/org.openhab.core.audio/src/main/java/org/eclipse/smarthome/core/audio/URLAudioStream.java +++ b/bundles/org.openhab.core.audio/src/main/java/org/eclipse/smarthome/core/audio/URLAudioStream.java @@ -23,6 +23,8 @@ import java.util.regex.Pattern; import org.apache.commons.io.IOUtils; +import org.eclipse.jdt.annotation.NonNullByDefault; +import org.eclipse.jdt.annotation.Nullable; import org.eclipse.smarthome.core.audio.utils.AudioStreamUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -35,6 +37,7 @@ * @author Kai Kreuzer - Refactored to not require a source * @author Christoph Weitkamp - Refactored use of filename extension */ +@NonNullByDefault public class URLAudioStream extends AudioStream { private static final Pattern PLS_STREAM_PATTERN = Pattern.compile("^File[0-9]=(.+)$"); @@ -48,12 +51,9 @@ public class URLAudioStream extends AudioStream { private final InputStream inputStream; private String url; - private Socket shoutCastSocket; + private @Nullable Socket shoutCastSocket; public URLAudioStream(String url) throws AudioException { - if (url == null) { - throw new IllegalArgumentException("url must not be null!"); - } this.url = url; this.audioFormat = new AudioFormat(AudioFormat.CONTAINER_NONE, AudioFormat.CODEC_MP3, false, 16, null, null); this.inputStream = createInputStream(); @@ -96,14 +96,15 @@ private InputStream createInputStream() throws AudioException { // Java does not parse non-standard headers used by SHOUTCast int port = streamUrl.getPort() > 0 ? streamUrl.getPort() : 80; // Manipulate User-Agent to receive a stream - shoutCastSocket = new Socket(streamUrl.getHost(), port); + Socket socket = new Socket(streamUrl.getHost(), port); + shoutCastSocket = socket; - OutputStream os = shoutCastSocket.getOutputStream(); + OutputStream os = socket.getOutputStream(); String userAgent = "WinampMPEG/5.09"; String req = "GET / HTTP/1.0\r\nuser-agent: " + userAgent + "\r\nIcy-MetaData: 1\r\nConnection: keep-alive\r\n\r\n"; os.write(req.getBytes()); - return shoutCastSocket.getInputStream(); + return socket.getInputStream(); } else { // getInputStream() method is more error-proof than openStream(), // because openStream() does openConnection().getInputStream(), diff --git a/bundles/org.openhab.core.audio/src/main/java/org/eclipse/smarthome/core/audio/UnsupportedAudioFormatException.java b/bundles/org.openhab.core.audio/src/main/java/org/eclipse/smarthome/core/audio/UnsupportedAudioFormatException.java index 6db03a834a6..0d2724e6a90 100644 --- a/bundles/org.openhab.core.audio/src/main/java/org/eclipse/smarthome/core/audio/UnsupportedAudioFormatException.java +++ b/bundles/org.openhab.core.audio/src/main/java/org/eclipse/smarthome/core/audio/UnsupportedAudioFormatException.java @@ -12,50 +12,55 @@ */ package org.eclipse.smarthome.core.audio; +import org.eclipse.jdt.annotation.NonNullByDefault; +import org.eclipse.jdt.annotation.Nullable; + /** * Thrown when a requested format is not supported by an {@link AudioSource} * or {@link AudioSink} implementation - * + * * @author Harald Kuhn - Initial contribution * @author Kelly Davis - Modified to match discussion in #584 */ +@NonNullByDefault public class UnsupportedAudioFormatException extends AudioException { private static final long serialVersionUID = 1L; - /** - * Unsupported {@link AudioFormat} - */ - private AudioFormat unsupportedFormat; + /** + * Unsupported {@link AudioFormat} + */ + private @Nullable AudioFormat unsupportedFormat; - /** - * Constructs a new exception with the specified detail message, unsupported format, and cause. - * - * @param message Detail message - * @param unsupportedFormat Unsupported format - * @param cause The cause - */ - public UnsupportedAudioFormatException(String message, AudioFormat unsupportedFormat, Throwable cause) { + /** + * Constructs a new exception with the specified detail message, unsupported format, and cause. + * + * @param message Detail message + * @param unsupportedFormat Unsupported format + * @param cause The cause + */ + public UnsupportedAudioFormatException(String message, @Nullable AudioFormat unsupportedFormat, + @Nullable Throwable cause) { super(message, cause); this.unsupportedFormat = unsupportedFormat; } - /** - * Constructs a new exception with the specified detail message and unsupported format. - * - * @param message Detail message - * @param unsupportedFormat Unsupported format - */ - public UnsupportedAudioFormatException(String message, AudioFormat unsupportedFormat) { + /** + * Constructs a new exception with the specified detail message and unsupported format. + * + * @param message Detail message + * @param unsupportedFormat Unsupported format + */ + public UnsupportedAudioFormatException(String message, @Nullable AudioFormat unsupportedFormat) { this(message, unsupportedFormat, null); } - /** - * Gets the unsupported format - * - * @return The unsupported format - */ - public AudioFormat getUnsupportedFormat() { + /** + * Gets the unsupported format + * + * @return The unsupported format + */ + public @Nullable AudioFormat getUnsupportedFormat() { return unsupportedFormat; } } diff --git a/bundles/org.openhab.core.audio/src/main/java/org/eclipse/smarthome/core/audio/UnsupportedAudioStreamException.java b/bundles/org.openhab.core.audio/src/main/java/org/eclipse/smarthome/core/audio/UnsupportedAudioStreamException.java index a59c3a1b289..6eb836a66c1 100644 --- a/bundles/org.openhab.core.audio/src/main/java/org/eclipse/smarthome/core/audio/UnsupportedAudioStreamException.java +++ b/bundles/org.openhab.core.audio/src/main/java/org/eclipse/smarthome/core/audio/UnsupportedAudioStreamException.java @@ -12,13 +12,16 @@ */ package org.eclipse.smarthome.core.audio; +import org.eclipse.jdt.annotation.NonNullByDefault; +import org.eclipse.jdt.annotation.Nullable; + /** * Thrown when a requested {@link AudioStream} is not supported by an {@link AudioSource} or {@link AudioSink} * implementation - * + * * @author Christoph Weitkamp - Initial contribution - * */ +@NonNullByDefault public class UnsupportedAudioStreamException extends AudioException { private static final long serialVersionUID = 1L; @@ -26,7 +29,7 @@ public class UnsupportedAudioStreamException extends AudioException { /** * Unsupported {@link AudioStream} */ - private Class unsupportedAudioStreamClass; + private @Nullable Class unsupportedAudioStreamClass; /** * Constructs a new exception with the specified detail message, unsupported format, and cause. @@ -35,8 +38,8 @@ public class UnsupportedAudioStreamException extends AudioException { * @param unsupportedAudioStreamClass The unsupported audio stream class * @param cause The cause */ - public UnsupportedAudioStreamException(String message, Class unsupportedAudioStreamClass, - Throwable cause) { + public UnsupportedAudioStreamException(String message, + @Nullable Class unsupportedAudioStreamClass, @Nullable Throwable cause) { super(message, cause); this.unsupportedAudioStreamClass = unsupportedAudioStreamClass; } @@ -47,7 +50,8 @@ public UnsupportedAudioStreamException(String message, Class unsupportedAudioStreamClass) { + public UnsupportedAudioStreamException(String message, + @Nullable Class unsupportedAudioStreamClass) { this(message, unsupportedAudioStreamClass, null); } @@ -56,7 +60,7 @@ public UnsupportedAudioStreamException(String message, Class getUnsupportedAudioStreamClass() { + public @Nullable Class getUnsupportedAudioStreamClass() { return unsupportedAudioStreamClass; } } diff --git a/bundles/org.openhab.core.audio/src/main/java/org/eclipse/smarthome/core/audio/internal/AudioConsoleCommandExtension.java b/bundles/org.openhab.core.audio/src/main/java/org/eclipse/smarthome/core/audio/internal/AudioConsoleCommandExtension.java index e2667e8aedd..89aaccb233d 100644 --- a/bundles/org.openhab.core.audio/src/main/java/org/eclipse/smarthome/core/audio/internal/AudioConsoleCommandExtension.java +++ b/bundles/org.openhab.core.audio/src/main/java/org/eclipse/smarthome/core/audio/internal/AudioConsoleCommandExtension.java @@ -20,6 +20,8 @@ import java.util.Set; import org.apache.commons.lang.ArrayUtils; +import org.eclipse.jdt.annotation.NonNullByDefault; +import org.eclipse.jdt.annotation.Nullable; import org.eclipse.smarthome.core.audio.AudioException; import org.eclipse.smarthome.core.audio.AudioManager; import org.eclipse.smarthome.core.audio.AudioSink; @@ -29,6 +31,7 @@ import org.eclipse.smarthome.io.console.Console; import org.eclipse.smarthome.io.console.extensions.AbstractConsoleCommandExtension; import org.eclipse.smarthome.io.console.extensions.ConsoleCommandExtension; +import org.osgi.service.component.annotations.Activate; import org.osgi.service.component.annotations.Component; import org.osgi.service.component.annotations.Reference; @@ -40,6 +43,7 @@ * @author Christoph Weitkamp - Added parameter to adjust the volume * @author Wouter Born - Sort audio sink and source options */ +@NonNullByDefault @Component(service = ConsoleCommandExtension.class) public class AudioConsoleCommandExtension extends AbstractConsoleCommandExtension { @@ -48,11 +52,15 @@ public class AudioConsoleCommandExtension extends AbstractConsoleCommandExtensio static final String SUBCMD_SOURCES = "sources"; static final String SUBCMD_SINKS = "sinks"; - private AudioManager audioManager; - private LocaleProvider localeProvider; + private final AudioManager audioManager; + private final LocaleProvider localeProvider; - public AudioConsoleCommandExtension() { + @Activate + public AudioConsoleCommandExtension(final @Reference AudioManager audioManager, + final @Reference LocaleProvider localeProvider) { super("audio", "Commands around audio enablement features."); + this.audioManager = audioManager; + this.localeProvider = localeProvider; } @Override @@ -66,7 +74,6 @@ public List getUsages() { "streams the sound from the url through the optionally specified audio sink(s)"), buildCommandUsage(SUBCMD_SOURCES, "lists the audio sources"), buildCommandUsage(SUBCMD_SINKS, "lists the audio sinks") }); - } @Override @@ -152,16 +159,15 @@ private void play(String[] args, Console console) { default: break; } - } - private void playOnSinks(String pattern, String fileName, PercentType volume, Console console) { + private void playOnSinks(String pattern, String fileName, @Nullable PercentType volume, Console console) { for (String sinkId : audioManager.getSinkIds(pattern)) { playOnSink(sinkId, fileName, volume, console); } } - private void playOnSink(String sinkId, String fileName, PercentType volume, Console console) { + private void playOnSink(@Nullable String sinkId, String fileName, @Nullable PercentType volume, Console console) { try { audioManager.playFile(fileName, sinkId, volume); } catch (AudioException e) { @@ -188,7 +194,7 @@ private void streamOnSinks(String pattern, String url, Console console) { } } - private void streamOnSink(String sinkId, String url, Console console) { + private void streamOnSink(@Nullable String sinkId, String url, Console console) { try { audioManager.stream(url, sinkId); } catch (AudioException e) { @@ -196,22 +202,4 @@ private void streamOnSink(String sinkId, String url, Console console) { } } - @Reference - protected void setAudioManager(AudioManager audioManager) { - this.audioManager = audioManager; - } - - protected void unsetAudioManager(AudioManager audioManager) { - this.audioManager = null; - } - - @Reference - protected void setLocaleProvider(LocaleProvider localeProvider) { - this.localeProvider = localeProvider; - } - - protected void unsetLocaleProvider(LocaleProvider localeProvider) { - this.localeProvider = null; - } - } diff --git a/bundles/org.openhab.core.audio/src/main/java/org/eclipse/smarthome/core/audio/internal/AudioManagerImpl.java b/bundles/org.openhab.core.audio/src/main/java/org/eclipse/smarthome/core/audio/internal/AudioManagerImpl.java index ab18bb2ce92..3f169efbf75 100644 --- a/bundles/org.openhab.core.audio/src/main/java/org/eclipse/smarthome/core/audio/internal/AudioManagerImpl.java +++ b/bundles/org.openhab.core.audio/src/main/java/org/eclipse/smarthome/core/audio/internal/AudioManagerImpl.java @@ -26,6 +26,8 @@ import java.util.Set; import java.util.concurrent.ConcurrentHashMap; +import org.eclipse.jdt.annotation.NonNullByDefault; +import org.eclipse.jdt.annotation.Nullable; import org.eclipse.smarthome.config.core.ConfigConstants; import org.eclipse.smarthome.config.core.ConfigOptionProvider; import org.eclipse.smarthome.config.core.ConfigurableService; @@ -60,6 +62,7 @@ * @author Christoph Weitkamp - Added parameter to adjust the volume * @author Wouter Born - Sort audio sink and source options */ +@NonNullByDefault @Component(immediate = true, configurationPid = "org.eclipse.smarthome.audio", property = { // Constants.SERVICE_PID + "=org.eclipse.smarthome.audio", // ConfigurableService.SERVICE_PROPERTY_CATEGORY + "=system", // @@ -82,8 +85,8 @@ public class AudioManagerImpl implements AudioManager, ConfigOptionProvider { /** * default settings filled through the service configuration */ - private String defaultSource; - private String defaultSink; + private @Nullable String defaultSource; + private @Nullable String defaultSink; @Activate protected void activate(Map config) { @@ -95,7 +98,7 @@ protected void deactivate() { } @Modified - void modified(Map config) { + void modified(@Nullable Map config) { if (config != null) { this.defaultSource = config.containsKey(CONFIG_DEFAULT_SOURCE) ? config.get(CONFIG_DEFAULT_SOURCE).toString() @@ -106,17 +109,17 @@ void modified(Map config) { } @Override - public void play(AudioStream audioStream) { + public void play(@Nullable AudioStream audioStream) { play(audioStream, null); } @Override - public void play(AudioStream audioStream, String sinkId) { + public void play(@Nullable AudioStream audioStream, @Nullable String sinkId) { play(audioStream, sinkId, null); } @Override - public void play(AudioStream audioStream, String sinkId, PercentType volume) { + public void play(@Nullable AudioStream audioStream, @Nullable String sinkId, @Nullable PercentType volume) { AudioSink sink = getSink(sinkId); if (sink != null) { PercentType oldVolume = null; @@ -162,17 +165,17 @@ public void playFile(String fileName) throws AudioException { } @Override - public void playFile(String fileName, PercentType volume) throws AudioException { + public void playFile(String fileName, @Nullable PercentType volume) throws AudioException { playFile(fileName, null, volume); } @Override - public void playFile(String fileName, String sinkId) throws AudioException { + public void playFile(String fileName, @Nullable String sinkId) throws AudioException { playFile(fileName, sinkId, null); } @Override - public void playFile(String fileName, String sinkId, PercentType volume) throws AudioException { + public void playFile(String fileName, @Nullable String sinkId, @Nullable PercentType volume) throws AudioException { Objects.requireNonNull(fileName, "File cannot be played as fileName is null."); File file = new File( @@ -182,18 +185,18 @@ public void playFile(String fileName, String sinkId, PercentType volume) throws } @Override - public void stream(String url) throws AudioException { + public void stream(@Nullable String url) throws AudioException { stream(url, null); } @Override - public void stream(String url, String sinkId) throws AudioException { + public void stream(@Nullable String url, @Nullable String sinkId) throws AudioException { AudioStream audioStream = url != null ? new URLAudioStream(url) : null; play(audioStream, sinkId, null); } @Override - public PercentType getVolume(String sinkId) throws IOException { + public PercentType getVolume(@Nullable String sinkId) throws IOException { AudioSink sink = getSink(sinkId); if (sink != null) { @@ -203,7 +206,7 @@ public PercentType getVolume(String sinkId) throws IOException { } @Override - public void setVolume(PercentType volume, String sinkId) throws IOException { + public void setVolume(PercentType volume, @Nullable String sinkId) throws IOException { AudioSink sink = getSink(sinkId); if (sink != null) { @@ -212,7 +215,7 @@ public void setVolume(PercentType volume, String sinkId) throws IOException { } @Override - public AudioSource getSource() { + public @Nullable AudioSource getSource() { AudioSource source = null; if (defaultSource != null) { source = audioSources.get(defaultSource); @@ -233,7 +236,7 @@ public Set getAllSources() { } @Override - public AudioSink getSink() { + public @Nullable AudioSink getSink() { AudioSink sink = null; if (defaultSink != null) { sink = audioSinks.get(defaultSink); @@ -268,7 +271,7 @@ public Set getSourceIds(String pattern) { } @Override - public AudioSink getSink(String sinkId) { + public @Nullable AudioSink getSink(@Nullable String sinkId) { return (sinkId == null) ? getSink() : audioSinks.get(sinkId); } @@ -287,7 +290,7 @@ public Set getSinkIds(String pattern) { } @Override - public Collection getParameterOptions(URI uri, String param, Locale locale) { + public @Nullable Collection getParameterOptions(URI uri, String param, @Nullable Locale locale) { if (uri.toString().equals(CONFIG_URI)) { final Locale safeLocale = locale != null ? locale : Locale.getDefault(); if (CONFIG_DEFAULT_SOURCE.equals(param)) { diff --git a/bundles/org.openhab.core.audio/src/main/java/org/eclipse/smarthome/core/audio/internal/AudioServlet.java b/bundles/org.openhab.core.audio/src/main/java/org/eclipse/smarthome/core/audio/internal/AudioServlet.java index 27e18601c99..faf4814f86c 100644 --- a/bundles/org.openhab.core.audio/src/main/java/org/eclipse/smarthome/core/audio/internal/AudioServlet.java +++ b/bundles/org.openhab.core.audio/src/main/java/org/eclipse/smarthome/core/audio/internal/AudioServlet.java @@ -29,6 +29,8 @@ import org.apache.commons.io.IOUtils; import org.apache.commons.lang.StringUtils; +import org.eclipse.jdt.annotation.NonNullByDefault; +import org.eclipse.jdt.annotation.Nullable; import org.eclipse.smarthome.core.audio.AudioException; import org.eclipse.smarthome.core.audio.AudioFormat; import org.eclipse.smarthome.core.audio.AudioHTTPServer; @@ -46,6 +48,7 @@ * * @author Kai Kreuzer - Initial contribution */ +@NonNullByDefault @Component public class AudioServlet extends SmartHomeServlet implements AudioHTTPServer { @@ -79,7 +82,7 @@ public void unsetHttpService(HttpService httpService) { super.unsetHttpService(httpService); } - private InputStream prepareInputStream(final String streamId, final HttpServletResponse resp) + private @Nullable InputStream prepareInputStream(final String streamId, final HttpServletResponse resp) throws AudioException { final AudioStream stream; final boolean multiAccess; @@ -125,7 +128,8 @@ private InputStream prepareInputStream(final String streamId, final HttpServletR } @Override - protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + protected void doGet(@NonNullByDefault({}) HttpServletRequest req, @NonNullByDefault({}) HttpServletResponse resp) + throws ServletException, IOException { removeTimedOutStreams(); final String streamId = StringUtils.substringBefore(StringUtils.substringAfterLast(req.getRequestURI(), "/"), diff --git a/bundles/org.openhab.core.audio/src/main/java/org/eclipse/smarthome/core/audio/utils/AudioStreamUtils.java b/bundles/org.openhab.core.audio/src/main/java/org/eclipse/smarthome/core/audio/utils/AudioStreamUtils.java index 4c634b93e71..c5e3344dede 100644 --- a/bundles/org.openhab.core.audio/src/main/java/org/eclipse/smarthome/core/audio/utils/AudioStreamUtils.java +++ b/bundles/org.openhab.core.audio/src/main/java/org/eclipse/smarthome/core/audio/utils/AudioStreamUtils.java @@ -12,27 +12,26 @@ */ package org.eclipse.smarthome.core.audio.utils; +import org.eclipse.jdt.annotation.NonNullByDefault; + /** * Some general filename and extension utilities. - * + * * @author Christoph Weitkamp - Initial contribution - * */ +@NonNullByDefault public class AudioStreamUtils { public static final String EXTENSION_SEPARATOR = "."; /** * Gets the base name of a filename. - * + * * @param filename the filename to query * @return the base name of the file or an empty string if none exists or {@code null} if the filename is * {@code null} */ public static String getBaseName(String filename) { - if (filename == null) { - return null; - } final int index = filename.lastIndexOf(EXTENSION_SEPARATOR); if (index == -1) { return filename; @@ -43,15 +42,12 @@ public static String getBaseName(String filename) { /** * Gets the extension of a filename. - * + * * @param filename the filename to retrieve the extension of * @return the extension of the file or an empty string if none exists or {@code null} if the filename is * {@code null} */ public static String getExtension(String filename) { - if (filename == null) { - return null; - } final int index = filename.lastIndexOf(EXTENSION_SEPARATOR); if (index == -1) { return ""; @@ -62,19 +58,13 @@ public static String getExtension(String filename) { /** * Checks if the extension of a filename matches the given. - * + * * @param filename the filename to check the extension of * @param extension the extension to check for * @return {@code true} if the filename has the specified extension */ public static boolean isExtension(String filename, String extension) { - if (filename == null) { - return false; - } - if (extension == null || extension.isEmpty()) { - return false; - } - return getExtension(filename).equals(extension); + return !extension.isEmpty() && getExtension(filename).equals(extension); } } diff --git a/bundles/org.openhab.core.audio/src/test/java/org/eclipse/smarthome/core/audio/internal/AudioConsoleTest.java b/bundles/org.openhab.core.audio/src/test/java/org/eclipse/smarthome/core/audio/internal/AudioConsoleTest.java index 49e362793c6..5effb279dfa 100644 --- a/bundles/org.openhab.core.audio/src/test/java/org/eclipse/smarthome/core/audio/internal/AudioConsoleTest.java +++ b/bundles/org.openhab.core.audio/src/test/java/org/eclipse/smarthome/core/audio/internal/AudioConsoleTest.java @@ -76,16 +76,15 @@ public void print(String s) { @Before public void setUp() throws IOException { fileHandler = new BundledSoundFileHandler(); - audioManager = new AudioManagerImpl(); audioSink = new AudioSinkFake(); - audioManager.addAudioSink(audioSink); - audioConsoleCommandExtension = new AudioConsoleCommandExtension(); - audioConsoleCommandExtension.setAudioManager(audioManager); + audioManager = new AudioManagerImpl(); + audioManager.addAudioSink(audioSink); LocaleProvider localeProvider = mock(LocaleProvider.class); when(localeProvider.getLocale()).thenReturn(Locale.getDefault()); - audioConsoleCommandExtension.setLocaleProvider(localeProvider); + + audioConsoleCommandExtension = new AudioConsoleCommandExtension(audioManager, localeProvider); } @After diff --git a/bundles/org.openhab.core.io.http/src/main/java/org/eclipse/smarthome/io/http/servlet/BaseSmartHomeServlet.java b/bundles/org.openhab.core.io.http/src/main/java/org/eclipse/smarthome/io/http/servlet/BaseSmartHomeServlet.java index b012f3c3dc6..6d36a8a00b1 100644 --- a/bundles/org.openhab.core.io.http/src/main/java/org/eclipse/smarthome/io/http/servlet/BaseSmartHomeServlet.java +++ b/bundles/org.openhab.core.io.http/src/main/java/org/eclipse/smarthome/io/http/servlet/BaseSmartHomeServlet.java @@ -17,6 +17,7 @@ import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; +import org.eclipse.jdt.annotation.NonNullByDefault; import org.osgi.service.http.HttpContext; import org.osgi.service.http.HttpService; import org.osgi.service.http.NamespaceException; @@ -28,6 +29,7 @@ * * @author Ɓukasz Dywicki - Initial contribution */ +@NonNullByDefault public abstract class BaseSmartHomeServlet extends HttpServlet { private static final long serialVersionUID = 6020752826735599455L; @@ -40,7 +42,7 @@ public abstract class BaseSmartHomeServlet extends HttpServlet { /** * Http service. */ - protected HttpService httpService; + protected @NonNullByDefault({}) HttpService httpService; protected void setHttpService(HttpService httpService) { this.httpService = httpService;