1
0
Fork 0
mirror of https://github.com/vanitasvitae/Smack.git synced 2024-11-26 05:52:06 +01:00

[core] Add XmlStringBuilder.setAppendApproach()

This commit is contained in:
Florian Schmaus 2023-11-28 10:58:24 +01:00
parent 04dc212db8
commit dac06b04c3

View file

@ -33,8 +33,6 @@ import org.jxmpp.util.XmppDateTime;
public class XmlStringBuilder implements Appendable, CharSequence, Element { public class XmlStringBuilder implements Appendable, CharSequence, Element {
public static final String RIGHT_ANGLE_BRACKET = Character.toString('>'); public static final String RIGHT_ANGLE_BRACKET = Character.toString('>');
public static final boolean FLAT_APPEND = false;
private final LazyStringBuilder sb; private final LazyStringBuilder sb;
private final XmlEnvironment effectiveXmlEnvironment; private final XmlEnvironment effectiveXmlEnvironment;
@ -595,10 +593,40 @@ public class XmlStringBuilder implements Appendable, CharSequence, Element {
return this; 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 @Override
public XmlStringBuilder append(CharSequence csq) { public XmlStringBuilder append(CharSequence csq) {
assert csq != null; assert csq != null;
if (FLAT_APPEND) { switch (APPEND_APPROACH) {
case SINGLE:
sb.append(csq);
break;
case FLAT:
if (csq instanceof XmlStringBuilder) { if (csq instanceof XmlStringBuilder) {
sb.append(((XmlStringBuilder) csq).sb); sb.append(((XmlStringBuilder) csq).sb);
} else if (csq instanceof LazyStringBuilder) { } else if (csq instanceof LazyStringBuilder) {
@ -606,8 +634,7 @@ public class XmlStringBuilder implements Appendable, CharSequence, Element {
} else { } else {
sb.append(csq); sb.append(csq);
} }
} else { break;
sb.append(csq);
} }
return this; return this;
} }