Skip to content

Commit

Permalink
Use isEmpty instead of 0 comparisons (#3999)
Browse files Browse the repository at this point in the history
`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>
  • Loading branch information
wborn committed Jan 2, 2024
1 parent a5316f9 commit 89b67ad
Show file tree
Hide file tree
Showing 10 changed files with 12 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -518,7 +518,7 @@ private Response getServiceItemList(@Nullable String serviceId) {
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.");
Expand Down Expand Up @@ -598,7 +598,7 @@ private Response putItemState(@Nullable String serviceId, String itemName, Strin
}

ZonedDateTime dateTime = null;
if (time != null && time.length() != 0) {
if (time != null && !time.isEmpty()) {
dateTime = convertTime(time);
}
if (dateTime == null || dateTime.toEpochSecond() == 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ private 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<>();
Expand Down Expand Up @@ -173,7 +173,7 @@ private static ModbusWriteRequestBlueprint constructBluerint(int unitId, @Nullab
}
// 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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ public void removeModelRepositoryChangeListener(ModelRepositoryChangeListener li
.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();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ public String getMessage() {

int i = 1;
for (ScriptError e : getErrors()) {
if (sb.length() > 0) {
if (!sb.isEmpty()) {
sb.append('\n');
}
sb.append(" ");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -736,7 +736,7 @@ private String transform(String label, boolean matchTransform, @Nullable String

@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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ public ExpireConfig(Item item, String configString, Map<String, Object> configur
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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ private boolean areThreadsFromPoolRunning(String poolName) {
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: " +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ public void parametersWithOptionsAndFiltersShouldLoadProperly() throws Exception
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) {
Expand Down

0 comments on commit 89b67ad

Please sign in to comment.