Skip to content

Commit 64af3a0

Browse files
committed
Use ArrayList instead of LinkedList for known size
Issue: SPR-16378
1 parent d7959ed commit 64af3a0

File tree

2 files changed

+26
-19
lines changed

2 files changed

+26
-19
lines changed

spring-jdbc/src/main/java/org/springframework/jdbc/core/SqlParameter.java

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2015 the original author or authors.
2+
* Copyright 2002-2018 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.
@@ -16,6 +16,7 @@
1616

1717
package org.springframework.jdbc.core;
1818

19+
import java.util.ArrayList;
1920
import java.util.LinkedList;
2021
import java.util.List;
2122

@@ -24,7 +25,7 @@
2425
/**
2526
* Object to represent a SQL parameter definition.
2627
*
27-
* <p>Parameters may be anonymous, in which case "name" is {@code null}.
28+
* <p>Parameters may be anonymous in which case "name" is {@code null}.
2829
* However, all parameters must define a SQL type according to {@link java.sql.Types}.
2930
*
3031
* @author Rod Johnson
@@ -34,16 +35,16 @@
3435
*/
3536
public class SqlParameter {
3637

37-
/** The name of the parameter, if any */
38+
// The name of the parameter, if any
3839
private String name;
3940

40-
/** SQL type constant from {@code java.sql.Types} */
41+
// SQL type constant from {@code java.sql.Types}
4142
private final int sqlType;
4243

43-
/** Used for types that are user-named like: STRUCT, DISTINCT, JAVA_OBJECT, named array types */
44+
// Used for types that are user-named like: STRUCT, DISTINCT, JAVA_OBJECT, named array types
4445
private String typeName;
4546

46-
/** The scale to apply in case of a NUMERIC or DECIMAL type, if any */
47+
// The scale to apply in case of a NUMERIC or DECIMAL type, if any
4748
private Integer scale;
4849

4950

@@ -177,12 +178,16 @@ public boolean isResultsParameter() {
177178
* to a List of SqlParameter objects as used in this package.
178179
*/
179180
public static List<SqlParameter> sqlTypesToAnonymousParameterList(int... types) {
180-
List<SqlParameter> result = new LinkedList<SqlParameter>();
181+
List<SqlParameter> result;
181182
if (types != null) {
183+
result = new ArrayList<SqlParameter>(types.length);
182184
for (int type : types) {
183185
result.add(new SqlParameter(type));
184186
}
185187
}
188+
else {
189+
result = new LinkedList<SqlParameter>();
190+
}
186191
return result;
187192
}
188193

spring-jdbc/src/main/java/org/springframework/jdbc/core/namedparam/NamedParameterUtils.java

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2017 the original author or authors.
2+
* Copyright 2002-2018 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.
@@ -20,7 +20,6 @@
2020
import java.util.Collection;
2121
import java.util.HashSet;
2222
import java.util.Iterator;
23-
import java.util.LinkedList;
2423
import java.util.List;
2524
import java.util.Map;
2625
import java.util.Set;
@@ -232,7 +231,6 @@ private static int skipCommentsAndQuotes(char[] statement, int position) {
232231
// character sequence ending comment or quote not found
233232
return statement.length;
234233
}
235-
236234
}
237235
}
238236
return position;
@@ -257,8 +255,11 @@ private static int skipCommentsAndQuotes(char[] statement, int position) {
257255
*/
258256
public static String substituteNamedParameters(ParsedSql parsedSql, SqlParameterSource paramSource) {
259257
String originalSql = parsedSql.getOriginalSql();
260-
StringBuilder actualSql = new StringBuilder();
261258
List<String> paramNames = parsedSql.getParameterNames();
259+
if (paramNames.isEmpty()) {
260+
return originalSql;
261+
}
262+
StringBuilder actualSql = new StringBuilder(originalSql.length());
262263
int lastIndex = 0;
263264
for (int i = 0; i < paramNames.size(); i++) {
264265
String paramName = paramNames.get(i);
@@ -282,26 +283,26 @@ public static String substituteNamedParameters(ParsedSql parsedSql, SqlParameter
282283
Object entryItem = entryIter.next();
283284
if (entryItem instanceof Object[]) {
284285
Object[] expressionList = (Object[]) entryItem;
285-
actualSql.append("(");
286+
actualSql.append('(');
286287
for (int m = 0; m < expressionList.length; m++) {
287288
if (m > 0) {
288289
actualSql.append(", ");
289290
}
290-
actualSql.append("?");
291+
actualSql.append('?');
291292
}
292-
actualSql.append(")");
293+
actualSql.append(')');
293294
}
294295
else {
295-
actualSql.append("?");
296+
actualSql.append('?');
296297
}
297298
}
298299
}
299300
else {
300-
actualSql.append("?");
301+
actualSql.append('?');
301302
}
302303
}
303304
else {
304-
actualSql.append("?");
305+
actualSql.append('?');
305306
}
306307
lastIndex = endIndex;
307308
}
@@ -416,9 +417,10 @@ public static int[] buildSqlTypeArray(ParsedSql parsedSql, SqlParameterSource pa
416417
*/
417418
public static List<SqlParameter> buildSqlParameterList(ParsedSql parsedSql, SqlParameterSource paramSource) {
418419
List<String> paramNames = parsedSql.getParameterNames();
419-
List<SqlParameter> params = new LinkedList<SqlParameter>();
420+
List<SqlParameter> params = new ArrayList<SqlParameter>(paramNames.size());
420421
for (String paramName : paramNames) {
421-
params.add(new SqlParameter(paramName, paramSource.getSqlType(paramName), paramSource.getTypeName(paramName)));
422+
params.add(new SqlParameter(
423+
paramName, paramSource.getSqlType(paramName), paramSource.getTypeName(paramName)));
422424
}
423425
return params;
424426
}

0 commit comments

Comments
 (0)