public class AppendableExt extends Object implements Appendable
All methods will use the Appendee.appendTo(com.healthmarketscience.common.util.AppendableExt)
method if passed an
Appendee.
Additionally, this wrapper adds two convenience methods:
append(Object)
- a method to append Objects by calling
String.valueOf(obj) on the object and appending the result to the
AppendableExt (unless the object is an Appendee or CharSequence)append(Iterable,Object)
- a method to append an iteration
(collection, etc) of objects separated by a delimiterFormatter
/Formattable
facility without
the extra overhead of parsing the format strings. If complicated formatting
is needed for the generated strings, Formatter should be used instead.
Examples:
// // *Without* using this interface. // public class Foo { private Bar b; public String toString() { return "Foo " + b; } } public class Bar { public String toString() { return "Bar"; } } Foo f = new Foo(); StringBuilder sb = new StringBuilder(); // this will involve copying multiple strings before actual append!!! sb.append(f); // // *With* using this interface. // public class Foo implements Appendee { private Bar b; public void appendTo(AppendableExt app) throws IOException { app.append("Foo").append(b); } } public class Bar implements Appendee { public void appendTo(AppendableExt app) throws IOException { app.append("Bar"); } } Foo f = new Foo(); AppendableExt ae = new AppendableExt(new StringBuilder()); // this will involve no extra copies, both strings ("Foo", "Bar") will be // written directly to the Appender ae.append(f);
Constructor and Description |
---|
AppendableExt(Appendable app)
Initialize a new AppendableExt based on the given Appendable.
|
AppendableExt(Appendable app,
Object context)
Initialize a new AppendableExt based on the given Appendable and
context.
|
Modifier and Type | Method and Description |
---|---|
AppendableExt |
append(Appendee a)
Will call the appendTo() method on the given object.
|
AppendableExt |
append(char c) |
AppendableExt |
append(CharSequence s) |
AppendableExt |
append(CharSequence s,
int start,
int end) |
AppendableExt |
append(Iterable<?> iable,
Object delimiter)
Will iterate the given Iterable and append each object separated by the
given delimiter.
|
AppendableExt |
append(Object o)
Will call append(String.valueOf(o)) with the given object (unless the
object is an Appendee or CharSequence in which case it will be passed to
the appropriate method).
|
Appendable |
getAppendable()
Get the underlying Appendable.
|
Object |
getContext() |
void |
setContext(Object newContext)
Sets the "context" that will be returned from subsequent
getContext() calls. |
String |
toString()
Get the result of calling toString() on the underlying Appendable.
|
public AppendableExt(Appendable app)
app
- initial underlying Appendablepublic AppendableExt(Appendable app, Object context)
app
- initial underlying Appendablecontext
- initial append contextpublic AppendableExt append(char c) throws IOException
append
in interface Appendable
IOException
public AppendableExt append(CharSequence s) throws IOException
Note that if the given CharSequence is also an Appendee, it will be handled as an Appendee instead.
append
in interface Appendable
IOException
public AppendableExt append(CharSequence s, int start, int end) throws IOException
Note that if the given CharSequence is also an Appendee, it will be handled as an Appendee instead (but the range will still be respected).
append
in interface Appendable
IOException
public AppendableExt append(Appendee a) throws IOException
a
- object to append to this AppendableExtIOException
- if the append failspublic AppendableExt append(Object o) throws IOException
o
- object to append to this AppendableExtIOException
- if the append failspublic AppendableExt append(Iterable<?> iable, Object delimiter) throws IOException
iable
- an Iterable objectdelimiter
- delimiter to append between each object in the iableIOException
- if the append failspublic Appendable getAppendable()
public String toString()
public Object getContext()
setContext(java.lang.Object)
, if any.public void setContext(Object newContext)
getContext()
calls. Useful for Appendee implementations that
want to change the behavior of nested, context-aware Appendee objects.newContext
- the new context for any subsequent append callsCopyright © 2006–2021 OpenHMS. All rights reserved.