Skip to content

Use Collections#isEmpty or String#isEmpty #35243

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public void setChildren(List<Component> children) {

@Override
public Component getObject() {
if (this.children != null && this.children.size() > 0) {
if (this.children != null && !this.children.isEmpty()) {
for (Component child : children) {
this.parent.addComponent(child);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -379,18 +379,18 @@ protected List<String> calculateFilenamesForLocale(String basename, Locale local
StringBuilder temp = new StringBuilder(basename);

temp.append('_');
if (language.length() > 0) {
if (!language.isEmpty()) {
temp.append(language);
result.add(0, temp.toString());
}

temp.append('_');
if (country.length() > 0) {
if (!country.isEmpty()) {
temp.append(country);
result.add(0, temp.toString());
}

if (variant.length() > 0 && (language.length() > 0 || country.length() > 0)) {
if (!variant.isEmpty() && (!language.isEmpty() || !country.isEmpty())) {
temp.append('_').append(variant);
result.add(0, temp.toString());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ private AbstractNamingEnumeration(SimpleNamingContext context, String proot) thr
}
}
}
if (contents.size() == 0) {
if (contents.isEmpty()) {
throw new NamingException("Invalid root: [" + context.root + proot + "]");
}
this.iterator = contents.values().iterator();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ public int getStepArgument(final int index) {
* @return the corresponding TypePath object, or {@literal null} if the path is empty.
*/
public static TypePath fromString(final String typePath) {
if (typePath == null || typePath.length() == 0) {
if (typePath == null || typePath.isEmpty()) {
return null;
}
int typePathLength = typePath.length();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public CallbackHelper(Class superclass, Class[] interfaces) {
abstract protected Object getCallback(Method method);

public Callback[] getCallbacks() {
if (callbacks.size() == 0) {
if (callbacks.isEmpty()) {
return new Callback[0];
}
if (callbacks.get(0) instanceof Callback) {
Expand All @@ -75,7 +75,7 @@ public Callback[] getCallbacks() {
}

public Class[] getCallbackTypes() {
if (callbacks.size() == 0) {
if (callbacks.isEmpty()) {
return new Class[0];
}
if (callbacks.get(0) instanceof Callback) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -755,7 +755,7 @@ public void generateClass(ClassVisitor v) throws Exception {
*/
protected void filterConstructors(Class sc, List constructors) {
CollectionUtils.filter(constructors, new VisibilityPredicate(sc, true));
if (constructors.size() == 0) {
if (constructors.isEmpty()) {
throw new IllegalArgumentException("No visible constructors in " + sc);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,20 +100,20 @@ public Resource findLocalizedResource(String name, String extension, @Nullable L
String variant = locale.getVariant();

// Check for file with language, country and variant localization.
if (variant.length() > 0) {
if (!variant.isEmpty()) {
String location =
name + this.separator + lang + this.separator + country + this.separator + variant + extension;
resource = this.resourceLoader.getResource(location);
}

// Check for file with language and country localization.
if ((resource == null || !resource.exists()) && country.length() > 0) {
if ((resource == null || !resource.exists()) && !country.isEmpty()) {
String location = name + this.separator + lang + this.separator + country + extension;
resource = this.resourceLoader.getResource(location);
}

// Check for document with language localization.
if ((resource == null || !resource.exists()) && lang.length() > 0) {
if ((resource == null || !resource.exists()) && !lang.isEmpty()) {
String location = name + this.separator + lang + extension;
resource = this.resourceLoader.getResource(location);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public void processPropertySource(PropertySourceDescriptor descriptor) throws IO
String name = descriptor.name();
String encoding = descriptor.encoding();
List<String> locations = descriptor.locations();
Assert.isTrue(locations.size() > 0, "At least one @PropertySource(value) location is required");
Assert.isTrue(!locations.isEmpty(), "At least one @PropertySource(value) location is required");
boolean ignoreResourceNotFound = descriptor.ignoreResourceNotFound();
PropertySourceFactory factory = (descriptor.propertySourceFactory() != null ?
instantiateClass(descriptor.propertySourceFactory()) : defaultPropertySourceFactory);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@ private boolean isPotentialMatch(String path, String[] pattDirs) {
pos += skipped;
skipped = skipSegment(path, pos, pattDir);
if (skipped < pattDir.length()) {
return (skipped > 0 || (pattDir.length() > 0 && isWildcardChar(pattDir.charAt(0))));
return (skipped > 0 || (!pattDir.isEmpty() && isWildcardChar(pattDir.charAt(0))));
}
pos += skipped;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public CommonsLogWriter(Log logger) {


public void write(char ch) {
if (ch == '\n' && this.buffer.length() > 0) {
if (ch == '\n' && !this.buffer.isEmpty()) {
logger.debug(this.buffer.toString());
this.buffer.setLength(0);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ else if (ch == '"') {
nextIndex++;
}
String parameter = mimeType.substring(index + 1, nextIndex).trim();
if (parameter.length() > 0) {
if (!parameter.isEmpty()) {
if (parameters == null) {
parameters = new LinkedHashMap<>(4);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -443,7 +443,7 @@ protected boolean isCandidateForProperty(Method method, Class<?> targetClass) {
*/
protected String[] getPropertyMethodSuffixes(String propertyName) {
String suffix = getPropertyMethodSuffix(propertyName);
if (suffix.length() > 0 && Character.isUpperCase(suffix.charAt(0))) {
if (!suffix.isEmpty() && Character.isUpperCase(suffix.charAt(0))) {
return new String[] {suffix};
}
return new String[] {suffix, StringUtils.capitalize(suffix)};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ private static String readScript(LineNumberReader lineNumberReader, String @Null
while (currentStatement != null) {
if ((blockCommentEndDelimiter != null && currentStatement.contains(blockCommentEndDelimiter)) ||
(commentPrefixes != null && !startsWithAny(currentStatement, commentPrefixes, 0))) {
if (scriptBuilder.length() > 0) {
if (!scriptBuilder.isEmpty()) {
scriptBuilder.append('\n');
}
scriptBuilder.append(currentStatement);
Expand Down Expand Up @@ -508,7 +508,7 @@ else if (!inSingleQuote && (c == '"')) {
if (!inSingleQuote && !inDoubleQuote) {
if (script.startsWith(separator, i)) {
// We've reached the end of the current statement
if (sb.length() > 0) {
if (!sb.isEmpty()) {
statements.add(sb.toString());
sb = new StringBuilder();
}
Expand Down Expand Up @@ -541,7 +541,7 @@ else if (script.startsWith(blockCommentStartDelimiter, i)) {
}
else if (c == ' ' || c == '\r' || c == '\n' || c == '\t') {
// Avoid multiple adjacent whitespace characters
if (sb.length() > 0 && sb.charAt(sb.length() - 1) != ' ') {
if (!sb.isEmpty() && sb.charAt(sb.length() - 1) != ' ') {
c = ' ';
}
else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ public List<Message<byte[]>> decode(ByteBuffer byteBuffer,
byteBuffer.mark();

String command = readCommand(byteBuffer);
if (command.length() > 0) {
if (!command.isEmpty()) {
StompHeaderAccessor headerAccessor = null;
byte[] payload = null;
if (byteBuffer.remaining() > 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ public void handleMessage(Message<?> message) throws MessagingException {

public void expectMessages(MessageExchange... messageExchanges) throws InterruptedException {
List<MessageExchange> expectedMessages = new ArrayList<>(Arrays.asList(messageExchanges));
while (expectedMessages.size() > 0) {
while (!expectedMessages.isEmpty()) {
Message<?> message = this.queue.poll(10000, TimeUnit.MILLISECONDS);
assertThat(message).as("Timed out waiting for messages, expected [" + expectedMessages + "]").isNotNull();
MessageExchange match = findMatch(expectedMessages, message);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ private static String readScript(LineNumberReader lineNumberReader, @Nullable St
StringBuilder scriptBuilder = new StringBuilder();
String currentLine = lineNumberReader.readLine();
while (currentLine != null) {
if (scriptBuilder.length() > 0) {
if (!scriptBuilder.isEmpty()) {
scriptBuilder.append('\n');
}
scriptBuilder.append(currentLine);
Expand Down Expand Up @@ -487,7 +487,7 @@ else if (!inSingleQuote && (c == '"')) {
if (!inSingleQuote && !inDoubleQuote) {
if (script.startsWith(separator, i)) {
// We've reached the end of the current statement
if (sb.length() > 0) {
if (!sb.isEmpty()) {
statements.add(sb.toString());
sb = new StringBuilder();
}
Expand Down Expand Up @@ -520,7 +520,7 @@ else if (script.startsWith(blockCommentStartDelimiter, i)) {
}
else if (c == ' ' || c == '\r' || c == '\n' || c == '\t') {
// Avoid multiple adjacent whitespace characters
if (sb.length() > 0 && sb.charAt(sb.length() - 1) != ' ') {
if (!sb.isEmpty() && sb.charAt(sb.length() - 1) != ' ') {
c = ' ';
}
else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ public static void assertModelAttributeValues(ModelAndView mav, Map<String, Obje
}
});

if (sb.length() != 0) {
if (!sb.isEmpty()) {
sb.insert(0, "Values of expected model do not match.\n");
fail(sb.toString());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ protected String serializeForm(MultiValueMap<String, String> formData, Charset c
StringBuilder builder = new StringBuilder();
formData.forEach((name, values) ->
values.forEach(value -> {
if (builder.length() != 0) {
if (!builder.isEmpty()) {
builder.append('&');
}
builder.append(URLEncoder.encode(name, charset));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -444,7 +444,7 @@ protected String serializeForm(MultiValueMap<String, Object> formData, Charset c
return;
}
values.forEach(value -> {
if (builder.length() != 0) {
if (!builder.isEmpty()) {
builder.append('&');
}
builder.append(URLEncoder.encode(name, charset));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -238,14 +238,14 @@ else if (c == '}') {
}
builder.append(c);
}
if (builder.length() > 0) {
if (!builder.isEmpty()) {
pattern.append(quote(builder));
}
return new TemplateInfo(variableNames, Pattern.compile(pattern.toString()));
}

private static String quote(StringBuilder builder) {
return (builder.length() > 0 ? Pattern.quote(builder.toString()) : "");
return (!builder.isEmpty() ? Pattern.quote(builder.toString()) : "");
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -567,7 +567,7 @@ else if (path1EndsWithSeparator || path2StartsWithSeparator) {
* @return {@code true} has more than zero elements
*/
private boolean hasLength(@Nullable PathContainer container) {
return container != null && container.elements().size() > 0;
return container != null && !container.elements().isEmpty();
}

private static int scoreByNormalizedLength(PathPattern pattern) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ private void insertQueryParams() {
for (String key : this.queryParams.keySet()) {
for (String value : this.queryParams.get(key)) {
this.servletRequest.addParameter(key, value);
query.append(query.length() > 0 ? "&" : "").append(key).append('=').append(value);
query.append(!query.isEmpty() ? "&" : "").append(key).append('=').append(value);
}
}
this.servletRequest.setQueryString(query.toString());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,7 @@ private String getHandlerDescription(Object handler) {
uriTemplateVariables.putAll(decodedVars);
}
}
if (logger.isTraceEnabled() && uriTemplateVariables.size() > 0) {
if (logger.isTraceEnabled() && !uriTemplateVariables.isEmpty()) {
logger.trace("URI variables " + uriTemplateVariables);
}
return buildPathExposingHandler(handler, bestMatch, pathWithinMapping, uriTemplateVariables);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ private String formatStatusAndView() {
sb.append("status=").append(this.statusCode);
}
if (this.view != null) {
sb.append(sb.length() != 0 ? ", " : "");
sb.append(!sb.isEmpty() ? ", " : "");
String viewName = getViewName();
sb.append("view=").append(viewName != null ? "\"" + viewName + "\"" : this.view);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public void setPath(@Nullable String path) {
if (path == null) {
path = "";
}
if (path.length() > 0 && !path.endsWith(PropertyAccessor.NESTED_PROPERTY_SEPARATOR)) {
if (!path.isEmpty() && !path.endsWith(PropertyAccessor.NESTED_PROPERTY_SEPARATOR)) {
path += PropertyAccessor.NESTED_PROPERTY_SEPARATOR;
}
this.path = path;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ public void handleRequest(HttpServletRequest servletRequest, HttpServletResponse
private String getSockJsPath(HttpServletRequest servletRequest) {
String attribute = HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE;
String path = (String) servletRequest.getAttribute(attribute);
return (path.length() > 0 && path.charAt(0) != '/' ? "/" + path : path);
return (!path.isEmpty() && path.charAt(0) != '/' ? "/" + path : path);
}

@Override
Expand Down