s/StringBuffer/StringBuilder, where possible.

Deprecated versions of the methods in StringUtil will continue to take
a StringBuffer argument for now.


git-svn-id: https://samskivert.googlecode.com/svn/trunk@1855 6335cc39-0255-0410-8fd6-9bcaacd3b74c
This commit is contained in:
ray
2006-06-13 21:58:37 +00:00
parent 9d6c997d8a
commit ce7a0f1c9f
25 changed files with 116 additions and 87 deletions
+1 -1
View File
@@ -220,7 +220,7 @@ public class JDBCUtil
*/
public static String escape (Object[] values)
{
StringBuffer buf = new StringBuffer();
StringBuilder buf = new StringBuilder();
for (int ii = 0; ii < values.length; ii++) {
if (ii > 0) {
buf.append(", ");
@@ -367,7 +367,7 @@ public class SimpleRepository extends Repository
{
ResultSetMetaData md = rs.getMetaData();
int ccount = md.getColumnCount();
StringBuffer buf = new StringBuffer();
StringBuilder buf = new StringBuilder();
for (int ii = 1; ii <= ccount; ii++) {
if (buf.length() > 0) {
buf.append(", ");
@@ -151,7 +151,7 @@ public class FieldMask
public String toString ()
{
// return a list of the modified fields
StringBuffer buf = new StringBuffer("FieldMask [modified={");
StringBuilder buf = new StringBuilder("FieldMask [modified={");
boolean added = false;
for (Map.Entry<String,Integer> entry : _descripMap.entrySet()) {
if (_modified[entry.getValue().intValue()]) {
+4 -4
View File
@@ -589,7 +589,7 @@ public class Table<T>
protected final String buildListOfAssignments (FieldMask mask)
{
StringBuffer sql = new StringBuffer();
StringBuilder sql = new StringBuilder();
int fcount = fields.length;
for (int i = 0; i < fcount; i++) {
// skip non-modified fields
@@ -672,7 +672,7 @@ public class Table<T>
protected final String buildUpdateWhere()
{
StringBuffer sql = new StringBuffer();
StringBuilder sql = new StringBuilder();
sql.append(" where ").append(primaryKeys[0]).append(" = ?");
for (int i = 1; i < primaryKeys.length; i++) {
sql.append(" and ").append(primaryKeys[i]).append(" = ?");
@@ -682,7 +682,7 @@ public class Table<T>
protected final String buildQueryList(T qbe, FieldMask mask, boolean like)
{
StringBuffer buf = new StringBuffer();
StringBuilder buf = new StringBuilder();
buildQueryList(buf, qbe, 0, nFields, mask, like);
if (buf.length() > 0) {
buf.insert(0, " where ");
@@ -784,7 +784,7 @@ public class Table<T>
}
protected final void buildQueryList (
StringBuffer buf, Object qbe, int i, int end, FieldMask mask,
StringBuilder buf, Object qbe, int i, int end, FieldMask mask,
boolean like)
{
try {
@@ -54,7 +54,7 @@ public class HttpPostUtil
BufferedReader reader = new BufferedReader(
new InputStreamReader(conn.getInputStream()));
StringBuffer buf = new StringBuffer();
StringBuilder buf = new StringBuilder();
for (String s; null != (s = reader.readLine()); ) {
buf.append(s);
}
+1 -1
View File
@@ -129,7 +129,7 @@ public class MACUtil
Process p = Runtime.getRuntime().exec(cmd);
BufferedReader cin = new BufferedReader(
new InputStreamReader(p.getInputStream()));
StringBuffer buffer= new StringBuffer();
StringBuilder buffer= new StringBuilder();
String line = "";
while (line != null)
+3 -3
View File
@@ -162,7 +162,7 @@ public class CDDB
_in.readLine();
// send a hello request
StringBuffer req = new StringBuffer("cddb hello ");
StringBuilder req = new StringBuilder("cddb hello ");
req.append(username).append(" ");
req.append(localhost).append(" ");
req.append(CLIENT_NAME).append(" ");
@@ -246,7 +246,7 @@ public class CDDB
}
// construct the query parameter
StringBuffer req = new StringBuffer("cddb query ");
StringBuilder req = new StringBuilder("cddb query ");
req.append(discid).append(" ");
req.append(frameOffsets.length).append(" ");
for (int i = 0; i < frameOffsets.length; i++) {
@@ -327,7 +327,7 @@ public class CDDB
}
// construct the query
StringBuffer req = new StringBuffer("cddb read ");
StringBuilder req = new StringBuilder("cddb read ");
req.append(category).append(" ");
req.append(discid);
@@ -479,7 +479,7 @@ public class UserRepository extends JORARepository
protected String genIdString (int[] userIds)
{
// build up the string we need for the query
StringBuffer ids = new StringBuffer();
StringBuilder ids = new StringBuilder();
for (int i = 0; i < userIds.length; i++) {
if (ids.length() > 0) {
ids.append(",");
@@ -34,7 +34,7 @@ public class UserUtil
public static String genAuthCode (User user)
{
// concatenate a bunch of secret stuff together
StringBuffer buf = new StringBuffer();
StringBuilder buf = new StringBuilder();
buf.append(user.password);
buf.append(System.currentTimeMillis());
buf.append(Math.random());
+1 -1
View File
@@ -49,7 +49,7 @@ public class DimenInfo
public String toString ()
{
StringBuffer buf = new StringBuffer();
StringBuilder buf = new StringBuilder();
buf.append("[count=").append(count);
buf.append(", totwid=").append(totwid);
buf.append(", tothei=").append(tothei);
+1 -1
View File
@@ -55,7 +55,7 @@ public class TestUtil
testdir = ".";
}
StringBuffer rpath = new StringBuffer(testdir);
StringBuilder rpath = new StringBuilder(testdir);
if (!path.startsWith("/")) {
rpath.append("/");
}
@@ -40,7 +40,7 @@ public class MessageUtil
*/
public static String compose (String key, Object[] args)
{
StringBuffer buf = new StringBuffer();
StringBuilder buf = new StringBuilder();
buf.append(key);
buf.append('|');
for (int i = 0; i < args.length; i++) {
@@ -87,7 +87,7 @@ public class MessageUtil
return value;
}
StringBuffer buf = new StringBuffer();
StringBuilder buf = new StringBuilder();
int vlength = value.length();
for (int i = 0; i < vlength; i++) {
char ch = value.charAt(i);
+1 -1
View File
@@ -399,7 +399,7 @@ public class ConfigUtil
throw new IOException(errmsg);
} else if (crowns.size() > 1) {
StringBuffer errmsg = new StringBuffer();
StringBuilder errmsg = new StringBuilder();
errmsg.append("Multiple top-level properties were found, ");
errmsg.append("one definitive top-level file must provide ");
errmsg.append("an order for all others:\n");
+1 -1
View File
@@ -582,7 +582,7 @@ public class Crypt
salt += "A";
}
StringBuffer buffer = new StringBuffer(" ");
StringBuilder buffer = new StringBuilder(" ");
char charZero = salt.charAt(0);
char charOne = salt.charAt(1);
+1 -1
View File
@@ -91,7 +91,7 @@ public class Histogram
public String summarize ()
{
long total = 0;
StringBuffer buf = new StringBuffer();
StringBuilder buf = new StringBuilder();
buf.append(_count).append(":");
for (int ii = 0; ii < _buckets.length; ii++) {
if (ii > 0) {
+1 -1
View File
@@ -275,7 +275,7 @@ public class IntIntMap
*/
public String toString ()
{
StringBuffer buf = new StringBuffer("[");
StringBuilder buf = new StringBuilder("[");
int[] keys = getKeys();
for (int ii = 0; ii < keys.length; ii++) {
if (ii > 0) {
+1 -2
View File
@@ -18,7 +18,6 @@
package com.samskivert.util;
import java.lang.StringBuffer;
import java.util.Random;
/**
@@ -43,7 +42,7 @@ public class KeyUtil
public static String generateRandomKey (Random rand, int length)
{
int numKeyChars = KEY_CHARS.length();
StringBuffer buf = new StringBuffer();
StringBuilder buf = new StringBuilder();
for (int ii = 0; ii < length; ii++) {
buf.append(KEY_CHARS.charAt(rand.nextInt(numKeyChars)));
}
+1 -1
View File
@@ -239,7 +239,7 @@ public class Queue
public String toString ()
{
StringBuffer buf = new StringBuffer();
StringBuilder buf = new StringBuilder();
buf.append("[count=").append(_count);
buf.append(", size=").append(_size);
+80 -50
View File
@@ -39,6 +39,7 @@ import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Enumeration;
import java.util.HashMap;
@@ -99,7 +100,7 @@ public class StringUtil
*/
public static String capitalize (String s)
{
if (blank(s)) {
if (isBlank(s)) {
return s;
}
char c = s.charAt(0);
@@ -127,7 +128,7 @@ public class StringUtil
return null;
}
int nn = source.length();
StringBuffer buf = new StringBuffer(nn);
StringBuilder buf = new StringBuilder(nn);
for (int ii=0; ii < nn; ii++) {
char c = source.charAt(ii);
if (validator.isValid(c)) {
@@ -143,7 +144,7 @@ public class StringUtil
*/
public static String sanitize (String source, String charRegex)
{
final StringBuffer buf = new StringBuffer(" ");
final StringBuilder buf = new StringBuilder(" ");
final Matcher matcher = Pattern.compile(charRegex).matcher(buf);
return sanitize(source, new CharacterValidator() {
public boolean isValid (char c) {
@@ -204,7 +205,7 @@ public class StringUtil
*/
public static String restrictHTML (String src, String[] regexes)
{
if (blank(src)) {
if (isBlank(src)) {
return src;
}
@@ -225,7 +226,7 @@ public class StringUtil
// now, the even elements of list contain untrusted text, the
// odd elements contain stuff that matched a regex
StringBuffer buf = new StringBuffer();
StringBuilder buf = new StringBuilder();
for (int jj=0, nn = list.size(); jj < nn; jj++) {
String s = list.get(jj);
if (jj % 2 == 0) {
@@ -248,7 +249,7 @@ public class StringUtil
return source;
}
StringBuffer sb = new StringBuffer(source.length() + 32);
StringBuilder sb = new StringBuilder(source.length() + 32);
int blength = before.length();
int start = 0;
@@ -317,11 +318,9 @@ public class StringUtil
*/
public static String fill (char c, int count)
{
StringBuffer buf = new StringBuffer();
for (int ii = 0; ii < count; ii++) {
buf.append(c);
}
return buf.toString();
char[] sameChars = new char[count];
Arrays.fill(sameChars, c);
return new String(sameChars);
}
/**
@@ -384,7 +383,7 @@ public class StringUtil
*/
public static String toString (Object val)
{
StringBuffer buf = new StringBuffer();
StringBuilder buf = new StringBuilder();
toString(buf, val);
return buf.toString();
}
@@ -399,7 +398,7 @@ public class StringUtil
public static String toString (
Object val, String openBox, String closeBox)
{
StringBuffer buf = new StringBuffer();
StringBuilder buf = new StringBuilder();
toString(buf, val, openBox, closeBox);
return buf.toString();
}
@@ -412,7 +411,11 @@ public class StringUtil
* @param buf the string buffer to which we will append the string.
* @param val the value from which to generate the string.
*/
public static void toString (StringBuffer buf, Object val)
public static void toString (StringBuilder buf, Object val)
{
toString(buf, val, "(", ")");
}
@Deprecated public static void toString (StringBuffer buf, Object val)
{
toString(buf, val, "(", ")");
}
@@ -429,8 +432,13 @@ public class StringUtil
* @param openBox the opening box character.
* @param closeBox the closing box character.
*/
public static void toString (StringBuffer buf, Object val,
String openBox, String closeBox)
public static void toString (
StringBuilder buf, Object val, String openBox, String closeBox)
{
toString(buf, val, openBox, closeBox, ", ");
}
@Deprecated public static void toString (
StringBuffer buf, Object val, String openBox, String closeBox)
{
toString(buf, val, openBox, closeBox, ", ");
}
@@ -448,7 +456,7 @@ public class StringUtil
* @param closeBox the closing box character.
* @param sep the separator string.
*/
public static void toString (StringBuffer buf, Object val,
public static void toString (StringBuilder buf, Object val,
String openBox, String closeBox, String sep)
{
if (val instanceof byte[]) {
@@ -539,8 +547,8 @@ public class StringUtil
}
buf.append(closeBox);
} else if (val instanceof Collection) {
toString(buf, ((Collection)val).iterator(), openBox, closeBox);
} else if (val instanceof Iterable) {
toString(buf, ((Iterable)val).iterator(), openBox, closeBox);
} else if (val instanceof Iterator) {
buf.append(openBox);
@@ -587,6 +595,14 @@ public class StringUtil
buf.append(val);
}
}
@Deprecated public static void toString (
StringBuffer buf, Object val, String openBox, String closeBox,
String sep)
{
StringBuilder sb = new StringBuilder();
toString(sb, val, openBox, closeBox, sep);
buf.append(sb);
}
/**
* Used to format objects in {@link
@@ -629,7 +645,7 @@ public class StringUtil
*/
public static String listToString (Object val, Formatter formatter)
{
StringBuffer buf = new StringBuffer();
StringBuilder buf = new StringBuilder();
listToString(buf, val, formatter);
return buf.toString();
}
@@ -640,7 +656,7 @@ public class StringUtil
* #listToString(Object,StringUtil.Formatter)} for more details.
*/
public static void listToString (
StringBuffer buf, Object val, Formatter formatter)
StringBuilder buf, Object val, Formatter formatter)
{
// get an iterator if this is a collection
if (val instanceof Collection) {
@@ -688,6 +704,13 @@ public class StringUtil
toString(buf, val);
}
}
@Deprecated public static void listToString (
StringBuffer buf, Object val, Formatter formatter)
{
StringBuilder sb = new StringBuilder();
listToString(sb, val, formatter);
buf.append(sb);
}
/**
* Generates a string representation of the supplied object by calling
@@ -707,7 +730,7 @@ public class StringUtil
*/
public static String fieldsToString (Object object, String sep)
{
StringBuffer buf = new StringBuffer("[");
StringBuilder buf = new StringBuilder("[");
fieldsToString(buf, object, sep);
return buf.append("]").toString();
}
@@ -723,17 +746,22 @@ public class StringUtil
* <p>Note: unlike the version of this method that returns a string,
* enclosing brackets are not included in the output of this method.
*/
public static void fieldsToString (StringBuffer buf, Object object)
public static void fieldsToString (StringBuilder buf, Object object)
{
fieldsToString(buf, object, ", ");
}
@Deprecated public static void fieldsToString (
StringBuffer buf, Object object)
{
fieldsToString(buf, object, ", ");
}
/**
* Like {@link #fieldsToString(StringBuffer,Object)} except that the
* Like {@link #fieldsToString(StringBuilder,Object)} except that the
* supplied separator will be used between fields.
*/
public static void fieldsToString (
StringBuffer buf, Object object, String sep)
StringBuilder buf, Object object, String sep)
{
Class clazz = object.getClass();
Field[] fields = clazz.getFields();
@@ -767,6 +795,13 @@ public class StringUtil
written++;
}
}
@Deprecated public static void fieldsToString (
StringBuffer buf, Object object, String sep)
{
StringBuilder sb = new StringBuilder();
fieldsToString(sb, object, sep);
buf.append(sb);
}
/**
* Formats a pair of coordinates such that positive values are
@@ -776,7 +811,7 @@ public class StringUtil
*/
public static String coordsToString (int x, int y)
{
StringBuffer buf = new StringBuffer();
StringBuilder buf = new StringBuilder();
coordsToString(buf, x, y);
return buf.toString();
}
@@ -787,7 +822,7 @@ public class StringUtil
* prefix. Examples would look like: <code>+3+4</code>
* <code>-5+7</code>, etc.
*/
public static void coordsToString (StringBuffer buf, int x, int y)
public static void coordsToString (StringBuilder buf, int x, int y)
{
if (x >= 0) {
buf.append("+");
@@ -798,6 +833,13 @@ public class StringUtil
}
buf.append(y);
}
@Deprecated public static void coordsToString (
StringBuffer buf, int x, int y)
{
StringBuilder sb = new StringBuilder();
coordsToString(sb, x, y);
buf.append(sb);
}
/**
* Attempts to generate a string representation of the object using
@@ -1156,7 +1198,7 @@ public class StringUtil
protected static String join (
Object[] values, String separator, boolean escape)
{
StringBuffer buf = new StringBuffer();
StringBuilder buf = new StringBuilder();
int vlength = values.length;
for (int i = 0; i < vlength; i++) {
if (i > 0) {
@@ -1231,8 +1273,8 @@ public class StringUtil
public static String toMatrixString (
int[] values, int colCount, int fieldWidth)
{
StringBuffer buf = new StringBuffer();
StringBuffer valbuf = new StringBuffer();
StringBuilder buf = new StringBuilder();
StringBuilder valbuf = new StringBuilder();
for (int i = 0; i < values.length; i++) {
// format the integer value
@@ -1265,7 +1307,7 @@ public class StringUtil
*/
public static String intervalToString (long millis)
{
StringBuffer buf = new StringBuffer();
StringBuilder buf = new StringBuilder();
boolean started = false;
long days = millis / (24 * 60 * 60 * 1000);
@@ -1343,7 +1385,7 @@ public class StringUtil
public static String unStudlyName (String name)
{
boolean seenLower = false;
StringBuffer nname = new StringBuffer();
StringBuilder nname = new StringBuilder();
int nlen = name.length();
for (int i = 0; i < nlen; i++) {
char c = name.charAt(i);
@@ -1362,7 +1404,7 @@ public class StringUtil
}
/**
* See {@link #stringCode(String,StringBuffer)}.
* See {@link #stringCode(String,StringBuilder)}.
*/
public static int stringCode (String value)
{
@@ -1387,7 +1429,7 @@ public class StringUtil
* @param encoded if non-null, a string buffer into which the
* characters used for the encoding will be recorded.
*/
public static int stringCode (String value, StringBuffer encoded)
public static int stringCode (String value, StringBuilder encoded)
{
int code = 0;
for (int ii = 0, uu = 0; ii < value.length(); ii++) {
@@ -1424,22 +1466,10 @@ public class StringUtil
protected static final HashIntMap<Integer> _letterToBits =
new HashIntMap<Integer>();
static {
_letterToBits.put('e', Integer.valueOf(0));
_letterToBits.put('t', Integer.valueOf(1));
_letterToBits.put('a', Integer.valueOf(2));
_letterToBits.put('o', Integer.valueOf(3));
_letterToBits.put('i', Integer.valueOf(4));
_letterToBits.put('n', Integer.valueOf(5));
_letterToBits.put('s', Integer.valueOf(6));
_letterToBits.put('r', Integer.valueOf(7));
_letterToBits.put('h', Integer.valueOf(8));
_letterToBits.put('l', Integer.valueOf(9));
_letterToBits.put('d', Integer.valueOf(10));
_letterToBits.put('c', Integer.valueOf(11));
_letterToBits.put('u', Integer.valueOf(12));
_letterToBits.put('m', Integer.valueOf(13));
_letterToBits.put('f', Integer.valueOf(14));
_letterToBits.put('p', Integer.valueOf(15));
String mostCommon = "etaoinsrhldcumfp";
for (int ii = mostCommon.length() - 1; ii >= 0; ii--) {
_letterToBits.put(mostCommon.charAt(ii), Integer.valueOf(ii));
}
// sorry g, w, y, b, v, k, x, j, q, z
}
}
+1 -1
View File
@@ -178,7 +178,7 @@ public class SystemInfo
*/
public String toString ()
{
StringBuffer buf = new StringBuffer();
StringBuilder buf = new StringBuilder();
buf.append("OS: ").append(osToString()).append("\n");
buf.append("JVM: ").append(jvmToString()).append("\n");
buf.append("Memory: ").append(memoryToString()).append("\n");
@@ -270,7 +270,7 @@ public class FormTool
*/
public String fixedCheckbox (String name, boolean value)
{
StringBuffer buf = new StringBuffer();
StringBuilder buf = new StringBuilder();
buf.append("<input type=\"checkbox\"");
buf.append(" name=\"").append(name).append("\"");
if (value) {
@@ -298,7 +298,7 @@ public class FormTool
public String fixedOption (
String name, String value, String item, Object selectedValue)
{
StringBuffer buf = new StringBuffer();
StringBuilder buf = new StringBuilder();
buf.append("<option value=\"").append(value).append("\"");
if (selectedValue.equals(value)) {
buf.append(" selected");
@@ -320,7 +320,7 @@ public class FormTool
*/
public String radio (String name, String value, String defaultValue)
{
StringBuffer buf = new StringBuffer();
StringBuilder buf = new StringBuilder();
buf.append("<input type=\"radio\"");
buf.append(" name=\"").append(name).append("\"");
buf.append(" value=\"").append(value).append("\"");
@@ -347,7 +347,7 @@ public class FormTool
*/
public String fixedTextarea (String name, String extra, Object value)
{
StringBuffer buf = new StringBuffer();
StringBuilder buf = new StringBuilder();
buf.append("<textarea name=\"").append(name).append("\"");
if (!StringUtil.isBlank(extra)) {
buf.append(" ").append(extra);
@@ -378,7 +378,7 @@ public class FormTool
protected String fixedInput (
String type, String name, Object value, String extra)
{
StringBuffer buf = new StringBuffer();
StringBuilder buf = new StringBuilder();
buf.append("<input type=\"").append(type).append("\"");
buf.append(" name=\"").append(name).append("\"");
buf.append(" value=\"").append(value).append("\"");
@@ -115,7 +115,7 @@ public class InvocationContext extends VelocityContext
*/
public String encodeAllParameters ()
{
StringBuffer buf = new StringBuffer();
StringBuilder buf = new StringBuilder();
Enumeration e = _req.getParameterNames();
while (e.hasMoreElements()) {
if (buf.length() > 0) {
@@ -62,7 +62,7 @@ public class SetNextFieldRule extends Rule
*/
public String toString ()
{
StringBuffer sb = new StringBuffer("SetNextFieldRule[");
StringBuilder sb = new StringBuilder("SetNextFieldRule[");
sb.append("fieldName=");
sb.append(_fieldName);
sb.append("]");
@@ -45,7 +45,7 @@ public class SimpleParser extends DefaultHandler
public void endElement (String uri, String localName, String qName)
{
finishElement(uri, localName, qName, _chars.toString().trim());
_chars = new StringBuffer();
_chars = new StringBuilder();
}
/**
@@ -76,7 +76,7 @@ public class SimpleParser extends DefaultHandler
{
try {
// read the XML input stream and construct the scene object
_chars = new StringBuffer();
_chars = new StringBuilder();
XMLUtil.parse(this, stream);
} catch (ParserConfigurationException pce) {
@@ -125,5 +125,5 @@ public class SimpleParser extends DefaultHandler
}
/** The character data gathered while parsing. */
protected StringBuffer _chars;
protected StringBuilder _chars;
}
@@ -99,7 +99,7 @@ public class ValidatedSetNextRule extends Rule
*/
public String toString ()
{
StringBuffer sb = new StringBuffer("ValidatedSetNextRule[");
StringBuilder sb = new StringBuilder("ValidatedSetNextRule[");
sb.append("methodName=");
sb.append(_methodName);
if (_paramType != null) {