Skip to content

Commit c86cca2

Browse files
committed
Change to use the StringJoiner instead of the StringBuilder
1 parent 5731bd6 commit c86cca2

File tree

5 files changed

+24
-50
lines changed

5 files changed

+24
-50
lines changed

src/main/java/org/apache/ibatis/builder/annotation/MapperAnnotationBuilder.java

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -348,15 +348,7 @@ void parseStatement(Method method) {
348348
String resultMapId = null;
349349
ResultMap resultMapAnnotation = method.getAnnotation(ResultMap.class);
350350
if (resultMapAnnotation != null) {
351-
String[] resultMaps = resultMapAnnotation.value();
352-
StringBuilder sb = new StringBuilder();
353-
for (String resultMap : resultMaps) {
354-
if (sb.length() > 0) {
355-
sb.append(",");
356-
}
357-
sb.append(resultMap);
358-
}
359-
resultMapId = sb.toString();
351+
resultMapId = String.join(",", resultMapAnnotation.value());
360352
} else if (isSelect) {
361353
resultMapId = parseResultMap(method);
362354
}

src/main/java/org/apache/ibatis/cache/CacheKey.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import java.io.Serializable;
1919
import java.util.ArrayList;
2020
import java.util.List;
21+
import java.util.StringJoiner;
2122

2223
import org.apache.ibatis.reflection.ArrayUtil;
2324

@@ -112,10 +113,10 @@ public int hashCode() {
112113

113114
@Override
114115
public String toString() {
115-
StringBuilder returnValue = new StringBuilder().append(hashcode).append(':').append(checksum);
116-
for (Object object : updateList) {
117-
returnValue.append(':').append(ArrayUtil.toString(object));
118-
}
116+
StringJoiner returnValue = new StringJoiner(":");
117+
returnValue.add(String.valueOf(hashcode));
118+
returnValue.add(String.valueOf(checksum));
119+
updateList.stream().map(ArrayUtil::toString).forEach(returnValue::add);
119120
return returnValue.toString();
120121
}
121122

src/main/java/org/apache/ibatis/logging/jdbc/ResultSetLogger.java

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import java.sql.Types;
2525
import java.util.HashSet;
2626
import java.util.Set;
27+
import java.util.StringJoiner;
2728

2829
import org.apache.ibatis.logging.Log;
2930
import org.apache.ibatis.reflection.ExceptionUtil;
@@ -90,39 +91,28 @@ public Object invoke(Object proxy, Method method, Object[] params) throws Throwa
9091
}
9192

9293
private void printColumnHeaders(ResultSetMetaData rsmd, int columnCount) throws SQLException {
93-
StringBuilder row = new StringBuilder();
94-
row.append(" Columns: ");
94+
StringJoiner row = new StringJoiner(", ", " Columns: ", "");
9595
for (int i = 1; i <= columnCount; i++) {
9696
if (BLOB_TYPES.contains(rsmd.getColumnType(i))) {
9797
blobColumns.add(i);
9898
}
99-
String colname = rsmd.getColumnLabel(i);
100-
row.append(colname);
101-
if (i != columnCount) {
102-
row.append(", ");
103-
}
99+
row.add(rsmd.getColumnLabel(i));
104100
}
105101
trace(row.toString(), false);
106102
}
107103

108104
private void printColumnValues(int columnCount) {
109-
StringBuilder row = new StringBuilder();
110-
row.append(" Row: ");
105+
StringJoiner row = new StringJoiner(", ", " Row: ", "");
111106
for (int i = 1; i <= columnCount; i++) {
112-
String colname;
113107
try {
114108
if (blobColumns.contains(i)) {
115-
colname = "<<BLOB>>";
109+
row.add("<<BLOB>>");
116110
} else {
117-
colname = rs.getString(i);
111+
row.add(rs.getString(i));
118112
}
119113
} catch (SQLException e) {
120114
// generally can't call getString() on a BLOB column
121-
colname = "<<Cannot Display>>";
122-
}
123-
row.append(colname);
124-
if (i != columnCount) {
125-
row.append(", ");
115+
row.add("<<Cannot Display>>");
126116
}
127117
}
128118
trace(row.toString(), false);

src/main/java/org/apache/ibatis/reflection/factory/DefaultObjectFactory.java

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2009-2018 the original author or authors.
2+
* Copyright 2009-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -19,14 +19,17 @@
1919
import java.lang.reflect.Constructor;
2020
import java.util.ArrayList;
2121
import java.util.Collection;
22+
import java.util.Collections;
2223
import java.util.HashMap;
2324
import java.util.HashSet;
2425
import java.util.List;
2526
import java.util.Map;
27+
import java.util.Optional;
2628
import java.util.Properties;
2729
import java.util.Set;
2830
import java.util.SortedSet;
2931
import java.util.TreeSet;
32+
import java.util.stream.Collectors;
3033

3134
import org.apache.ibatis.reflection.ReflectionException;
3235
import org.apache.ibatis.reflection.Reflector;
@@ -84,22 +87,10 @@ private <T> T instantiateClass(Class<T> type, List<Class<?>> constructorArgType
8487
}
8588
}
8689
} catch (Exception e) {
87-
StringBuilder argTypes = new StringBuilder();
88-
if (constructorArgTypes != null && !constructorArgTypes.isEmpty()) {
89-
for (Class<?> argType : constructorArgTypes) {
90-
argTypes.append(argType.getSimpleName());
91-
argTypes.append(",");
92-
}
93-
argTypes.deleteCharAt(argTypes.length() - 1); // remove trailing ,
94-
}
95-
StringBuilder argValues = new StringBuilder();
96-
if (constructorArgs != null && !constructorArgs.isEmpty()) {
97-
for (Object argValue : constructorArgs) {
98-
argValues.append(String.valueOf(argValue));
99-
argValues.append(",");
100-
}
101-
argValues.deleteCharAt(argValues.length() - 1); // remove trailing ,
102-
}
90+
String argTypes = Optional.ofNullable(constructorArgTypes).orElseGet(Collections::emptyList)
91+
.stream().map(Class::getSimpleName).collect(Collectors.joining(","));
92+
String argValues = Optional.ofNullable(constructorArgs).orElseGet(Collections::emptyList)
93+
.stream().map(String::valueOf).collect(Collectors.joining(","));
10394
throw new ReflectionException("Error instantiating " + type + " with invalid types (" + argTypes + ") or values (" + argValues + "). Cause: " + e, e);
10495
}
10596
}

src/main/java/org/apache/ibatis/scripting/xmltags/DynamicContext.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import java.util.HashMap;
1919
import java.util.Map;
20+
import java.util.StringJoiner;
2021

2122
import ognl.OgnlContext;
2223
import ognl.OgnlRuntime;
@@ -38,7 +39,7 @@ public class DynamicContext {
3839
}
3940

4041
private final ContextMap bindings;
41-
private final StringBuilder sqlBuilder = new StringBuilder();
42+
private final StringJoiner sqlBuilder = new StringJoiner(" ");
4243
private int uniqueNumber = 0;
4344

4445
public DynamicContext(Configuration configuration, Object parameterObject) {
@@ -61,8 +62,7 @@ public void bind(String name, Object value) {
6162
}
6263

6364
public void appendSql(String sql) {
64-
sqlBuilder.append(sql);
65-
sqlBuilder.append(" ");
65+
sqlBuilder.add(sql);
6666
}
6767

6868
public String getSql() {

0 commit comments

Comments
 (0)