Skip to content

Commit

Permalink
[core] Add XmlStringBuilder.setAppendApproach()
Browse files Browse the repository at this point in the history
  • Loading branch information
Flowdalic committed Nov 28, 2023
1 parent 04dc212 commit dac06b0
Showing 1 changed file with 32 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@
public class XmlStringBuilder implements Appendable, CharSequence, Element {
public static final String RIGHT_ANGLE_BRACKET = Character.toString('>');

public static final boolean FLAT_APPEND = false;

private final LazyStringBuilder sb;

private final XmlEnvironment effectiveXmlEnvironment;
Expand Down Expand Up @@ -595,19 +593,48 @@ public XmlStringBuilder condAttribute(boolean condition, String name, String val
return this;
}

enum AppendApproach {
/**
* Simply add the given CharSequence to this builder.
*/
SINGLE,

/**
* If the given CharSequence is a {@link XmlStringBuilder} or {@link LazyStringBuilder}, then copy the
* references of the lazy strings parts into this builder. This approach flattens the string builders into one,
* yielding a different performance characteristic.
*/
FLAT,
}

private static AppendApproach APPEND_APPROACH = AppendApproach.SINGLE;

/**
* Set the builders approach on how to append new char sequences.
*
* @param appendApproach the append approach.
*/
public static void setAppendMethod(AppendApproach appendApproach) {
Objects.requireNonNull(appendApproach);
APPEND_APPROACH = appendApproach;
}

@Override
public XmlStringBuilder append(CharSequence csq) {
assert csq != null;
if (FLAT_APPEND) {
switch (APPEND_APPROACH) {
case SINGLE:
sb.append(csq);
break;
case FLAT:
if (csq instanceof XmlStringBuilder) {
sb.append(((XmlStringBuilder) csq).sb);
} else if (csq instanceof LazyStringBuilder) {
sb.append((LazyStringBuilder) csq);
} else {
sb.append(csq);
}
} else {
sb.append(csq);
break;
}
return this;
}
Expand Down

0 comments on commit dac06b0

Please sign in to comment.