I'm not sure we need 4, it's just a convenient method that may be slower than if the user code calls toString() (because of profile pollution), so i'm not sure i pull it's own weight.
And about adding a default method into CharSequence, the default method should return a CharSequence or String ? and what about the other implementations, AbstractStringBuilder and CharBuffer at least ? Rémi ----- Mail original ----- > De: "Jim Laskey" <james.las...@oracle.com> > À: "Brian Goetz" <brian.go...@oracle.com> > Cc: "core-libs-dev" <core-libs-dev@openjdk.java.net> > Envoyé: Jeudi 15 Février 2018 18:34:19 > Objet: Re: RFR: 8197594 - String and character repeat > Very reasonable approach. > > >> On Feb 15, 2018, at 1:31 PM, Brian Goetz <brian.go...@oracle.com> wrote: >> >> I suggest merging 1 and 4 by making it an instance method on CS with a >> default >> in CS and an override on string and string builder? >> >> Sent from my MacBook Wheel >> >>> On Feb 15, 2018, at 9:20 AM, Jim Laskey <james.las...@oracle.com> wrote: >>> >>> This is a pre-CSR code review [1] for String repeat methods (Enhancement). >>> >>> The proposal is to introduce four new methods; >>> >>> 1. public String repeat(final int count) >>> 2. public static String repeat(final char ch, final int count) >>> 3. public static String repeat(final int codepoint, final int count) >>> 4. public static String repeat(final CharSequence seq, final int count) >>> >>> For the sake of transparency, only 1 is necessary, 2-4 are convenience >>> methods. >>> In the case of 2, “*”.repeat(10) performs as well as String.repeat(‘*’, 10). >>> 3 and 4 convert to String before calling 1. >>> >>> Performance runs with jmh (results as comment in [2]) show that these >>> methods are significantly faster that StringBuilder equivalents. >>> - fewer memory allocations >>> - fewer char to byte array conversions >>> - faster pyramid replication vs O(N) copying >>> >>> I left StringBuilder out of scope. It falls under the category of >>> Appendables#append with repeat. A much bigger project. >>> >>> All comments welcome. Especially around the need for convenience >>> methods, the JavaDoc content and expanding the tests. >>> >>> — Jim >>> >>> [1] webrev: http://cr.openjdk.java.net//oj/home/jlaskey/8197594/webrev-00 >>> [2] jbs: https://bugs.openjdk.java.net/browse/JDK-8197594 >>>