From 81e3a05614943c981de47d1e26ddd70f055910a4 Mon Sep 17 00:00:00 2001 From: Holger Friedrich Date: Sun, 7 Sep 2025 19:54:05 +0200 Subject: [PATCH] Simplify switch statements, Java 21 (#5012) Signed-off-by: Holger Friedrich --- .../internal/MediaActionTypeProvider.java | 16 ++++----- .../org/openhab/core/automation/Rule.java | 16 ++++----- .../automation/internal/RuleRegistryImpl.java | 19 ++++------ .../AutomationResourceBundlesTracker.java | 13 +++---- .../parser/ValidationException.java | 2 +- .../i18n/I18nConfigOptionsProvider.java | 36 ++++++++----------- .../ItemConsoleCommandExtensionTest.java | 13 +++---- .../internal/profile/ProfileTypeResource.java | 11 +++--- .../core/semantics/SemanticsService.java | 2 +- .../internal/java/MissingServiceAnalyzer.java | 19 ++++------ .../ThingConfigDescriptionAliasProvider.java | 13 +++---- .../thing/link/ItemChannelLinkRegistry.java | 2 +- .../AbstractResourceIconProviderTest.java | 19 ++++------ .../CommunicationManagerOSGiTest.java | 11 +++--- 14 files changed, 74 insertions(+), 118 deletions(-) diff --git a/bundles/org.openhab.core.automation.module.media/src/main/java/org/openhab/core/automation/module/media/internal/MediaActionTypeProvider.java b/bundles/org.openhab.core.automation.module.media/src/main/java/org/openhab/core/automation/module/media/internal/MediaActionTypeProvider.java index bf5f1c4cb..6ca8e3292 100644 --- a/bundles/org.openhab.core.automation.module.media/src/main/java/org/openhab/core/automation/module/media/internal/MediaActionTypeProvider.java +++ b/bundles/org.openhab.core.automation.module.media/src/main/java/org/openhab/core/automation/module/media/internal/MediaActionTypeProvider.java @@ -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 diff --git a/bundles/org.openhab.core.automation/src/main/java/org/openhab/core/automation/Rule.java b/bundles/org.openhab.core.automation/src/main/java/org/openhab/core/automation/Rule.java index adcdb1cd5..45e96055e 100644 --- a/bundles/org.openhab.core.automation/src/main/java/org/openhab/core/automation/Rule.java +++ b/bundles/org.openhab.core.automation/src/main/java/org/openhab/core/automation/Rule.java @@ -212,16 +212,12 @@ public interface Rule extends Identifiable { 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; + }; } } } diff --git a/bundles/org.openhab.core.automation/src/main/java/org/openhab/core/automation/internal/RuleRegistryImpl.java b/bundles/org.openhab.core.automation/src/main/java/org/openhab/core/automation/internal/RuleRegistryImpl.java index f808c22cc..d18d5f65d 100644 --- a/bundles/org.openhab.core.automation/src/main/java/org/openhab/core/automation/internal/RuleRegistryImpl.java +++ b/bundles/org.openhab.core.automation/src/main/java/org/openhab/core/automation/internal/RuleRegistryImpl.java @@ -597,18 +597,13 @@ public class RuleRegistryImpl extends AbstractRegistrytrue if the type and value matching or false 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 - || configValue instanceof Double doubleValue && doubleValue.intValue() == doubleValue; - case DECIMAL: - return configValue instanceof BigDecimal || configValue instanceof Double; - } - return false; + 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 -> configValue instanceof BigDecimal || configValue instanceof Double; + }; } /** diff --git a/bundles/org.openhab.core.automation/src/main/java/org/openhab/core/automation/internal/provider/AutomationResourceBundlesTracker.java b/bundles/org.openhab.core.automation/src/main/java/org/openhab/core/automation/internal/provider/AutomationResourceBundlesTracker.java index 53740a5d2..d418dbc09 100644 --- a/bundles/org.openhab.core.automation/src/main/java/org/openhab/core/automation/internal/provider/AutomationResourceBundlesTracker.java +++ b/bundles/org.openhab.core.automation/src/main/java/org/openhab/core/automation/internal/provider/AutomationResourceBundlesTracker.java @@ -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); + }; } /** diff --git a/bundles/org.openhab.core.automation/src/main/java/org/openhab/core/automation/parser/ValidationException.java b/bundles/org.openhab.core.automation/src/main/java/org/openhab/core/automation/parser/ValidationException.java index 4ca261a4c..6daa14c6a 100644 --- a/bundles/org.openhab.core.automation/src/main/java/org/openhab/core/automation/parser/ValidationException.java +++ b/bundles/org.openhab.core.automation/src/main/java/org/openhab/core/automation/parser/ValidationException.java @@ -122,6 +122,6 @@ public class ValidationException extends Exception { public enum ObjectType { MODULE_TYPE, TEMPLATE, - RULE; + RULE } } diff --git a/bundles/org.openhab.core.config.core/src/main/java/org/openhab/core/config/core/internal/i18n/I18nConfigOptionsProvider.java b/bundles/org.openhab.core.config.core/src/main/java/org/openhab/core/config/core/internal/i18n/I18nConfigOptionsProvider.java index 53affb706..7fc8e7558 100644 --- a/bundles/org.openhab.core.config.core/src/main/java/org/openhab/core/config/core/internal/i18n/I18nConfigOptionsProvider.java +++ b/bundles/org.openhab.core.config.core/src/main/java/org/openhab/core/config/core/internal/i18n/I18nConfigOptionsProvider.java @@ -50,31 +50,25 @@ public class I18nConfigOptionsProvider implements ConfigOptionProvider { @Override public @Nullable Collection 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 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 processTimeZoneParam() { diff --git a/bundles/org.openhab.core.io.console/src/test/java/org/openhab/core/io/console/internal/extension/ItemConsoleCommandExtensionTest.java b/bundles/org.openhab.core.io.console/src/test/java/org/openhab/core/io/console/internal/extension/ItemConsoleCommandExtensionTest.java index d91917671..2ed26466f 100644 --- a/bundles/org.openhab.core.io.console/src/test/java/org/openhab/core/io/console/internal/extension/ItemConsoleCommandExtensionTest.java +++ b/bundles/org.openhab.core.io.console/src/test/java/org/openhab/core/io/console/internal/extension/ItemConsoleCommandExtensionTest.java @@ -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(); diff --git a/bundles/org.openhab.core.io.rest.core/src/main/java/org/openhab/core/io/rest/core/internal/profile/ProfileTypeResource.java b/bundles/org.openhab.core.io.rest.core/src/main/java/org/openhab/core/io/rest/core/internal/profile/ProfileTypeResource.java index 0a7eecbd3..e5bec3ad8 100644 --- a/bundles/org.openhab.core.io.rest.core/src/main/java/org/openhab/core/io/rest/core/internal/profile/ProfileTypeResource.java +++ b/bundles/org.openhab.core.io.rest.core/src/main/java/org/openhab/core/io/rest/core/internal/profile/ProfileTypeResource.java @@ -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 matchesItemType(@Nullable String itemType) { diff --git a/bundles/org.openhab.core.semantics/src/main/java/org/openhab/core/semantics/SemanticsService.java b/bundles/org.openhab.core.semantics/src/main/java/org/openhab/core/semantics/SemanticsService.java index 5fb78ac65..c7139935d 100644 --- a/bundles/org.openhab.core.semantics/src/main/java/org/openhab/core/semantics/SemanticsService.java +++ b/bundles/org.openhab.core.semantics/src/main/java/org/openhab/core/semantics/SemanticsService.java @@ -87,5 +87,5 @@ public interface SemanticsService { */ default List getItemSemanticsProblems(Item item) { return List.of(); - }; + } } diff --git a/bundles/org.openhab.core.test/src/main/java/org/openhab/core/test/internal/java/MissingServiceAnalyzer.java b/bundles/org.openhab.core.test/src/main/java/org/openhab/core/test/internal/java/MissingServiceAnalyzer.java index 79beed39d..87ab4ca08 100644 --- a/bundles/org.openhab.core.test/src/main/java/org/openhab/core/test/internal/java/MissingServiceAnalyzer.java +++ b/bundles/org.openhab.core.test/src/main/java/org/openhab/core/test/internal/java/MissingServiceAnalyzer.java @@ -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 + ""; + }; } } diff --git a/bundles/org.openhab.core.thing/src/main/java/org/openhab/core/thing/internal/ThingConfigDescriptionAliasProvider.java b/bundles/org.openhab.core.thing/src/main/java/org/openhab/core/thing/internal/ThingConfigDescriptionAliasProvider.java index 00865cadf..0536fb9d4 100644 --- a/bundles/org.openhab.core.thing/src/main/java/org/openhab/core/thing/internal/ThingConfigDescriptionAliasProvider.java +++ b/bundles/org.openhab.core.thing/src/main/java/org/openhab/core/thing/internal/ThingConfigDescriptionAliasProvider.java @@ -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) { diff --git a/bundles/org.openhab.core.thing/src/main/java/org/openhab/core/thing/link/ItemChannelLinkRegistry.java b/bundles/org.openhab.core.thing/src/main/java/org/openhab/core/thing/link/ItemChannelLinkRegistry.java index 5a4c06c9b..06c67097c 100644 --- a/bundles/org.openhab.core.thing/src/main/java/org/openhab/core/thing/link/ItemChannelLinkRegistry.java +++ b/bundles/org.openhab.core.thing/src/main/java/org/openhab/core/thing/link/ItemChannelLinkRegistry.java @@ -227,6 +227,6 @@ public class ItemChannelLinkRegistry extends AbstractLinkRegistry 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) { diff --git a/itests/org.openhab.core.thing.tests/src/main/java/org/openhab/core/thing/internal/CommunicationManagerOSGiTest.java b/itests/org.openhab.core.thing.tests/src/main/java/org/openhab/core/thing/internal/CommunicationManagerOSGiTest.java index c6a02ffca..105c306b1 100644 --- a/itests/org.openhab.core.thing.tests/src/main/java/org/openhab/core/thing/internal/CommunicationManagerOSGiTest.java +++ b/itests/org.openhab.core.thing.tests/src/main/java/org/openhab/core/thing/internal/CommunicationManagerOSGiTest.java @@ -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()) {