Skip to content

Commit

Permalink
Fix null type mismatch errors (#2976)
Browse files Browse the repository at this point in the history
This fixes the null type errors in Eclipse which were introduced by #2906.

Signed-off-by: Wouter Born <github@maindrain.net>
  • Loading branch information
wborn authored May 23, 2022
1 parent ad3a3c1 commit 86ee4dc
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 61 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -81,7 +78,7 @@ public void setUp() throws ScriptException {

@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));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -290,37 +290,35 @@ public String interpret(String text) throws InterpretationException {

@Override
public String interpret(String text, @Nullable String hliIdList) throws InterpretationException {
List<HumanLanguageInterpreter> interpreters;
List<HumanLanguageInterpreter> 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);
}
}
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 (!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;
}
}
}
if (error != null) {
throw error;

if (hliIdList == null) {
throw new InterpretationException("No human language interpreter available!");
} else {
throw new InterpretationException("No human language interpreter can be found for " + hliIdList);
}
return answer;
}

private @Nullable Voice getVoice(String id) {
Expand Down Expand Up @@ -354,27 +352,13 @@ public String interpret(String text, @Nullable String hliIdList) throws Interpre
// Return the first concrete AudioFormat found
for (AudioFormat currentAudioFormat : audioFormats) {
// Check if currentAudioFormat is abstract
if (null == currentAudioFormat.getCodec()) {
continue;
}
if (null == currentAudioFormat.getContainer()) {
continue;
}
if (null == currentAudioFormat.isBigEndian()) {
if ((null == currentAudioFormat.getCodec()) || (null == currentAudioFormat.getContainer())
|| (null == currentAudioFormat.isBigEndian()) || (null == currentAudioFormat.getBitDepth())) {
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;
}

Expand All @@ -388,15 +372,9 @@ public String interpret(String text, @Nullable String hliIdList) throws Interpre
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;
}

Expand Down Expand Up @@ -813,7 +791,7 @@ public Set<Voice> getAllVoices() {
}

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
.collectingAndThen(Collectors.toCollection(LinkedHashSet::new), Collections::unmodifiableSet));
}
Expand All @@ -836,9 +814,8 @@ private Comparator<Voice> createVoiceComparator(Locale locale) {
return (tts1 == null || tts2 == null) ? 0
: tts1.getLabel(locale).compareToIgnoreCase(tts2.getLabel(locale));
};
Comparator<Voice> byVoiceLocale = (Voice v1, Voice v2) -> {
return v1.getLocale().getDisplayName(locale).compareToIgnoreCase(v2.getLocale().getDisplayName(locale));
};
Comparator<Voice> byVoiceLocale = (Voice v1, Voice v2) -> v1.getLocale().getDisplayName(locale)
.compareToIgnoreCase(v2.getLocale().getDisplayName(locale));
return byTTSLabel.thenComparing(byVoiceLocale).thenComparing(Voice::getLabel);
}

Expand Down

0 comments on commit 86ee4dc

Please sign in to comment.