Simplify switch statements, Java 21 (#5012)

Signed-off-by: Holger Friedrich <mail@holger-friedrich.de>
This commit is contained in:
Holger Friedrich
2025-09-07 19:54:05 +02:00
committed by GitHub
parent adfa286a3a
commit 81e3a05614
14 changed files with 74 additions and 118 deletions
@@ -61,16 +61,12 @@ public class MediaActionTypeProvider implements ModuleTypeProvider {
@SuppressWarnings("unchecked")
@Override
public @Nullable ModuleType getModuleType(String uid, @Nullable Locale locale) {
switch (uid) {
case PlayActionHandler.TYPE_ID:
return getPlayActionType(locale);
case SayActionHandler.TYPE_ID:
return getSayActionType(locale);
case SynthesizeActionHandler.TYPE_ID:
return getSynthesizeActionType(locale);
default:
return null;
}
return switch (uid) {
case PlayActionHandler.TYPE_ID -> getPlayActionType(locale);
case SayActionHandler.TYPE_ID -> getSayActionType(locale);
case SynthesizeActionHandler.TYPE_ID -> getSynthesizeActionType(locale);
default -> null;
};
}
@Override
@@ -212,16 +212,12 @@ public interface Rule extends Identifiable<String> {
return NO_TEMPLATE;
}
String s = templateState.trim().toLowerCase(Locale.ROOT);
switch (s) {
case "instantiated":
return INSTANTIATED;
case "pending":
return PENDING;
case "template-missing":
return TEMPLATE_MISSING;
default:
return NO_TEMPLATE;
}
return switch (s) {
case "instantiated" -> INSTANTIATED;
case "pending" -> PENDING;
case "template-missing" -> TEMPLATE_MISSING;
default -> NO_TEMPLATE;
};
}
}
}
@@ -597,18 +597,13 @@ public class RuleRegistryImpl extends AbstractRegistry<Rule, String, RuleProvide
* @return <code>true</code> if the type and value matching or <code>false</code> in the opposite.
*/
private boolean checkType(Type type, Object configValue) {
switch (type) {
case TEXT:
return configValue instanceof String;
case BOOLEAN:
return configValue instanceof Boolean;
case INTEGER:
return configValue instanceof BigDecimal || configValue instanceof Integer
return switch (type) {
case TEXT -> configValue instanceof String;
case BOOLEAN -> configValue instanceof Boolean;
case INTEGER -> configValue instanceof BigDecimal || configValue instanceof Integer
|| configValue instanceof Double doubleValue && doubleValue.intValue() == doubleValue;
case DECIMAL:
return configValue instanceof BigDecimal || configValue instanceof Double;
}
return false;
case DECIMAL -> configValue instanceof BigDecimal || configValue instanceof Double;
};
}
/**
@@ -275,14 +275,11 @@ public class AutomationResourceBundlesTracker implements BundleTrackerCustomizer
}
private BundleEvent initializeEvent(Bundle bundle) {
switch (bundle.getState()) {
case Bundle.INSTALLED:
return new BundleEvent(BundleEvent.INSTALLED, bundle);
case Bundle.RESOLVED:
return new BundleEvent(BundleEvent.RESOLVED, bundle);
default:
return new BundleEvent(BundleEvent.STARTED, bundle);
}
return switch (bundle.getState()) {
case Bundle.INSTALLED -> new BundleEvent(BundleEvent.INSTALLED, bundle);
case Bundle.RESOLVED -> new BundleEvent(BundleEvent.RESOLVED, bundle);
default -> new BundleEvent(BundleEvent.STARTED, bundle);
};
}
/**
@@ -122,6 +122,6 @@ public class ValidationException extends Exception {
public enum ObjectType {
MODULE_TYPE,
TEMPLATE,
RULE;
RULE
}
}
@@ -50,31 +50,25 @@ public class I18nConfigOptionsProvider implements ConfigOptionProvider {
@Override
public @Nullable Collection<ParameterOption> getParameterOptions(URI uri, String param, @Nullable String context,
@Nullable Locale locale) {
switch (uri.toString()) {
case "system:i18n":
return processParamType(param, locale, locale != null ? locale : Locale.getDefault());
case "profile:system:timestamp-offset":
return TIMEZONE.equals(param) ? processTimeZoneParam() : null;
default:
return null;
}
return switch (uri.toString()) {
case "system:i18n" -> processParamType(param, locale, locale != null ? locale : Locale.getDefault());
case "profile:system:timestamp-offset" -> TIMEZONE.equals(param) ? processTimeZoneParam() : null;
default -> null;
};
}
private @Nullable Collection<ParameterOption> processParamType(String param, @Nullable Locale locale,
Locale translation) {
switch (param) {
case LANGUAGE:
return getAvailable(locale,
l -> new ParameterOption(l.getLanguage(), l.getDisplayLanguage(translation)));
case REGION:
return getAvailable(locale, l -> new ParameterOption(l.getCountry(), l.getDisplayCountry(translation)));
case VARIANT:
return getAvailable(locale, l -> new ParameterOption(l.getVariant(), l.getDisplayVariant(translation)));
case TIMEZONE:
return processTimeZoneParam();
default:
return null;
}
return switch (param) {
case LANGUAGE ->
getAvailable(locale, l -> new ParameterOption(l.getLanguage(), l.getDisplayLanguage(translation)));
case REGION ->
getAvailable(locale, l -> new ParameterOption(l.getCountry(), l.getDisplayCountry(translation)));
case VARIANT ->
getAvailable(locale, l -> new ParameterOption(l.getVariant(), l.getDisplayVariant(translation)));
case TIMEZONE -> processTimeZoneParam();
default -> null;
};
}
private Collection<ParameterOption> processTimeZoneParam() {
@@ -110,14 +110,11 @@ public class ItemConsoleCommandExtensionTest {
var item4 = new SwitchItem("Item4");
item3.addTag("Tag1");
when(managedItemProviderMock.get(anyString())).thenAnswer(invocation -> {
switch ((String) invocation.getArguments()[0]) {
case "Item3":
return item3;
case "Item4":
return item4;
default:
return null;
}
return switch ((String) invocation.getArguments()[0]) {
case "Item3" -> item3;
case "Item4" -> item4;
default -> null;
};
});
var candidates = new ArrayList<String>();
@@ -128,13 +128,10 @@ public class ProfileTypeResource implements RESTResource {
// requested to filter against an unknown channel type -> do not return a ProfileType
return t -> false;
}
switch (channelType.getKind()) {
case STATE:
return t -> stateProfileMatchesProfileType(t, channelType);
case TRIGGER:
return t -> triggerProfileMatchesProfileType(t, channelType);
}
return t -> false;
return switch (channelType.getKind()) {
case STATE -> t -> stateProfileMatchesProfileType(t, channelType);
case TRIGGER -> t -> triggerProfileMatchesProfileType(t, channelType);
};
}
private Predicate<ProfileType> matchesItemType(@Nullable String itemType) {
@@ -87,5 +87,5 @@ public interface SemanticsService {
*/
default List<ItemSemanticsProblem> getItemSemanticsProblems(Item item) {
return List.of();
};
}
}
@@ -108,17 +108,12 @@ public class MissingServiceAnalyzer {
}
private String getState(int state) {
switch (state) {
case ComponentConfigurationDTO.UNSATISFIED_CONFIGURATION:
return "UNSATISFIED_CONFIGURATION";
case ComponentConfigurationDTO.UNSATISFIED_REFERENCE:
return "UNSATISFIED_REFERENCE";
case ComponentConfigurationDTO.SATISFIED:
return "SATISFIED";
case ComponentConfigurationDTO.ACTIVE:
return "ACTIVE";
default:
return state + "";
}
return switch (state) {
case ComponentConfigurationDTO.UNSATISFIED_CONFIGURATION -> "UNSATISFIED_CONFIGURATION";
case ComponentConfigurationDTO.UNSATISFIED_REFERENCE -> "UNSATISFIED_REFERENCE";
case ComponentConfigurationDTO.SATISFIED -> "SATISFIED";
case ComponentConfigurationDTO.ACTIVE -> "ACTIVE";
default -> state + "";
};
}
}
@@ -66,14 +66,11 @@ public class ThingConfigDescriptionAliasProvider implements ConfigDescriptionAli
return null;
}
switch (uri.getScheme()) {
case "thing":
return getThingConfigDescriptionURI(uri);
case "channel":
return getChannelConfigDescriptionURI(uri);
default:
return null;
}
return switch (uri.getScheme()) {
case "thing" -> getThingConfigDescriptionURI(uri);
case "channel" -> getChannelConfigDescriptionURI(uri);
default -> null;
};
}
private @Nullable URI getThingConfigDescriptionURI(URI uri) {
@@ -227,6 +227,6 @@ public class ItemChannelLinkRegistry extends AbstractLinkRegistry<ItemChannelLin
public enum ItemChannelLinkProblem {
THING_CHANNEL_MISSING,
ITEM_MISSING,
ITEM_AND_THING_CHANNEL_MISSING;
ITEM_AND_THING_CHANNEL_MISSING
}
}
@@ -52,18 +52,13 @@ public class AbstractResourceIconProviderTest {
provider = new AbstractResourceIconProvider(i18nProviderMock) {
@Override
protected @Nullable InputStream getResource(String iconset, String resourceName) {
switch (resourceName) {
case "x-30.png":
return new ByteArrayInputStream("x-30.png".getBytes());
case "x-y z.png":
return new ByteArrayInputStream("x-y z.png".getBytes());
case "a-bb-ccc-30.png":
return new ByteArrayInputStream("a-bb-ccc-30.png".getBytes());
case "a-bb-ccc-y z.png":
return new ByteArrayInputStream("a-bb-ccc-y z.png".getBytes());
default:
return null;
}
return switch (resourceName) {
case "x-30.png" -> new ByteArrayInputStream("x-30.png".getBytes());
case "x-y z.png" -> new ByteArrayInputStream("x-y z.png".getBytes());
case "a-bb-ccc-30.png" -> new ByteArrayInputStream("a-bb-ccc-30.png".getBytes());
case "a-bb-ccc-y z.png" -> new ByteArrayInputStream("a-bb-ccc-y z.png".getBytes());
default -> null;
};
}
private String substringAfterLast(String str, String separator) {
@@ -176,13 +176,10 @@ public class CommunicationManagerOSGiTest extends JavaOSGiTest {
itemStateConverterMock, eventPublisherMock, safeCaller, thingRegistryMock);
doAnswer(invocation -> {
switch (((Channel) invocation.getArguments()[0]).getKind()) {
case STATE:
return new ProfileTypeUID("test:state");
case TRIGGER:
return new ProfileTypeUID("test:trigger");
}
return null;
return switch (((Channel) invocation.getArguments()[0]).getKind()) {
case STATE -> new ProfileTypeUID("test:state");
case TRIGGER -> new ProfileTypeUID("test:trigger");
};
}).when(profileAdvisorMock).getSuggestedProfileTypeUID(isA(Channel.class), isA(String.class));
doAnswer(invocation -> {
switch (((ProfileTypeUID) invocation.getArguments()[0]).toString()) {