From 86ee4dc2b5802cab3fc3d7a575847e7ab40314d4 Mon Sep 17 00:00:00 2001 From: Wouter Born Date: Mon, 23 May 2022 16:30:08 +0200 Subject: [PATCH] Fix null type mismatch errors (#2976) This fixes the null type errors in Eclipse which were introduced by #2906. Signed-off-by: Wouter Born --- .../ScriptTransformationServiceTest.java | 15 ++-- .../core/voice/internal/VoiceManagerImpl.java | 83 +++++++------------ 2 files changed, 36 insertions(+), 62 deletions(-) diff --git a/bundles/org.openhab.core.automation.module.script/src/test/java/org/openhab/core/automation/module/script/ScriptTransformationServiceTest.java b/bundles/org.openhab.core.automation.module.script/src/test/java/org/openhab/core/automation/module/script/ScriptTransformationServiceTest.java index 0dbd23a4c0..b907434f01 100644 --- a/bundles/org.openhab.core.automation.module.script/src/test/java/org/openhab/core/automation/module/script/ScriptTransformationServiceTest.java +++ b/bundles/org.openhab.core.automation.module.script/src/test/java/org/openhab/core/automation/module/script/ScriptTransformationServiceTest.java @@ -13,16 +13,13 @@ package org.openhab.core.automation.module.script; import static org.hamcrest.MatcherAssert.assertThat; -import static org.hamcrest.Matchers.instanceOf; -import static org.hamcrest.Matchers.is; +import static org.hamcrest.Matchers.*; import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.mockito.ArgumentMatchers.*; import static org.mockito.ArgumentMatchers.any; -import static org.mockito.ArgumentMatchers.anyString; -import static org.mockito.ArgumentMatchers.eq; -import static org.mockito.Mockito.times; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.verifyNoMoreInteractions; -import static org.mockito.Mockito.when; +import static org.mockito.Mockito.*; + +import java.util.Objects; import javax.script.ScriptContext; import javax.script.ScriptEngine; @@ -81,7 +78,7 @@ public class ScriptTransformationServiceTest { @Test 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)); } diff --git a/bundles/org.openhab.core.voice/src/main/java/org/openhab/core/voice/internal/VoiceManagerImpl.java b/bundles/org.openhab.core.voice/src/main/java/org/openhab/core/voice/internal/VoiceManagerImpl.java index 7fb1ea3b77..eb4318ff39 100644 --- a/bundles/org.openhab.core.voice/src/main/java/org/openhab/core/voice/internal/VoiceManagerImpl.java +++ b/bundles/org.openhab.core.voice/src/main/java/org/openhab/core/voice/internal/VoiceManagerImpl.java @@ -290,37 +290,35 @@ public class VoiceManagerImpl implements VoiceManager, ConfigOptionProvider { @Override public String interpret(String text, @Nullable String hliIdList) throws InterpretationException { - List interpreters; + List interpreters = new ArrayList<>(); if (hliIdList == null) { HumanLanguageInterpreter interpreter = getHLI(); - if (interpreter == null) { - throw new InterpretationException("No human language interpreter available!"); + if (interpreter != null) { + interpreters.add(interpreter); } - interpreters = List.of(interpreter); } else { 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; - Locale locale = localeProvider.getLocale(); - for (var interpreter : interpreters) { - try { - 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 (hliIdList == null) { + throw new InterpretationException("No human language interpreter available!"); + } else { + throw new InterpretationException("No human language interpreter can be found for " + hliIdList); } - if (error != null) { - throw error; - } - return answer; } private @Nullable Voice getVoice(String id) { @@ -354,27 +352,13 @@ public class VoiceManagerImpl implements VoiceManager, ConfigOptionProvider { // Return the first concrete AudioFormat found for (AudioFormat currentAudioFormat : audioFormats) { // Check if currentAudioFormat is abstract - if (null == currentAudioFormat.getCodec()) { + if ((null == currentAudioFormat.getCodec()) || (null == currentAudioFormat.getContainer()) + || (null == currentAudioFormat.isBigEndian()) || (null == currentAudioFormat.getBitDepth())) { 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 - if (!AudioFormat.CONTAINER_WAVE.equals(currentAudioFormat.getContainer())) { + if ((null == currentAudioFormat.getBitRate()) || (null == currentAudioFormat.getFrequency()) + || !AudioFormat.CONTAINER_WAVE.equals(currentAudioFormat.getContainer())) { continue; } @@ -388,15 +372,9 @@ public class VoiceManagerImpl implements VoiceManager, ConfigOptionProvider { AudioFormat format = currentAudioFormat; // Not all Codecs and containers can be supported - if (null == format.getCodec()) { - continue; - } - if (null == format.getContainer()) { - continue; - } - // Prefer WAVE container - if (!AudioFormat.CONTAINER_WAVE.equals(format.getContainer())) { + if ((null == format.getCodec()) || (null == format.getContainer()) + || !AudioFormat.CONTAINER_WAVE.equals(format.getContainer())) { continue; } @@ -813,7 +791,7 @@ public class VoiceManagerImpl implements VoiceManager, ConfigOptionProvider { } private Set 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 .collectingAndThen(Collectors.toCollection(LinkedHashSet::new), Collections::unmodifiableSet)); } @@ -836,9 +814,8 @@ public class VoiceManagerImpl implements VoiceManager, ConfigOptionProvider { return (tts1 == null || tts2 == null) ? 0 : tts1.getLabel(locale).compareToIgnoreCase(tts2.getLabel(locale)); }; - Comparator byVoiceLocale = (Voice v1, Voice v2) -> { - return v1.getLocale().getDisplayName(locale).compareToIgnoreCase(v2.getLocale().getDisplayName(locale)); - }; + Comparator byVoiceLocale = (Voice v1, Voice v2) -> v1.getLocale().getDisplayName(locale) + .compareToIgnoreCase(v2.getLocale().getDisplayName(locale)); return byTTSLabel.thenComparing(byVoiceLocale).thenComparing(Voice::getLabel); }