Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!eternal-september.org!feeder.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: Daniele Futtorovic Newsgroups: comp.lang.java.programmer Subject: Re: StringBuilder.append and CharSequence Date: Thu, 26 Sep 2013 23:11:58 +0200 Organization: A noiseless patient Spider Lines: 52 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Injection-Date: Thu, 26 Sep 2013 21:12:03 +0000 (UTC) Injection-Info: mx05.eternal-september.org; posting-host="6f9010336c08059d3af092c0462bb2e7"; logging-data="15570"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/8LUUWw7na/ABdWKJBArZC" User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:17.0) Gecko/20130801 Thunderbird/17.0.8 In-Reply-To: Cancel-Lock: sha1:ac6pJlTdfcdpc2rv1P+hBoY1U0Y= Xref: csiph.com comp.lang.java.programmer:25365 On 2013-09-26 19:37, Andreas Leitgeb allegedly wrote: > markspace wrote: >> On 9/25/2013 1:02 PM, markspace wrote: >>> On 9/25/2013 7:19 AM, Andreas Leitgeb wrote: >>>> Is it safe to use "stringBuilder.append(this);" in a CharSequence >>>> implementation's .toString(), or could this lead to a recursive >>>> loop, if some version of a Java class library implements the >>>> append() for CharSequence by using it's toString() ? >>> [...] >> Actually, I mis-read your problem statement, I think. Having >> implemented a couple of CharSequence classes, the only rational approach >> would be to copy the chars out of a CharSequence one at a time with a >> loop and charAt(). toString() must create an immutable String and so >> must create a new buffer, so that's an extra buffer copy, which seems >> undesirable no matter who's implementing the append() method. > > That's my point. My outset is, that eventually I need to append a number > of copies of some particular char to a StringBuilder, and my approach is: > > static class ChSeq implements CharSequence { > int m_len; char m_ch; > public ChSeq(int len, char ch) { m_len = len; m_ch = ch; } > public char charAt(int pos) { return m_ch; }// OutOfBounds? meh! > public int length() { return m_len; } > public String toString() { // just 'cause it's required... > return new StringBuilder(this).toString(); } > public CharSequence subSequence(int fr,int to) { > return ChSeq(to-fr,m_ch); } > } // not yet tested! > // then later: > sb.append( new ChSeq(n,ch) ); > > This would look like a reasonable OO approach to the problem, but > if StringBuilder.append actually used my .toString(), then it would > of course crash&burn. > > If instead I had to implement the toString() by doing the loop myself > and hammer the char n times to the temporary StringBuilder, then I'd > instead just write a static method that would take sb,n,ch and feed n > copies of ch to sb. > I'd make the toString() as follows to be on the safe side: char[] cs = new char[ m_len ]; Arrays.fill( cs, m_ch ); return new String(cs); If that hits performance, then you have got a conflict between your goal of being OO and that of performance, and you'll have to scrap one. -- DF.