# HG changeset patch # User igerasim # Date 1409162899 -14400 # Node ID 1a19360ff12260cf45be79737917db34e869da5a # Parent 98453f165e21495c7fb81816c55b1ef3aa9b9b66 8054714: Use StringJoiner where it makes the code cleaner Reviewed-by: psandoz, redestad diff -r 98453f165e21 -r 1a19360ff122 jdk/src/java.base/share/classes/java/io/FilePermission.java --- a/jdk/src/java.base/share/classes/java/io/FilePermission.java Wed Aug 27 11:33:45 2014 +0800 +++ b/jdk/src/java.base/share/classes/java/io/FilePermission.java Wed Aug 27 22:08:19 2014 +0400 @@ -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.ArrayList; import java.util.Vector; import java.util.Collections; +import java.util.StringJoiner; import sun.security.util.SecurityConstants; /** @@ -556,39 +557,25 @@ * @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) { + sj.add("write"); } - - if ((mask & WRITE) == WRITE) { - if (comma) sb.append(','); - else comma = true; - sb.append("write"); + if ((mask & EXECUTE) == EXECUTE) { + sj.add("execute"); + } + if ((mask & DELETE) == DELETE) { + sj.add("delete"); + } + if ((mask & READLINK) == READLINK) { + sj.add("readlink"); } - if ((mask & EXECUTE) == EXECUTE) { - if (comma) sb.append(','); - else comma = true; - sb.append("execute"); - } - - if ((mask & DELETE) == DELETE) { - if (comma) sb.append(','); - else comma = true; - sb.append("delete"); - } - - if ((mask & READLINK) == READLINK) { - if (comma) sb.append(','); - else comma = true; - sb.append("readlink"); - } - - return sb.toString(); + return sj.toString(); } /** diff -r 98453f165e21 -r 1a19360ff122 jdk/src/java.base/share/classes/java/io/ObjectOutputStream.java --- a/jdk/src/java.base/share/classes/java/io/ObjectOutputStream.java Wed Aug 27 11:33:45 2014 +0800 +++ b/jdk/src/java.base/share/classes/java/io/ObjectOutputStream.java Wed Aug 27 22:08:19 2014 +0400 @@ -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.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 @@ * 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(); } } diff -r 98453f165e21 -r 1a19360ff122 jdk/src/java.base/share/classes/java/lang/Class.java --- a/jdk/src/java.base/share/classes/java/lang/Class.java Wed Aug 27 11:33:45 2014 +0800 +++ b/jdk/src/java.base/share/classes/java/lang/Class.java Wed Aug 27 22:08:19 2014 +0400 @@ -54,6 +54,7 @@ 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 @@ 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 */ diff -r 98453f165e21 -r 1a19360ff122 jdk/src/java.base/share/classes/java/lang/invoke/MethodType.java --- a/jdk/src/java.base/share/classes/java/lang/invoke/MethodType.java Wed Aug 27 11:33:45 2014 +0800 +++ b/jdk/src/java.base/share/classes/java/lang/invoke/MethodType.java Wed Aug 27 22:08:19 2014 +0400 @@ -33,6 +33,7 @@ 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 @@ */ @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(); } diff -r 98453f165e21 -r 1a19360ff122 jdk/src/java.base/share/classes/java/net/SocketPermission.java --- a/jdk/src/java.base/share/classes/java/net/SocketPermission.java Wed Aug 27 11:33:45 2014 +0800 +++ b/jdk/src/java.base/share/classes/java/net/SocketPermission.java Wed Aug 27 22:08:19 2014 +0400 @@ -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.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 @@ * @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(); } /** diff -r 98453f165e21 -r 1a19360ff122 jdk/src/java.base/share/classes/java/text/CalendarBuilder.java --- a/jdk/src/java.base/share/classes/java/text/CalendarBuilder.java Wed Aug 27 11:33:45 2014 +0800 +++ b/jdk/src/java.base/share/classes/java/text/CalendarBuilder.java Wed Aug 27 22:08:19 2014 +0400 @@ -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 @@ } 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) { diff -r 98453f165e21 -r 1a19360ff122 jdk/src/java.base/share/classes/java/util/Locale.java --- a/jdk/src/java.base/share/classes/java/util/Locale.java Wed Aug 27 11:33:45 2014 +0800 +++ b/jdk/src/java.base/share/classes/java/util/Locale.java Wed Aug 27 22:08:19 2014 +0400 @@ -2059,14 +2059,11 @@ // 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 diff -r 98453f165e21 -r 1a19360ff122 jdk/src/java.base/share/classes/java/util/PropertyPermission.java --- a/jdk/src/java.base/share/classes/java/util/PropertyPermission.java Wed Aug 27 11:33:45 2014 +0800 +++ b/jdk/src/java.base/share/classes/java/util/PropertyPermission.java Wed Aug 27 22:08:19 2014 +0400 @@ -324,20 +324,16 @@ * @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(); } /** diff -r 98453f165e21 -r 1a19360ff122 jdk/src/java.base/share/classes/sun/net/www/MimeEntry.java --- a/jdk/src/java.base/share/classes/sun/net/www/MimeEntry.java Wed Aug 27 11:33:45 2014 +0800 +++ b/jdk/src/java.base/share/classes/sun/net/www/MimeEntry.java Wed Aug 27 22:08:19 2014 +0400 @@ -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 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() { diff -r 98453f165e21 -r 1a19360ff122 jdk/src/java.base/share/classes/sun/net/www/protocol/http/HttpURLConnection.java --- a/jdk/src/java.base/share/classes/sun/net/www/protocol/http/HttpURLConnection.java Wed Aug 27 11:33:45 2014 +0800 +++ b/jdk/src/java.base/share/classes/sun/net/www/protocol/http/HttpURLConnection.java Wed Aug 27 22:08:19 2014 +0400 @@ -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.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 @@ } List l = entry.getValue(); if (l != null && !l.isEmpty()) { - StringBuilder cookieValue = new StringBuilder(); + StringJoiner cookieValue = new StringJoiner("; "); for (String value : l) { - cookieValue.append(value).append("; "); + cookieValue.add(value); } - // strip off the trailing '; ' - try { - requests.add(key, cookieValue.substring(0, cookieValue.length() - 2)); - } catch (StringIndexOutOfBoundsException ignored) { - // no-op - } + requests.add(key, cookieValue.toString()); } } } @@ -2870,20 +2866,14 @@ sun.misc.JavaNetHttpCookieAccess access = sun.misc.SharedSecrets.getJavaNetHttpCookieAccess(); - StringBuilder retValue = new StringBuilder(); + StringJoiner retValue = new StringJoiner(","); // RFC 2965, comma separated List 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; diff -r 98453f165e21 -r 1a19360ff122 jdk/src/java.base/share/classes/sun/util/locale/BaseLocale.java --- a/jdk/src/java.base/share/classes/sun/util/locale/BaseLocale.java Wed Aug 27 11:33:45 2014 +0800 +++ b/jdk/src/java.base/share/classes/sun/util/locale/BaseLocale.java Wed Aug 27 22:08:19 2014 +0400 @@ -32,6 +32,7 @@ package sun.util.locale; +import java.util.StringJoiner; public final class BaseLocale { @@ -120,33 +121,20 @@ @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 diff -r 98453f165e21 -r 1a19360ff122 jdk/src/java.base/share/classes/sun/util/locale/LanguageTag.java --- a/jdk/src/java.base/share/classes/sun/util/locale/LanguageTag.java Wed Aug 27 11:33:45 2014 +0800 +++ b/jdk/src/java.base/share/classes/sun/util/locale/LanguageTag.java Wed Aug 27 22:08:19 2014 +0400 @@ -37,6 +37,7 @@ import java.util.List; import java.util.Map; import java.util.Set; +import java.util.StringJoiner; public class LanguageTag { // @@ -473,21 +474,18 @@ } 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(); } } } diff -r 98453f165e21 -r 1a19360ff122 jdk/src/java.base/share/classes/sun/util/locale/UnicodeLocaleExtension.java --- a/jdk/src/java.base/share/classes/sun/util/locale/UnicodeLocaleExtension.java Wed Aug 27 11:33:45 2014 +0800 +++ b/jdk/src/java.base/share/classes/sun/util/locale/UnicodeLocaleExtension.java Wed Aug 27 22:08:19 2014 +0400 @@ -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.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 @@ } 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 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()); } } diff -r 98453f165e21 -r 1a19360ff122 jdk/src/java.base/share/classes/sun/util/locale/provider/RuleBasedBreakIterator.java --- a/jdk/src/java.base/share/classes/sun/util/locale/provider/RuleBasedBreakIterator.java Wed Aug 27 11:33:45 2014 +0800 +++ b/jdk/src/java.base/share/classes/sun/util/locale/provider/RuleBasedBreakIterator.java Wed Aug 27 22:08:19 2014 +0400 @@ -547,12 +547,7 @@ */ @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) + ']'; } /** diff -r 98453f165e21 -r 1a19360ff122 jdk/src/java.smartcardio/share/classes/javax/smartcardio/CardPermission.java --- a/jdk/src/java.smartcardio/share/classes/javax/smartcardio/CardPermission.java Wed Aug 27 11:33:45 2014 +0800 +++ b/jdk/src/java.smartcardio/share/classes/javax/smartcardio/CardPermission.java Wed Aug 27 22:08:19 2014 +0400 @@ -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 @@ 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(); }