Skip to content

Commit a1e1b69

Browse files
xzel23svanteschubert
authored andcommitted
string refactorings
- StringBuffer -> StringBuilder - use String.repeat() instead of loops - use String.contains() instead indexOf()
1 parent d763d33 commit a1e1b69

File tree

14 files changed

+26
-30
lines changed

14 files changed

+26
-30
lines changed

odfdom/src/main/java/org/odftoolkit/odfdom/changes/ChangesFileSaxHandler.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1325,9 +1325,7 @@ public void startElement(String uri, String localName, String qName, Attributes
13251325
addText(/*mCachedTableOps, */ "\u0020");
13261326
// mCharsForOperation.append('\u0020');
13271327
} else {
1328-
for (int i = 0; i < quantity; i++) {
1329-
mCharsForOperation.append('\u0020');
1330-
}
1328+
mCharsForOperation.append("\u0020".repeat(Math.max(0, quantity)));
13311329
addText(/*mCachedTableOps, */ mCharsForOperation);
13321330
}
13331331

odfdom/src/main/java/org/odftoolkit/odfdom/incubator/doc/text/OdfTextExtractor.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -126,9 +126,7 @@ public void visit(TextSElement ele) {
126126
if (count == null) {
127127
count = 1;
128128
}
129-
for (int i = 0; i < count; i++) {
130-
mTextBuilder.append(' ');
131-
}
129+
mTextBuilder.append(" ".repeat(Math.max(0, count)));
132130
}
133131

