Use getOrDefault() to reduce null checks (#19416)

Signed-off-by: Michael Lobstein <michael.lobstein@gmail.com>
This commit is contained in:
mlobstein
2025-10-02 08:06:53 +02:00
committed by GitHub
parent 04fe2dcef0
commit 4440af9d1a
4 changed files with 34 additions and 44 deletions
@@ -278,8 +278,7 @@ public enum AmplifierModel {
}
public String getZoneName(String zoneId) {
final String zoneName = zoneIdMap.get(zoneId);
return zoneName != null ? zoneName : "";
return zoneIdMap.getOrDefault(zoneId, "");
}
public String getCmdPrefix() {
@@ -330,14 +330,12 @@ public class NuvoHandler extends BaseThingHandler implements NuvoMessageEventLis
// Also add any openHAB NuvoNet source favorites to the list
IntStream.range(1, MAX_SRC + 1).forEach(src -> {
NuvoEnum source = NuvoEnum.valueOf(SOURCE + src);
String[] favorites = favoriteMap.get(source);
if (favorites != null) {
IntStream.range(0, favorites.length).forEach(fav -> {
favoriteLabelsStateOptions.add(new StateOption(String.valueOf(src * 100 + fav),
favPrefixMap.get(source) + favorites[fav]));
});
}
final NuvoEnum source = NuvoEnum.valueOf(SOURCE + src);
final String[] favorites = favoriteMap.getOrDefault(source, new String[0]);
IntStream.range(0, favorites.length).forEach(fav -> {
favoriteLabelsStateOptions.add(
new StateOption(String.valueOf(src * 100 + fav), favPrefixMap.get(source) + favorites[fav]));
});
});
// Put the global favorites labels on all active zones
@@ -1135,21 +1133,18 @@ public class NuvoHandler extends BaseThingHandler implements NuvoMessageEventLis
sourceMenuStateOptions);
}
String[] favorites = favoriteMap.get(source);
if (favorites != null) {
connector.sendCommand(source.getId() + "FAVORITES"
+ (favorites.length < 20 ? favorites.length : 20) + COMMA
+ (source.getNum() == 1 ? ONE : ZERO) + COMMA + (source.getNum() == 2 ? ONE : ZERO)
+ COMMA + (source.getNum() == 3 ? ONE : ZERO) + COMMA
+ (source.getNum() == 4 ? ONE : ZERO) + COMMA + (source.getNum() == 5 ? ONE : ZERO)
+ COMMA + (source.getNum() == 6 ? ONE : ZERO));
Thread.sleep(SLEEP_BETWEEN_CMD_MS);
final String[] favorites = favoriteMap.getOrDefault(source, new String[0]);
connector.sendCommand(source.getId() + "FAVORITES" + (favorites.length < 20 ? favorites.length : 20)
+ COMMA + (source.getNum() == 1 ? ONE : ZERO) + COMMA + (source.getNum() == 2 ? ONE : ZERO)
+ COMMA + (source.getNum() == 3 ? ONE : ZERO) + COMMA + (source.getNum() == 4 ? ONE : ZERO)
+ COMMA + (source.getNum() == 5 ? ONE : ZERO) + COMMA
+ (source.getNum() == 6 ? ONE : ZERO));
Thread.sleep(SLEEP_BETWEEN_CMD_MS);
for (int i = 0; i < (favorites.length < 20 ? favorites.length : 20); i++) {
connector.sendCommand(source.getId() + "FAVORITESITEM" + (i + 1000) + ",0,0,\""
+ favPrefixMap.get(source) + favorites[i] + "\"");
Thread.sleep(SLEEP_BETWEEN_CMD_MS);
}
for (int i = 0; i < (favorites.length < 20 ? favorites.length : 20); i++) {
connector.sendCommand(source.getId() + "FAVORITESITEM" + (i + 1000) + ",0,0,\""
+ favPrefixMap.get(source) + favorites[i] + "\"");
Thread.sleep(SLEEP_BETWEEN_CMD_MS);
}
if (showReady) {
@@ -1607,7 +1602,7 @@ public class NuvoHandler extends BaseThingHandler implements NuvoMessageEventLis
}
private String getFavorite(NuvoEnum source, int playlistIdx) {
final String[] favoritesArr = favoriteMap.get(source);
return favoritesArr != null ? favoritesArr[playlistIdx] : BLANK;
final String[] favoritesArr = favoriteMap.getOrDefault(source, new String[0]);
return playlistIdx < favoritesArr.length ? favoritesArr[playlistIdx] : BLANK;
}
}
@@ -400,7 +400,7 @@ public class OppoHandler extends BaseThingHandler implements OppoMessageEventLis
// Player sent a time code update ie: 000 000 T 00:00:01
// g1 = title(movie only; cd always 000), g2 = chapter(movie)/track(cd), g3 = time display code,
// g4 = time
Matcher matcher = TIME_CODE_PATTERN.matcher(updateData);
final Matcher matcher = TIME_CODE_PATTERN.matcher(updateData);
if (matcher.find()) {
// only update these when chapter/track changes to prevent spamming the channels with
// unnecessary updates
@@ -470,7 +470,7 @@ public class OppoHandler extends BaseThingHandler implements OppoMessageEventLis
break;
case QTK:
// example: 02/10, split off both numbers
String[] track = updateData.split(SLASH);
final String[] track = updateData.split(SLASH);
if (track.length == 2) {
updateChannelState(CHANNEL_CURRENT_TITLE, track[0]);
updateChannelState(CHANNEL_TOTAL_TITLE, track[1]);
@@ -478,7 +478,7 @@ public class OppoHandler extends BaseThingHandler implements OppoMessageEventLis
break;
case QCH:
// example: 03/03, split off the both numbers
String[] chapter = updateData.split(SLASH);
final String[] chapter = updateData.split(SLASH);
if (chapter.length == 2) {
updateChannelState(CHANNEL_CURRENT_CHAPTER, chapter[0]);
updateChannelState(CHANNEL_TOTAL_CHAPTER, chapter[1]);
@@ -487,14 +487,12 @@ public class OppoHandler extends BaseThingHandler implements OppoMessageEventLis
case UPL:
case QPL:
// try to normalize the slightly different responses between UPL and QPL
String playStatus = OppoStatusCodes.PLAYBACK_STATUS.get(updateData);
if (playStatus == null) {
playStatus = updateData;
}
currentPlayMode = OppoStatusCodes.PLAYBACK_STATUS.getOrDefault(updateData, updateData);
// if playback has stopped, we have to zero out Time, Title and Track info and so on manually
if (NO_DISC.equals(playStatus) || LOADING.equals(playStatus) || OPEN.equals(playStatus)
|| CLOSE.equals(playStatus) || STOP.equals(playStatus)) {
if (NO_DISC.equals(currentPlayMode) || LOADING.equals(currentPlayMode)
|| OPEN.equals(currentPlayMode) || CLOSE.equals(currentPlayMode)
|| STOP.equals(currentPlayMode)) {
updateChannelState(CHANNEL_CURRENT_TITLE, ZERO);
updateChannelState(CHANNEL_TOTAL_TITLE, ZERO);
updateChannelState(CHANNEL_CURRENT_CHAPTER, ZERO);
@@ -503,12 +501,12 @@ public class OppoHandler extends BaseThingHandler implements OppoMessageEventLis
updateChannelState(CHANNEL_AUDIO_TYPE, UNDEF);
updateChannelState(CHANNEL_SUBTITLE_TYPE, UNDEF);
}
updateChannelState(CHANNEL_PLAY_MODE, playStatus);
updateChannelState(CHANNEL_PLAY_MODE, currentPlayMode);
updateState(CHANNEL_CONTROL,
PLAY.equals(playStatus) ? PlayPauseType.PLAY : PlayPauseType.PAUSE);
PLAY.equals(currentPlayMode) ? PlayPauseType.PLAY : PlayPauseType.PAUSE);
// ejecting the disc does not produce a UDT message, so clear disc type manually
if (OPEN.equals(playStatus) || NO_DISC.equals(playStatus)) {
if (OPEN.equals(currentPlayMode) || NO_DISC.equals(currentPlayMode)) {
updateChannelState(CHANNEL_DISC_TYPE, UNKNOW_DISC);
currentDiscType = BLANK;
}
@@ -516,10 +514,9 @@ public class OppoHandler extends BaseThingHandler implements OppoMessageEventLis
// if switching to play mode and not a CD then query the subtitle type...
// because if subtitles were on when playback stopped, they got nulled out above
// and the subtitle update message ("UST") is not sent when play starts like it is for audio
if (PLAY.equals(playStatus) && !CDDA.equals(currentDiscType)) {
if (PLAY.equals(currentPlayMode) && !CDDA.equals(currentDiscType)) {
connector.sendCommand(OppoCommand.QUERY_SUBTITLE_TYPE);
}
currentPlayMode = playStatus;
break;
case QRP:
updateChannelState(CHANNEL_REPEAT_MODE, updateData);
@@ -530,8 +527,7 @@ public class OppoHandler extends BaseThingHandler implements OppoMessageEventLis
case UDT:
case QDT:
// try to normalize the slightly different responses between UDT and QDT
final String discType = OppoStatusCodes.DISC_TYPE.get(updateData);
currentDiscType = (discType != null ? discType : updateData);
currentDiscType = OppoStatusCodes.DISC_TYPE.getOrDefault(updateData, updateData);
updateChannelState(CHANNEL_DISC_TYPE, currentDiscType);
break;
case UAT:
@@ -559,7 +555,7 @@ public class OppoHandler extends BaseThingHandler implements OppoMessageEventLis
break;
case UVO:
// example: _480I60 1080P60 - 1st source res, 2nd output res
String[] resolution = updateData.replace(UNDERSCORE, BLANK).split(SPACE);
final String[] resolution = updateData.replace(UNDERSCORE, BLANK).split(SPACE);
if (resolution.length == 2) {
updateChannelState(CHANNEL_SOURCE_RESOLUTION, resolution[0]);
updateChannelState(CHANNEL_OUTPUT_RESOLUTION, resolution[1]);
@@ -158,7 +158,7 @@ public class PanaBlurayHandler extends BaseThingHandler {
// update playerMode if different
if (!playerMode.equals(playerStatusArr[3])) {
playerMode = playerStatusArr[3];
final String i18nKey = STATUS_MAP.get(playerMode) != null ? STATUS_MAP.get(playerMode) : "unknown";
final String i18nKey = STATUS_MAP.getOrDefault(playerMode, "unknown");
updateState(PLAYER_STATUS, new StringType(translationProvider.getText(bundle, "status." + i18nKey,
i18nKey, localeProvider.getLocale())));
updateState(CONTROL, PLAY_STATUS.equals(playerMode) ? PlayPauseType.PLAY : PlayPauseType.PAUSE);