8054714: Use StringJoiner where it makes the code cleaner

Reviewed-by: psandoz, redestad
This commit is contained in:
Ivan Gerasimov 2014-08-27 22:08:19 +04:00
parent ee35cf6516
commit e164c8603b
15 changed files with 96 additions and 195 deletions

@ -1,5 +1,5 @@
/*
* Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -31,6 +31,7 @@ import java.util.List;
import java.util.ArrayList;
import java.util.Vector;
import java.util.Collections;
import java.util.StringJoiner;
import sun.security.util.SecurityConstants;
/**
@ -556,39 +557,25 @@ public final class FilePermission extends Permission implements Serializable {
* @return the canonical string representation of the actions.
*/
private static String getActions(int mask) {
StringBuilder sb = new StringBuilder();
boolean comma = false;
StringJoiner sj = new StringJoiner(",");
if ((mask & READ) == READ) {
comma = true;
sb.append("read");
sj.add("read");
}
if ((mask & WRITE) == WRITE) {
if (comma) sb.append(',');
else comma = true;
sb.append("write");
sj.add("write");
}
if ((mask & EXECUTE) == EXECUTE) {
if (comma) sb.append(',');
else comma = true;
sb.append("execute");
sj.add("execute");
}
if ((mask & DELETE) == DELETE) {
if (comma) sb.append(',');
else comma = true;
sb.append("delete");
sj.add("delete");
}
if ((mask & READLINK) == READLINK) {
if (comma) sb.append(',');
else comma = true;
sb.append("readlink");
sj.add("readlink");
}
return sb.toString();
return sj.toString();
}
/**

@ -1,5 +1,5 @@
/*
* Copyright (c) 1996, 2013, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1996, 2014, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -32,6 +32,7 @@ import java.security.PrivilegedAction;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.StringJoiner;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import static java.io.ObjectStreamClass.processQueue;
@ -2465,15 +2466,11 @@ public class ObjectOutputStream
* Returns a string representation of this object
*/
public String toString() {
StringBuilder buffer = new StringBuilder();
if (!stack.isEmpty()) {
for(int i = stack.size(); i > 0; i-- ) {
buffer.append(stack.get(i - 1));
if (i != 1)
buffer.append('\n');
}
StringJoiner sj = new StringJoiner("\n");
for (int i = stack.size() - 1; i >= 0; i--) {
sj.add(stack.get(i));
}
return buffer.toString();
return sj.toString();
}
}

@ -54,6 +54,7 @@ import java.util.Set;
import java.util.Map;
import java.util.HashMap;
import java.util.Objects;
import java.util.StringJoiner;
import sun.misc.Unsafe;
import sun.reflect.CallerSensitive;
import sun.reflect.ConstantPool;
@ -3141,19 +3142,14 @@ public final class Class<T> implements java.io.Serializable,
private native Class<?>[] getDeclaredClasses0();
private static String argumentTypesToString(Class<?>[] argTypes) {
StringBuilder buf = new StringBuilder();
buf.append("(");
StringJoiner sj = new StringJoiner(", ", "(", ")");
if (argTypes != null) {
for (int i = 0; i < argTypes.length; i++) {
if (i > 0) {
buf.append(", ");
}
Class<?> c = argTypes[i];
buf.append((c == null) ? "null" : c.getName());
sj.add((c == null) ? "null" : c.getName());
}
}
buf.append(")");
return buf.toString();
return sj.toString();
}
/** use serialVersionUID from JDK 1.1 for interoperability */

@ -33,6 +33,7 @@ import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.StringJoiner;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.ConcurrentHashMap;
import sun.invoke.util.BytecodeDescriptor;
@ -717,15 +718,12 @@ class MethodType implements java.io.Serializable {
*/
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("(");
StringJoiner sj = new StringJoiner(",", "(",
")" + rtype.getSimpleName());
for (int i = 0; i < ptypes.length; i++) {
if (i > 0) sb.append(",");
sb.append(ptypes[i].getSimpleName());
sj.add(ptypes[i].getSimpleName());
}
sb.append(")");
sb.append(rtype.getSimpleName());
return sb.toString();
return sj.toString();
}

