[picotts] Add null annotations (#10392)

Signed-off-by: Wouter Born <github@maindrain.net>
This commit is contained in:
Wouter Born 2021-03-27 18:34:00 +01:00 committed by GitHub
parent bfa7f32dd5
commit 1fa1f54272
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 15 additions and 8 deletions

View File

@ -18,6 +18,8 @@ import java.io.FileNotFoundException;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.openhab.core.audio.AudioException; import org.openhab.core.audio.AudioException;
import org.openhab.core.audio.AudioFormat; import org.openhab.core.audio.AudioFormat;
import org.openhab.core.audio.AudioStream; import org.openhab.core.audio.AudioStream;
@ -29,6 +31,7 @@ import org.openhab.core.voice.Voice;
* *
* @author Florian Schmidt - Initial Contribution * @author Florian Schmidt - Initial Contribution
*/ */
@NonNullByDefault
class PicoTTSAudioStream extends FixedLengthAudioStream { class PicoTTSAudioStream extends FixedLengthAudioStream {
private final Voice voice; private final Voice voice;
private final String text; private final String text;
@ -36,7 +39,7 @@ class PicoTTSAudioStream extends FixedLengthAudioStream {
private final InputStream inputStream; private final InputStream inputStream;
private long length; private long length;
private File file; private @Nullable File file;
public PicoTTSAudioStream(String text, Voice voice, AudioFormat audioFormat) throws AudioException { public PicoTTSAudioStream(String text, Voice voice, AudioFormat audioFormat) throws AudioException {
this.text = text; this.text = text;
@ -57,7 +60,8 @@ class PicoTTSAudioStream extends FixedLengthAudioStream {
try { try {
Process process = Runtime.getRuntime().exec(command); Process process = Runtime.getRuntime().exec(command);
process.waitFor(); process.waitFor();
file = new File(outputFile); File file = new File(outputFile);
this.file = file;
this.length = file.length(); this.length = file.length();
return getFileInputStream(file); return getFileInputStream(file);
} catch (IOException e) { } catch (IOException e) {
@ -68,9 +72,6 @@ class PicoTTSAudioStream extends FixedLengthAudioStream {
} }
private InputStream getFileInputStream(File file) throws AudioException { private InputStream getFileInputStream(File file) throws AudioException {
if (file == null) {
throw new IllegalArgumentException("file must not be null");
}
if (file.exists()) { if (file.exists()) {
try { try {
return new FileInputStream(file); return new FileInputStream(file);
@ -119,6 +120,7 @@ class PicoTTSAudioStream extends FixedLengthAudioStream {
@Override @Override
public InputStream getClonedStream() throws AudioException { public InputStream getClonedStream() throws AudioException {
File file = this.file;
if (file != null) { if (file != null) {
return getFileInputStream(file); return getFileInputStream(file);
} else { } else {

View File

@ -18,6 +18,8 @@ import java.util.Set;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import java.util.stream.Stream; import java.util.stream.Stream;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.openhab.core.audio.AudioException; import org.openhab.core.audio.AudioException;
import org.openhab.core.audio.AudioFormat; import org.openhab.core.audio.AudioFormat;
import org.openhab.core.audio.AudioStream; import org.openhab.core.audio.AudioStream;
@ -30,6 +32,7 @@ import org.osgi.service.component.annotations.Component;
* @author Florian Schmidt - Initial Contribution * @author Florian Schmidt - Initial Contribution
*/ */
@Component @Component
@NonNullByDefault
public class PicoTTSService implements TTSService { public class PicoTTSService implements TTSService {
private final Set<Voice> voices = Stream private final Set<Voice> voices = Stream
.of(new PicoTTSVoice("de-DE"), new PicoTTSVoice("en-US"), new PicoTTSVoice("en-GB"), .of(new PicoTTSVoice("de-DE"), new PicoTTSVoice("en-US"), new PicoTTSVoice("en-GB"),
@ -51,8 +54,8 @@ public class PicoTTSService implements TTSService {
@Override @Override
public AudioStream synthesize(String text, Voice voice, AudioFormat requestedFormat) throws TTSException { public AudioStream synthesize(String text, Voice voice, AudioFormat requestedFormat) throws TTSException {
if (text == null || text.isEmpty()) { if (text.isEmpty()) {
throw new TTSException("The passed text can not be null or empty"); throw new TTSException("The passed text can not be empty");
} }
if (!this.voices.contains(voice)) { if (!this.voices.contains(voice)) {
@ -80,7 +83,7 @@ public class PicoTTSService implements TTSService {
} }
@Override @Override
public String getLabel(Locale locale) { public String getLabel(@Nullable Locale locale) {
return "PicoTTS"; return "PicoTTS";
} }
} }

View File

@ -14,6 +14,7 @@ package org.openhab.voice.picotts.internal;
import java.util.Locale; import java.util.Locale;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.openhab.core.voice.Voice; import org.openhab.core.voice.Voice;
/** /**
@ -21,6 +22,7 @@ import org.openhab.core.voice.Voice;
* *
* @author Florian Schmidt - Initial Contribution * @author Florian Schmidt - Initial Contribution
*/ */
@NonNullByDefault
public class PicoTTSVoice implements Voice { public class PicoTTSVoice implements Voice {
private final String languageTag; private final String languageTag;