Minor code cleanup (#3942)

Signed-off-by: Holger Friedrich <mail@holger-friedrich.de>
This commit is contained in:
Holger Friedrich
2023-12-23 12:54:29 +01:00
committed by GitHub
parent 839ba1ab4c
commit bada23fdb8
15 changed files with 42 additions and 45 deletions
@@ -421,6 +421,7 @@ public class OAuthClientServiceImpl implements OAuthClientService {
* @param key The name of the key to add to the auth form
* @param value The value of the new auth form param
*/
@Override
public void addExtraAuthField(String key, String value) {
if (extraAuthFields == null) {
extraAuthFields = new Fields();
@@ -66,7 +66,7 @@ public final class ConfigStatusMessage {
* The type for a pending message. It should be used if the transmission of the configuration parameter to the
* entity is pending.
*/
PENDING;
PENDING
}
/** The name of the configuration parameter. */
@@ -14,7 +14,6 @@ package org.openhab.core.config.core.xml.osgi;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Enumeration;
import java.util.HashMap;
@@ -151,7 +150,7 @@ public class XmlDocumentBundleTracker<@NonNull T> extends BundleTracker<Bundle>
if (bundleTracker == null || bundleTracker.getBundles() == null) {
return Set.of();
}
return (Set<Bundle>) Arrays.stream(bundleTracker.getBundles()).collect(Collectors.toSet());
return Set.of(bundleTracker.getBundles());
}
@Override
@@ -63,7 +63,7 @@ public class ProcessAddonFinder extends BaseAddonFinder {
return command.get();
}
Optional<String[]> args = h.info().arguments();
if (!args.isPresent()) {
if (args.isEmpty()) {
return "";
}
String[] argsArray = args.get();
@@ -200,13 +200,13 @@ public class PersistenceManager implements ItemRegistryChangeListener, StateChan
for (PersistenceConfig itemCfg : itemConfig.items()) {
if (itemCfg instanceof PersistenceAllConfig) {
return true;
} else if (itemCfg instanceof PersistenceItemConfig) {
if (item.getName().equals(((PersistenceItemConfig) itemCfg).getItem())) {
} else if (itemCfg instanceof PersistenceItemConfig persistenceItemConfig) {
if (item.getName().equals(persistenceItemConfig.getItem())) {
return true;
}
} else if (itemCfg instanceof PersistenceGroupConfig) {
} else if (itemCfg instanceof PersistenceGroupConfig persistenceGroupConfig) {
try {
Item gItem = itemRegistry.getItem(((PersistenceGroupConfig) itemCfg).getGroup());
Item gItem = itemRegistry.getItem(persistenceGroupConfig.getGroup());
if (gItem instanceof GroupItem gItem2 && gItem2.getAllMembers().contains(item)) {
return true;
}
@@ -233,16 +233,16 @@ public class PersistenceManager implements ItemRegistryChangeListener, StateChan
// otherwise, go through the detailed definitions
Set<Item> items = new HashSet<>();
for (Object itemCfg : config.items()) {
if (itemCfg instanceof PersistenceItemConfig) {
String itemName = ((PersistenceItemConfig) itemCfg).getItem();
if (itemCfg instanceof PersistenceItemConfig persistenceItemConfig) {
String itemName = persistenceItemConfig.getItem();
try {
items.add(itemRegistry.getItem(itemName));
} catch (ItemNotFoundException e) {
logger.debug("Item '{}' does not exist.", itemName);
}
}
if (itemCfg instanceof PersistenceGroupConfig) {
String groupName = ((PersistenceGroupConfig) itemCfg).getGroup();
if (itemCfg instanceof PersistenceGroupConfig persistenceGroupConfig) {
String groupName = persistenceGroupConfig.getGroup();
try {
Item gItem = itemRegistry.getItem(groupName);
if (gItem instanceof GroupItem groupItem) {
@@ -160,7 +160,7 @@ public class JavaOSGiTest extends JavaTest {
private <T> ServiceReference<T> @Nullable [] getServices(final Class<T> clazz) {
try {
@SuppressWarnings("unchecked")
ServiceReference<T> serviceReferences[] = (ServiceReference<T>[]) bundleContext
ServiceReference<T>[] serviceReferences = (ServiceReference<T>[]) bundleContext
.getServiceReferences(clazz.getName(), null);
return serviceReferences;
} catch (InvalidSyntaxException e) {
@@ -77,32 +77,29 @@ public class ColorChannelHandler extends AbstractTransformingChannelHandler {
public Optional<State> toState(String string) {
State newState = UnDefType.UNDEF;
if (string.equals(channelConfig.onValue)) {
if (state instanceof HSBType) {
newState = new HSBType(((HSBType) state).getHue(), ((HSBType) state).getSaturation(),
PercentType.HUNDRED);
if (state instanceof HSBType hsbState) {
newState = new HSBType(hsbState.getHue(), hsbState.getSaturation(), PercentType.HUNDRED);
} else {
newState = HSBType.WHITE;
}
} else if (string.equals(channelConfig.offValue)) {
if (state instanceof HSBType) {
newState = new HSBType(((HSBType) state).getHue(), ((HSBType) state).getSaturation(), PercentType.ZERO);
if (state instanceof HSBType hsbState) {
newState = new HSBType(hsbState.getHue(), hsbState.getSaturation(), PercentType.ZERO);
} else {
newState = HSBType.BLACK;
}
} else if (string.equals(channelConfig.increaseValue) && state instanceof HSBType) {
BigDecimal newBrightness = ((HSBType) state).getBrightness().toBigDecimal().add(channelConfig.step);
} else if (string.equals(channelConfig.increaseValue) && state instanceof HSBType hsbState) {
BigDecimal newBrightness = hsbState.getBrightness().toBigDecimal().add(channelConfig.step);
if (HUNDRED.compareTo(newBrightness) < 0) {
newBrightness = HUNDRED;
}
newState = new HSBType(((HSBType) state).getHue(), ((HSBType) state).getSaturation(),
new PercentType(newBrightness));
} else if (string.equals(channelConfig.decreaseValue) && state instanceof HSBType) {
BigDecimal newBrightness = ((HSBType) state).getBrightness().toBigDecimal().subtract(channelConfig.step);
newState = new HSBType(hsbState.getHue(), hsbState.getSaturation(), new PercentType(newBrightness));
} else if (string.equals(channelConfig.decreaseValue) && state instanceof HSBType hsbState) {
BigDecimal newBrightness = hsbState.getBrightness().toBigDecimal().subtract(channelConfig.step);
if (BigDecimal.ZERO.compareTo(newBrightness) > 0) {
newBrightness = BigDecimal.ZERO;
}
newState = new HSBType(((HSBType) state).getHue(), ((HSBType) state).getSaturation(),
new PercentType(newBrightness));
newState = new HSBType(hsbState.getHue(), hsbState.getSaturation(), new PercentType(newBrightness));
} else {
Matcher matcher = TRIPLE_MATCHER.matcher(string);
if (matcher.matches()) {
@@ -56,8 +56,8 @@ public class DimmerChannelHandler extends AbstractTransformingChannelHandler {
return string;
}
if (command instanceof PercentType) {
return ((PercentType) command).toString();
if (command instanceof PercentType percentCommand) {
return percentCommand.toString();
}
throw new IllegalArgumentException("Command type '" + command.toString() + "' not supported");
@@ -71,14 +71,14 @@ public class DimmerChannelHandler extends AbstractTransformingChannelHandler {
newState = PercentType.HUNDRED;
} else if (string.equals(channelConfig.offValue)) {
newState = PercentType.ZERO;
} else if (string.equals(channelConfig.increaseValue) && state instanceof PercentType) {
BigDecimal newBrightness = ((PercentType) state).toBigDecimal().add(channelConfig.step);
} else if (string.equals(channelConfig.increaseValue) && state instanceof PercentType percentState) {
BigDecimal newBrightness = percentState.toBigDecimal().add(channelConfig.step);
if (HUNDRED.compareTo(newBrightness) < 0) {
newBrightness = HUNDRED;
}
newState = new PercentType(newBrightness);
} else if (string.equals(channelConfig.decreaseValue) && state instanceof PercentType) {
BigDecimal newBrightness = ((PercentType) state).toBigDecimal().subtract(channelConfig.step);
} else if (string.equals(channelConfig.decreaseValue) && state instanceof PercentType percentState) {
BigDecimal newBrightness = percentState.toBigDecimal().subtract(channelConfig.step);
if (BigDecimal.ZERO.compareTo(newBrightness) > 0) {
newBrightness = BigDecimal.ZERO;
}
@@ -51,7 +51,7 @@ public class RollershutterChannelHandler extends AbstractTransformingChannelHand
return string;
}
if (command instanceof PercentType) {
if (command instanceof PercentType percentCommand) {
final String downValue = channelConfig.downValue;
final String upValue = channelConfig.upValue;
if (command.equals(PercentType.HUNDRED) && downValue != null) {
@@ -59,7 +59,7 @@ public class RollershutterChannelHandler extends AbstractTransformingChannelHand
} else if (command.equals(PercentType.ZERO) && upValue != null) {
return upValue;
} else {
return ((PercentType) command).toString();
return percentCommand.toString();
}
}
@@ -1090,7 +1090,7 @@ public class ThingManagerImpl implements ReadyTracker, ThingManager, ThingTracke
}
// create a thing builder and apply the update instructions
ThingBuilder thingBuilder = thing instanceof Bridge ? BridgeBuilder.create((Bridge) thing)
ThingBuilder thingBuilder = thing instanceof Bridge bridge ? BridgeBuilder.create(bridge)
: ThingBuilder.create(thing);
instructions.forEach(instruction -> instruction.perform(thing, thingBuilder));
int newThingTypeVersion = instructions.get(instructions.size() - 1).getThingTypeVersion();
@@ -39,7 +39,7 @@ public class TriggerEventStringProfileTest {
@Test
public void testEventStringItem() {
TriggerProfile profile = new TriggerEventStringProfile((callbackMock));
TriggerProfile profile = new TriggerEventStringProfile(callbackMock);
profile.onTriggerFromHandler(CommonTriggerEvents.PRESSED);
verify(callbackMock, times(1)).sendCommand(eq(new StringType(CommonTriggerEvents.PRESSED)));
}
@@ -165,8 +165,8 @@ public class DialogProcessor implements KSListener, STTListener {
return;
}
try {
if (ksService instanceof KSEdgeService) {
((KSEdgeService) ksService).spot(this);
if (ksService instanceof KSEdgeService service) {
service.spot(this);
} else {
AudioStream stream = dialogContext.source().getInputStream(fmt);
streamKS = stream;
@@ -309,8 +309,7 @@ public class DialogProcessor implements KSListener, STTListener {
processing = value;
String item = dialogContext.listeningItem();
if (item != null && ItemUtil.isValidItemName(item)) {
OnOffType command = (value) ? OnOffType.ON : OnOffType.OFF;
eventPublisher.post(ItemEventFactory.createCommandEvent(item, command));
eventPublisher.post(ItemEventFactory.createCommandEvent(item, OnOffType.from(value)));
}
}
@@ -105,7 +105,7 @@ public class NumberItem extends GenericItem implements MetadataAwareItem {
public void send(QuantityType<?> command) {
if (dimension == null) {
DecimalType strippedCommand = new DecimalType(command.toBigDecimal());
DecimalType strippedCommand = new DecimalType(command);
internalSend(strippedCommand);
} else if (command.getUnit().isCompatible(unit) || command.getUnit().inverse().isCompatible(unit)) {
internalSend(command);
@@ -142,8 +142,7 @@ public class NumberItem extends GenericItem implements MetadataAwareItem {
if (state instanceof QuantityType<?> quantityType) {
if (dimension == null) {
// QuantityType update to a NumberItem without unit, strip unit
DecimalType plainState = new DecimalType(quantityType.toBigDecimal());
return plainState;
return new DecimalType(quantityType);
} else {
// QuantityType update to a NumberItem with unit, convert to item unit (if possible)
Unit<?> stateUnit = quantityType.getUnit();
@@ -124,10 +124,12 @@ public final class CurrencyUnit implements Unit<Currency>, Comparable<Unit<Curre
this.name = name;
}
@Override
public String getName() {
return name;
}
@Override
public @Nullable String getSymbol() {
return symbol;
}
@@ -212,6 +214,7 @@ public final class CurrencyUnit implements Unit<Currency>, Comparable<Unit<Curre
"Could not get factor for converting " + this.getName() + " to " + that.getName());
}
@Override
public final Unit<?> multiply(@NonNullByDefault({}) Unit<?> that) {
return that.equals(ONE) ? this : ProductUnit.ofProduct(this, that);
}
@@ -108,7 +108,7 @@ public class DynamicThingUpdateOSGiTest extends JavaOSGiTest {
}
private ThingHandlerFactory createThingHandlerFactory() {
ThingHandlerFactory thingHandlerFactory = new BaseThingHandlerFactory() {
return new BaseThingHandlerFactory() {
@Override
public boolean supportsThingType(ThingTypeUID thingTypeUID) {
@@ -121,7 +121,6 @@ public class DynamicThingUpdateOSGiTest extends JavaOSGiTest {
return thingHandler;
}
};
return thingHandlerFactory;
}
@Test