Closed GoogleCodeExporter closed 9 years ago
Original comment by cgdecker@google.com
on 22 Aug 2013 at 8:00
[deleted comment]
interface Appender {
<A extends Appendable> A writeTo(A a);
}
abstract class TemplateAppender {
public <A extends Appendable> A writeTo(A a) {
appendTo(DataAppendable.of(a));
return a;
}
protected abstract void appendTo(DataAppendable a);
@Override public String toString() {
try {
return writeTo(new StringBuilder()).toString();
}
catch (IOException unused) {
throw new Error();
}
}
}
class DataAppendable implements Appendable {
public static DataAppendable of(Appendable a) {
// if already a DataAppendable, cast and return
// possibly specialize in certain known Appendables
// such as StringBuilders/Buffers/Streams etc.
// otherwise, wrap specified and return.
}
// Appendable only provides append(char|CharSequence)
// methods in StringBuilder may be convenient all around...
public DataAppendable append(Object|long|int|double|boolean) {...}
}
Original comment by jysjys1...@gmail.com
on 1 Sep 2013 at 5:51
> throw new Error();
Effective Java, Item 58 (Use checked exceptions for recoverable conditions and
runtime exceptions for programming errors) advises against that:
> While the Java Language Specification does not require it, there is a strong
convention that errors are reserved for use by the JVM to indicate resource
defi-
ciencies, invariant failures, or other conditions that make it impossible to
continue
execution.
Original comment by SeanPFl...@googlemail.com
on 3 Sep 2013 at 7:53
One last note:
Appender sounds as if it should be some 2-arity function, whilst SelfAppending
captures well that the implementation can append itself to an appender:
interface Appender<T> {
public <A extends Appendable> A writeTo(T context, A appendable);
}
interface SelfAppending {
public <A extends Appendable> A writeTo(A appendable);
}
Original comment by jysjys1...@gmail.com
on 3 Sep 2013 at 5:54
The suggested DataAppendable may be convenient. It exists in the Java API as
Printwriter, but unfortunately that one swallows all exceptions (which is not
desired here). However, its usefulness is orthogonal to Appender. We do not
need a TemplateAppender just to use something like DataAppendable, because each
implementation of appendTo(Appendable) can just wrap the Appendable itself it
needed.
An abstract base implementation of Appender that provides a toString()
implementation is indeed useful, I also have one. It is nice to have when you
want to write an anonymous class that implements Appender.
I already mentioned that a base implementation of toString() should be
available. I think the correct exception to throw in this method would be
AssertionError, and of course it should wrap the IOException.
Original comment by phwend...@gmail.com
on 4 Sep 2013 at 6:49
I am not sure whether there is a usecase for a 2-arity Appender. I have never
encountered one. What kind of stuff would be provided as context?
Also it is problematic because the desired equivalency of the appendTo/writeTo
and toString methods cannot be provided, so such an interface is not as
backwards compatible as the 1-arity interface (the nice thing about the latter
is that you can use it everywhere in your code, and code that does know about
it has benefits, but it also works everywhere else).
For example, a Joiner could normally not use a 2-arity appender interface, as
long as it does not get methods added that also provide a context, and then the
Joiner interface would blow up IMHO.
Original comment by phwend...@gmail.com
on 4 Sep 2013 at 6:56
> I am not sure whether there is a usecase for a 2-arity Appender.
I was only pointing out that the name "Appender" seems to suggest that the
object would be appending "something" to an appendable object--the appended
"something" seems vague; whereas, some name that captures appending oneself to
an appendable object could clarify what's being appended: SelfAppending, for
lack of a better name, gives the impression that the implementing object
appends itself. It's just a name, but it's not bad for names to be as
self-documenting as possible.
Original comment by jysjys1...@gmail.com
on 5 Sep 2013 at 2:31
This issue has been migrated to GitHub.
It can be found at https://github.com/google/guava/issues/<issue id>
Original comment by cgdecker@google.com
on 1 Nov 2014 at 4:12
Original comment by cgdecker@google.com
on 1 Nov 2014 at 4:17
Original comment by cgdecker@google.com
on 3 Nov 2014 at 9:08
Original issue reported on code.google.com by
phwend...@gmail.com
on 22 Aug 2013 at 10:37