提交 86c8c165 编写于 作者: R rupashka

6432566: Replace usage of StringBuffer with StringBuilder in Swing

Reviewed-by: malenkov
上级 ea742e00
......@@ -1322,13 +1322,11 @@ public class DebugGraphics extends Graphics {
}
String toShortString() {
StringBuffer buffer = new StringBuffer("Graphics" + (isDrawingBuffer() ? "<B>" : "") + "(" + graphicsID + "-" + debugOptions + ")");
return buffer.toString();
return "Graphics" + (isDrawingBuffer() ? "<B>" : "") + "(" + graphicsID + "-" + debugOptions + ")";
}
String pointToString(int x, int y) {
StringBuffer buffer = new StringBuffer("(" + x + ", " + y + ")");
return buffer.toString();
return "(" + x + ", " + y + ")";
}
/** Enables/disables diagnostic information about every graphics
......
......@@ -1334,13 +1334,13 @@ public class DefaultCaret extends Rectangle implements Caret, FocusListener, Mou
&& component.getClientProperty("JPasswordField.cutCopyAllowed") !=
Boolean.TRUE) {
//fix for 4793761
StringBuffer txt = null;
StringBuilder txt = null;
char echoChar = ((JPasswordField)component).getEchoChar();
int p0 = Math.min(getDot(), getMark());
int p1 = Math.max(getDot(), getMark());
for (int i = p0; i < p1; i++) {
if (txt == null) {
txt = new StringBuffer();
txt = new StringBuilder();
}
txt.append(echoChar);
}
......@@ -1675,7 +1675,6 @@ public class DefaultCaret extends Rectangle implements Caret, FocusListener, Mou
}
return;
}
int adjust = 0;
int offset = e.getOffset();
int length = e.getLength();
int newDot = dot;
......@@ -1759,7 +1758,6 @@ public class DefaultCaret extends Rectangle implements Caret, FocusListener, Mou
}
int offs0 = e.getOffset();
int offs1 = offs0 + e.getLength();
int adjust = 0;
int newDot = dot;
boolean adjustDotBias = false;
int newMark = mark;
......
......@@ -132,7 +132,7 @@ public class DefaultStyledDocument extends AbstractDocument implements StyledDoc
// install the content
Content c = getContent();
int n = data.length;
StringBuffer sb = new StringBuffer();
StringBuilder sb = new StringBuilder();
for (int i = 0; i < n; i++) {
ElementSpec es = data[i];
if (es.getLength() > 0) {
......@@ -191,7 +191,7 @@ public class DefaultStyledDocument extends AbstractDocument implements StyledDoc
// install the content
Content c = getContent();
int n = data.length;
StringBuffer sb = new StringBuffer();
StringBuilder sb = new StringBuilder();
for (int i = 0; i < n; i++) {
ElementSpec es = data[i];
if (es.getLength() > 0) {
......
......@@ -30,7 +30,6 @@ import java.text.*;
import java.text.AttributedCharacterIterator.Attribute;
import java.util.*;
import javax.swing.*;
import javax.swing.text.*;
/**
* <code>InternationalFormatter</code> extends <code>DefaultFormatter</code>,
......@@ -875,7 +874,6 @@ public class InternationalFormatter extends DefaultFormatter {
(f instanceof AttributedCharacterIterator.Attribute)) {
AttributedCharacterIterator.Attribute field =
(AttributedCharacterIterator.Attribute)f;
int index = 0;
iterator.first();
while (iterator.getIndex() < start) {
......
......@@ -35,10 +35,7 @@ import java.util.HashMap;
import java.util.Hashtable;
import java.util.Enumeration;
import java.util.Vector;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.concurrent.*;
......@@ -4058,7 +4055,7 @@ public abstract class JTextComponent extends JComponent implements Scrollable, A
private static final Object KEYMAP_TABLE =
new StringBuilder("JTextComponent_KeymapTable");
private JTextComponent editor;
//
// member variables used for on-the-spot input method
// editing style support
......@@ -4748,14 +4745,14 @@ public abstract class JTextComponent extends JComponent implements Scrollable, A
processKeyEvent(ke);
}
} else {
StringBuffer strBuf = new StringBuffer();
StringBuilder strBuf = new StringBuilder();
for (char c = text.current(); commitCount > 0;
c = text.next(), commitCount--) {
strBuf.append(c);
}
// map it to an ActionEvent
mapCommittedTextToAction(new String(strBuf));
mapCommittedTextToAction(strBuf.toString());
}
// Remember latest committed text end index
......@@ -4801,7 +4798,7 @@ public abstract class JTextComponent extends JComponent implements Scrollable, A
private void createComposedTextAttribute(int composedIndex,
AttributedCharacterIterator text) {
Document doc = getDocument();
StringBuffer strBuf = new StringBuffer();
StringBuilder strBuf = new StringBuilder();
// create attributed string with no attributes
for (char c = text.setIndex(composedIndex);
......@@ -4809,7 +4806,7 @@ public abstract class JTextComponent extends JComponent implements Scrollable, A
strBuf.append(c);
}
composedTextContent = new String(strBuf);
composedTextContent = strBuf.toString();
composedTextAttribute = new SimpleAttributeSet();
composedTextAttribute.addAttribute(StyleConstants.ComposedTextAttribute,
new AttributedString(text, composedIndex, text.getEndIndex()));
......
......@@ -29,7 +29,6 @@ import java.io.*;
import java.text.*;
import java.util.*;
import javax.swing.*;
import javax.swing.text.*;
/**
* <code>MaskFormatter</code> is used to format and edit strings. The behavior
......@@ -385,7 +384,7 @@ public class MaskFormatter extends DefaultFormatter {
*/
public String valueToString(Object value) throws ParseException {
String sValue = (value == null) ? "" : value.toString();
StringBuffer result = new StringBuffer();
StringBuilder result = new StringBuilder();
String placeholder = getPlaceholder();
int[] valueCounter = { 0 };
......@@ -484,7 +483,7 @@ public class MaskFormatter extends DefaultFormatter {
* Invokes <code>append</code> on the mask characters in
* <code>mask</code>.
*/
private void append(StringBuffer result, String value, int[] index,
private void append(StringBuilder result, String value, int[] index,
String placeholder, MaskCharacter[] mask)
throws ParseException {
for (int counter = 0, maxCounter = mask.length;
......@@ -611,13 +610,13 @@ public class MaskFormatter extends DefaultFormatter {
* Removes the literal characters from the passed in string.
*/
private String stripLiteralChars(String string) {
StringBuffer sb = null;
StringBuilder sb = null;
int last = 0;
for (int counter = 0, max = string.length(); counter < max; counter++){
if (isLiteral(counter)) {
if (sb == null) {
sb = new StringBuffer();
sb = new StringBuilder();
if (counter > 0) {
sb.append(string.substring(0, counter));
}
......@@ -715,10 +714,10 @@ public class MaskFormatter extends DefaultFormatter {
*/
boolean canReplace(ReplaceHolder rh) {
// This method is rather long, but much of the burden is in
// maintaining a String and swapping to a StringBuffer only if
// maintaining a String and swapping to a StringBuilder only if
// absolutely necessary.
if (!getAllowsInvalid()) {
StringBuffer replace = null;
StringBuilder replace = null;
String text = rh.text;
int tl = (text != null) ? text.length() : 0;
......@@ -737,7 +736,7 @@ public class MaskFormatter extends DefaultFormatter {
char aChar = text.charAt(textIndex);
if (aChar != getCharacter(rh.offset + counter, aChar)) {
if (replace == null) {
replace = new StringBuffer();
replace = new StringBuilder();
if (textIndex > 0) {
replace.append(text.substring(0, textIndex));
}
......@@ -758,7 +757,7 @@ public class MaskFormatter extends DefaultFormatter {
}
}
else if (textIndex > 0) {
replace = new StringBuffer(max);
replace = new StringBuilder(max);
replace.append(text.substring(0, textIndex));
replace.append(getLiteral(rh.offset + counter));
if (textIndex < tl) {
......@@ -780,7 +779,7 @@ public class MaskFormatter extends DefaultFormatter {
else if (textIndex >= tl) {
// placeholder
if (replace == null) {
replace = new StringBuffer();
replace = new StringBuilder();
if (text != null) {
replace.append(text);
}
......@@ -863,7 +862,7 @@ public class MaskFormatter extends DefaultFormatter {
* Appends the necessary character in <code>formatting</code> at
* <code>index</code> to <code>buff</code>.
*/
public void append(StringBuffer buff, String formatting, int[] index,
public void append(StringBuilder buff, String formatting, int[] index,
String placeholder)
throws ParseException {
boolean inString = index[0] < formatting.length();
......
......@@ -27,7 +27,6 @@ package javax.swing.text;
import java.lang.reflect.*;
import java.text.*;
import java.util.*;
import javax.swing.text.*;
/**
* <code>NumberFormatter</code> subclasses <code>InternationalFormatter</code>
......@@ -132,7 +131,7 @@ public class NumberFormatter extends InternationalFormatter {
DecimalFormatSymbols dfs = getDecimalFormatSymbols();
if (dfs != null) {
StringBuffer sb = new StringBuffer();
StringBuilder sb = new StringBuilder();
sb.append(dfs.getCurrencySymbol());
sb.append(dfs.getDecimalSeparator());
......@@ -239,13 +238,6 @@ public class NumberFormatter extends InternationalFormatter {
return null;
}
/**
*/
private boolean isValidInsertionCharacter(char aChar) {
return (Character.isDigit(aChar) || specialChars.indexOf(aChar) != -1);
}
/**
* Subclassed to return false if <code>text</code> contains in an invalid
* character to insert, that is, it is not a digit
......@@ -402,28 +394,6 @@ public class NumberFormatter extends InternationalFormatter {
return false;
}
/**
* Returns true if the range offset to length identifies the only
* integer field.
*/
private boolean isOnlyIntegerField(int offset, int length) {
if (isValidMask()) {
int start = getAttributeStart(NumberFormat.Field.INTEGER);
if (start != -1) {
AttributedCharacterIterator iterator = getIterator();
iterator.setIndex(start);
if (offset > start || iterator.getRunLimit(
NumberFormat.Field.INTEGER) > (offset + length)) {
return false;
}
return true;
}
}
return false;
}
/**
* Invoked to toggle the sign. For this to work the value class
* must have a single arg constructor that takes a String.
......
......@@ -25,7 +25,6 @@
package javax.swing.text;
import java.util.Vector;
import javax.swing.event.*;
/**
* A plain document that maintains no character attributes. The
......@@ -118,7 +117,7 @@ public class PlainDocument extends AbstractDocument {
Object filterNewlines = getProperty("filterNewlines");
if ((filterNewlines instanceof Boolean) && filterNewlines.equals(Boolean.TRUE)) {
if ((str != null) && (str.indexOf('\n') >= 0)) {
StringBuffer filtered = new StringBuffer(str);
StringBuilder filtered = new StringBuilder(str);
int n = filtered.length();
for (int i = 0; i < n; i++) {
if (filtered.charAt(i) == '\n') {
......@@ -204,11 +203,9 @@ public class PlainDocument extends AbstractDocument {
}
}
if (hasBreaks) {
int rmCount = 1;
removed.addElement(rmCandidate);
if ((offset + length == rmOffs1) && (lastOffset != rmOffs1) &&
((index+1) < lineMap.getElementCount())) {
rmCount += 1;
Element e = lineMap.getElement(index+1);
removed.addElement(e);
rmOffs1 = e.getEndOffset();
......
......@@ -199,7 +199,7 @@ public class TabSet implements Serializable
*/
public String toString() {
int tabCount = getTabCount();
StringBuffer buffer = new StringBuffer("[ ");
StringBuilder buffer = new StringBuilder("[ ");
for(int counter = 0; counter < tabCount; counter++) {
if(counter > 0)
......
......@@ -362,7 +362,7 @@ public class FormView extends ComponentView implements ActionListener {
*/
public void actionPerformed(ActionEvent evt) {
Element element = getElement();
StringBuffer dataBuffer = new StringBuffer();
StringBuilder dataBuffer = new StringBuilder();
HTMLDocument doc = (HTMLDocument)getDocument();
AttributeSet attr = element.getAttributes();
......@@ -508,7 +508,7 @@ public class FormView extends ComponentView implements ActionListener {
*/
protected void imageSubmit(String imageData) {
StringBuffer dataBuffer = new StringBuffer();
StringBuilder dataBuffer = new StringBuilder();
Element elem = getElement();
HTMLDocument hdoc = (HTMLDocument)elem.getDocument();
getFormData(dataBuffer);
......@@ -589,7 +589,7 @@ public class FormView extends ComponentView implements ActionListener {
* @param targetElement the element that triggered the
* form submission
*/
void getFormData(StringBuffer buffer) {
private void getFormData(StringBuilder buffer) {
Element formE = getFormElement();
if (formE != null) {
ElementIterator it = new ElementIterator(formE);
......@@ -623,7 +623,7 @@ public class FormView extends ComponentView implements ActionListener {
* data is loaded in name/value pairs.
*
*/
private void loadElementDataIntoBuffer(Element elem, StringBuffer buffer) {
private void loadElementDataIntoBuffer(Element elem, StringBuilder buffer) {
AttributeSet attr = elem.getAttributes();
String name = (String)attr.getAttribute(HTML.Attribute.NAME);
......@@ -692,29 +692,6 @@ public class FormView extends ComponentView implements ActionListener {
}
if (path != null && path.length() > 0) {
value = path;
/*
try {
Reader reader = new BufferedReader(new FileReader(path));
StringBuffer buffer = new StringBuffer();
char[] cBuff = new char[1024];
int read;
try {
while ((read = reader.read(cBuff)) != -1) {
buffer.append(cBuff, 0, read);
}
} catch (IOException ioe) {
buffer = null;
}
try {
reader.close();
} catch (IOException ioe) {}
if (buffer != null) {
value = buffer.toString();
}
} catch (IOException ioe) {}
*/
}
}
return value;
......@@ -740,7 +717,7 @@ public class FormView extends ComponentView implements ActionListener {
* form element. Basically, only items that are selected
* and have their name attribute set are added to the buffer.
*/
private void loadSelectData(AttributeSet attr, StringBuffer buffer) {
private void loadSelectData(AttributeSet attr, StringBuilder buffer) {
String name = (String)attr.getAttribute(HTML.Attribute.NAME);
if (name == null) {
......@@ -771,7 +748,7 @@ public class FormView extends ComponentView implements ActionListener {
* URLEncoder.encode() method before being added to the
* buffer.
*/
private void appendBuffer(StringBuffer buffer, String name, String value) {
private void appendBuffer(StringBuilder buffer, String name, String value) {
if (buffer.length() > 0) {
buffer.append('&');
}
......
......@@ -691,11 +691,11 @@ public class MinimalHTMLWriter extends AbstractWriter {
if (styleNameMapping == null) {
return style;
}
StringBuffer sb = null;
StringBuilder sb = null;
for (int counter = style.length() - 1; counter >= 0; counter--) {
if (!isValidCharacter(style.charAt(counter))) {
if (sb == null) {
sb = new StringBuffer(style);
sb = new StringBuilder(style);
}
sb.setCharAt(counter, 'a');
}
......
......@@ -998,7 +998,7 @@ public class StyleSheet extends StyleContext {
void addRule(String[] selector, AttributeSet declaration,
boolean isLinked) {
int n = selector.length;
StringBuffer sb = new StringBuffer();
StringBuilder sb = new StringBuilder();
sb.append(selector[0]);
for (int counter = 1; counter < n; counter++) {
sb.append(' ');
......
......@@ -1470,7 +1470,7 @@ class Parser implements DTDConstants {
*/
public String parseDTDMarkup() throws IOException {
StringBuffer strBuff = new StringBuffer();
StringBuilder strBuff = new StringBuilder();
ch = readCh();
while(true) {
switch (ch) {
......
......@@ -160,7 +160,7 @@ abstract class AbstractFilter extends OutputStream
public void write(byte[] buf, int off, int len)
throws IOException
{
StringBuffer accumulator = null;
StringBuilder accumulator = null;
while (len > 0) {
short b = (short)buf[off];
......@@ -178,7 +178,7 @@ abstract class AbstractFilter extends OutputStream
char ch = translationTable[b];
if (ch != (char)0) {
if (accumulator == null)
accumulator = new StringBuffer();
accumulator = new StringBuilder();
accumulator.append(ch);
}
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册