Skip to content

Commit

Permalink
Rework more commons-lang usages (openhab#10314)
Browse files Browse the repository at this point in the history
* Reworks many commons-lang usages to use standard Java
* Updates all remaining commons.lang imports to commons.lang3

Related to openhab#7722

Signed-off-by: Wouter Born <github@maindrain.net>
  • Loading branch information
wborn authored and thinkingstone committed Nov 7, 2021
1 parent 08cd2f7 commit ae72c32
Show file tree
Hide file tree
Showing 257 changed files with 907 additions and 1,126 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;

import org.apache.commons.lang.StringUtils;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.openhab.binding.ambientweather.internal.config.BridgeConfig;
Expand Down Expand Up @@ -128,7 +127,7 @@ public void initialize() {
*/
private boolean hasApplicationKey() {
String configApplicationKey = getConfigAs(BridgeConfig.class).applicationKey;
if (StringUtils.isEmpty(configApplicationKey)) {
if (configApplicationKey == null || configApplicationKey.isEmpty()) {
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, "Missing application key");
return false;
}
Expand All @@ -141,7 +140,7 @@ private boolean hasApplicationKey() {
*/
private boolean hasApiKey() {
String configApiKey = getConfigAs(BridgeConfig.class).apiKey;
if (StringUtils.isEmpty(configApiKey)) {
if (configApiKey == null || configApiKey.isEmpty()) {
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, "Missing API key");
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import org.apache.commons.lang.StringUtils;
import org.json.JSONObject;
import org.openhab.binding.ambientweather.internal.model.DeviceJson;
import org.openhab.binding.ambientweather.internal.model.EventDataGenericJson;
Expand Down Expand Up @@ -318,8 +317,9 @@ private synchronized void handleData(String jsonData) {
logger.debug("Listener: Data: {}", jsonData);
try {
EventDataGenericJson data = gson.fromJson(jsonData, EventDataGenericJson.class);
if (StringUtils.isNotEmpty(data.macAddress)) {
sendWeatherDataToHandler(data.macAddress, jsonData);
String macAddress = data == null ? null : data.macAddress;
if (macAddress != null && !macAddress.isEmpty()) {
sendWeatherDataToHandler(macAddress, jsonData);
}
} catch (JsonSyntaxException e) {
logger.info("Listener: Exception parsing subscribed event: {}", e.getMessage());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@

import javax.measure.quantity.Angle;

import org.apache.commons.lang.time.DateFormatUtils;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.openhab.binding.astro.internal.action.AstroActions;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import static java.util.Calendar.SECOND;
import static java.util.Collections.singletonList;
import static java.util.stream.Collectors.toList;
import static org.apache.commons.lang.time.DateUtils.truncatedEquals;
import static org.apache.commons.lang3.time.DateUtils.truncatedEquals;
import static org.openhab.binding.astro.internal.AstroBindingConstants.*;
import static org.openhab.binding.astro.internal.util.DateTimeUtils.*;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import java.util.Date;
import java.util.regex.Pattern;

import org.apache.commons.lang.time.DateUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.openhab.binding.astro.internal.config.AstroChannelConfig;
import org.openhab.binding.astro.internal.model.Range;
import org.slf4j.Logger;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;

import org.apache.commons.lang.StringUtils;
import org.eclipse.jetty.client.HttpClient;
import org.eclipse.jetty.client.api.ContentResponse;
import org.eclipse.jetty.client.api.Request;
Expand Down Expand Up @@ -288,17 +287,17 @@ private void configure() {
String username = configuration.user;
String password = configuration.password;

if (StringUtils.isBlank(username)) {
if (username == null || username.isBlank()) {
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, "username must not be empty");
return;
}

if (StringUtils.isBlank(password)) {
if (password == null || password.isBlank()) {
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, "password must not be empty");
return;
}

if (StringUtils.isBlank(host)) {
if (host == null || host.isBlank()) {
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, "hostname must not be empty");
return;
}
Expand Down Expand Up @@ -443,7 +442,7 @@ private void pollAutelisController() {
}
}

if (StringUtils.isEmpty((value))) {
if (value == null || value.isEmpty()) {
continue;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@
*/
package org.openhab.binding.bigassfan.internal;

import org.apache.commons.lang.StringUtils;

/**
* The {@link BigAssFanConfig} is responsible for storing the BigAssFan thing configuration.
*
Expand Down Expand Up @@ -60,13 +58,13 @@ public void setMacAddress(String macAddress) {
}

public boolean isValid() {
if (StringUtils.isBlank(label)) {
if (label == null || label.isBlank()) {
return false;
}
if (StringUtils.isBlank(ipAddress)) {
if (ipAddress == null || ipAddress.isBlank()) {
return false;
}
if (StringUtils.isBlank(macAddress)) {
if (macAddress == null || macAddress.isBlank()) {
return false;
}
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang.StringUtils;
import org.openhab.binding.bigassfan.internal.BigAssFanConfig;
import org.openhab.binding.bigassfan.internal.utils.BigAssFanConverter;
import org.openhab.core.common.ThreadPoolManager;
Expand Down Expand Up @@ -668,7 +667,7 @@ private String readMessage() {
}

private void processMessage(String incomingMessage) {
if (StringUtils.isEmpty(incomingMessage)) {
if (incomingMessage == null || incomingMessage.isEmpty()) {
return;
}

Expand Down Expand Up @@ -742,11 +741,11 @@ private void processMessage(String incomingMessage) {

private boolean isMe(String idFromDevice) {
// Check match on MAC address
if (StringUtils.equalsIgnoreCase(idFromDevice, macAddress)) {
if (macAddress.equalsIgnoreCase(idFromDevice)) {
return true;
}
// Didn't match MAC address, check match for label
if (StringUtils.equalsIgnoreCase(idFromDevice, label)) {
if (label.equalsIgnoreCase(idFromDevice)) {
return true;
}
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang3.ArrayUtils;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@

import javax.measure.quantity.Power;

import org.apache.commons.lang.StringUtils;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.openhab.binding.bluetooth.BluetoothDevice.ConnectionState;
Expand Down Expand Up @@ -193,7 +192,7 @@ protected void updateAdapterLocation() {
if (device != null) {
BluetoothAdapter adapter = device.getAdapter();
String location = adapter.getLocation();
if (location != null || StringUtils.isBlank(location)) {
if (location != null && !location.isBlank()) {
updateState(BluetoothBindingConstants.CHANNEL_TYPE_ADAPTER_LOCATION, new StringType(location));
} else {
updateState(BluetoothBindingConstants.CHANNEL_TYPE_ADAPTER_LOCATION, UnDefType.NULL);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
*/
package org.openhab.binding.bluetooth;

import org.apache.commons.lang.RandomStringUtils;
import org.apache.commons.lang3.RandomStringUtils;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.openhab.core.thing.ThingUID;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.BiConsumer;

import org.apache.commons.lang.RandomStringUtils;
import org.apache.commons.lang3.RandomStringUtils;
import org.eclipse.jdt.annotation.NonNull;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang.StringEscapeUtils;
import org.apache.commons.lang3.StringEscapeUtils;
import org.openhab.core.types.StateOption;

import com.google.gson.annotations.Expose;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,15 @@
*/
package org.openhab.binding.bsblan.internal.api;

import static org.openhab.binding.bsblan.internal.BsbLanBindingConstants.*;
import static org.openhab.binding.bsblan.internal.BsbLanBindingConstants.API_TIMEOUT;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.util.HashSet;
import java.util.Set;
import java.util.stream.Collectors;

import org.apache.commons.lang.StringUtils;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.openhab.binding.bsblan.internal.api.dto.BsbLanApiContentDTO;
Expand Down Expand Up @@ -50,17 +49,14 @@ public BsbLanApiCaller(BsbLanBridgeConfiguration config) {
}

public @Nullable BsbLanApiParameterQueryResponseDTO queryParameter(Integer parameterId) {
Set<Integer> parameters = new HashSet<>();

parameters.add(parameterId);
return queryParameters(parameters);
return queryParameters(Set.of(parameterId));
}

public @Nullable BsbLanApiParameterQueryResponseDTO queryParameters(Set<Integer> parameterIds) {
// note: make the request even if parameterIds is empty as
// thing OFFLINE/ONLINE detection relies on a response

String apiPath = String.format("/JQ=%s", StringUtils.join(parameterIds, ","));
String apiPath = String.format("/JQ=%s",
parameterIds.stream().map(String::valueOf).collect(Collectors.joining(",")));
return makeRestCall(BsbLanApiParameterQueryResponseDTO.class, "GET", apiPath, null);
}

Expand Down Expand Up @@ -96,21 +92,21 @@ public boolean setParameter(Integer parameterId, String value, BsbLanApiParamete
}

private String createApiBaseUrl() {
final String host = StringUtils.trimToEmpty(bridgeConfig.host);
final String username = StringUtils.trimToEmpty(bridgeConfig.username);
final String password = StringUtils.trimToEmpty(bridgeConfig.password);
final String passkey = StringUtils.trimToEmpty(bridgeConfig.passkey);
final String host = bridgeConfig.host == null ? "" : bridgeConfig.host.trim();
final String username = bridgeConfig.username == null ? "" : bridgeConfig.username.trim();
final String password = bridgeConfig.password == null ? "" : bridgeConfig.password.trim();
final String passkey = bridgeConfig.passkey == null ? "" : bridgeConfig.passkey.trim();

StringBuilder url = new StringBuilder();
url.append("http://");
if (StringUtils.isNotBlank(username) && StringUtils.isNotBlank(password)) {
if (!username.isBlank() && !password.isBlank()) {
url.append(username).append(":").append(password).append("@");
}
url.append(host);
if (bridgeConfig.port != 80) {
url.append(":").append(bridgeConfig.port);
}
if (StringUtils.isNotBlank(passkey)) {
if (!passkey.isBlank()) {
url.append("/").append(passkey);
}
return url.toString();
Expand All @@ -134,7 +130,7 @@ private String createApiBaseUrl() {
if (request != null) {
String content = BsbLanApiContentConverter.toJson(request);
logger.trace("api request content: '{}'", content);
if (StringUtils.isNotBlank(content)) {
if (!content.isBlank()) {
contentStream = new ByteArrayInputStream(content.getBytes(Charset.forName("UTF-8")));
contentType = "application/json";
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,8 @@
import java.util.Set;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
import java.util.stream.*;
import java.util.stream.Collectors;

import org.apache.commons.lang.StringUtils;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.openhab.binding.bsblan.internal.api.BsbLanApiCaller;
Expand Down Expand Up @@ -78,7 +77,8 @@ public void initialize() {
bridgeConfig = getConfigAs(BsbLanBridgeConfiguration.class);

// validate 'host' configuration
if (StringUtils.isBlank(bridgeConfig.host)) {
String host = bridgeConfig.host;
if (host == null || host.isBlank()) {
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR,
"Parameter 'host' is mandatory and must be configured");
return;
Expand Down Expand Up @@ -128,8 +128,10 @@ private void doRefresh() {
BsbLanApiCaller apiCaller = new BsbLanApiCaller(bridgeConfig);

// refresh all parameters
Set<Integer> parameterIds = things.stream().filter(thing -> thing instanceof BsbLanParameterHandler)
.map(thing -> (BsbLanParameterHandler) thing).map(thing -> thing.getParameterId())
Set<Integer> parameterIds = things.stream() //
.filter(thing -> thing instanceof BsbLanParameterHandler) //
.map(thing -> (BsbLanParameterHandler) thing) //
.map(thing -> thing.getParameterId()) //
.collect(Collectors.toSet());

cachedParameterQueryResponse = apiCaller.queryParameters(parameterIds);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@

import static org.openhab.binding.bsblan.internal.BsbLanBindingConstants.*;

import org.apache.commons.lang.StringEscapeUtils;
import org.apache.commons.lang3.StringEscapeUtils;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.openhab.binding.bsblan.internal.api.dto.BsbLanApiParameterDTO;
import org.openhab.binding.bsblan.internal.handler.BsbLanParameterHandler;
import org.openhab.core.library.types.DecimalType;
import org.openhab.core.library.types.OnOffType;
import org.openhab.core.library.types.StringType;
Expand Down Expand Up @@ -74,7 +75,7 @@ private static State getStateForDescriptionChannel(BsbLanApiParameterDTO paramet
}

private static State getStateForUnitChannel(BsbLanApiParameterDTO parameter) {
String value = StringEscapeUtils.unescapeHtml(parameter.unit);
String value = StringEscapeUtils.unescapeHtml4(parameter.unit);
return new StringType(value);
}

Expand Down Expand Up @@ -110,7 +111,7 @@ private static State getStateForSwitchValueChannel(BsbLanApiParameterDTO paramet

/**
* Converts a Command back to a value which is sent to the BSB-LAN device afterwards.
*
*
* @param channelId
* @param command
* @return null if conversion fails or channel is readonly.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

import org.apache.commons.lang.StringUtils;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.openhab.binding.buienradar.internal.buienradarapi.BuienradarPredictionAPI;
import org.openhab.binding.buienradar.internal.buienradarapi.Prediction;
Expand Down Expand Up @@ -81,7 +80,7 @@ public void initialize() {
this.config = getConfigAs(BuienradarConfiguration.class);

boolean configValid = true;
if (StringUtils.trimToNull(config.location) == null) {
if (config.location == null || config.location.isBlank()) {
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR,
"@text/offline.conf-error-missing-location");
configValid = false;
Expand Down
Loading

0 comments on commit ae72c32

Please sign in to comment.