134132
/* (non-Javadoc)

odfdom/src/main/java/org/odftoolkit/odfdom/incubator/search/TextSelection.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -563,7 +563,7 @@ private void delete(int fromindex, int leftLength, Node pNode) {
563563
// the start index is in this node
564564
if (node.getNodeType() == Node.TEXT_NODE) {
565565
String value = node.getNodeValue();
566-
StringBuffer buffer = new StringBuffer();
566+
StringBuilder buffer = new StringBuilder();
567567
buffer.append(value.substring(0, fromindex));
568568
int endLength = fromindex + leftLength;
569569
int nextLength = value.length() - endLength;
@@ -755,7 +755,7 @@ private void insertSpan(OdfTextSpan textSpan, int fromindex, Node pNode) {
755755
// after
756756
// the result node
757757
String value = node.getNodeValue();
758-
StringBuffer buffer = new StringBuffer();
758+
StringBuilder buffer = new StringBuilder();
759759
buffer.append(value.substring(0, fromindex));
760760
// insert the text span in appropriate position
761761
node.setNodeValue(buffer.toString());

odfdom/src/main/java/org/odftoolkit/odfdom/pkg/OdfElement.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2640,7 +2640,7 @@ public String getTextContent() {
26402640
buffer.append(((TextSpanElement) node).getTextContent());
26412641
} else if (node.getNodeName().equals("text:s")) {
26422642
Integer count = ((TextSElement) node).getTextCAttribute();
2643-
for (int j = 0; j < (count != null ? count : 1); j++) buffer.append(' ');
2643+
buffer.append(" ".repeat(Math.max(0, (count != null ? count : 1))));
26442644
} else if (node.getNodeName().equals("text:tab")) buffer.append('\t');
26452645
else if (node.getNodeName().equals("text:line-break")) {
26462646
String lineseperator = System.getProperty("line.separator");

odfdom/src/main/java/org/odftoolkit/odfdom/pkg/OdfPackage.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2432,20 +2432,20 @@ static String normalizePath(String path) {
24322432
LOG.severe(errMsg);
24332433
throw new IllegalArgumentException(errMsg);
24342434
} else if (!mightBeExternalReference(path)) {
2435-
if (path.equals(EMPTY_STRING)) {
2435+
if (path.isEmpty()) {
24362436
path = SLASH;
24372437
} else {
24382438
// exchange all backslash "\" with a slash "/"
2439-
if (path.indexOf('\\') != -1) {
2439+
if (path.contains("\\")) {
24402440
path = BACK_SLASH_PATTERN.matcher(path).replaceAll(SLASH);
24412441
}
24422442
// exchange all double slash "//" with a slash "/"
2443-
while (path.indexOf("//") != -1) {
2443+
while (path.contains("//")) {
24442444
path = DOUBLE_SLASH_PATTERN.matcher(path).replaceAll(SLASH);
24452445
}
24462446
// if directory replacements (e.g. ..) exist, resolve and remove
24472447
// them
2448-
if (path.indexOf("/.") != -1 || path.indexOf("./") != -1) {
2448+
if (path.contains("/.") || path.contains("./")) {
24492449
path = removeChangeDirectories(path);
24502450
}
24512451
if (path.startsWith(SLASH) && !path.equals(SLASH)) {

odfdom/src/main/java/org/odftoolkit/odfdom/pkg/OdfXMLFactory.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ private static String getOdfIncubatorNodeClassName(String prefix, String localNa
155155
boolean contains = false;
156156
StringBuilder className = new StringBuilder();
157157

158-
if (localName.indexOf(LOCAL_NAME_DELIMITER) != -1) {
158+
if (localName.contains(LOCAL_NAME_DELIMITER)) {
159159
StringTokenizer stok = new StringTokenizer(localName, LOCAL_NAME_DELIMITER);
160160
while (stok.hasMoreElements()) {
161161
String substr = stok.nextToken();
@@ -179,7 +179,7 @@ private static String getOdfIncubatorNodeClassName(String prefix, String localNa
179179

180180
private static String getOdfPKGNodeClassName(String prefix, String localName, String nodeType) {
181181
StringBuilder className = new StringBuilder("org.odftoolkit.odfdom.pkg." + prefix + ".");
182-
if (localName.indexOf(LOCAL_NAME_DELIMITER) != -1) {
182+
if (localName.contains(LOCAL_NAME_DELIMITER)) {
183183
StringTokenizer stok = new StringTokenizer(localName, LOCAL_NAME_DELIMITER);
184184
while (stok.hasMoreElements()) {
185185
className = className.append(toUpperCaseFirstCharacter(stok.nextToken()));
@@ -195,7 +195,7 @@ private static String getOdfDOMNodeClassName(String prefix, String localName, St
195195
StringBuilder className =
196196
new StringBuilder("org.odftoolkit.odfdom.dom." + nodeType + "." + prefix + ".");
197197
className = className.append(toUpperCaseFirstCharacter(prefix));
198-
if (localName.indexOf(LOCAL_NAME_DELIMITER) != -1) {
198+
if (localName.contains(LOCAL_NAME_DELIMITER)) {
199199
StringTokenizer stok = new StringTokenizer(localName, LOCAL_NAME_DELIMITER);
200200
while (stok.hasMoreElements()) {
201201
className = className.append(toUpperCaseFirstCharacter(stok.nextToken()));
@@ -230,7 +230,7 @@ public static OdfElement newOdfElement(OdfFileDom dom, OdfName name) throws DOME
230230
if (oldPrefix != null
231231
&& !oldPrefix.equals(newPrefix)
232232
// "_1" is the suffix added by OdfFileDom to an existing Namespace
233-
&& newPrefix.indexOf("__") == -1) {
233+
&& !newPrefix.contains("__")) {
234234
// look up again if there is a class registered for this prefix
235235
element = newOdfElement(dom, adaptedName);
236236
} else {
@@ -269,7 +269,7 @@ public static OdfAttribute newOdfAttribute(OdfFileDom dom, OdfName name) throws
269269
OdfName adaptedName = addNamespaceToDom(name, dom);
270270
String newPrefix = adaptedName.getPrefix();
271271
// in case the prefix was changed as it existed before
272-
if (!prefix.equals(newPrefix) && newPrefix.indexOf("__") == -1) {
272+
if (!prefix.equals(newPrefix) && !newPrefix.contains("__")) {
273273
// look up again if there is a class registered for this prefix
274274
attr = newOdfAttribute(dom, adaptedName);
275275
} else {

odfdom/src/main/java/org/odftoolkit/odfdom/type/CURIEs.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public CURIEs(List<CURIE> curies) throws IllegalArgumentException {
3737
if ((curies == null) || (curies.size() == 0)) {
3838
throw new IllegalArgumentException("parameter can not be null for CURIEs");
3939
}
40-
StringBuffer aRet = new StringBuffer();
40+
StringBuilder aRet = new StringBuilder();
4141
Iterator<CURIE> aIter = curies.iterator();
4242
while (aIter.hasNext()) {
4343
if (aRet.length() > 0) {

odfdom/src/main/java/org/odftoolkit/odfdom/type/CellRangeAddressList.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public CellRangeAddressList(List<CellRangeAddress> cellRangeAddressList)
4747
throw new IllegalArgumentException("parameter can not be null for CellRangeAddressList");
4848
}
4949

50-
StringBuffer aRet = new StringBuffer();
50+
StringBuilder aRet = new StringBuilder();
5151
Iterator<CellRangeAddress> aIter = cellRangeAddressList.iterator();
5252
while (aIter.hasNext()) {
5353
if (aRet.length() > 0) {

odfdom/src/main/java/org/odftoolkit/odfdom/type/IDREFS.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public IDREFS(List<IDREF> idRefList) throws IllegalArgumentException {
4242
if ((idRefList == null) || (idRefList.size() == 0)) {
4343
throw new IllegalArgumentException("parameter can not be null for IDREFS");
4444
}
45-
StringBuffer aRet = new StringBuffer();
45+
StringBuilder aRet = new StringBuilder();
4646
Iterator<IDREF> aIter = idRefList.iterator();
4747
while (aIter.hasNext()) {
4848
if (aRet.length() > 0) {

odfdom/src/main/java/org/odftoolkit/odfdom/type/StyleNameRefs.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public StyleNameRefs(List<StyleName> styleNames) throws IllegalArgumentException
4242
if (styleNames == null) {
4343
throw new IllegalArgumentException("parameter can not be null for StyleNameRefs");
4444
}
45-
StringBuffer aRet = new StringBuffer();
45+
StringBuilder aRet = new StringBuilder();
4646
Iterator<StyleName> aIter = styleNames.iterator();
4747
while (aIter.hasNext()) {
4848
if (aRet.length() > 0) {

0 commit comments

Comments
 (0)