Skip to content

Commit c67d0f1

Browse files
authored
[java] Refactor code to use StringBuilder instead of StringBuffer
1 parent 9850c95 commit c67d0f1

4 files changed

Lines changed: 20 additions & 24 deletions

File tree

java/src/com/thoughtworks/selenium/DefaultRemoteCommand.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,12 @@ public DefaultRemoteCommand(String command, String[] args) {
5252

5353
@Override
5454
public String getCommandURLString() {
55-
StringBuffer sb = new StringBuffer("cmd=");
55+
StringBuilder sb = new StringBuilder("cmd=");
5656
sb.append(Urls.urlEncode(command));
5757
if (args == null) return sb.toString();
5858
for (int i = 0; i < args.length; i++) {
5959
sb.append('&');
60-
sb.append(Integer.toString(i + 1));
60+
sb.append(i + 1);
6161
sb.append('=');
6262
sb.append(Urls.urlEncode(args[i]));
6363
}

java/src/com/thoughtworks/selenium/HttpCommandProcessor.java

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@
4646
public class HttpCommandProcessor implements CommandProcessor {
4747

4848
private String pathToServlet;
49-
private String browserStartCommand;
50-
private String browserURL;
49+
private final String browserStartCommand;
50+
private final String browserURL;
5151
private String sessionId;
5252
private String extensionJs;
5353
private String rcServerLocation;
@@ -65,8 +65,7 @@ public class HttpCommandProcessor implements CommandProcessor {
6565
*/
6666
public HttpCommandProcessor(String serverHost, int serverPort, String browserStartCommand,
6767
String browserURL) {
68-
rcServerLocation = serverHost +
69-
":" + Integer.toString(serverPort);
68+
rcServerLocation = serverHost + ":" + serverPort;
7069
this.pathToServlet = "http://" + rcServerLocation + "/selenium-server/driver/";
7170
this.browserStartCommand = browserStartCommand;
7271
this.browserURL = browserURL;
@@ -131,7 +130,7 @@ public String executeCommandOnServlet(String command) {
131130
}
132131

133132
private String stringContentsOfInputStream(Reader rdr) throws IOException {
134-
StringBuffer sb = new StringBuffer();
133+
StringBuilder sb = new StringBuilder();
135134
int c;
136135
try {
137136
while ((c = rdr.read()) != -1) {
@@ -219,7 +218,7 @@ protected void closeResources(HttpURLConnection conn, Writer wr, Reader rdr) {
219218
}
220219

221220
private String buildCommandBody(String command) {
222-
StringBuffer sb = new StringBuffer();
221+
StringBuilder sb = new StringBuilder();
223222
sb.append(command);
224223
if (sessionId != null) {
225224
sb.append("&sessionId=");
@@ -307,13 +306,13 @@ public String[] getStringArray(String commandName, String[] args) {
307306
*/
308307
public static String[] parseCSV(String input) {
309308
List<String> output = new ArrayList<>();
310-
StringBuffer sb = new StringBuffer();
309+
StringBuilder sb = new StringBuilder();
311310
for (int i = 0; i < input.length(); i++) {
312311
char c = input.charAt(i);
313312
switch (c) {
314313
case ',':
315314
output.add(sb.toString());
316-
sb = new StringBuffer();
315+
sb = new StringBuilder();
317316
continue;
318317
case '\\':
319318
i++;
@@ -324,7 +323,7 @@ public static String[] parseCSV(String input) {
324323
}
325324
}
326325
output.add(sb.toString());
327-
return output.toArray(new String[output.size()]);
326+
return output.toArray(new String[0]);
328327
}
329328

330329
@Override
@@ -338,7 +337,7 @@ public Number getNumber(String commandName, String[] args) {
338337
}
339338
if (n instanceof Long && n.intValue() == n.longValue()) {
340339
// SRC-315 we should return Integers if possible
341-
return Integer.valueOf(n.intValue());
340+
return n.intValue();
342341
}
343342
return n;
344343
}

java/src/com/thoughtworks/selenium/SeleneseTestBase.java

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public class SeleneseTestBase {
4949
/** Use this object to run all of your selenium tests */
5050
protected Selenium selenium;
5151

52-
protected StringBuffer verificationErrors = new StringBuffer();
52+
protected StringBuilder verificationErrors = new StringBuilder();
5353

5454
public SeleneseTestBase() {
5555
super();
@@ -184,7 +184,7 @@ public void verifyEquals(Object expected, Object actual) {
184184
*/
185185
public void verifyEquals(boolean expected, boolean actual) {
186186
try {
187-
assertEquals(Boolean.valueOf(expected), Boolean.valueOf(actual));
187+
assertEquals(expected, actual);
188188
} catch (Error e) {
189189
verificationErrors.append(throwableToString(e));
190190
}
@@ -346,10 +346,7 @@ public void verifyEquals(String[] expected, String[] actual) {
346346
}
347347

348348
private static String verifyEqualsAndReturnComparisonDumpIfNot(String[] expected, String[] actual) {
349-
boolean misMatch = false;
350-
if (expected.length != actual.length) {
351-
misMatch = true;
352-
}
349+
boolean misMatch = expected.length != actual.length;
353350
for (int j = 0; j < expected.length; j++) {
354351
if (!seleniumEquals(expected[j], actual[j])) {
355352
misMatch = true;
@@ -364,9 +361,9 @@ private static String verifyEqualsAndReturnComparisonDumpIfNot(String[] expected
364361
}
365362

366363
private static String stringArrayToString(String[] sa) {
367-
StringBuffer sb = new StringBuffer("{");
368-
for (int j = 0; j < sa.length; j++) {
369-
sb.append(" ").append("\"").append(sa[j]).append("\"");
364+
StringBuilder sb = new StringBuilder("{");
365+
for (String s : sa) {
366+
sb.append(" ").append("\"").append(s).append("\"");
370367
}
371368
sb.append(" }");
372369
return sb.toString();
@@ -380,7 +377,7 @@ private static String throwableToString(Throwable t) {
380377
}
381378

382379
public static String join(String[] sa, char c) {
383-
StringBuffer sb = new StringBuffer();
380+
StringBuilder sb = new StringBuilder();
384381
for (int j = 0; j < sa.length; j++) {
385382
sb.append(sa[j]);
386383
if (j < sa.length - 1) {
@@ -477,7 +474,7 @@ public void checkForVerificationErrors() {
477474

478475
/** Clears out the list of verification errors */
479476
public void clearVerificationErrors() {
480-
verificationErrors = new StringBuffer();
477+
verificationErrors = new StringBuilder();
481478
}
482479

483480
/** checks for verification errors and stops the browser

java/test/org/openqa/selenium/support/events/EventFiringDecoratorTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public class EventFiringDecoratorTest {
4747

4848
static class CollectorListener implements WebDriverListener {
4949

50-
StringBuffer acc = new StringBuffer();
50+
protected final StringBuilder acc = new StringBuilder();
5151

5252
@Override
5353
public void beforeAnyCall(Object target, Method method, Object[] args) {

0 commit comments

Comments
 (0)