Skip to content

Commit

Permalink
Perf: Replace StringBuffer with StringBuilder
Browse files Browse the repository at this point in the history
  • Loading branch information
nealeu committed Dec 28, 2023
1 parent 780d72b commit d048a41
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public OutputFilter(String name) {
}

public void filter(Calendar calendar,
StringBuffer buffer) {
StringBuilder buffer) {
// If expansion of recurrence is required what we have to do
// is create a whole new calendar object with the new expanded
// components in it and then write that one out.
Expand Down Expand Up @@ -276,7 +276,7 @@ else if (comp instanceof VToDo)
}

private void filterProperties(PropertyList properties,
StringBuffer buffer) {
StringBuilder buffer) {
if (isAllProperties()) {
buffer.append(properties.toString());
return;
Expand All @@ -297,15 +297,15 @@ private void filterProperties(PropertyList properties,
}

private void chompPropertyValue(Property property,
StringBuffer buffer) {
StringBuilder buffer) {
buffer.append(property.getName()).
append(property.getParameters()).
append(':').
append("\n");
}

private void filterSubComponents(ComponentList subComponents,
StringBuffer buffer) {
StringBuilder buffer) {
if (isAllSubComponents() && getLimit() != null) {
buffer.append(subComponents.toString());
return;
Expand Down Expand Up @@ -386,7 +386,7 @@ private boolean includeOverride(VEvent event) {
}

private void filterSubComponent(Component subComponent,
StringBuffer buffer) {
StringBuilder buffer) {
buffer.append(Component.BEGIN).
append(':').
append(subComponent.getName()).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,9 @@ public Set<Item> processFilter(Session session, ItemFilter filter) {
* @return hibernate query built using HQL
*/
public Query buildQuery(Session session, ItemFilter filter) {
StringBuffer selectBuf = new StringBuffer();
StringBuffer whereBuf = new StringBuffer();
StringBuffer orderBuf = new StringBuffer();
StringBuilder selectBuf = new StringBuilder();
StringBuilder whereBuf = new StringBuilder();
StringBuilder orderBuf = new StringBuilder();

HashMap<String, Object> params = new HashMap<String, Object>();

Expand Down Expand Up @@ -131,8 +131,8 @@ else if(filter instanceof ContentItemFilter)
return hqlQuery;
}

private void handleItemFilter(StringBuffer selectBuf,
StringBuffer whereBuf, HashMap<String, Object> params,
private void handleItemFilter(StringBuilder selectBuf,
StringBuilder whereBuf, HashMap<String, Object> params,
ItemFilter filter) {

if(selectBuf.toString() != null && selectBuf.toString().isEmpty())
Expand All @@ -159,8 +159,8 @@ private void handleItemFilter(StringBuffer selectBuf,

}

private void handleAttributeFilters(StringBuffer selectBuf,
StringBuffer whereBuf, HashMap<String, Object> params,
private void handleAttributeFilters(StringBuilder selectBuf,
StringBuilder whereBuf, HashMap<String, Object> params,
ItemFilter filter) {
for(AttributeFilter attrFilter: filter.getAttributeFilters()) {
if(attrFilter instanceof TextAttributeFilter)
Expand All @@ -170,8 +170,8 @@ private void handleAttributeFilters(StringBuffer selectBuf,
}
}

private void handleTextAttributeFilter(StringBuffer selectBuf,
StringBuffer whereBuf, HashMap<String, Object> params,
private void handleTextAttributeFilter(StringBuilder selectBuf,
StringBuilder whereBuf, HashMap<String, Object> params,
TextAttributeFilter filter) {

String alias = "ta" + params.size();
Expand All @@ -181,8 +181,8 @@ private void handleTextAttributeFilter(StringBuffer selectBuf,
formatExpression(whereBuf, params, alias + ".value", filter.getValue());
}

private void handleStampFilters(StringBuffer selectBuf,
StringBuffer whereBuf, HashMap<String, Object> params,
private void handleStampFilters(StringBuilder selectBuf,
StringBuilder whereBuf, HashMap<String, Object> params,
ItemFilter filter) {
for(StampFilter stampFilter: filter.getStampFilters()) {
if(stampFilter instanceof EventStampFilter)
Expand All @@ -192,8 +192,8 @@ private void handleStampFilters(StringBuffer selectBuf,
}
}

private void handleStampFilter(StringBuffer selectBuf,
StringBuffer whereBuf, HashMap<String, Object> params,
private void handleStampFilter(StringBuilder selectBuf,
StringBuilder whereBuf, HashMap<String, Object> params,
StampFilter filter) {

String toAppend = "";
Expand All @@ -204,8 +204,8 @@ private void handleStampFilter(StringBuffer selectBuf,
appendWhere(whereBuf, toAppend);
}

private void handleAttributeFilter(StringBuffer selectBuf,
StringBuffer whereBuf, HashMap<String, Object> params,
private void handleAttributeFilter(StringBuilder selectBuf,
StringBuilder whereBuf, HashMap<String, Object> params,
AttributeFilter filter) {

String param = "param" + params.size();
Expand All @@ -219,8 +219,8 @@ private void handleAttributeFilter(StringBuffer selectBuf,
params.put(param, filter.getQname());
}

private void handleEventStampFilter(StringBuffer selectBuf,
StringBuffer whereBuf, HashMap<String, Object> params,
private void handleEventStampFilter(StringBuilder selectBuf,
StringBuilder whereBuf, HashMap<String, Object> params,
EventStampFilter filter) {

selectBuf.append(", HibBaseEventStamp es");
Expand Down Expand Up @@ -250,8 +250,8 @@ private void handleEventStampFilter(StringBuffer selectBuf,
}
}

private void handleNoteItemFilter(StringBuffer selectBuf,
StringBuffer whereBuf, StringBuffer orderBuf, HashMap<String, Object> params,
private void handleNoteItemFilter(StringBuilder selectBuf,
StringBuilder whereBuf, StringBuilder orderBuf, HashMap<String, Object> params,
NoteItemFilter filter) {
selectBuf.append("select i from HibNoteItem i");
handleItemFilter(selectBuf, whereBuf, params, filter);
Expand Down Expand Up @@ -301,8 +301,8 @@ private void handleNoteItemFilter(StringBuffer selectBuf,
}
}

private void handleContentItemFilter(StringBuffer selectBuf,
StringBuffer whereBuf, StringBuffer orderBuf, HashMap<String, Object> params,
private void handleContentItemFilter(StringBuilder selectBuf,
StringBuilder whereBuf, StringBuilder orderBuf, HashMap<String, Object> params,
ContentItemFilter filter) {

if(selectBuf.toString() != null && selectBuf.toString().isEmpty()) {
Expand All @@ -312,14 +312,14 @@ private void handleContentItemFilter(StringBuffer selectBuf,
}


private void appendWhere(StringBuffer whereBuf, String toAppend) {
private void appendWhere(StringBuilder whereBuf, String toAppend) {
if(whereBuf.toString() != null && whereBuf.toString().isEmpty())
whereBuf.append(" where ").append(toAppend);
else
whereBuf.append(" and ").append(toAppend);
}

private void appendOrder(StringBuffer orderBuf, String toAppend) {
private void appendOrder(StringBuilder orderBuf, String toAppend) {
if(orderBuf.toString() != null && orderBuf.toString().isEmpty())
orderBuf.append(" order by ").append(toAppend);
else
Expand Down Expand Up @@ -438,7 +438,7 @@ private Collection<ContentItem> processMasterNote(NoteItem note,
return results;
}

private void formatExpression(StringBuffer whereBuf,
private void formatExpression(StringBuilder whereBuf,
HashMap<String, Object> params, String propName,
FilterCriteria fc) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ protected Document readXmlResponse(MockHttpServletResponse response)
/** */
protected String toAbsoluteUrl(MockHttpServletRequest request,
String path) {
StringBuffer url = new StringBuffer(request.getScheme());
StringBuilder url = new StringBuilder(request.getScheme());
url.append("://").append(request.getServerName());
if ((request.isSecure() && request.getServerPort() != 443) ||
(request.getServerPort() != 80)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public void testExpandEvent() throws Exception {
filter.setAllSubComponents();
filter.setAllProperties();

StringBuffer buffer = new StringBuffer();
StringBuilder buffer = new StringBuilder();
filter.filter(calendar, buffer);
StringReader sr = new StringReader(buffer.toString());

Expand Down Expand Up @@ -106,7 +106,7 @@ public void testExpandEventWithOverrides() throws Exception {
filter.setAllSubComponents();
filter.setAllProperties();

StringBuffer buffer = new StringBuffer();
StringBuilder buffer = new StringBuilder();
filter.filter(calendar, buffer);
StringReader sr = new StringReader(buffer.toString());

Expand Down Expand Up @@ -157,7 +157,7 @@ public void REMOVEDtestExpandNonRecurringEvent() throws Exception {
filter.setAllSubComponents();
filter.setAllProperties();

StringBuffer buffer = new StringBuffer();
StringBuilder buffer = new StringBuilder();
filter.filter(calendar, buffer);
StringReader sr = new StringReader(buffer.toString());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public void testLimitRecurrenceSet() throws Exception {
filter.setAllSubComponents();
filter.setAllProperties();

StringBuffer buffer = new StringBuffer();
StringBuilder buffer = new StringBuilder();
filter.filter(calendar, buffer);
StringReader sr = new StringReader(buffer.toString());

Expand Down Expand Up @@ -90,7 +90,7 @@ public void testLimitFloatingRecurrenceSet() throws Exception {
filter.setAllSubComponents();
filter.setAllProperties();

StringBuffer buffer = new StringBuffer();
StringBuilder buffer = new StringBuilder();
filter.filter(calendar, buffer);
StringReader sr = new StringReader(buffer.toString());

Expand Down Expand Up @@ -123,7 +123,7 @@ public void testLimitRecurrenceSetThisAndFuture() throws Exception {
filter.setAllSubComponents();
filter.setAllProperties();

StringBuffer buffer = new StringBuffer();
StringBuilder buffer = new StringBuilder();
filter.filter(calendar, buffer);
StringReader sr = new StringReader(buffer.toString());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ public Set findItemChildren(Item item) {

/** */
public String getItemPath(Item item) {
StringBuffer path = new StringBuffer();
StringBuilder path = new StringBuilder();
LinkedList<String> hierarchy = new LinkedList<>();
hierarchy.addFirst(item.getName());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ public boolean equals(Object o) {

/** */
public String toString() {
StringBuffer buf = new StringBuffer();
StringBuilder buf = new StringBuilder();
buf.append("{").
append(namespace).
append("}").
Expand Down
20 changes: 10 additions & 10 deletions core/src/test/java/org/osaf/cosmo/util/UriTemplateTest.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
/*
* Copyright 2008 Open Source Applications Foundation
*
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*
* http://www.apache.org/licenses/LICENSE-2.0
*
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Expand All @@ -24,30 +24,30 @@ public class UriTemplateTest extends TestCase {
private final static int MIN_CHARCODE = 0x21; //ASCII range
private final static int MAX_CHARCODE = 0x7e; //ASCII range
private Random rnd = new Random();

public void testUnescapeSpaces() throws Exception {
Assert.assertEquals("test test", UriTemplate.unescapeSegment("test+test"));
}

private String getPlaceHolder(int length) {
return getPlaceHolder(length, "/{}");
}

private String getPlaceHolder(int length, String delims) {
if (length <= 0)
length = 1;
StringBuffer result = new StringBuffer();
StringBuilder result = new StringBuilder();
while (result.length() < length) {
char ch = (char)(rnd.nextInt(MAX_CHARCODE - MIN_CHARCODE) + MIN_CHARCODE);
if (delims.indexOf(ch) < 0 && Character.isJavaIdentifierPart(ch))
result.append(ch);
}
return result.toString();
}

public void testBindAbsolute() throws Exception {
String username = getPlaceHolder(10);
Assert.assertEquals("Error binding template: ", "/" + username + "/Inbox", new UriTemplate("/{username}/Inbox").bindAbsolute(false, "", username));
}
}

}

0 comments on commit d048a41

Please sign in to comment.