@ -1,5 +1,5 @@
/*
* Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -30,6 +30,7 @@ import java.util.Vector;
import java.util.List;
import java.util.ArrayList;
import java.util.Collections;
import java.util.StringJoiner;
import java.util.StringTokenizer;
import java.net.InetAddress;
import java.security.Permission;
@ -1112,36 +1113,21 @@ public final class SocketPermission extends Permission
* @param mask a specific integer action mask to translate into a string
* @return the canonical string representation of the actions
*/
private static String getActions(int mask)
{
StringBuilder sb = new StringBuilder();
boolean comma = false;
private static String getActions(int mask) {
StringJoiner sj = new StringJoiner(",");
if ((mask & CONNECT) == CONNECT) {
comma = true;
sb.append("connect");
sj.add("connect");
}
if ((mask & LISTEN) == LISTEN) {
if (comma) sb.append(',');
else comma = true;
sb.append("listen");
sj.add("listen");
}
if ((mask & ACCEPT) == ACCEPT) {
if (comma) sb.append(',');
else comma = true;
sb.append("accept");
sj.add("accept");
}
if ((mask & RESOLVE) == RESOLVE) {
if (comma) sb.append(',');
else comma = true;
sb.append("resolve");
sj.add("resolve");
}
return sb.toString();
return sj.toString();
}
/**

@ -1,5 +1,5 @@
/*
* Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2010, 2014, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -26,6 +26,7 @@
package java.text;
import java.util.Calendar;
import java.util.StringJoiner;
import static java.util.GregorianCalendar.*;
/**
@ -146,19 +147,13 @@ class CalendarBuilder {
}
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("CalendarBuilder:[");
StringJoiner sj = new StringJoiner(",", "CalendarBuilder:[", "]");
for (int i = 0; i < field.length; i++) {
if (isSet(i)) {
sb.append(i).append('=').append(field[MAX_FIELD + i]).append(',');
sj.add(i + "=" + field[MAX_FIELD + i]);
}
}
int lastIndex = sb.length() - 1;
if (sb.charAt(lastIndex) == ',') {
sb.setLength(lastIndex);
}
sb.append(']');
return sb.toString();
return sj.toString();
}
static int toISODayOfWeek(int calendarDayOfWeek) {

@ -2059,14 +2059,11 @@ public final class Locale implements Cloneable, Serializable {
// If we have no list patterns, compose the list in a simple,
// non-localized way.
if (listPattern == null || listCompositionPattern == null) {
StringBuilder result = new StringBuilder();
StringJoiner sj = new StringJoiner(",");
for (int i = 0; i < stringList.length; ++i) {
if (i > 0) {
result.append(',');
}
result.append(stringList[i]);
sj.add(stringList[i]);
}
return result.toString();
return sj.toString();
}
// Compose the list down to three elements if necessary

@ -324,20 +324,16 @@ public final class PropertyPermission extends BasicPermission {
* @return the canonical string representation of the actions.
*/
static String getActions(int mask) {
StringBuilder sb = new StringBuilder();
boolean comma = false;
if ((mask & READ) == READ) {
comma = true;
sb.append("read");
switch (mask & (READ|WRITE)) {
case READ:
return SecurityConstants.PROPERTY_READ_ACTION;
case WRITE:
return SecurityConstants.PROPERTY_WRITE_ACTION;
case READ|WRITE:
return SecurityConstants.PROPERTY_RW_ACTION;
default:
return "";
}
if ((mask & WRITE) == WRITE) {
if (comma) sb.append(',');
else comma = true;
sb.append("write");
}
return sb.toString();
}
/**

@ -1,5 +1,5 @@
/*
* Copyright (c) 1994, 2002, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1994, 2014, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -26,6 +26,7 @@
package sun.net.www;
import java.net.URL;
import java.io.*;
import java.util.StringJoiner;
import java.util.StringTokenizer;
public class MimeEntry implements Cloneable {
@ -281,52 +282,34 @@ public class MimeEntry implements Cloneable {
}
public synchronized String toProperty() {
StringBuilder sb = new StringBuilder();
String separator = "; ";
boolean needSeparator = false;
StringJoiner sj = new StringJoiner("; ");
int action = getAction();
if (action != MimeEntry.UNKNOWN) {
sb.append("action=" + actionKeywords[action]);
needSeparator = true;
sj.add("action=" + actionKeywords[action]);
}
String command = getLaunchString();
if (command != null && command.length() > 0) {
if (needSeparator) {
sb.append(separator);
}
sb.append("application=" + command);
needSeparator = true;
sj.add("application=" + command);
}
if (getImageFileName() != null) {
if (needSeparator) {
sb.append(separator);
}
sb.append("icon=" + getImageFileName());
needSeparator = true;
String image = getImageFileName();
if (image != null) {
sj.add("icon=" + image);
}
String extensions = getExtensionsAsList();
if (extensions.length() > 0) {
if (needSeparator) {
sb.append(separator);
}
sb.append("file_extensions=" + extensions);
needSeparator = true;
sj.add("file_extensions=" + extensions);
}
String description = getDescription();
if (description != null && !description.equals(getType())) {
if (needSeparator) {
sb.append(separator);
}
sb.append("description=" + description);
sj.add("description=" + description);
}
return sb.toString();
return sj.toString();
}
public String toString() {

@ -1,5 +1,5 @@
/*
* Copyright (c) 1995, 2013, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1995, 2014, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -64,6 +64,7 @@ import java.util.Iterator;
import java.util.HashSet;
import java.util.HashMap;
import java.util.Set;
import java.util.StringJoiner;
import sun.net.*;
import sun.net.www.*;
import sun.net.www.http.HttpClient;
@ -1386,16 +1387,11 @@ public class HttpURLConnection extends java.net.HttpURLConnection {
}
List<String> l = entry.getValue();
if (l != null && !l.isEmpty()) {
StringBuilder cookieValue = new StringBuilder();
StringJoiner cookieValue = new StringJoiner("; ");
for (String value : l) {
cookieValue.append(value).append("; ");
}
// strip off the trailing '; '
try {
requests.add(key, cookieValue.substring(0, cookieValue.length() - 2));
} catch (StringIndexOutOfBoundsException ignored) {
// no-op
cookieValue.add(value);
}
requests.add(key, cookieValue.toString());
}
}
}
@ -2870,20 +2866,14 @@ public class HttpURLConnection extends java.net.HttpURLConnection {
sun.misc.JavaNetHttpCookieAccess access =
sun.misc.SharedSecrets.getJavaNetHttpCookieAccess();
StringBuilder retValue = new StringBuilder();
StringJoiner retValue = new StringJoiner(","); // RFC 2965, comma separated
List<HttpCookie> cookies = access.parse(value);
boolean multipleCookies = false;
for (HttpCookie cookie : cookies) {
// skip HttpOnly cookies
if (cookie.isHttpOnly())
continue;
if (multipleCookies)
retValue.append(','); // RFC 2965, comma separated
retValue.append(access.header(cookie));
multipleCookies = true;
if (!cookie.isHttpOnly())
retValue.add(access.header(cookie));
}
return retValue.length() == 0 ? "" : retValue.toString();
return retValue.toString();
}
return value;

@ -32,6 +32,7 @@
package sun.util.locale;
import java.util.StringJoiner;
public final class BaseLocale {
@ -120,33 +121,20 @@ public final class BaseLocale {
@Override
public String toString() {
StringBuilder buf = new StringBuilder();
StringJoiner sj = new StringJoiner(", ");
if (language.length() > 0) {
buf.append("language=");
buf.append(language);
sj.add("language=" + language);
}
if (script.length() > 0) {
if (buf.length() > 0) {
buf.append(", ");
}
buf.append("script=");
buf.append(script);
sj.add("script=" + script);
}
if (region.length() > 0) {
if (buf.length() > 0) {
buf.append(", ");
}
buf.append("region=");
buf.append(region);
sj.add("region=" + region);
}
if (variant.length() > 0) {
if (buf.length() > 0) {
buf.append(", ");
}
buf.append("variant=");
buf.append(variant);
sj.add("variant=" + variant);
}
return buf.toString();
return sj.toString();
}
@Override

@ -37,6 +37,7 @@ import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.StringJoiner;
public class LanguageTag {
//
@ -473,21 +474,18 @@ public class LanguageTag {
}
if (!varitr.isDone()) {
// ill-formed variant subtags
StringBuilder buf = new StringBuilder();
StringJoiner sj = new StringJoiner(SEP);
while (!varitr.isDone()) {
String prvv = varitr.current();
if (!isPrivateuseSubtag(prvv)) {
// cannot use private use subtag - truncated
break;
}
if (buf.length() > 0) {
buf.append(SEP);
}
buf.append(prvv);
sj.add(prvv);
varitr.next();
}
if (buf.length() > 0) {
privuseVar = buf.toString();
if (sj.length() > 0) {
privuseVar = sj.toString();
}
}
}

@ -1,6 +1,6 @@
/*
* Copyright (c) 2010, 2011, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2010, 2014, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -38,6 +38,7 @@ import java.util.Map.Entry;
import java.util.Set;
import java.util.SortedMap;
import java.util.SortedSet;
import java.util.StringJoiner;
public class UnicodeLocaleExtension extends Extension {
public static final char SINGLETON = 'u';
@ -70,20 +71,20 @@ public class UnicodeLocaleExtension extends Extension {
}
if (!this.attributes.isEmpty() || !this.keywords.isEmpty()) {
StringBuilder sb = new StringBuilder();
StringJoiner sj = new StringJoiner(LanguageTag.SEP);
for (String attribute : this.attributes) {
sb.append(LanguageTag.SEP).append(attribute);
sj.add(attribute);
}
for (Entry<String, String> keyword : this.keywords.entrySet()) {
String key = keyword.getKey();
String value = keyword.getValue();
sb.append(LanguageTag.SEP).append(key);
sj.add(key);
if (value.length() > 0) {
sb.append(LanguageTag.SEP).append(value);
sj.add(value);
}
}
setValue(sb.substring(1)); // skip leading '-'
setValue(sj.toString());
}
}

@ -547,12 +547,7 @@ class RuleBasedBreakIterator extends BreakIterator {
*/
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append('[');
sb.append("checksum=0x");
sb.append(Long.toHexString(checksum));
sb.append(']');
return sb.toString();
return "[checksum=0x" + Long.toHexString(checksum) + ']';
}
/**

@ -1,5 +1,5 @@
/*
* Copyright (c) 2005, 2006, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2005, 2014, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -26,7 +26,7 @@
package javax.smartcardio;
import java.io.*;
import java.util.StringJoiner;
import java.security.Permission;
/**
@ -180,20 +180,14 @@ public class CardPermission extends Permission {
if (mask == A_ALL) {
return S_ALL;
}
boolean first = true;
StringBuilder sb = new StringBuilder();
StringJoiner sj = new StringJoiner(",");
for (int i = 0; i < ARRAY_MASKS.length; i++) {
int action = ARRAY_MASKS[i];
final int action = ARRAY_MASKS[i];
if ((mask & action) == action) {
if (first == false) {
sb.append(",");
} else {
first = false;
}
sb.append(ARRAY_STRINGS[i]);
sj.add(ARRAY_STRINGS[i]);
}
}
return sb.toString();
return sj.toString();
}