8333396: Use StringBuilder internally for java.text.Format.* formatting

Reviewed-by: naoto, liach, jlu
This commit is contained in:
lingjun.cg 2024-07-22 02:01:08 +00:00
parent fd741a88e8
commit 4da9915875
16 changed files with 764 additions and 73 deletions

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 1996, 2023, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1996, 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -148,7 +148,8 @@ public abstract class Format implements Serializable, Cloneable {
}
/**
* Formats an object to produce a string. This is equivalent to
* Formats an object to produce a string.
* This method returns a string that would be equal to the string returned by
* <blockquote>
* {@link #format(Object, StringBuffer, FieldPosition) format}<code>(obj,
* new StringBuffer(), new FieldPosition(0)).toString();</code>
@ -160,7 +161,11 @@ public abstract class Format implements Serializable, Cloneable {
* object
*/
public final String format (Object obj) {
return format(obj, new StringBuffer(), new FieldPosition(0)).toString();
if ("java.text".equals(getClass().getPackageName())) {
return format(obj, StringBufFactory.of(), new FieldPosition(0)).toString();
} else {
return format(obj, new StringBuffer(), new FieldPosition(0)).toString();
}
}
/**
@ -185,6 +190,12 @@ public abstract class Format implements Serializable, Cloneable {
StringBuffer toAppendTo,
FieldPosition pos);
StringBuf format(Object obj,
StringBuf toAppendTo,
FieldPosition pos) {
throw new UnsupportedOperationException("Subclasses should override this method");
}
/**
* Formats an Object producing an {@code AttributedCharacterIterator}.
* You can use the returned {@code AttributedCharacterIterator}
@ -394,7 +405,7 @@ public abstract class Format implements Serializable, Cloneable {
* NOT modify it.
*/
public void formatted(Format.Field attr, Object value, int start,
int end, StringBuffer buffer);
int end, StringBuf buffer);
/**
* Notified when a particular region of the String is formatted.
@ -408,6 +419,38 @@ public abstract class Format implements Serializable, Cloneable {
* NOT modify it.
*/
public void formatted(int fieldID, Format.Field attr, Object value,
int start, int end, StringBuffer buffer);
int start, int end, StringBuf buffer);
}
/**
* StringBuf is the minimal common interface of {@code StringBuffer} and {@code StringBuilder}.
* It is used by the various {@code Format} implementations as the internal string buffer.
*/
sealed interface StringBuf
permits StringBufFactory.StringBufferImpl, StringBufFactory.StringBuilderImpl {
int length();
String substring(int start, int end);
String substring(int start);
StringBuf append(char c);
StringBuf append(String str);
StringBuf append(int i);
StringBuf append(char[] str, int offset, int len);
StringBuf append(CharSequence s, int start, int end);
StringBuf append(StringBuffer sb);
boolean isProxyStringBuilder();
StringBuffer asStringBuffer();
StringBuilder asStringBuilder();
}
}