Skip to content

Commit

Permalink
Java 17 features (A-G) (openhab#15516)
Browse files Browse the repository at this point in the history
- add missing @OverRide
- Java style array syntax
- remove redundant modifiers
- always move String constants to left side in comparisons
- simplify lambda expressions and return statements
- use replace instead of replaceAll w/o regex
- instanceof matching and multiline strings

Signed-off-by: Holger Friedrich <mail@holger-friedrich.de>
  • Loading branch information
holgerfriedrich authored Sep 5, 2023
1 parent a0dc5c0 commit cf10b3e
Show file tree
Hide file tree
Showing 486 changed files with 2,053 additions and 1,955 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

import static org.openhab.binding.adorne.internal.AdorneBindingConstants.*;

import java.util.Collections;
import java.util.Set;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
Expand Down Expand Up @@ -52,7 +52,7 @@ public class AdorneDiscoveryService extends AbstractDiscoveryService implements
*/
public AdorneDiscoveryService() {
// Passing false as last argument to super constructor turns off background discovery
super(Collections.singleton(new ThingTypeUID(BINDING_ID, "-")), DISCOVERY_TIMEOUT_SECONDS, false);
super(Set.of(new ThingTypeUID(BINDING_ID, "-")), DISCOVERY_TIMEOUT_SECONDS, false);

// We create the hub controller with default host and port. In the future we could let users create hubs
// manually with custom host and port settings and then perform discovery here for those hubs.
Expand All @@ -76,10 +76,9 @@ protected void startScan() {

// Future enhancement: Need a timeout for each future execution to recover from bugs in the hub controller, but
// Java8 doesn't yet offer that
adorneHubController.start().thenCompose(Void -> {
// We use the hub's MAC address as its unique identifier
return adorneHubController.getMACAddress();
}).thenCompose(macAddress -> {
adorneHubController.start().thenCompose(Void ->
// We use the hub's MAC address as its unique identifier
adorneHubController.getMACAddress()).thenCompose(macAddress -> {
String macAddressNoColon = macAddress.replace(':', '-'); // Colons are not allowed in ThingUIDs
bridgeUID[0] = new ThingUID(THING_TYPE_HUB, macAddressNoColon);
// We have fully discovered the hub
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,10 @@ public void handleCommand(ChannelUID channelUID, Command command) {
if (channelUID.getId().equals(CHANNEL_BRIGHTNESS)) {
if (command instanceof RefreshType) {
refreshBrightness();
} else if (command instanceof PercentType) {
} else if (command instanceof PercentType percentCommand) {
// Change the brightness through the hub controller
AdorneHubController adorneHubController = getAdorneHubController();
int level = ((PercentType) command).intValue();
int level = percentCommand.intValue();
if (level >= 1 && level <= 100) { // Ignore commands outside of the supported 1-100 range
adorneHubController.setBrightness(zoneId, level);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,8 @@ public void putMsg(String cmd) {
return null; // Eat empty messages
}
logger.debug("Received message {}", msg);
if (msg instanceof JsonObject) {
msgJsonObject = (JsonObject) msg;
if (msg instanceof JsonObject object) {
msgJsonObject = object;
}
return msgJsonObject;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public Map<WasteType, CollectionDate> getCollectionDates() throws IOException {

final Elements table = doc.select("table");

if (table.size() == 0) {
if (table.isEmpty()) {
logger.warn("No result table found.");
return Collections.emptyMap();
}
Expand All @@ -91,7 +91,7 @@ public Map<WasteType, CollectionDate> getCollectionDates() throws IOException {

while (rowIt.hasNext()) {
final Element currentRow = rowIt.next();
if (!currentRow.tagName().equals("tr")) {
if (!"tr".equals(currentRow.tagName())) {
continue;
}
// Skip header, empty and download button rows.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@

import static org.openhab.binding.airq.internal.AirqBindingConstants.THING_TYPE_AIRQ;

import java.util.Collections;
import java.util.Set;

import org.eclipse.jdt.annotation.NonNullByDefault;
Expand All @@ -38,7 +37,7 @@
@Component(configurationPid = "binding.airq", service = ThingHandlerFactory.class)
public class AirqHandlerFactory extends BaseThingHandlerFactory {

private static final Set<ThingTypeUID> SUPPORTED_THING_TYPES_UIDS = Collections.singleton(THING_TYPE_AIRQ);
private static final Set<ThingTypeUID> SUPPORTED_THING_TYPES_UIDS = Set.of(THING_TYPE_AIRQ);
private final HttpClientFactory httpClientFactory;

@Activate
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,12 @@ public enum Pollutant {
new ConcentrationRange(205, 404, TWO_HUNDRED), new ConcentrationRange(405, 504, THREE_HUNDRED),
new ConcentrationRange(505, 604, FOUR_HUNDRED));

public static enum SensitiveGroup {
public enum SensitiveGroup {
RESPIRATORY,
HEART,
ELDERLY,
CHILDREN,
ASTHMA;
ASTHMA
}

public final Set<SensitiveGroup> sensitiveGroups;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@
* @author Gaël L'hopital - Initial contribution
*/
public class ResponseRoot {
public static enum ResponseStatus {
public enum ResponseStatus {
@SerializedName("error")
ERROR,
@SerializedName("ok")
OK;
OK
}

protected ResponseStatus status = ResponseStatus.OK;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ public class SensitiveGroupConfiguration {

public @Nullable SensitiveGroup asSensitiveGroup() {
try {
SensitiveGroup value = SensitiveGroup.valueOf(group);
return value;
return SensitiveGroup.valueOf(group);
} catch (IllegalArgumentException e) {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import static org.openhab.binding.airquality.internal.AirQualityBindingConstants.*;
import static org.openhab.binding.airquality.internal.config.AirQualityConfiguration.LOCATION;

import java.util.Collections;
import java.util.Set;

import org.eclipse.jdt.annotation.NonNullByDefault;
Expand Down Expand Up @@ -43,7 +42,7 @@
@NonNullByDefault
public class AirQualityDiscoveryService extends AbstractDiscoveryService implements ThingHandlerService {
private static final int DISCOVER_TIMEOUT_SECONDS = 2;
private static final Set<ThingTypeUID> SUPPORTED_THING_TYPES_UIDS = Collections.singleton(THING_TYPE_STATION);
private static final Set<ThingTypeUID> SUPPORTED_THING_TYPES_UIDS = Set.of(THING_TYPE_STATION);

private final Logger logger = LoggerFactory.getLogger(AirQualityDiscoveryService.class);

Expand All @@ -59,9 +58,8 @@ public AirQualityDiscoveryService() {

@Override
public void setThingHandler(@Nullable ThingHandler handler) {
if (handler instanceof AirQualityBridgeHandler) {
final AirQualityBridgeHandler bridgeHandler = (AirQualityBridgeHandler) handler;
this.bridgeHandler = bridgeHandler;
if (handler instanceof AirQualityBridgeHandler bridgeHandlerInstance) {
this.bridgeHandler = bridgeHandlerInstance;
this.locationProvider = bridgeHandler.getLocationProvider();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@
*/
package org.openhab.binding.airvisualnode.internal;

import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;

import org.eclipse.jdt.annotation.NonNullByDefault;
Expand Down Expand Up @@ -51,12 +48,10 @@ public class AirVisualNodeBindingConstants {
.getUID().getId();

// List of all supported Thing UIDs
public static final Set<ThingTypeUID> SUPPORTED_THING_TYPES_UIDS = Collections
.unmodifiableSet(new HashSet<>(Arrays.asList(THING_TYPE_AVNODE)));
public static final Set<ThingTypeUID> SUPPORTED_THING_TYPES_UIDS = Set.of(THING_TYPE_AVNODE);

// List of all supported Channel ids
public static final Set<String> SUPPORTED_CHANNEL_IDS = Collections
.unmodifiableSet(new HashSet<>(Arrays.asList(CHANNEL_CO2, CHANNEL_HUMIDITY, CHANNEL_AQI_US, CHANNEL_PM_25,
CHANNEL_PM_10, CHANNEL_PM_01, CHANNEL_TEMP_CELSIUS, CHANNEL_BATTERY_LEVEL, CHANNEL_WIFI_STRENGTH,
CHANNEL_TIMESTAMP, CHANNEL_USED_MEMORY)));
public static final Set<String> SUPPORTED_CHANNEL_IDS = Set.of(CHANNEL_CO2, CHANNEL_HUMIDITY, CHANNEL_AQI_US,
CHANNEL_PM_25, CHANNEL_PM_10, CHANNEL_PM_01, CHANNEL_TEMP_CELSIUS, CHANNEL_BATTERY_LEVEL,
CHANNEL_WIFI_STRENGTH, CHANNEL_TIMESTAMP, CHANNEL_USED_MEMORY);
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

import java.io.IOException;
import java.net.UnknownHostException;
import java.util.Collections;
import java.util.Set;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
import java.util.regex.Matcher;
Expand Down Expand Up @@ -55,7 +55,7 @@ public class AirVisualNodeDiscoveryService extends AbstractDiscoveryService {
private @Nullable ScheduledFuture<?> backgroundDiscoveryFuture;

public AirVisualNodeDiscoveryService() {
super(Collections.singleton(AirVisualNodeBindingConstants.THING_TYPE_AVNODE), 600, true);
super(Set.of(AirVisualNodeBindingConstants.THING_TYPE_AVNODE), 600, true);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ public Measurements(int co2Ppm, int humidityRH, int pm25AQICN, int pm25AQIUS, fl
this.vocPpb = vocPpb;
}

@Override
public int getCo2Ppm() {
return co2Ppm;
}
Expand All @@ -57,6 +58,7 @@ public void setCo2Ppm(int co2Ppm) {
this.co2Ppm = co2Ppm;
}

@Override
public int getHumidityRH() {
return humidityRH;
}
Expand All @@ -65,6 +67,7 @@ public void setHumidityRH(int humidityRH) {
this.humidityRH = humidityRH;
}

@Override
public int getPm25AQICN() {
return pm25AQICN;
}
Expand All @@ -73,6 +76,7 @@ public void setPm25AQICN(int pm25AQICN) {
this.pm25AQICN = pm25AQICN;
}

@Override
public int getPm25AQIUS() {
return pm25AQIUS;
}
Expand All @@ -91,6 +95,7 @@ public float getPm10Ugm3() {
return 0;
}

@Override
public float getPm25Ugm3() {
return pm25Ugm3;
}
Expand All @@ -99,6 +104,7 @@ public void setPm25Ugm3(float pm25Ugm3) {
this.pm25Ugm3 = pm25Ugm3;
}

@Override
public float getTemperatureC() {
return temperatureC;
}
Expand All @@ -107,6 +113,7 @@ public void setTemperatureC(float temperatureC) {
this.temperatureC = temperatureC;
}

@Override
public float getTemperatureF() {
return temperatureF;
}
Expand All @@ -115,6 +122,7 @@ public void setTemperatureF(float temperatureF) {
this.temperatureF = temperatureF;
}

@Override
public int getVocPpb() {
return vocPpb;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public NodeData(DateAndTime dateAndTime, Measurements measurements, String seria
this.status = status;
}

@Override
public DateAndTime getDateAndTime() {
return dateAndTime;
}
Expand All @@ -48,6 +49,7 @@ public void setDateAndTime(DateAndTime dateAndTime) {
this.dateAndTime = dateAndTime;
}

@Override
public MeasurementsInterface getMeasurements() {
return measurements;
}
Expand All @@ -56,6 +58,7 @@ public void setMeasurements(Measurements measurements) {
this.measurements = measurements;
}

@Override
public String getSerialNumber() {
return serialNumber;
}
Expand All @@ -64,6 +67,7 @@ public void setSerialNumber(String serialNumber) {
this.serialNumber = serialNumber;
}

@Override
public Settings getSettings() {
return settings;
}
Expand All @@ -72,6 +76,7 @@ public void setSettings(Settings settings) {
this.settings = settings;
}

@Override
public Status getStatus() {
return status;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ public Measurements(int co2Ppm, int humidityRH, int pm25AQICN, int pm25AQIUS, fl
this.vocPpb = vocPpb;
}

@Override
public int getCo2Ppm() {
return co2Ppm;
}
Expand All @@ -74,6 +75,7 @@ public void setCo2Ppm(int co2Ppm) {
this.co2Ppm = co2Ppm;
}

@Override
public int getHumidityRH() {
return humidityRH;
}
Expand All @@ -82,6 +84,7 @@ public void setHumidityRH(int humidityRH) {
this.humidityRH = humidityRH;
}

@Override
public int getPm25AQICN() {
return pm25AQICN;
}
Expand All @@ -90,6 +93,7 @@ public void setPm25AQICN(int pm25AQICN) {
this.pm25AQICN = pm25AQICN;
}

@Override
public int getPm25AQIUS() {
return pm25AQIUS;
}
Expand All @@ -98,6 +102,7 @@ public void setPm25AQIUS(int pm25AQIUS) {
this.pm25AQIUS = pm25AQIUS;
}

@Override
public float getPm01Ugm3() {
return pm01Ugm3;
}
Expand All @@ -106,6 +111,7 @@ public void setPm01Ugm3(float pm01Ugm3) {
this.pm01Ugm3 = pm01Ugm3;
}

@Override
public float getPm10Ugm3() {
return pm10Ugm3;
}
Expand All @@ -114,6 +120,7 @@ public void setPm10Ugm3(float pm10Ugm3) {
this.pm10Ugm3 = pm10Ugm3;
}

@Override
public float getPm25Ugm3() {
return pm25Ugm3;
}
Expand All @@ -122,6 +129,7 @@ public void setPm25Ugm3(float pm25Ugm3) {
this.pm25Ugm3 = pm25Ugm3;
}

@Override
public float getTemperatureC() {
return temperatureC;
}
Expand All @@ -130,6 +138,7 @@ public void setTemperatureC(float temperatureC) {
this.temperatureC = temperatureC;
}

@Override
public float getTemperatureF() {
return temperatureF;
}
Expand All @@ -138,6 +147,7 @@ public void setTemperatureF(float temperatureF) {
this.temperatureF = temperatureF;
}

@Override
public int getVocPpb() {
return vocPpb;
}
Expand Down
Loading

0 comments on commit cf10b3e

Please sign in to comment.