Use StringJoiner where possible to simplify String joining
This commit is contained in:
parent
f087fd5a93
commit
cb4d6f097c
|
@ -21,6 +21,7 @@ import java.util.Arrays;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
import java.util.StringJoiner;
|
||||||
|
|
||||||
import org.springframework.lang.Nullable;
|
import org.springframework.lang.Nullable;
|
||||||
|
|
||||||
|
@ -709,18 +710,11 @@ public abstract class ObjectUtils {
|
||||||
if (length == 0) {
|
if (length == 0) {
|
||||||
return EMPTY_ARRAY;
|
return EMPTY_ARRAY;
|
||||||
}
|
}
|
||||||
StringBuilder sb = new StringBuilder();
|
StringJoiner sj = new StringJoiner(ARRAY_ELEMENT_SEPARATOR, ARRAY_START, ARRAY_END);
|
||||||
for (int i = 0; i < length; i++) {
|
for (Object o : array) {
|
||||||
if (i == 0) {
|
sj.add(String.valueOf(o));
|
||||||
sb.append(ARRAY_START);
|
|
||||||
}
|
}
|
||||||
else {
|
return sj.toString();
|
||||||
sb.append(ARRAY_ELEMENT_SEPARATOR);
|
|
||||||
}
|
|
||||||
sb.append(String.valueOf(array[i]));
|
|
||||||
}
|
|
||||||
sb.append(ARRAY_END);
|
|
||||||
return sb.toString();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -19,6 +19,7 @@ package org.springframework.expression.spel.ast;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.StringJoiner;
|
||||||
|
|
||||||
import org.springframework.asm.MethodVisitor;
|
import org.springframework.asm.MethodVisitor;
|
||||||
import org.springframework.expression.EvaluationException;
|
import org.springframework.expression.EvaluationException;
|
||||||
|
@ -102,17 +103,13 @@ public class InlineList extends SpelNodeImpl {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toStringAST() {
|
public String toStringAST() {
|
||||||
StringBuilder sb = new StringBuilder("{");
|
StringJoiner sj = new StringJoiner(",", "{", "}");
|
||||||
// String ast matches input string, not the 'toString()' of the resultant collection, which would use []
|
// String ast matches input string, not the 'toString()' of the resultant collection, which would use []
|
||||||
int count = getChildCount();
|
int count = getChildCount();
|
||||||
for (int c = 0; c < count; c++) {
|
for (int c = 0; c < count; c++) {
|
||||||
if (c > 0) {
|
sj.add(getChild(c).toStringAST());
|
||||||
sb.append(",");
|
|
||||||
}
|
}
|
||||||
sb.append(getChild(c).toStringAST());
|
return sj.toString();
|
||||||
}
|
|
||||||
sb.append("}");
|
|
||||||
return sb.toString();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -633,15 +633,11 @@ public class HttpHeaders implements MultiValueMap<String, String>, Serializable
|
||||||
* as specified by the {@code Accept-Charset} header.
|
* as specified by the {@code Accept-Charset} header.
|
||||||
*/
|
*/
|
||||||
public void setAcceptCharset(List<Charset> acceptableCharsets) {
|
public void setAcceptCharset(List<Charset> acceptableCharsets) {
|
||||||
StringBuilder builder = new StringBuilder();
|
StringJoiner joiner = new StringJoiner(", ");
|
||||||
for (Iterator<Charset> iterator = acceptableCharsets.iterator(); iterator.hasNext();) {
|
for (Charset charset : acceptableCharsets) {
|
||||||
Charset charset = iterator.next();
|
joiner.add(charset.name().toLowerCase(Locale.ENGLISH));
|
||||||
builder.append(charset.name().toLowerCase(Locale.ENGLISH));
|
|
||||||
if (iterator.hasNext()) {
|
|
||||||
builder.append(", ");
|
|
||||||
}
|
}
|
||||||
}
|
set(ACCEPT_CHARSET, joiner.toString());
|
||||||
set(ACCEPT_CHARSET, builder.toString());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -17,7 +17,7 @@
|
||||||
package org.springframework.web.reactive.result.condition;
|
package org.springframework.web.reactive.result.condition;
|
||||||
|
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.Iterator;
|
import java.util.StringJoiner;
|
||||||
|
|
||||||
import org.springframework.lang.Nullable;
|
import org.springframework.lang.Nullable;
|
||||||
|
|
||||||
|
@ -50,16 +50,12 @@ public abstract class AbstractRequestCondition<T extends AbstractRequestConditio
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
StringBuilder builder = new StringBuilder("[");
|
String infix = getToStringInfix();
|
||||||
for (Iterator<?> iterator = getContent().iterator(); iterator.hasNext();) {
|
StringJoiner joiner = new StringJoiner(infix, "[", "]");
|
||||||
Object expression = iterator.next();
|
for (Object expression : getContent()) {
|
||||||
builder.append(expression.toString());
|
joiner.add(expression.toString());
|
||||||
if (iterator.hasNext()) {
|
|
||||||
builder.append(getToStringInfix());
|
|
||||||
}
|
}
|
||||||
}
|
return joiner.toString();
|
||||||
builder.append("]");
|
|
||||||
return builder.toString();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -17,7 +17,7 @@
|
||||||
package org.springframework.web.servlet.mvc.condition;
|
package org.springframework.web.servlet.mvc.condition;
|
||||||
|
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.Iterator;
|
import java.util.StringJoiner;
|
||||||
|
|
||||||
import org.springframework.lang.Nullable;
|
import org.springframework.lang.Nullable;
|
||||||
|
|
||||||
|
@ -74,16 +74,12 @@ public abstract class AbstractRequestCondition<T extends AbstractRequestConditio
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
StringBuilder builder = new StringBuilder("[");
|
String infix = getToStringInfix();
|
||||||
for (Iterator<?> iterator = getContent().iterator(); iterator.hasNext();) {
|
StringJoiner joiner = new StringJoiner(infix, "[", "]");
|
||||||
Object expression = iterator.next();
|
for (Object expression : getContent()) {
|
||||||
builder.append(expression.toString());
|
joiner.add(expression.toString());
|
||||||
if (iterator.hasNext()) {
|
|
||||||
builder.append(getToStringInfix());
|
|
||||||
}
|
}
|
||||||
}
|
return joiner.toString();
|
||||||
builder.append("]");
|
|
||||||
return builder.toString();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue