RFR 8170348: Appendable.appendN(char, int) method to append multiple copies of char
ivan.gerasimov at oracle.com
Sun Dec 11 20:35:26 UTC 2016
Thank you Roger for the comments!
On 08.12.2016 1:28, Roger Riggs wrote:
> Hi Ivan,
> A few comments...
> Appendable.appendN default method:
> I'd expect many time n is small so allocating a new StringBuilder
> every time seems
> not optimal. A simple loop with append(c) would have fewer
> (memory) side effects.
> And in particular for n=0, it could avoid doing anything.
The rationale here was to implement `all or nothing` approach.
If we add chars one by one in a loop and hit OOM, then the Appendable
would be partially modified, which is not good.
A special case for the size of zero can be added, of course.
> I agree with Claes' comment suggesting that IAE for negative
> lengths is a pain
> and defining it to append 0 would be natural in many use cases.
I see that most votes are for throwing the exception if n is negative.
Let's leave it this way!
> You could beneficially replace StringBuffer with StringBuilder
> while you are there.
> (and maybe pre-size the builder).
Sure, let's use StringBuilder.
I wasn't sure how to easily calculate the required capacity of the
builder here though.
Please find the updated webrev here:
With kind regards,
More information about the core-libs-dev