Fix null type mismatch errors (#2976)

This fixes the null type errors in Eclipse which were introduced by #2906.

Signed-off-by: Wouter Born <github@maindrain.net>
pull/2978/head
Wouter Born 2022-05-23 16:30:08 +02:00 committed by GitHub
parent ad3a3c1caf
commit 86ee4dc2b5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 36 additions and 62 deletions

View File

@ -13,16 +13,13 @@
package org.openhab.core.automation.module.script; package org.openhab.core.automation.module.script;
import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.instanceOf; import static org.hamcrest.Matchers.*;
import static org.hamcrest.Matchers.is;
import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.ArgumentMatchers.*;
import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.Mockito.*;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.times; import java.util.Objects;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoMoreInteractions;
import static org.mockito.Mockito.when;
import javax.script.ScriptContext; import javax.script.ScriptContext;
import javax.script.ScriptEngine; import javax.script.ScriptEngine;
@ -81,7 +78,7 @@ public class ScriptTransformationServiceTest {
@Test @Test
public void success() throws TransformationException { public void success() throws TransformationException {
String returnValue = service.transform(SCRIPT_TYPE + ":" + SCRIPT_UID, "input"); String returnValue = Objects.requireNonNull(service.transform(SCRIPT_TYPE + ":" + SCRIPT_UID, "input"));
assertThat(returnValue, is(SCRIPT_OUTPUT)); assertThat(returnValue, is(SCRIPT_OUTPUT));
} }

View File

@ -290,37 +290,35 @@ public class VoiceManagerImpl implements VoiceManager, ConfigOptionProvider {
@Override @Override
public String interpret(String text, @Nullable String hliIdList) throws InterpretationException { public String interpret(String text, @Nullable String hliIdList) throws InterpretationException {
List<HumanLanguageInterpreter> interpreters; List<HumanLanguageInterpreter> interpreters = new ArrayList<>();
if (hliIdList == null) { if (hliIdList == null) {
HumanLanguageInterpreter interpreter = getHLI(); HumanLanguageInterpreter interpreter = getHLI();
if (interpreter == null) { if (interpreter != null) {
throw new InterpretationException("No human language interpreter available!"); interpreters.add(interpreter);
} }
interpreters = List.of(interpreter);
} else { } else {
interpreters = getHLIsByIds(hliIdList); interpreters = getHLIsByIds(hliIdList);
if (interpreters.isEmpty()) { }
throw new InterpretationException("No human language interpreter can be found for " + hliIdList);
if (!interpreters.isEmpty()) {
Locale locale = localeProvider.getLocale();
for (var interpreter : interpreters) {
try {
String answer = interpreter.interpret(locale, text);
logger.debug("Interpretation result: {}", answer);
return answer;
} catch (InterpretationException e) {
logger.debug("Interpretation exception: {}", e.getMessage());
throw e;
}
} }
} }
String answer = null;
InterpretationException error = null; if (hliIdList == null) {
Locale locale = localeProvider.getLocale(); throw new InterpretationException("No human language interpreter available!");
for (var interpreter : interpreters) { } else {
try { throw new InterpretationException("No human language interpreter can be found for " + hliIdList);
answer = interpreter.interpret(locale, text);
logger.debug("Interpretation result: {}", answer);
error = null;
break;
} catch (InterpretationException e) {
logger.debug("Interpretation exception: {}", e.getMessage());
error = e;
}
} }
if (error != null) {
throw error;
}
return answer;
} }
private @Nullable Voice getVoice(String id) { private @Nullable Voice getVoice(String id) {
@ -354,27 +352,13 @@ public class VoiceManagerImpl implements VoiceManager, ConfigOptionProvider {
// Return the first concrete AudioFormat found // Return the first concrete AudioFormat found
for (AudioFormat currentAudioFormat : audioFormats) { for (AudioFormat currentAudioFormat : audioFormats) {
// Check if currentAudioFormat is abstract // Check if currentAudioFormat is abstract
if (null == currentAudioFormat.getCodec()) { if ((null == currentAudioFormat.getCodec()) || (null == currentAudioFormat.getContainer())
|| (null == currentAudioFormat.isBigEndian()) || (null == currentAudioFormat.getBitDepth())) {
continue; continue;
} }
if (null == currentAudioFormat.getContainer()) {
continue;
}
if (null == currentAudioFormat.isBigEndian()) {
continue;
}
if (null == currentAudioFormat.getBitDepth()) {
continue;
}
if (null == currentAudioFormat.getBitRate()) {
continue;
}
if (null == currentAudioFormat.getFrequency()) {
continue;
}
// Prefer WAVE container // Prefer WAVE container
if (!AudioFormat.CONTAINER_WAVE.equals(currentAudioFormat.getContainer())) { if ((null == currentAudioFormat.getBitRate()) || (null == currentAudioFormat.getFrequency())
|| !AudioFormat.CONTAINER_WAVE.equals(currentAudioFormat.getContainer())) {
continue; continue;
} }
@ -388,15 +372,9 @@ public class VoiceManagerImpl implements VoiceManager, ConfigOptionProvider {
AudioFormat format = currentAudioFormat; AudioFormat format = currentAudioFormat;
// Not all Codecs and containers can be supported // Not all Codecs and containers can be supported
if (null == format.getCodec()) {
continue;
}
if (null == format.getContainer()) {
continue;
}
// Prefer WAVE container // Prefer WAVE container
if (!AudioFormat.CONTAINER_WAVE.equals(format.getContainer())) { if ((null == format.getCodec()) || (null == format.getContainer())
|| !AudioFormat.CONTAINER_WAVE.equals(format.getContainer())) {
continue; continue;
} }
@ -813,7 +791,7 @@ public class VoiceManagerImpl implements VoiceManager, ConfigOptionProvider {
} }
private Set<Voice> getAllVoicesSorted(Locale locale) { private Set<Voice> getAllVoicesSorted(Locale locale) {
return ttsServices.values().stream().map(s -> s.getAvailableVoices()).flatMap(Collection::stream) return ttsServices.values().stream().map(TTSService::getAvailableVoices).flatMap(Collection::stream)
.sorted(createVoiceComparator(locale)).collect(Collectors .sorted(createVoiceComparator(locale)).collect(Collectors
.collectingAndThen(Collectors.toCollection(LinkedHashSet::new), Collections::unmodifiableSet)); .collectingAndThen(Collectors.toCollection(LinkedHashSet::new), Collections::unmodifiableSet));
} }
@ -836,9 +814,8 @@ public class VoiceManagerImpl implements VoiceManager, ConfigOptionProvider {
return (tts1 == null || tts2 == null) ? 0 return (tts1 == null || tts2 == null) ? 0
: tts1.getLabel(locale).compareToIgnoreCase(tts2.getLabel(locale)); : tts1.getLabel(locale).compareToIgnoreCase(tts2.getLabel(locale));
}; };
Comparator<Voice> byVoiceLocale = (Voice v1, Voice v2) -> { Comparator<Voice> byVoiceLocale = (Voice v1, Voice v2) -> v1.getLocale().getDisplayName(locale)
return v1.getLocale().getDisplayName(locale).compareToIgnoreCase(v2.getLocale().getDisplayName(locale)); .compareToIgnoreCase(v2.getLocale().getDisplayName(locale));
};
return byTTSLabel.thenComparing(byVoiceLocale).thenComparing(Voice::getLabel); return byTTSLabel.thenComparing(byVoiceLocale).thenComparing(Voice::getLabel);
} }