HHH-6258: Performance enhancement to toFragmentString method to only append to the StringBuffer, and not do inserts into the beginning, which causes additional buffer allocations and copies that are unnecessary. Also, changed the StringBuffer to a StringBuilder.

This commit is contained in:
andy.miller 2011-05-27 14:12:57 -06:00 committed by Steve Ebersole
parent 02a866c702
commit bf066b8121
1 changed files with 60 additions and 44 deletions

View File

@ -68,49 +68,65 @@ public class InFragment {
} }
public String toFragmentString() { public String toFragmentString() {
if ( values.size()==0 ) return "1=2";
StringBuffer buf = new StringBuffer( values.size() * 5 ); if (values.size() == 0) {
return "1=2";
}
StringBuilder buf = new StringBuilder(values.size() * 5);
if (values.size() == 1) {
Object value = values.get(0);
buf.append(columnName); buf.append(columnName);
//following doesn't handle (null, not null) but unnecessary
//since this would mean all rows
if ( values.size()>1 ) {
boolean allowNull = false;
buf.append(" in (");
Iterator iter = values.iterator();
while ( iter.hasNext() ) {
Object value = iter.next();
if ( NULL.equals(value) ) {
allowNull = true;
}
else if ( NOT_NULL.equals(value) ) {
throw new IllegalArgumentException("not null makes no sense for in expression");
}
else {
buf.append(value);
buf.append(", ");
}
}
buf.setLength( buf.length()-2 );
buf.append(')');
if (allowNull) {
buf.insert(0, " is null or ")
.insert(0, columnName)
.insert(0, '(')
.append(')');
}
}
else {
Object value = values.iterator().next();
if (NULL.equals(value)) { if (NULL.equals(value)) {
buf.append(" is null"); buf.append(" is null");
} } else {
else if ( NOT_NULL.equals(value) ) { if (NOT_NULL.equals(value)) {
buf.append(" is not null"); buf.append(" is not null");
} } else {
else { buf.append('=').append(value);
buf.append("=").append(value);
} }
} }
return buf.toString(); return buf.toString();
} }
boolean allowNull = false;
for (Object value : values) {
if (NULL.equals(value)) {
allowNull = true;
} else {
if (NOT_NULL.equals(value)) {
throw new IllegalArgumentException("not null makes no sense for in expression");
}
}
}
if (allowNull) {
buf.append('(').append(columnName).append(" is null or ").append(columnName).append(" in (");
} else {
buf.append(columnName).append(" in (");
}
for (Object value : values) {
if (NULL.equals(value)) {
;
} else {
buf.append(value);
buf.append(", ");
}
}
buf.setLength(buf.length() - 2);
if (allowNull) {
buf.append("))");
} else {
buf.append(')');
}
return buf.toString();
}
} }