Use isEmpty instead of 0 comparisons (#3999)

`isEmpty()` expresses the intent more clearly and is therefore preferred.
Counting the number of elements can also be an expensive operation e.g. when using linked lists.

Signed-off-by: Wouter Born <github@maindrain.net>
This commit is contained in:
Wouter Born 2024-01-02 18:47:36 +01:00 committed by GitHub
parent a5316f920e
commit 89b67adbd7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 12 additions and 12 deletions

View File

@ -518,7 +518,7 @@ public class PersistenceResource implements RESTResource {
private Response deletePersistenceItemData(@Nullable String serviceId, String itemName, @Nullable String timeBegin,
@Nullable String timeEnd) {
// For deleting, we must specify a service id - don't use the default service
if (serviceId == null || serviceId.length() == 0) {
if (serviceId == null || serviceId.isEmpty()) {
logger.debug("Persistence service must be specified for delete operations.");
return JSONResponse.createErrorResponse(Status.BAD_REQUEST,
"Persistence service must be specified for delete operations.");
@ -598,7 +598,7 @@ public class PersistenceResource implements RESTResource {
}
ZonedDateTime dateTime = null;
if (time != null && time.length() != 0) {
if (time != null && !time.isEmpty()) {
dateTime = convertTime(time);
}
if (dateTime == null || dateTime.toEpochSecond() == 0) {

View File

@ -92,7 +92,7 @@ public final class WriteRequestJsonUtilities {
*/
public static Collection<ModbusWriteRequestBlueprint> fromJson(int unitId, String jsonString) {
JsonArray jsonArray = JsonParser.parseString(jsonString).getAsJsonArray();
if (jsonArray.size() == 0) {
if (jsonArray.isEmpty()) {
return new LinkedList<>();
}
Deque<ModbusWriteRequestBlueprint> writes = new LinkedList<>();
@ -173,7 +173,7 @@ public final class WriteRequestJsonUtilities {
}
// fall-through to WRITE_MULTIPLE_COILS
case WRITE_MULTIPLE_COILS:
if (valuesElem.size() == 0) {
if (valuesElem.isEmpty()) {
throw new IllegalArgumentException("Must provide at least one coil");
} else if (valuesElem.size() > ModbusConstants.MAX_BITS_WRITE_COUNT) {
throw new IllegalArgumentException(

View File

@ -55,7 +55,7 @@ public class MqttBrokerConnectionConfig {
*/
public String getBrokerID() {
final String name = this.name;
if (name != null && name.length() > 0) {
if (name != null && !name.isEmpty()) {
return name;
} else {
StringBuilder b = new StringBuilder();

View File

@ -267,7 +267,7 @@ public class ModelRepositoryImpl implements ModelRepository {
.append(MessageFormat.format("[{0},{1}]: {2}\n", Integer.toString(diagnostic.getLine()),
Integer.toString(diagnostic.getColumn()), diagnostic.getMessage()));
}
if (criticalErrors.length() > 0) {
if (!criticalErrors.isEmpty()) {
return criticalErrors.toString();
}

View File

@ -134,7 +134,7 @@ public abstract class ScriptException extends Exception {
int i = 1;
for (ScriptError e : getErrors()) {
if (sb.length() > 0) {
if (!sb.isEmpty()) {
sb.append('\n');
}
sb.append(" ");

View File

@ -736,7 +736,7 @@ public class ItemUIRegistryImpl implements ItemUIRegistry {
@Override
public @Nullable Widget getWidget(Sitemap sitemap, String id) {
if (id.length() > 0) {
if (!id.isEmpty()) {
// see if the id is an itemName and try to get the widget for it
Widget w = getWidget(id);
if (w == null) {

View File

@ -336,7 +336,7 @@ public class ExpireManager implements EventSubscriber, RegistryChangeListener<It
ignoreStateUpdates = getBooleanConfigValue(configuration, CONFIG_IGNORE_STATE_UPDATES);
ignoreCommands = getBooleanConfigValue(configuration, CONFIG_IGNORE_COMMANDS);
if ((stateOrCommand != null) && (stateOrCommand.length() > 0)) {
if ((stateOrCommand != null) && (!stateOrCommand.isEmpty())) {
if (stateOrCommand.startsWith(COMMAND_PREFIX)) {
String commandString = stateOrCommand.substring(COMMAND_PREFIX.length());
expireCommand = TypeParser.parseCommand(item.getAcceptedCommandTypes(), commandString);

View File

@ -129,7 +129,7 @@ public class StringUtils {
final String delimiter = "_";
StringBuilder capitalizedFully = new StringBuilder();
for (String splitStr : str.split(delimiter)) {
if (splitStr.length() > 0) {
if (!splitStr.isEmpty()) {
capitalizedFully.append(splitStr.substring(0, 1).toUpperCase());
}
if (splitStr.length() > 1) {

View File

@ -249,7 +249,7 @@ public class QueueingThreadPoolExecutorTest {
for (int i = 0; i < n; i++) {
// we can only test if name is at least one character,
// otherwise there will be threads found (handles poolName="")
if (poolName.length() > 0) {
if (!poolName.isEmpty()) {
if (l[i].getName().startsWith(poolName)) {
// enable printout to see threads
// System.out.println("areThreadsFromPoolRunning: " +

View File

@ -141,7 +141,7 @@ public class ConfigDescriptionsTest extends JavaOSGiTest {
private String join(Collection<?> elements, String separator) {
StringBuilder sb = new StringBuilder();
for (Object element : elements) {
if (sb.length() > 0) {
if (!sb.isEmpty()) {
sb.append(separator);
}
if (element != null) {