From c2a0739f1fbffa6857a40849e01e72bfd1a15aea Mon Sep 17 00:00:00 2001 From: Wouter Born Date: Sat, 20 Jan 2024 09:55:50 +0100 Subject: [PATCH] Fix several compiler warnings (#4041) * Fix several compiler warnings * Add missing null annotations * Add missing null checks * Remove use of deprecated SecurityManager * Remove redundant null checks * Remove unused variables * Fix raw use of parameterized class Signed-off-by: Wouter Born --- .../internal/RuleExecutionSimulator.java | 4 +-- .../commands/AutomationCommandsPluggable.java | 6 +--- .../internal/ruleengine/WrappedModule.java | 3 +- .../core/config/core/Configuration.java | 2 +- .../internal/PersistenceManagerTest.java | 3 +- .../type/AbstractChannelTypeBuilder.java | 3 +- .../thing/profiles/ProfileTypeBuilder.java | 3 +- .../core/thing/type/ChannelTypeBuilder.java | 3 +- .../core/common/NamedThreadFactory.java | 4 +-- .../common/QueueingThreadPoolExecutor.java | 7 +---- .../org/openhab/core/common/SafeCaller.java | 3 +- .../core/common/SafeCallerBuilder.java | 3 +- .../common/registry/AbstractRegistry.java | 11 +++++--- .../common/SafeCallerBuilderImpl.java | 3 +- .../core/internal/common/SafeCallerImpl.java | 3 +- .../MetadataCommandDescriptionProvider.java | 5 ++-- .../org/openhab/core/items/GenericItem.java | 6 ---- .../org/openhab/core/items/GroupItem.java | 18 ++++-------- .../core/library/unit/CurrencyUnit.java | 9 +++--- .../internal/i18n/I18nProviderImplTest.java | 8 +++--- .../items/ItemStateConverterImplTest.java | 2 +- .../openhab/core/items/GenericItemTest.java | 20 ------------- .../library/dimension/DataAmountTest.java | 6 ++-- .../core/library/types/DateTimeTypeTest.java | 28 +++++++++++-------- .../core/library/types/PercentTypeTest.java | 1 - 25 files changed, 67 insertions(+), 97 deletions(-) diff --git a/bundles/org.openhab.core.automation/src/main/java/org/openhab/core/automation/internal/RuleExecutionSimulator.java b/bundles/org.openhab.core.automation/src/main/java/org/openhab/core/automation/internal/RuleExecutionSimulator.java index 54a6ae99767..11123e8bedf 100644 --- a/bundles/org.openhab.core.automation/src/main/java/org/openhab/core/automation/internal/RuleExecutionSimulator.java +++ b/bundles/org.openhab.core.automation/src/main/java/org/openhab/core/automation/internal/RuleExecutionSimulator.java @@ -95,9 +95,7 @@ private List simulateExecutionsForRule(Rule rule, ZonedDateTime f // Only triggers that are time-based will be considered within the simulation if (triggerHandler instanceof TimeBasedTriggerHandler handler) { SchedulerTemporalAdjuster temporalAdjuster = handler.getTemporalAdjuster(); - if (temporalAdjuster != null) { - executions.addAll(simulateExecutionsForCronBasedRule(rule, from, until, temporalAdjuster)); - } + executions.addAll(simulateExecutionsForCronBasedRule(rule, from, until, temporalAdjuster)); } } logger.debug("Created {} rule simulations for rule {}.", executions.size(), rule.getName()); diff --git a/bundles/org.openhab.core.automation/src/main/java/org/openhab/core/automation/internal/commands/AutomationCommandsPluggable.java b/bundles/org.openhab.core.automation/src/main/java/org/openhab/core/automation/internal/commands/AutomationCommandsPluggable.java index 22016c63311..5d25ef87f1a 100644 --- a/bundles/org.openhab.core.automation/src/main/java/org/openhab/core/automation/internal/commands/AutomationCommandsPluggable.java +++ b/bundles/org.openhab.core.automation/src/main/java/org/openhab/core/automation/internal/commands/AutomationCommandsPluggable.java @@ -132,11 +132,7 @@ public void execute(String[] args, Console console) { } String res = super.executeCommand(command, params); - if (res == null) { - console.println(String.format("Unsupported command %s", command)); - } else { - console.println(res); - } + console.println(res); } @Override diff --git a/bundles/org.openhab.core.automation/src/main/java/org/openhab/core/automation/internal/ruleengine/WrappedModule.java b/bundles/org.openhab.core.automation/src/main/java/org/openhab/core/automation/internal/ruleengine/WrappedModule.java index e23481a1509..db8b61e9b5b 100644 --- a/bundles/org.openhab.core.automation/src/main/java/org/openhab/core/automation/internal/ruleengine/WrappedModule.java +++ b/bundles/org.openhab.core.automation/src/main/java/org/openhab/core/automation/internal/ruleengine/WrappedModule.java @@ -12,6 +12,7 @@ */ package org.openhab.core.automation.internal.ruleengine; +import org.eclipse.jdt.annotation.NonNull; import org.eclipse.jdt.annotation.NonNullByDefault; import org.eclipse.jdt.annotation.Nullable; import org.openhab.core.automation.Module; @@ -23,7 +24,7 @@ * @author Markus Rathgeb - Initial contribution */ @NonNullByDefault -public class WrappedModule { +public class WrappedModule<@NonNull M extends Module, H extends ModuleHandler> { private final M module; private @Nullable H handler; diff --git a/bundles/org.openhab.core.config.core/src/main/java/org/openhab/core/config/core/Configuration.java b/bundles/org.openhab.core.config.core/src/main/java/org/openhab/core/config/core/Configuration.java index 0deb58dc146..1356b0913ed 100644 --- a/bundles/org.openhab.core.config.core/src/main/java/org/openhab/core/config/core/Configuration.java +++ b/bundles/org.openhab.core.config.core/src/main/java/org/openhab/core/config/core/Configuration.java @@ -12,7 +12,7 @@ */ package org.openhab.core.config.core; -import static java.util.Collections.*; +import static java.util.Collections.synchronizedMap; import static org.openhab.core.config.core.ConfigUtil.normalizeTypes; import java.util.ArrayList; diff --git a/bundles/org.openhab.core.persistence/src/test/java/org/openhab/core/persistence/internal/PersistenceManagerTest.java b/bundles/org.openhab.core.persistence/src/test/java/org/openhab/core/persistence/internal/PersistenceManagerTest.java index 5b195369ca9..7312d55a798 100644 --- a/bundles/org.openhab.core.persistence/src/test/java/org/openhab/core/persistence/internal/PersistenceManagerTest.java +++ b/bundles/org.openhab.core.persistence/src/test/java/org/openhab/core/persistence/internal/PersistenceManagerTest.java @@ -29,6 +29,7 @@ import java.util.Objects; import java.util.Set; +import org.eclipse.jdt.annotation.NonNull; import org.eclipse.jdt.annotation.NonNullByDefault; import org.eclipse.jdt.annotation.Nullable; import org.junit.jupiter.api.BeforeEach; @@ -125,7 +126,7 @@ public String getName() { private @NonNullByDefault({}) @Mock ScheduledCompletableFuture scheduledFutureMock; private @NonNullByDefault({}) @Mock ItemRegistry itemRegistryMock; private @NonNullByDefault({}) @Mock SafeCaller safeCallerMock; - private @NonNullByDefault({}) @Mock SafeCallerBuilder safeCallerBuilderMock; + private @NonNullByDefault({}) @Mock SafeCallerBuilder<@NonNull QueryablePersistenceService> safeCallerBuilderMock; private @NonNullByDefault({}) @Mock ReadyService readyServiceMock; private @NonNullByDefault({}) @Mock PersistenceServiceConfigurationRegistry persistenceServiceConfigurationRegistryMock; diff --git a/bundles/org.openhab.core.thing/src/main/java/org/openhab/core/thing/internal/type/AbstractChannelTypeBuilder.java b/bundles/org.openhab.core.thing/src/main/java/org/openhab/core/thing/internal/type/AbstractChannelTypeBuilder.java index 3ad29583e13..6e18770a90e 100644 --- a/bundles/org.openhab.core.thing/src/main/java/org/openhab/core/thing/internal/type/AbstractChannelTypeBuilder.java +++ b/bundles/org.openhab.core.thing/src/main/java/org/openhab/core/thing/internal/type/AbstractChannelTypeBuilder.java @@ -17,6 +17,7 @@ import java.util.HashSet; import java.util.Set; +import org.eclipse.jdt.annotation.NonNull; import org.eclipse.jdt.annotation.NonNullByDefault; import org.eclipse.jdt.annotation.Nullable; import org.openhab.core.thing.type.ChannelType; @@ -30,7 +31,7 @@ */ @NonNullByDefault @SuppressWarnings("unchecked") -abstract class AbstractChannelTypeBuilder> implements ChannelTypeBuilder { +abstract class AbstractChannelTypeBuilder<@NonNull T extends ChannelTypeBuilder> implements ChannelTypeBuilder { protected final ChannelTypeUID channelTypeUID; protected final String label; diff --git a/bundles/org.openhab.core.thing/src/main/java/org/openhab/core/thing/profiles/ProfileTypeBuilder.java b/bundles/org.openhab.core.thing/src/main/java/org/openhab/core/thing/profiles/ProfileTypeBuilder.java index f3b76813bd2..b8e4fd1767f 100644 --- a/bundles/org.openhab.core.thing/src/main/java/org/openhab/core/thing/profiles/ProfileTypeBuilder.java +++ b/bundles/org.openhab.core.thing/src/main/java/org/openhab/core/thing/profiles/ProfileTypeBuilder.java @@ -16,6 +16,7 @@ import java.util.Collection; import java.util.HashSet; +import org.eclipse.jdt.annotation.NonNull; import org.eclipse.jdt.annotation.NonNullByDefault; import org.openhab.core.thing.internal.profiles.StateProfileTypeImpl; import org.openhab.core.thing.internal.profiles.TriggerProfileTypeImpl; @@ -31,7 +32,7 @@ * @param the concrete {@link ProfileType} sub-interface. */ @NonNullByDefault -public final class ProfileTypeBuilder { +public final class ProfileTypeBuilder<@NonNull T extends ProfileType> { @FunctionalInterface private interface ProfileTypeFactory { diff --git a/bundles/org.openhab.core.thing/src/main/java/org/openhab/core/thing/type/ChannelTypeBuilder.java b/bundles/org.openhab.core.thing/src/main/java/org/openhab/core/thing/type/ChannelTypeBuilder.java index c0bd2938a8b..5fbc703a75b 100644 --- a/bundles/org.openhab.core.thing/src/main/java/org/openhab/core/thing/type/ChannelTypeBuilder.java +++ b/bundles/org.openhab.core.thing/src/main/java/org/openhab/core/thing/type/ChannelTypeBuilder.java @@ -15,6 +15,7 @@ import java.net.URI; import java.util.Collection; +import org.eclipse.jdt.annotation.NonNull; import org.eclipse.jdt.annotation.NonNullByDefault; import org.openhab.core.thing.internal.type.StateChannelTypeBuilderImpl; import org.openhab.core.thing.internal.type.TriggerChannelTypeBuilderImpl; @@ -25,7 +26,7 @@ * @author Stefan Triller - Initial contribution */ @NonNullByDefault -public interface ChannelTypeBuilder> { +public interface ChannelTypeBuilder<@NonNull T extends ChannelTypeBuilder> { /** * Specify whether this is an advanced channel, default is false * diff --git a/bundles/org.openhab.core/src/main/java/org/openhab/core/common/NamedThreadFactory.java b/bundles/org.openhab.core/src/main/java/org/openhab/core/common/NamedThreadFactory.java index 902ad48f43c..c579c38f2e3 100644 --- a/bundles/org.openhab.core/src/main/java/org/openhab/core/common/NamedThreadFactory.java +++ b/bundles/org.openhab.core/src/main/java/org/openhab/core/common/NamedThreadFactory.java @@ -77,9 +77,7 @@ public NamedThreadFactory(final String id, final boolean daemonize, final int pr this.daemonize = daemonize; this.priority = priority; this.namePrefix = "OH-" + id + "-"; - final SecurityManager securityManager = System.getSecurityManager(); - this.group = securityManager != null ? securityManager.getThreadGroup() - : Thread.currentThread().getThreadGroup(); + this.group = Thread.currentThread().getThreadGroup(); } @Override diff --git a/bundles/org.openhab.core/src/main/java/org/openhab/core/common/QueueingThreadPoolExecutor.java b/bundles/org.openhab.core/src/main/java/org/openhab/core/common/QueueingThreadPoolExecutor.java index 5a9c99011d0..40050d83244 100644 --- a/bundles/org.openhab.core/src/main/java/org/openhab/core/common/QueueingThreadPoolExecutor.java +++ b/bundles/org.openhab.core/src/main/java/org/openhab/core/common/QueueingThreadPoolExecutor.java @@ -167,10 +167,6 @@ public void execute(Runnable command) { if (taskQueue.isEmpty()) { super.execute(command); } else { - if (command == null) { - throw new IllegalArgumentException("Command can not be null."); - } - // ignore incoming tasks when the executor is shutdown if (!isShutdown()) { addToQueue(command); @@ -240,8 +236,7 @@ private static class CommonThreadFactory implements ThreadFactory { public CommonThreadFactory(String name) { this.name = name; - SecurityManager s = System.getSecurityManager(); - group = (s != null) ? s.getThreadGroup() : Thread.currentThread().getThreadGroup(); + group = Thread.currentThread().getThreadGroup(); } @Override diff --git a/bundles/org.openhab.core/src/main/java/org/openhab/core/common/SafeCaller.java b/bundles/org.openhab.core/src/main/java/org/openhab/core/common/SafeCaller.java index bd2a6b3f41c..0ab47d2a551 100644 --- a/bundles/org.openhab.core/src/main/java/org/openhab/core/common/SafeCaller.java +++ b/bundles/org.openhab.core/src/main/java/org/openhab/core/common/SafeCaller.java @@ -14,6 +14,7 @@ import java.util.concurrent.TimeUnit; +import org.eclipse.jdt.annotation.NonNull; import org.eclipse.jdt.annotation.NonNullByDefault; /** @@ -39,5 +40,5 @@ public interface SafeCaller { * @param interfaceType the interface which defines the relevant methods * @return a safe call builder instance. */ - SafeCallerBuilder create(T target, Class interfaceType); + SafeCallerBuilder<@NonNull T> create(T target, Class interfaceType); } diff --git a/bundles/org.openhab.core/src/main/java/org/openhab/core/common/SafeCallerBuilder.java b/bundles/org.openhab.core/src/main/java/org/openhab/core/common/SafeCallerBuilder.java index 672aad63806..7d53111e87b 100644 --- a/bundles/org.openhab.core/src/main/java/org/openhab/core/common/SafeCallerBuilder.java +++ b/bundles/org.openhab.core/src/main/java/org/openhab/core/common/SafeCallerBuilder.java @@ -14,6 +14,7 @@ import java.util.function.Consumer; +import org.eclipse.jdt.annotation.NonNull; import org.eclipse.jdt.annotation.NonNullByDefault; /** @@ -24,7 +25,7 @@ * @param */ @NonNullByDefault -public interface SafeCallerBuilder { +public interface SafeCallerBuilder<@NonNull T> { /** * Creates a dynamic proxy with the according properties which guards the caller from hanging implementations in the diff --git a/bundles/org.openhab.core/src/main/java/org/openhab/core/common/registry/AbstractRegistry.java b/bundles/org.openhab.core/src/main/java/org/openhab/core/common/registry/AbstractRegistry.java index 18e38436570..dd4f86330a7 100644 --- a/bundles/org.openhab.core/src/main/java/org/openhab/core/common/registry/AbstractRegistry.java +++ b/bundles/org.openhab.core/src/main/java/org/openhab/core/common/registry/AbstractRegistry.java @@ -188,11 +188,14 @@ public void added(Provider provider, E element) { */ private boolean added(Provider provider, E element, Collection providerElements) { final K uid = element.getUID(); - if (identifierToElement.containsKey(uid)) { + @Nullable + E existingElement = identifierToElement.get(uid); + if (existingElement != null) { + Provider existingElementProvider = elementToProvider.get(existingElement); logger.debug( "Cannot add \"{}\" with key \"{}\". It exists already from provider \"{}\"! Failed to add a second with the same UID from provider \"{}\"!", element.getClass().getSimpleName(), uid, - elementToProvider.get(identifierToElement.get(uid)).getClass().getSimpleName(), + existingElementProvider != null ? existingElementProvider.getClass().getSimpleName() : null, provider.getClass().getSimpleName()); return false; } @@ -245,7 +248,7 @@ public void removed(Provider provider, E element) { return; } Provider elementProvider = elementToProvider.get(existingElement); - if (!elementProvider.equals(provider)) { + if (elementProvider != null && !elementProvider.equals(provider)) { logger.error( "Provider '{}' is not allowed to remove element '{}' with key '{}' from the registry because it was added by provider '{}'.", provider.getClass().getSimpleName(), element.getClass().getSimpleName(), uid, @@ -439,7 +442,7 @@ protected void addProvider(Provider provider) { } elementsAdded.forEach(this::notifyListenersAboutAddedElement); - if (provider instanceof ManagedProvider && readyService != null) { + if (provider instanceof ManagedProvider && providerClazz != null && readyService != null) { readyService.markReady( new ReadyMarker("managed", providerClazz.getSimpleName().replace("Provider", "").toLowerCase())); } diff --git a/bundles/org.openhab.core/src/main/java/org/openhab/core/internal/common/SafeCallerBuilderImpl.java b/bundles/org.openhab.core/src/main/java/org/openhab/core/internal/common/SafeCallerBuilderImpl.java index 7479d27c018..dac3993da84 100644 --- a/bundles/org.openhab.core/src/main/java/org/openhab/core/internal/common/SafeCallerBuilderImpl.java +++ b/bundles/org.openhab.core/src/main/java/org/openhab/core/internal/common/SafeCallerBuilderImpl.java @@ -18,6 +18,7 @@ import java.util.function.Consumer; import java.util.stream.Stream; +import org.eclipse.jdt.annotation.NonNull; import org.eclipse.jdt.annotation.NonNullByDefault; import org.eclipse.jdt.annotation.Nullable; import org.openhab.core.common.SafeCaller; @@ -31,7 +32,7 @@ * @param */ @NonNullByDefault -public class SafeCallerBuilderImpl implements SafeCallerBuilder { +public class SafeCallerBuilderImpl<@NonNull T> implements SafeCallerBuilder { private final T target; private final Class[] interfaceTypes; diff --git a/bundles/org.openhab.core/src/main/java/org/openhab/core/internal/common/SafeCallerImpl.java b/bundles/org.openhab.core/src/main/java/org/openhab/core/internal/common/SafeCallerImpl.java index 91d234e727b..09d520b226e 100644 --- a/bundles/org.openhab.core/src/main/java/org/openhab/core/internal/common/SafeCallerImpl.java +++ b/bundles/org.openhab.core/src/main/java/org/openhab/core/internal/common/SafeCallerImpl.java @@ -17,6 +17,7 @@ import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; +import org.eclipse.jdt.annotation.NonNull; import org.eclipse.jdt.annotation.NonNullByDefault; import org.eclipse.jdt.annotation.Nullable; import org.openhab.core.common.SafeCaller; @@ -62,7 +63,7 @@ public void deactivate() { } @Override - public SafeCallerBuilder create(T target, Class interfaceType) { + public SafeCallerBuilder<@NonNull T> create(T target, Class interfaceType) { return new SafeCallerBuilderImpl<>(target, new Class[] { interfaceType }, manager); } diff --git a/bundles/org.openhab.core/src/main/java/org/openhab/core/internal/items/MetadataCommandDescriptionProvider.java b/bundles/org.openhab.core/src/main/java/org/openhab/core/internal/items/MetadataCommandDescriptionProvider.java index e19ac5d64d6..0b5b72d47ea 100644 --- a/bundles/org.openhab.core/src/main/java/org/openhab/core/internal/items/MetadataCommandDescriptionProvider.java +++ b/bundles/org.openhab.core/src/main/java/org/openhab/core/internal/items/MetadataCommandDescriptionProvider.java @@ -60,8 +60,9 @@ public MetadataCommandDescriptionProvider(final @Reference MetadataRegistry meta if (metadata != null) { try { CommandDescriptionImpl commandDescription = new CommandDescriptionImpl(); - if (metadata.getConfiguration().containsKey("options")) { - Stream.of(metadata.getConfiguration().get("options").toString().split(",")).forEach(o -> { + Object options = metadata.getConfiguration().get("options"); + if (options != null) { + Stream.of(options.toString().split(",")).forEach(o -> { if (o.contains("=")) { var pair = parseValueLabelPair(o.trim()); commandDescription.addCommandOption(new CommandOption(pair[0], pair[1])); diff --git a/bundles/org.openhab.core/src/main/java/org/openhab/core/items/GenericItem.java b/bundles/org.openhab.core/src/main/java/org/openhab/core/items/GenericItem.java index c54928db20e..57bc3e76370 100644 --- a/bundles/org.openhab.core/src/main/java/org/openhab/core/items/GenericItem.java +++ b/bundles/org.openhab.core/src/main/java/org/openhab/core/items/GenericItem.java @@ -131,9 +131,6 @@ public List getGroupNames() { */ @Override public void addGroupName(String groupItemName) { - if (groupItemName == null) { - throw new IllegalArgumentException("Group item name must not be null!"); - } if (!groupNames.contains(groupItemName)) { groupNames.add(groupItemName); } @@ -161,9 +158,6 @@ public void addGroupNames(List groupItemNames) { */ @Override public void removeGroupName(String groupItemName) { - if (groupItemName == null) { - throw new IllegalArgumentException("Group item name must not be null!"); - } groupNames.remove(groupItemName); } diff --git a/bundles/org.openhab.core/src/main/java/org/openhab/core/items/GroupItem.java b/bundles/org.openhab.core/src/main/java/org/openhab/core/items/GroupItem.java index f955926f6fa..d6f6d0d1c00 100644 --- a/bundles/org.openhab.core/src/main/java/org/openhab/core/items/GroupItem.java +++ b/bundles/org.openhab.core/src/main/java/org/openhab/core/items/GroupItem.java @@ -163,10 +163,6 @@ public Set getMembers(Predicate filterItem) { * @throws IllegalArgumentException if the given item is null */ public void addMember(Item item) { - if (item == null) { - throw new IllegalArgumentException("Item must not be null!"); - } - boolean added = members.addIfAbsent(item); // in case membership is constructed programmatically this sanitizes @@ -190,9 +186,6 @@ private void unregisterStateListener(Item old) { } public void replaceMember(Item oldItem, Item newItem) { - if (oldItem == null || newItem == null) { - throw new IllegalArgumentException("Items must not be null!"); - } int index = members.indexOf(oldItem); if (index > -1) { Item old = members.set(index, newItem); @@ -208,9 +201,6 @@ public void replaceMember(Item oldItem, Item newItem) { * @throws IllegalArgumentException if the given item is null */ public void removeMember(Item item) { - if (item == null) { - throw new IllegalArgumentException("Item must not be null!"); - } members.remove(item); unregisterStateListener(item); } @@ -287,6 +277,7 @@ public void send(Command command) { @Override protected void internalSend(Command command) { + EventPublisher eventPublisher = this.eventPublisher; if (eventPublisher != null) { for (Item member : members) { // try to send the command to the bus @@ -304,7 +295,8 @@ protected void internalSend(Command command) { newState = function.getStateAs(getStateMembers(getMembers()), typeClass); } - if (newState == null && baseItem != null && baseItem instanceof GenericItem item) { + Item baseItem = this.baseItem; + if (newState == null && baseItem instanceof GenericItem item) { // we use the transformation method from the base item item.setState(state); newState = baseItem.getStateAs(typeClass); @@ -363,6 +355,7 @@ public void stateChanged(Item item, State oldState, State newState) { public void stateUpdated(Item item, State state) { State oldState = this.state; State newState = oldState; + ItemStateConverter itemStateConverter = this.itemStateConverter; if (function != null && baseItem != null && itemStateConverter != null) { State calculatedState = function.calculate(getStateMembers(getMembers())); newState = itemStateConverter.convertToAcceptedState(calculatedState, baseItem); @@ -377,7 +370,8 @@ public void stateUpdated(Item item, State state) { @Override public void setState(State state) { State oldState = this.state; - if (baseItem != null && baseItem instanceof GenericItem item) { + Item baseItem = this.baseItem; + if (baseItem instanceof GenericItem item) { item.setState(state); this.state = baseItem.getState(); } else { diff --git a/bundles/org.openhab.core/src/main/java/org/openhab/core/library/unit/CurrencyUnit.java b/bundles/org.openhab.core/src/main/java/org/openhab/core/library/unit/CurrencyUnit.java index 43cd1d861a2..6f6eaaa1b60 100644 --- a/bundles/org.openhab.core/src/main/java/org/openhab/core/library/unit/CurrencyUnit.java +++ b/bundles/org.openhab.core/src/main/java/org/openhab/core/library/unit/CurrencyUnit.java @@ -12,12 +12,8 @@ */ package org.openhab.core.library.unit; -import static org.eclipse.jdt.annotation.DefaultLocation.FIELD; -import static org.eclipse.jdt.annotation.DefaultLocation.PARAMETER; -import static org.eclipse.jdt.annotation.DefaultLocation.RETURN_TYPE; -import static org.eclipse.jdt.annotation.DefaultLocation.TYPE_BOUND; +import static org.eclipse.jdt.annotation.DefaultLocation.*; import static org.openhab.core.library.unit.CurrencyUnits.BASE_CURRENCY; -import static tech.units.indriya.AbstractUnit.ONE; import java.math.BigDecimal; import java.math.MathContext; @@ -70,6 +66,7 @@ public CurrencyUnit(String name, @Nullable String symbol) throws IllegalArgument this.name = name; } + @Override public UnitConverter getSystemConverter() { return internalGetConverterTo(getSystemUnit()); } @@ -94,6 +91,7 @@ public Dimension getDimension() { return DIMENSION; } + @Override public void setName(@NonNullByDefault({}) String name) { this.name = name; } @@ -108,6 +106,7 @@ public String getName() { return symbol; } + @Override public void setSymbol(@Nullable String s) { this.symbol = s; } diff --git a/bundles/org.openhab.core/src/test/java/org/openhab/core/internal/i18n/I18nProviderImplTest.java b/bundles/org.openhab.core/src/test/java/org/openhab/core/internal/i18n/I18nProviderImplTest.java index 69434679753..4e45eb19b0c 100644 --- a/bundles/org.openhab.core/src/test/java/org/openhab/core/internal/i18n/I18nProviderImplTest.java +++ b/bundles/org.openhab.core/src/test/java/org/openhab/core/internal/i18n/I18nProviderImplTest.java @@ -99,7 +99,7 @@ public void setup() { public void assertThatConfigurationWasSet() { i18nProviderImpl.modified((Map) initialConfig); - PointType location = i18nProviderImpl.getLocation(); + PointType location = Objects.requireNonNull(i18nProviderImpl.getLocation()); Locale setLocale = i18nProviderImpl.getLocale(); assertThat(location.toString(), is(LOCATION_ZERO)); @@ -129,7 +129,7 @@ public void assertThatDefaultLocaleWillBeUsedAndLocationIsSet() { conf.put(LOCATION, LOCATION_DARMSTADT); i18nProviderImpl.modified(conf); - PointType location = i18nProviderImpl.getLocation(); + PointType location = Objects.requireNonNull(i18nProviderImpl.getLocation()); Locale setLocale = i18nProviderImpl.getLocale(); assertThat(location.toString(), is(LOCATION_DARMSTADT)); @@ -138,7 +138,7 @@ public void assertThatDefaultLocaleWillBeUsedAndLocationIsSet() { @Test public void assertThatActivateSetsLocaleAndLocation() { - PointType location = i18nProviderImpl.getLocation(); + PointType location = Objects.requireNonNull(i18nProviderImpl.getLocation()); Locale setLocale = i18nProviderImpl.getLocale(); assertThat(location.toString(), is(LOCATION_ZERO)); @@ -162,7 +162,7 @@ public void assertThatInvalidTimeZoneFallsbackToDefaultTimeZone() { public void assertThatConfigurationChangeWorks() { i18nProviderImpl.modified(buildRUConfig()); - PointType location = i18nProviderImpl.getLocation(); + PointType location = Objects.requireNonNull(i18nProviderImpl.getLocation()); Locale setLocale = i18nProviderImpl.getLocale(); assertThat(location.toString(), is(LOCATION_HAMBURG)); diff --git a/bundles/org.openhab.core/src/test/java/org/openhab/core/internal/items/ItemStateConverterImplTest.java b/bundles/org.openhab.core/src/test/java/org/openhab/core/internal/items/ItemStateConverterImplTest.java index 6df03a24e5c..7d71f8242e0 100644 --- a/bundles/org.openhab.core/src/test/java/org/openhab/core/internal/items/ItemStateConverterImplTest.java +++ b/bundles/org.openhab.core/src/test/java/org/openhab/core/internal/items/ItemStateConverterImplTest.java @@ -146,7 +146,7 @@ public void numberItemMiredConversion(Locale locale) { State originalState = new QuantityType<>("153 mired"); State convertedState = itemStateConverter.convertToAcceptedState(originalState, item); - assertThat(((QuantityType) convertedState).intValue(), is(6535)); + assertThat(((QuantityType) convertedState).intValue(), is(6535)); } @ParameterizedTest diff --git a/bundles/org.openhab.core/src/test/java/org/openhab/core/items/GenericItemTest.java b/bundles/org.openhab.core/src/test/java/org/openhab/core/items/GenericItemTest.java index f86ac7b2de4..d0beafcd55e 100644 --- a/bundles/org.openhab.core/src/test/java/org/openhab/core/items/GenericItemTest.java +++ b/bundles/org.openhab.core/src/test/java/org/openhab/core/items/GenericItemTest.java @@ -18,7 +18,6 @@ import static org.mockito.ArgumentMatchers.*; import static org.mockito.Mockito.*; -import java.util.Arrays; import java.util.List; import java.util.Locale; @@ -106,25 +105,6 @@ public void testItemPostsEventsCorrectly() { assertEquals(ItemStateUpdatedEvent.TYPE, updated.getType()); } - @Test - public void testAddGroupNameWithNull() { - TestItem item = new TestItem("member1"); - assertThrows(IllegalArgumentException.class, () -> item.addGroupName(toNull())); - } - - @Test - public void testAddGroupNamesWithNull() { - TestItem item = new TestItem("member1"); - assertThrows(IllegalArgumentException.class, - () -> item.addGroupNames(Arrays.asList("group-a", toNull(), "group-b"))); - } - - @Test - public void testRemoveGroupNameWithNull() { - TestItem item = new TestItem("member1"); - assertThrows(IllegalArgumentException.class, () -> item.removeGroupName(toNull())); - } - @Test public void testGetStateAsWithSameType() { TestItem item = new TestItem("member1"); diff --git a/bundles/org.openhab.core/src/test/java/org/openhab/core/library/dimension/DataAmountTest.java b/bundles/org.openhab.core/src/test/java/org/openhab/core/library/dimension/DataAmountTest.java index 164756d242f..cfc564029bb 100644 --- a/bundles/org.openhab.core/src/test/java/org/openhab/core/library/dimension/DataAmountTest.java +++ b/bundles/org.openhab.core/src/test/java/org/openhab/core/library/dimension/DataAmountTest.java @@ -30,7 +30,7 @@ public class DataAmountTest { @Test public void testBToMB() { - QuantityType quantityType = new QuantityType("1000 B"); + QuantityType quantityType = new QuantityType<>("1000 B"); QuantityType converted = quantityType.toUnit("MB"); assertThat(converted.toString(), is(equalTo("0.001 MB"))); @@ -38,7 +38,7 @@ public void testBToMB() { @Test public void testKBToMB() { - QuantityType quantityType = new QuantityType("1000 kB"); + QuantityType quantityType = new QuantityType<>("1000 kB"); QuantityType converted = quantityType.toUnit("MB"); assertThat(converted.toString(), is(equalTo("1 MB"))); @@ -46,7 +46,7 @@ public void testKBToMB() { @Test public void testInvertibleUnit() { - QuantityType quantityType = new QuantityType("1000 kB"); + QuantityType quantityType = new QuantityType<>("1000 kB"); QuantityType inverted = quantityType.toInvertibleUnit(Units.MEGABYTE); assertThat(quantityType, is(equalTo(inverted))); diff --git a/bundles/org.openhab.core/src/test/java/org/openhab/core/library/types/DateTimeTypeTest.java b/bundles/org.openhab.core/src/test/java/org/openhab/core/library/types/DateTimeTypeTest.java index 9bda1e8f8e4..8b240bff5eb 100644 --- a/bundles/org.openhab.core/src/test/java/org/openhab/core/library/types/DateTimeTypeTest.java +++ b/bundles/org.openhab.core/src/test/java/org/openhab/core/library/types/DateTimeTypeTest.java @@ -27,6 +27,7 @@ import java.util.List; import java.util.Locale; import java.util.Map; +import java.util.Objects; import java.util.TimeZone; import java.util.concurrent.TimeUnit; @@ -301,12 +302,7 @@ public void createDate(ParameterSet parameterSet) { TimeZone inputTimeZone = parameterSet.inputTimeZone; String inputTimeString = parameterSet.inputTimeString; if (inputTimeMap != null && inputTimeZone != null) { - int durationInNano = (int) TimeUnit.NANOSECONDS.convert(inputTimeMap.get("milliseconds"), - TimeUnit.MILLISECONDS); - - LocalDateTime dateTime = LocalDateTime.of(inputTimeMap.get("year"), inputTimeMap.get("month") + 1, - inputTimeMap.get("date"), inputTimeMap.get("hourOfDay"), inputTimeMap.get("minute"), - inputTimeMap.get("second"), durationInNano); + LocalDateTime dateTime = createLocalDateTimeFromInput(inputTimeMap); ZonedDateTime zonedDate = ZonedDateTime.of(dateTime, inputTimeZone.toZoneId()); dt1 = new DateTimeType(zonedDate); dt3 = new DateTimeType( @@ -368,12 +364,7 @@ private DateTimeType createDateTimeType(ParameterSet parameterSet) throws DateTi TimeZone inputTimeZone = parameterSet.inputTimeZone; String inputTimeString = parameterSet.inputTimeString; if (inputTimeMap != null && inputTimeZone != null) { - int durationInNano = (int) TimeUnit.NANOSECONDS.convert(inputTimeMap.get("milliseconds"), - TimeUnit.MILLISECONDS); - - LocalDateTime dateTime = LocalDateTime.of(inputTimeMap.get("year"), inputTimeMap.get("month") + 1, - inputTimeMap.get("date"), inputTimeMap.get("hourOfDay"), inputTimeMap.get("minute"), - inputTimeMap.get("second"), durationInNano); + LocalDateTime dateTime = createLocalDateTimeFromInput(inputTimeMap); ZonedDateTime zonedDate = ZonedDateTime.of(dateTime, inputTimeZone.toZoneId()); return new DateTimeType(zonedDate); } else if (inputTimeString != null) { @@ -381,4 +372,17 @@ private DateTimeType createDateTimeType(ParameterSet parameterSet) throws DateTi } throw new DateTimeException("Invalid inputs in parameter set"); } + + private LocalDateTime createLocalDateTimeFromInput(Map inputTimeMap) { + Integer year = Objects.requireNonNull(inputTimeMap.get("year")); + Integer month = Objects.requireNonNull(inputTimeMap.get("month")); + Integer dayOfMonth = Objects.requireNonNull(inputTimeMap.get("date")); + Integer hourOfDay = Objects.requireNonNull(inputTimeMap.get("hourOfDay")); + Integer minute = Objects.requireNonNull(inputTimeMap.get("minute")); + Integer second = Objects.requireNonNull(inputTimeMap.get("second")); + Integer milliseconds = Objects.requireNonNull(inputTimeMap.get("milliseconds")); + int durationInNano = (int) TimeUnit.NANOSECONDS.convert(milliseconds, TimeUnit.MILLISECONDS); + + return LocalDateTime.of(year, month + 1, dayOfMonth, hourOfDay, minute, second, durationInNano); + } } diff --git a/bundles/org.openhab.core/src/test/java/org/openhab/core/library/types/PercentTypeTest.java b/bundles/org.openhab.core/src/test/java/org/openhab/core/library/types/PercentTypeTest.java index 85b6ad81b24..12f40dd88e2 100644 --- a/bundles/org.openhab.core/src/test/java/org/openhab/core/library/types/PercentTypeTest.java +++ b/bundles/org.openhab.core/src/test/java/org/openhab/core/library/types/PercentTypeTest.java @@ -91,7 +91,6 @@ public void testLocalizedStringConstruction(Locale defaultLocale) { // Construction for each locale should always return the same result regardless of the current default locale Stream.of(Locale.ENGLISH, Locale.GERMAN).forEach(locale -> { char ds = DecimalFormatSymbols.getInstance(locale).getDecimalSeparator(); - char gs = DecimalFormatSymbols.getInstance(locale).getGroupingSeparator(); assertEquals(new PercentType("0"), new PercentType("0", locale)); assertEquals(new PercentType("0.000"), new PercentType(String.format("0%s000", ds), locale));