Skip to content

Commit

Permalink
Reduce SAT warnings
Browse files Browse the repository at this point in the history
Signed-off-by: Holger Friedrich <mail@holger-friedrich.de>
  • Loading branch information
holgerfriedrich committed Jan 12, 2025
1 parent 6dffaa6 commit 529b384
Show file tree
Hide file tree
Showing 20 changed files with 33 additions and 111 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -298,11 +298,9 @@ protected boolean remoteEnabled() {
try {
Configuration configuration = configurationAdmin.getConfiguration("org.openhab.addons", null);
Dictionary<String, Object> properties = configuration.getProperties();
if (properties == null) {
// if we can't determine a set property, we use true (default is remote enabled)
return true;
}
return ConfigParser.valueAsOrElse(properties.get(CONFIG_REMOTE_ENABLED), Boolean.class, true);
// if we can't determine a set property, we use true (default is remote enabled)
return (properties == null)
|| ConfigParser.valueAsOrElse(properties.get(CONFIG_REMOTE_ENABLED), Boolean.class, true);
} catch (IOException e) {
return true;
}
Expand All @@ -312,11 +310,9 @@ protected boolean includeIncompatible() {
try {
Configuration configuration = configurationAdmin.getConfiguration("org.openhab.addons", null);
Dictionary<String, Object> properties = configuration.getProperties();
if (properties == null) {
// if we can't determine a set property, we use false (default is show compatible only)
return false;
}
return ConfigParser.valueAsOrElse(properties.get(CONFIG_INCLUDE_INCOMPATIBLE), Boolean.class, false);
// if we can't determine a set property, we use false (default is show compatible only)
return (properties != null)
&& ConfigParser.valueAsOrElse(properties.get(CONFIG_INCLUDE_INCOMPATIBLE), Boolean.class, false);
} catch (IOException e) {
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -221,11 +221,8 @@ protected synchronized void unsetStorageCipher(StorageCipher storageCipher) {
}

private boolean isExpired(@Nullable Instant lastUsed) {
if (lastUsed == null) {
return false;
}
// (last used + 183 days < now) then it is expired
return lastUsed.plus(EXPIRE_DAYS, ChronoUnit.DAYS).isBefore(Instant.now());
return (lastUsed != null) && lastUsed.plus(EXPIRE_DAYS, ChronoUnit.DAYS).isBefore(Instant.now());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,38 +89,22 @@ public boolean isSatisfied(Map<String, @Nullable Object> context) {
case "GT":
case ">":
// Greater
if (toCompare == null) {
return false;
} else {
return compare(toCompare, rightValue) > 0;
}
return (toCompare != null) && (compare(toCompare, rightValue) > 0);
case "gte":
case "GTE":
case ">=":
case "=>":
// Greater or equal
if (toCompare == null) {
return false;
} else {
return compare(toCompare, rightValue) >= 0;
}
return (toCompare != null) && (compare(toCompare, rightValue) >= 0);
case "lt":
case "LT":
case "<":
if (toCompare == null) {
return false;
} else {
return compare(toCompare, rightValue) < 0;
}
return (toCompare != null) && (compare(toCompare, rightValue) < 0);
case "lte":
case "LTE":
case "<=":
case "=<":
if (toCompare == null) {
return false;
} else {
return compare(toCompare, rightValue) <= 0;
}
return (toCompare != null) && (compare(toCompare, rightValue) <= 0);
case "matches":
// Matcher...
if (toCompare instanceof String string1 && rightValue instanceof String string2) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,6 @@ private String inferKey(boolean checkAddonType, URI configDescriptionURI, String
}

private boolean isValidPropertyKey(@Nullable String key) {
if (key != null) {
return !DELIMITER.matcher(key).find();
}
return false;
return (key != null) && !DELIMITER.matcher(key).find();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -193,10 +193,7 @@ private FloatIntrospection() {

@Override
boolean isAssignable(Object value) {
if (!super.isAssignable(value)) {
return isBigDecimalInstance(value);
}
return true;
return super.isAssignable(value) || isBigDecimalInstance(value);
}
}

Expand All @@ -208,10 +205,7 @@ private IntegerIntrospection() {

@Override
boolean isAssignable(Object value) {
if (!super.isAssignable(value)) {
return isBigDecimalInstance(value);
}
return true;
return super.isAssignable(value) || isBigDecimalInstance(value);
}
}

Expand All @@ -223,18 +217,12 @@ private StringIntrospection() {

@Override
boolean isMinViolated(Object value, BigDecimal min) {
if (min == null) {
return false;
}
return ((String) value).length() < min.intValueExact();
return (min != null) && (((String) value).length() < min.intValueExact());
}

@Override
boolean isMaxViolated(Object value, BigDecimal max) {
if (max == null) {
return false;
}
return ((String) value).length() > max.intValueExact();
return (max != null) && (((String) value).length() > max.intValueExact());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -142,10 +142,7 @@ public SddpDevice(Map<String, String> headers, boolean offline) {
*/
@Override
public boolean equals(@Nullable Object obj) {
if (obj instanceof SddpDevice other) {
return Objects.equals(from, other.from);
}
return false;
return (obj instanceof SddpDevice other) && Objects.equals(from, other.from);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,10 +123,8 @@ public void run() {

private boolean isResultExpired(DiscoveryResult result, Instant now) {
long ttl = result.getTimeToLive();
if (ttl == DiscoveryResult.TTL_UNLIMITED) {
return false;
}
return Instant.ofEpochMilli(result.getTimestamp()).plusSeconds(ttl).isBefore(now);
return (ttl != DiscoveryResult.TTL_UNLIMITED)
&& Instant.ofEpochMilli(result.getTimestamp()).plusSeconds(ttl).isBefore(now);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,8 @@ public class AddonConsoleCommandExtension extends AbstractConsoleCommandExtensio
private class AddonConsoleCommandCompleter implements ConsoleCommandCompleter {
@Override
public boolean complete(String[] args, int cursorArgumentIndex, int cursorPosition, List<String> candidates) {
if (cursorArgumentIndex <= 0) {
return SUBCMD_COMPLETER.complete(args, cursorArgumentIndex, cursorPosition, candidates);
}
return false;
return (cursorArgumentIndex <= 0)
&& SUBCMD_COMPLETER.complete(args, cursorArgumentIndex, cursorPosition, candidates);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -794,10 +794,7 @@ private boolean isLeaf(EList<Widget> children) {

private boolean blockUntilChangeOccurs(String sitemapname, @Nullable String pageId) {
EList<Widget> widgets = subscriptions.collectWidgets(sitemapname, pageId);
if (widgets.isEmpty()) {
return false;
}
return waitForChanges(widgets);
return (!widgets.isEmpty()) && waitForChanges(widgets);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -212,10 +212,7 @@ public Map<String, String> getProperties() {

@Override
public boolean isSuccessorVersion(@Nullable String firmwareVersion) {
if (firmwareVersion == null) {
return false;
}
return version.compare(new Version(firmwareVersion)) > 0;
return (firmwareVersion != null) && (version.compare(new Version(firmwareVersion)) > 0);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
import java.time.Instant;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.openhab.core.library.types.DateTimeType;
import org.openhab.core.thing.profiles.ProfileCallback;
import org.openhab.core.thing.profiles.ProfileContext;
Expand Down Expand Up @@ -63,10 +62,6 @@ public TimestampOffsetProfile(ProfileCallback callback, ProfileContext context)
}
}

private @Nullable String toStringOrNull(@Nullable Object value) {
return value == null ? null : value.toString();
}

@Override
public ProfileTypeUID getProfileTypeUID() {
return SystemProfiles.TIMESTAMP_OFFSET;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,7 @@ protected AbstractLink() {

@Override
public boolean equals(@Nullable Object obj) {
if (obj instanceof AbstractLink link) {
return getUID().equals(link.getUID());
}
return false;
return (obj instanceof AbstractLink link) && getUID().equals(link.getUID());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,10 +126,7 @@ protected List<String> getExtensibleChannelTypeIds(Map<String, String> attribute

protected boolean getListed(Map<String, String> attributes) {
String listedFlag = attributes.get("listed");
if (listedFlag != null) {
return Boolean.parseBoolean(listedFlag);
}
return true;
return (listedFlag == null) || Boolean.parseBoolean(listedFlag);
}

protected @Nullable String getRepresentationProperty(NodeIterator nodeIterator) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -748,10 +748,7 @@ public List<DialogRegistration> getDialogRegistrations() {
}

private boolean checkLocales(Set<Locale> supportedLocales, Locale locale) {
if (supportedLocales.isEmpty()) {
return true;
}
return supportedLocales.stream().anyMatch(sLocale -> {
return supportedLocales.isEmpty() || supportedLocales.stream().anyMatch(sLocale -> {
var country = sLocale.getCountry();
return Objects.equals(sLocale.getLanguage(), locale.getLanguage())
&& (country == null || country.isBlank() || country.equals(locale.getCountry()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,6 @@ public boolean equals(@Nullable Object obj) {
return false;
}
LocalizedKey other = (LocalizedKey) obj;
if (!Objects.equals(key, other.key)) {
return false;
}
return Objects.equals(locale, other.locale);
return Objects.equals(key, other.key) && Objects.equals(locale, other.locale);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public class ThreadedEventHandler implements Closeable {
logger.trace("inspect event: {}", event);
if (event == null) {
logger.debug("Hey, you have really very few events.");
} else if (event.equals(notifyEvent)) {
} else if (event.equals(notifyEvent)) { // NOPMD
// received an internal notification
} else {
worker.handleEvent(event);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,7 @@ public void clearCache() {
* @return true if the specified resource is managed by this instance, otherwise false
*/
public boolean containsResource(String resource) {
if (resource != null) {
return this.resourceNames.contains(resource);
}

return false;
return (resource != null) && this.resourceNames.contains(resource);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -196,10 +196,7 @@ public boolean isEquivalentTo(@NonNullByDefault({}) Unit<Currency> that) {

@Override
public boolean equals(@Nullable Object obj) {
if (obj instanceof CurrencyUnit that) {
return (name.equals(that.name) && Objects.equals(symbol, that.symbol));
}
return false;
return (obj instanceof CurrencyUnit that) && (name.equals(that.name) && Objects.equals(symbol, that.symbol));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -591,7 +591,7 @@ public AssertingThread(Runnable runnable) {
@Override
public void run() {
try {
super.run();
super.run(); // NOPMD
} catch (AssertionError e) {
AssertingThread.this.assertionError = e;
} catch (RuntimeException e) {
Expand Down
7 changes: 2 additions & 5 deletions tools/static-code-analysis/pmd/suppressions.properties
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
org.openhab.core.common.registry.AbstractRegistry=AvoidCatchingThrowable
org.openhab.core.internal.common.SafeCallManagerImpl=CompareObjectsWithEquals
org.openhab.core.internal.events.ThreadedEventHandler=CompareObjectsWithEquals
org.openhab.core.internal.events.ThreadedEventHandler=EmptyIfStmt
org.openhab.core.thing.internal.ChannelItemProvider=CompareObjectsWithEquals
org.openhab.core.thing.internal.ThingManager=CompareObjectsWithEquals
org.openhab.core.io.console.karaf.internal.OSGiConsole=SystemPrintln
org.openhab.core.io.console.rfc147.internal.CommandWrapper=SystemPrintln
org.openhab.core.io.console.rfc147.internal.OSGiConsole=SystemPrintln
org.openhab.core.io.net.http.internal.SecureHttpClientFactory=AvoidThrowingRawExceptionTypes
org.openhab.core.io.transport.modbus.internal.ModbusManagerImpl=MoreThanOneLogger
org.openhab.core.model.core.internal.ModelRepositoryImpl=AvoidCatchingNPE
org.openhab.core.model.script.interpreter.ScriptInterpreter=AvoidCatchingThrowable
org.openhab.core.ui.internal.proxy.ProxyServletService=AvoidCatchingThrowable
Expand All @@ -16,3 +12,4 @@ org.openhab.core.automation.internal.RuleRegistryImpl=CompareObjectsWithEquals
org.openhab.core.automation.internal.provider.AutomationResourceBundlesEventQueue=AvoidCatchingThrowable
org.openhab.core.io.console.karaf.internal.InstallServiceCommand=SystemPrintln
org.openhab.core.common.PoolBasedSequentialScheduledExecutorService=CompareObjectsWithEquals
org.openhab.core.tools.UpgradeTool=SystemPrintln

0 comments on commit 529b384

Please sign in to comment.