Skip to content

Commit

Permalink
[mqtt] Fix most SAT findings (openhab#12492)
Browse files Browse the repository at this point in the history
Signed-off-by: Wouter Born <github@maindrain.net>
Signed-off-by: Andras Uhrin <andras.uhrin@gmail.com>
  • Loading branch information
wborn authored and andrasU committed Nov 12, 2022
1 parent f5025bb commit 209aabc
Show file tree
Hide file tree
Showing 60 changed files with 491 additions and 482 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import java.util.concurrent.TimeoutException;
import java.util.concurrent.atomic.AtomicBoolean;

import org.eclipse.jdt.annotation.NonNull;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.openhab.binding.mqtt.generic.values.OnOffValue;
Expand Down Expand Up @@ -327,8 +326,8 @@ public void postChannelCommand(ChannelUID channelUID, Command value) {
}

@Override
public void removeAvailabilityTopic(@NonNull String availability_topic) {
availabilityStates.computeIfPresent(availability_topic, (topic, state) -> {
public void removeAvailabilityTopic(String availabilityTopic) {
availabilityStates.computeIfPresent(availabilityTopic, (topic, state) -> {
if (connection != null && state != null) {
state.stop();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,14 @@
*/
package org.openhab.binding.mqtt.generic.mapping;

import org.eclipse.jdt.annotation.NonNullByDefault;

/**
* Color modes supported by the binding.
*
* @author Aitor Iturrioz - Initial contribution
*/
@NonNullByDefault
public enum ColorMode {
HSB,
RGB,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,31 +81,31 @@ static Object numberConvert(Object value, Class<?> type) throws IllegalArgumentE
String typeName = type.getSimpleName();
if (value instanceof BigDecimal && !type.equals(BigDecimal.class)) {
BigDecimal bdValue = (BigDecimal) value;
if (type.equals(Float.class) || typeName.equals("float")) {
if (type.equals(Float.class) || "float".equals(typeName)) {
result = bdValue.floatValue();
} else if (type.equals(Double.class) || typeName.equals("double")) {
} else if (type.equals(Double.class) || "double".equals(typeName)) {
result = bdValue.doubleValue();
} else if (type.equals(Long.class) || typeName.equals("long")) {
} else if (type.equals(Long.class) || "long".equals(typeName)) {
result = bdValue.longValue();
} else if (type.equals(Integer.class) || typeName.equals("int")) {
} else if (type.equals(Integer.class) || "int".equals(typeName)) {
result = bdValue.intValue();
}
} else
// Handle the conversion case of String to Float,Double,Long,Integer,BigDecimal and the respective
// primitive types
if (value instanceof String && !type.equals(String.class)) {
String bdValue = (String) value;
if (type.equals(Float.class) || typeName.equals("float")) {
if (type.equals(Float.class) || "float".equals(typeName)) {
result = Float.valueOf(bdValue);
} else if (type.equals(Double.class) || typeName.equals("double")) {
} else if (type.equals(Double.class) || "double".equals(typeName)) {
result = Double.valueOf(bdValue);
} else if (type.equals(Long.class) || typeName.equals("long")) {
} else if (type.equals(Long.class) || "long".equals(typeName)) {
result = Long.valueOf(bdValue);
} else if (type.equals(BigDecimal.class)) {
result = new BigDecimal(bdValue);
} else if (type.equals(Integer.class) || typeName.equals("int")) {
} else if (type.equals(Integer.class) || "int".equals(typeName)) {
result = Integer.valueOf(bdValue);
} else if (type.equals(Boolean.class) || typeName.equals("boolean")) {
} else if (type.equals(Boolean.class) || "boolean".equals(typeName)) {
result = Boolean.valueOf(bdValue);
} else if (type.isEnum()) {
@SuppressWarnings({ "rawtypes", "unchecked" })
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,24 @@
import java.util.function.Supplier;
import java.util.stream.Collector;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;

/**
* Collector to combine a stream of CompletableFutures.
*
* @author Jochen Klein - Initial contribution
*
*/
@NonNullByDefault
public class FutureCollector {

public static <X> Collector<CompletableFuture<X>, Set<CompletableFuture<X>>, CompletableFuture<Void>> allOf() {
return Collector.<CompletableFuture<X>, Set<CompletableFuture<X>>, CompletableFuture<Void>> of(
public static <X> Collector<CompletableFuture<X>, Set<CompletableFuture<X>>, CompletableFuture<@Nullable Void>> allOf() {
return Collector.<CompletableFuture<X>, Set<CompletableFuture<X>>, CompletableFuture<@Nullable Void>> of(
(Supplier<Set<CompletableFuture<X>>>) HashSet::new, Set::add, (left, right) -> {
left.addAll(right);
return left;
}, a -> {
return CompletableFuture.allOf(a.toArray(new CompletableFuture[a.size()]));
}, Collector.Characteristics.UNORDERED);
}, a -> CompletableFuture.allOf(a.toArray(new CompletableFuture[a.size()])),
Collector.Characteristics.UNORDERED);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,8 @@
package org.openhab.binding.mqtt.generic.values;

import java.math.BigDecimal;
import java.util.List;
import java.util.Locale;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import javax.ws.rs.NotSupportedException;

Expand Down Expand Up @@ -63,8 +62,7 @@ public class ColorValue extends Value {
* @param onBrightness When receiving a ON command, the brightness percentage is set to this value
*/
public ColorValue(ColorMode colorMode, @Nullable String onValue, @Nullable String offValue, int onBrightness) {
super(CoreItemFactory.COLOR,
Stream.of(OnOffType.class, PercentType.class, StringType.class).collect(Collectors.toList()));
super(CoreItemFactory.COLOR, List.of(OnOffType.class, PercentType.class, StringType.class));

if (onBrightness > 100) {
throw new IllegalArgumentException("Brightness parameter must be <= 100");
Expand Down Expand Up @@ -112,8 +110,8 @@ public void update(Command command) throws IllegalArgumentException {
Integer.parseInt(split[2]));
break;
case XYY:
HSBType temp_state = HSBType.fromXY(Float.parseFloat(split[0]), Float.parseFloat(split[1]));
state = new HSBType(temp_state.getHue(), temp_state.getSaturation(), new PercentType(split[2]));
HSBType tempState = HSBType.fromXY(Float.parseFloat(split[0]), Float.parseFloat(split[1]));
state = new HSBType(tempState.getHue(), tempState.getSaturation(), new PercentType(split[2]));
break;
default:
logger.warn("Non supported color mode");
Expand Down Expand Up @@ -146,21 +144,21 @@ public String getMQTTpublishValue(@Nullable String pattern) {
}
}

HSBType hsb_state = (HSBType) state;
HSBType hsbState = (HSBType) state;

switch (this.colorMode) {
case HSB:
return String.format(formatPattern, hsb_state.getHue().intValue(), hsb_state.getSaturation().intValue(),
hsb_state.getBrightness().intValue());
return String.format(formatPattern, hsbState.getHue().intValue(), hsbState.getSaturation().intValue(),
hsbState.getBrightness().intValue());
case RGB:
PercentType[] rgb = hsb_state.toRGB();
PercentType[] rgb = hsbState.toRGB();
return String.format(formatPattern, rgb[0].toBigDecimal().multiply(factor).intValue(),
rgb[1].toBigDecimal().multiply(factor).intValue(),
rgb[2].toBigDecimal().multiply(factor).intValue());
case XYY:
PercentType[] xyY = hsb_state.toXY();
PercentType[] xyY = hsbState.toXY();
return String.format(Locale.ROOT, formatPattern, xyY[0].floatValue() / 100.0f,
xyY[1].floatValue() / 100.0f, hsb_state.getBrightness().floatValue());
xyY[1].floatValue() / 100.0f, hsbState.getBrightness().floatValue());
default:
throw new NotSupportedException(String.format("Non supported color mode: {}", this.colorMode));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@
package org.openhab.binding.mqtt.generic.values;

import java.time.format.DateTimeFormatter;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import java.util.List;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
Expand All @@ -32,7 +31,7 @@
@NonNullByDefault
public class DateTimeValue extends Value {
public DateTimeValue() {
super(CoreItemFactory.DATETIME, Stream.of(DateTimeType.class, StringType.class).collect(Collectors.toList()));
super(CoreItemFactory.DATETIME, List.of(DateTimeType.class, StringType.class));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
*/
package org.openhab.binding.mqtt.generic.values;

import java.util.Collections;
import java.util.List;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.openhab.core.library.CoreItemFactory;
Expand All @@ -26,7 +26,7 @@
@NonNullByDefault
public class ImageValue extends Value {
public ImageValue() {
super(CoreItemFactory.IMAGE, Collections.emptyList());
super(CoreItemFactory.IMAGE, List.of());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,9 @@
package org.openhab.binding.mqtt.generic.values;

import java.math.BigDecimal;
import java.util.List;
import java.util.Locale;
import java.util.stream.Collectors;
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.library.CoreItemFactory;
Expand All @@ -33,11 +31,11 @@
@NonNullByDefault
public class LocationValue extends Value {
public LocationValue() {
super(CoreItemFactory.LOCATION, Stream.of(PointType.class, StringType.class).collect(Collectors.toList()));
super(CoreItemFactory.LOCATION, List.of(PointType.class, StringType.class));
}

@Override
public @NonNull String getMQTTpublishValue(@Nullable String pattern) {
public String getMQTTpublishValue(@Nullable String pattern) {
String formatPattern = pattern;
PointType point = ((PointType) state);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@
package org.openhab.binding.mqtt.generic.values;

import java.math.BigDecimal;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import java.util.List;

import javax.measure.Unit;

Expand Down Expand Up @@ -53,8 +52,7 @@ public class NumberValue extends Value {

public NumberValue(@Nullable BigDecimal min, @Nullable BigDecimal max, @Nullable BigDecimal step,
@Nullable Unit<?> unit) {
super(CoreItemFactory.NUMBER, Stream.of(QuantityType.class, IncreaseDecreaseType.class, UpDownType.class)
.collect(Collectors.toList()));
super(CoreItemFactory.NUMBER, List.of(QuantityType.class, IncreaseDecreaseType.class, UpDownType.class));
this.min = min;
this.max = max;
this.step = step == null ? BigDecimal.ONE : step;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@
*/
package org.openhab.binding.mqtt.generic.values;

import java.util.stream.Collectors;
import java.util.stream.Stream;
import java.util.List;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
Expand Down Expand Up @@ -65,7 +64,7 @@ public OnOffValue(@Nullable String onValue, @Nullable String offValue) {
*/
public OnOffValue(@Nullable String onState, @Nullable String offState, @Nullable String onCommand,
@Nullable String offCommand) {
super(CoreItemFactory.SWITCH, Stream.of(OnOffType.class, StringType.class).collect(Collectors.toList()));
super(CoreItemFactory.SWITCH, List.of(OnOffType.class, StringType.class));
this.onState = onState == null ? OnOffType.ON.name() : onState;
this.offState = offState == null ? OnOffType.OFF.name() : offState;
this.onCommand = onCommand == null ? OnOffType.ON.name() : onCommand;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@
*/
package org.openhab.binding.mqtt.generic.values;

import java.util.stream.Collectors;
import java.util.stream.Stream;
import java.util.List;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
Expand All @@ -36,7 +35,7 @@ public class OpenCloseValue extends Value {
* Creates a contact Open/Close type.
*/
public OpenCloseValue() {
super(CoreItemFactory.CONTACT, Stream.of(OpenClosedType.class, StringType.class).collect(Collectors.toList()));
super(CoreItemFactory.CONTACT, List.of(OpenClosedType.class, StringType.class));
this.openString = OpenClosedType.OPEN.name();
this.closeString = OpenClosedType.CLOSED.name();
}
Expand All @@ -48,7 +47,7 @@ public OpenCloseValue() {
* @param closeValue The OFF value string. This will be compared to MQTT messages.
*/
public OpenCloseValue(@Nullable String openValue, @Nullable String closeValue) {
super(CoreItemFactory.CONTACT, Stream.of(OpenClosedType.class, StringType.class).collect(Collectors.toList()));
super(CoreItemFactory.CONTACT, List.of(OpenClosedType.class, StringType.class));
this.openString = openValue == null ? OpenClosedType.OPEN.name() : openValue;
this.closeString = closeValue == null ? OpenClosedType.CLOSED.name() : closeValue;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@

import java.math.BigDecimal;
import java.math.MathContext;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import java.util.List;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
Expand Down Expand Up @@ -58,8 +57,8 @@ public class PercentageValue extends Value {

public PercentageValue(@Nullable BigDecimal min, @Nullable BigDecimal max, @Nullable BigDecimal step,
@Nullable String onValue, @Nullable String offValue) {
super(CoreItemFactory.DIMMER, Stream.of(DecimalType.class, QuantityType.class, IncreaseDecreaseType.class,
OnOffType.class, UpDownType.class, StringType.class).collect(Collectors.toList()));
super(CoreItemFactory.DIMMER, List.of(DecimalType.class, QuantityType.class, IncreaseDecreaseType.class,
OnOffType.class, UpDownType.class, StringType.class));
this.onValue = onValue;
this.offValue = offValue;
this.min = min == null ? BigDecimal.ZERO : min;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@
*/
package org.openhab.binding.mqtt.generic.values;

import java.util.stream.Collectors;
import java.util.stream.Stream;
import java.util.List;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
Expand Down Expand Up @@ -51,8 +50,7 @@ public class RollershutterValue extends Value {
*/
public RollershutterValue(@Nullable String upString, @Nullable String downString, @Nullable String stopString) {
super(CoreItemFactory.ROLLERSHUTTER,
Stream.of(UpDownType.class, StopMoveType.class, PercentType.class, StringType.class)
.collect(Collectors.toList()));
List.of(UpDownType.class, StopMoveType.class, PercentType.class, StringType.class));
this.upString = upString;
this.downString = downString;
this.stopString = stopString == null ? StopMoveType.STOP.name() : stopString;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

import static java.util.function.Predicate.not;

import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;
Expand Down Expand Up @@ -45,7 +45,7 @@ public class TextValue extends Value {
* will be allowed.
*/
public TextValue(String[] states) {
super(CoreItemFactory.STRING, Collections.singletonList(StringType.class));
super(CoreItemFactory.STRING, List.of(StringType.class));
Set<String> s = Stream.of(states).filter(not(String::isBlank)).collect(Collectors.toSet());
if (!s.isEmpty()) {
this.states = s;
Expand All @@ -55,7 +55,7 @@ public TextValue(String[] states) {
}

public TextValue() {
super(CoreItemFactory.STRING, Collections.singletonList(StringType.class));
super(CoreItemFactory.STRING, List.of(StringType.class));
this.states = null;
}

Expand Down
Loading

0 comments on commit 209aabc

Please sign in to comment.