Skip to content

Commit e4fa764

Browse files
authored
Merge pull request #1115 from stanfordnlp/StringBuilder
Use StringBuilder instead of StringBuffer
2 parents 7298494 + a955159 commit e4fa764

35 files changed

+165
-166
lines changed

src/edu/stanford/nlp/ie/machinereading/BasicRelationFeatureFactory.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -563,7 +563,7 @@ else if (gender.equals("FEMALE"))
563563
}
564564

565565
String sentToString(CoreMap sentence) {
566-
StringBuffer os = new StringBuffer();
566+
StringBuilder os = new StringBuilder();
567567
List<CoreLabel> tokens = sentence.get(TokensAnnotation.class);
568568
if(tokens != null){
569569
boolean first = true;

src/edu/stanford/nlp/ie/machinereading/common/SimpleTokenize.java

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11

22
package edu.stanford.nlp.ie.machinereading.common;
3-
import edu.stanford.nlp.util.logging.Redwood;
4-
53
import java.util.ArrayList;
64
import java.util.StringTokenizer;
75

6+
import edu.stanford.nlp.util.logging.Redwood;
7+
88
/**
99
* Simple string tokenization
1010
*/
@@ -52,19 +52,19 @@ private static int findWhitespace(String s, int start) {
5252
}
5353

5454
/**
55-
* Replaces all occurences of \" with "
55+
* Replaces all occurrences of \" with "
5656
*/
5757
private static String normalizeQuotes(String str) {
58-
StringBuffer buffer = new StringBuffer();
58+
StringBuilder builder = new StringBuilder();
5959
for (int i = 0; i < str.length(); i++) {
6060
// do not include \ if followed by "
6161
if (str.charAt(i) == '\\' && i < str.length() - 1 && str.charAt(i + 1) == '\"') {
6262
continue;
6363
} else {
64-
buffer.append(str.charAt(i));
64+
builder.append(str.charAt(i));
6565
}
6666
}
67-
return buffer.toString();
67+
return builder.toString();
6868
}
6969

7070
/**
@@ -121,15 +121,15 @@ public static ArrayList<String> tokenizeWithQuotes(String line) {
121121
* \
122122
*/
123123
public static String quotify(String str) {
124-
StringBuffer buffer = new StringBuffer();
125-
buffer.append('\"');
124+
StringBuilder builder = new StringBuilder();
125+
builder.append('\"');
126126
for (int i = 0; i < str.length(); i++) {
127127
if (str.charAt(i) == '\"')
128-
buffer.append('\\');
129-
buffer.append(str.charAt(i));
128+
builder.append('\\');
129+
builder.append(str.charAt(i));
130130
}
131-
buffer.append('\"');
132-
return buffer.toString();
131+
builder.append('\"');
132+
return builder.toString();
133133
}
134134

135135
/** Implements a simple test */

src/edu/stanford/nlp/ie/machinereading/domains/ace/AceReader.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ private List<CoreMap> readDocument(String prefix, Annotation corpus) throws IOEx
238238
/*
239239
for (int sentenceIndex = 0; sentenceIndex < aceDocument.getSentenceCount(); sentenceIndex++) {
240240
List<AceToken> tokens = aceDocument.getSentence(sentenceIndex);
241-
StringBuffer b = new StringBuffer();
241+
StringBuilder b = new StringBuilder();
242242
for(AceToken t: tokens) b.append(t.getLiteral() + " " );
243243
logger.info("SENTENCE: " + b.toString());
244244
}

src/edu/stanford/nlp/ie/machinereading/domains/ace/reader/AceCharSeq.java

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -40,27 +40,27 @@ public AceCharSeq(String text, int start, int end) {
4040
}
4141

4242
public String toXml(String label, int offset) {
43-
StringBuffer buffer = new StringBuffer();
44-
AceElement.appendOffset(buffer, offset);
45-
buffer.append('<').append(label).append(">\n");
46-
47-
AceElement.appendOffset(buffer, offset + 2);
48-
buffer.append("<charseq START=\"").append(mByteOffset.start()).append("\" END=\"").append(mByteOffset.end()).append("\">");
49-
buffer.append(mText).append("</charseq>");
50-
buffer.append('\n');
51-
52-
AceElement.appendOffset(buffer, offset);
53-
buffer.append("</").append(label).append('>');
54-
return buffer.toString();
43+
StringBuilder builder = new StringBuilder();
44+
AceElement.appendOffset(builder, offset);
45+
builder.append('<').append(label).append(">\n");
46+
47+
AceElement.appendOffset(builder, offset + 2);
48+
builder.append("<charseq START=\"").append(mByteOffset.start()).append("\" END=\"").append(mByteOffset.end()).append("\">");
49+
builder.append(mText).append("</charseq>");
50+
builder.append('\n');
51+
52+
AceElement.appendOffset(builder, offset);
53+
builder.append("</").append(label).append('>');
54+
return builder.toString();
5555
}
5656

5757
public String toXml(int offset) {
58-
StringBuffer buffer = new StringBuffer();
58+
StringBuilder builder = new StringBuilder();
5959

60-
AceElement.appendOffset(buffer, offset + 2);
61-
buffer.append("<charseq START=\"").append(mByteOffset.start()).append("\" END=\"").append(mByteOffset.end()).append("\">");
62-
buffer.append(mText).append("</charseq>");
63-
return buffer.toString();
60+
AceElement.appendOffset(builder, offset + 2);
61+
builder.append("<charseq START=\"").append(mByteOffset.start()).append("\" END=\"").append(mByteOffset.end()).append("\">");
62+
builder.append(mText).append("</charseq>");
63+
return builder.toString();
6464
}
6565

6666
public String getText() {
@@ -162,9 +162,9 @@ public String toString() {
162162

163163
/*
164164
* private AceToken makePhrase(Vector<AceToken> tokens, Span span) {
165-
* StringBuffer word = new StringBuffer(); StringBuffer lemma = new
166-
* StringBuffer(); StringBuffer pos = new StringBuffer(); StringBuffer chunk =
167-
* new StringBuffer(); StringBuffer nerc = new StringBuffer();
165+
* StringBuilder word = new StringBuilder(); StringBuilder lemma = new
166+
* StringBuilder(); StringBuilder pos = new StringBuilder(); StringBuilder chunk =
167+
* new StringBuilder(); StringBuilder nerc = new StringBuilder();
168168
*
169169
* for(int i = span.mStart; i <= span.mEnd; i ++){ if(i > span.mStart){
170170
* word.append("_"); lemma.append("_"); pos.append("_"); chunk.append("_");

src/edu/stanford/nlp/ie/machinereading/domains/ace/reader/AceDocument.java

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -229,48 +229,48 @@ public String toString() {
229229
}
230230

231231
public String toXml(int offset) {
232-
StringBuffer buffer = new StringBuffer();
233-
appendOffset(buffer, offset);
234-
buffer.append("<?xml version=\"1.0\"?>\n");
235-
appendOffset(buffer, offset);
236-
buffer.append("<!DOCTYPE source_file SYSTEM \"apf.v5.1.2.dtd\">\n");
237-
appendOffset(buffer, offset);
238-
buffer.append("<source_file URI=\"" + mId + ".sgm\" SOURCE=\"" + mSource
232+
StringBuilder builder = new StringBuilder();
233+
appendOffset(builder, offset);
234+
builder.append("<?xml version=\"1.0\"?>\n");
235+
appendOffset(builder, offset);
236+
builder.append("<!DOCTYPE source_file SYSTEM \"apf.v5.1.2.dtd\">\n");
237+
appendOffset(builder, offset);
238+
builder.append("<source_file URI=\"" + mId + ".sgm\" SOURCE=\"" + mSource
239239
+ "\" TYPE=\"text\" AUTHOR=\"LDC\" ENCODING=\"UTF-8\">\n");
240-
appendOffset(buffer, offset);
241-
buffer.append("<document DOCID=\"" + getId() + "\">\n");
240+
appendOffset(builder, offset);
241+
builder.append("<document DOCID=\"" + getId() + "\">\n");
242242

243243
// display all entities
244244
Set<String> entKeys = mEntities.keySet();
245245
for (String key : entKeys) {
246246
AceEntity e = mEntities.get(key);
247-
buffer.append(e.toXml(offset));
248-
buffer.append("\n");
247+
builder.append(e.toXml(offset));
248+
builder.append("\n");
249249
}
250250

251251
// display all relations
252252
Set<String> relKeys = mRelations.keySet();
253253
for (String key : relKeys) {
254254
AceRelation r = mRelations.get(key);
255255
if (!r.getType().equals(AceRelation.NIL_LABEL)) {
256-
buffer.append(r.toXml(offset));
257-
buffer.append("\n");
256+
builder.append(r.toXml(offset));
257+
builder.append("\n");
258258
}
259259
}
260260

261261
// TODO: display all events
262262

263-
appendOffset(buffer, offset);
264-
buffer.append("</document>\n");
265-
appendOffset(buffer, offset);
266-
buffer.append("</source_file>\n");
267-
return buffer.toString();
263+
appendOffset(builder, offset);
264+
builder.append("</document>\n");
265+
appendOffset(builder, offset);
266+
builder.append("</source_file>\n");
267+
return builder.toString();
268268
}
269269

270270
private String tokensWithByteSpan(int start, int end) {
271-
StringBuffer buf = new StringBuffer();
271+
StringBuilder builder = new StringBuilder();
272272
boolean doPrint = false;
273-
buf.append("...");
273+
builder.append("...");
274274
for (AceToken mToken : mTokens) {
275275
// start printing
276276
if (doPrint == false && mToken.getByteOffset().start() > start - 20
@@ -284,11 +284,11 @@ else if (doPrint == true && mToken.getByteOffset().start() > end + 20) {
284284
}
285285

286286
if (doPrint) {
287-
buf.append(" " + mToken.display());
287+
builder.append(" " + mToken.display());
288288
}
289289
}
290-
buf.append("...");
291-
return buf.toString();
290+
builder.append("...");
291+
return builder.toString();
292292
}
293293

294294
/**
@@ -751,11 +751,11 @@ public int countCommas(int start, int end) {
751751

752752
private void readRawBytes(String fileName) throws IOException {
753753
BufferedReader in = new BufferedReader(new FileReader(fileName));
754-
StringBuffer buf = new StringBuffer();
754+
StringBuilder builder = new StringBuilder();
755755
int c;
756756
while ((c = in.read()) >= 0)
757-
buf.append((char) c);
758-
mRawBuffer = buf.toString();
757+
builder.append((char) c);
758+
mRawBuffer = builder.toString();
759759
// System.out.println(mRawBuffer);
760760
in.close();
761761
}
@@ -826,7 +826,7 @@ private void readPredictedEntityBoundaries(BufferedReader is) throws java.io.IOE
826826

827827
public AceCharSeq makeCharSeq(int startToken, int endToken) {
828828
/*
829-
* StringBuffer buf = new StringBuffer(); for(int i = startToken; i <
829+
* StringBuilder buf = new StringBuilder(); for(int i = startToken; i <
830830
* endToken; i ++){ if(i > startToken) buf.append(" ");
831831
* buf.append(mTokens.get(i).getLiteral()); }
832832
*/

src/edu/stanford/nlp/ie/machinereading/domains/ace/reader/AceElement.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,10 @@ public AceElement(String id) {
1414

1515
public String getId() { return mId; }
1616

17-
// todo [cdm 2014]: Change this to using StringBuilder or Appendable or similar
18-
public static void appendOffset(StringBuffer buffer,
17+
public static void appendOffset(StringBuilder builder,
1918
int offset) {
2019
for(int i = 0; i < offset; i ++){
21-
buffer.append(' ');
20+
builder.append(' ');
2221
}
2322
}
2423

src/edu/stanford/nlp/ie/machinereading/domains/ace/reader/AceEntity.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public void addMention(AceEntityMention m) {
4444
public String getClasss() { return mClass; }
4545

4646
public String toXml(int offset) {
47-
StringBuffer buffer = new StringBuffer();
47+
StringBuilder buffer = new StringBuilder();
4848
appendOffset(buffer, offset);
4949
buffer.append("<entity ID=\"" + getId() + "\" TYPE =\"" +
5050
AceToken.OTHERS.get(mType) +

src/edu/stanford/nlp/ie/machinereading/domains/ace/reader/AceEntityMention.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ public List<AceEventMention> getEventMentions() {
7878
}
7979

8080
public String toXml(int offset) {
81-
StringBuffer buffer = new StringBuffer();
81+
StringBuilder buffer = new StringBuilder();
8282
String mentionType = mType;
8383

8484
appendOffset(buffer, offset);

src/edu/stanford/nlp/ie/machinereading/domains/ace/reader/AceMentionArgument.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,24 +18,24 @@ public AceMentionArgument(String role,
1818
public String getRole() { return mRole; }
1919

2020
public String toXml(int offset) {
21-
StringBuffer buffer = new StringBuffer();
22-
AceElement.appendOffset(buffer, offset);
23-
buffer.append("<" + mentionType + "_mention_argument REFID=\"" + mContent.getId() +
21+
StringBuilder builder = new StringBuilder();
22+
AceElement.appendOffset(builder, offset);
23+
builder.append("<" + mentionType + "_mention_argument REFID=\"" + mContent.getId() +
2424
"\" ROLE=\"" + mRole + "\">\n");
2525

2626

2727
//buffer.append(getContent().toXml(offset + 2));
2828
AceCharSeq ext = getContent().getExtent();
29-
buffer.append(ext.toXml("extent", offset + 2));
30-
buffer.append("\n");
29+
builder.append(ext.toXml("extent", offset + 2));
30+
builder.append("\n");
3131

32-
AceElement.appendOffset(buffer, offset);
33-
buffer.append("</" + mentionType + "_mention_argument>");
34-
return buffer.toString();
32+
AceElement.appendOffset(builder, offset);
33+
builder.append("</" + mentionType + "_mention_argument>");
34+
return builder.toString();
3535
}
3636

3737
public String toXmlShort(int offset) {
38-
StringBuffer buffer = new StringBuffer();
38+
StringBuilder buffer = new StringBuilder();
3939
AceElement.appendOffset(buffer, offset);
4040
buffer.append("<" + mentionType + "_argument REFID=\"" +
4141
mContent.getParent().getId() +

src/edu/stanford/nlp/ie/machinereading/domains/ace/reader/AceRelation.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public AceRelationMention getMention(int which) {
5050
public void setSubtype(String s) { mSubtype = s; }
5151

5252
public String toXml(int offset) {
53-
StringBuffer buffer = new StringBuffer();
53+
StringBuilder buffer = new StringBuilder();
5454
appendOffset(buffer, offset);
5555
buffer.append("<relation ID=\"" + getId() + "\" TYPE =\"" + mType +
5656
"\" SUBTYPE=\"" + mSubtype + "\" MODALITY=\"" + mModality +

src/edu/stanford/nlp/ie/machinereading/domains/ace/reader/AceRelationMention.java

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -74,28 +74,28 @@ public int getMaxTokenEnd() {
7474
}
7575

7676
public String toXml(int offset) {
77-
StringBuffer buffer = new StringBuffer();
78-
appendOffset(buffer, offset);
79-
buffer.append("<relation_mention ID=\"" + getId() + "\"");
77+
StringBuilder builder = new StringBuilder();
78+
appendOffset(builder, offset);
79+
builder.append("<relation_mention ID=\"" + getId() + "\"");
8080
if(mLexicalCondition != null)
81-
buffer.append(" LEXICALCONDITION=\"" + mLexicalCondition + "\"");
82-
buffer.append(">\n");
81+
builder.append(" LEXICALCONDITION=\"" + mLexicalCondition + "\"");
82+
builder.append(">\n");
8383

84-
buffer.append(mExtent.toXml("extent", offset + 2));
85-
buffer.append("\n");
84+
builder.append(mExtent.toXml("extent", offset + 2));
85+
builder.append("\n");
8686

8787
AceRelationMentionArgument arg1 = getArgs()[0];
8888
AceRelationMentionArgument arg2 = getArgs()[1];
8989
if(arg1.getRole().equals("Arg-1")){ // left to right
90-
buffer.append(arg1.toXml(offset + 2) + "\n");
91-
buffer.append(arg2.toXml(offset + 2) + "\n");
90+
builder.append(arg1.toXml(offset + 2) + "\n");
91+
builder.append(arg2.toXml(offset + 2) + "\n");
9292
} else { // right to left
93-
buffer.append(arg2.toXml(offset + 2) + "\n");
94-
buffer.append(arg1.toXml(offset + 2) + "\n");
93+
builder.append(arg2.toXml(offset + 2) + "\n");
94+
builder.append(arg1.toXml(offset + 2) + "\n");
9595
}
9696

97-
appendOffset(buffer, offset);
98-
buffer.append("</relation_mention>");
99-
return buffer.toString();
97+
appendOffset(builder, offset);
98+
builder.append("</relation_mention>");
99+
return builder.toString();
100100
}
101101
}

0 commit comments

Comments
 (0)