Interface Output
-
- All Superinterfaces:
Appendable,AutoCloseable,Closeable
- All Known Subinterfaces:
Output.Readable
public interface Output extends Appendable, Closeable
Unifies the different classes that can handle text output and formatting, such asPrintStream,PrintWriter, andStringBuilder.The methods in this interface are defined to return the
Outputinstance itself (this), in order to allow chaining method calls in a "fluent" style.
-
-
Nested Class Summary
Nested Classes Modifier and Type Interface Description static interfaceOutput.ReadableAn extension ofOutputthat signals that what was written can be read back.
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Default Methods Modifier and Type Method Description default Outputand(Output output)Fluent API for multiplexing multipleOutputinstances.default Outputappend(boolean x)Append a boolean value.Outputappend(char x)default Outputappend(char[] str)Append an array of characters.default Outputappend(char[] str, int offset, int len)Append a range from an array of characters.default Outputappend(double x)Append a double-precision floating-point number.default Outputappend(float x)Append a floating-point number.default Outputappend(int x)Append an integer.default Outputappend(long x)Append a long integer.default Outputappend(CharSequence str)default Outputappend(CharSequence str, int start, int end)default Outputappend(String str)Append a string.default <T> Outputappend(T value, BiConsumer<T,Output> writer)Append an arbitrary object, formatted by a supplied method for writing the object.default OutputappendCodePoint(int codePoint)Appends the string representation of thecodePointargument to this sequence.default voidclose()static intcodePointAt(CharSequence str, int index)Get the Unicode code point of the specified character sequence at the specified index.default Outputescape(CharSequence str, int start, int end, IntFunction<String> replacement)Escape the code points of the given character sequence according to the given replacement function.default Outputescape(CharSequence str, IntFunction<String> replacement)Escape the code points of the given character sequence according to the given replacement function.default voidflush()Flushes the underlying output.default Outputformat(String format, Object... args)Appends a formatted string to this output stream using the specified format string and arguments.default Outputformat(Locale l, String format, Object... args)Appends a formatted string to this output stream using the specified format string and arguments.static voidgetChars(CharSequence source, int begin, int end, char[] target, int off)Copies characters from the sourceCharSequenceinto the destination character array.static OutputlineNumbers(Output output)Wrap anOutputin another instance that prepends a line number to every line written.static Stringlines(String... lines)Join several strings as lines into one string with a line separator in between.static Outputmultiplex(Output... output)Combine multipleOutputinstances to one instance that writes to all combined instances.static Output.Readablenowhere()Get anOutputinstance that writes nowhere.static Outputoutput(OutputStream stream)Adapt anOutputStreamto the Output interface.static Outputoutput(Writer writer)Adapt aWriterto the Output interface.static Output.Readableoutput(StringBuffer buffer)Adapt aStringBufferto the Output interface.static Output.Readableoutput(StringBuilder builder)Adapt aStringBuilderto the Output interface.static Outputoutput(CharBuffer buffer)Adapt aCharBufferto the Output interface.static Outputoutput(Path path)Create anOutputthat writes to the specified file.default OutputprintLines(String text, String separator)Append a specified string, with a given separator inserted between each line in the string.default Outputprintln()Append a line separator.default Outputprintln(boolean x)Append a boolean value and a line separator.default Outputprintln(char x)Append the specified character and a line separator.default Outputprintln(char[] str)Append an array of characters and a line separator.default Outputprintln(double x)Append a double-precision floating-point number and a line separator.default Outputprintln(float x)Append a floating-point number and a line separator.default Outputprintln(int x)Append an integer and a line separator.default Outputprintln(long x)Append a long integer and a line separator.default Outputprintln(String str)Append a string and a line separator.default Outputrepeat(int cp, int times)static OutputstdErr()Get anOutputinstance that writes tothe standard error output stream.static OutputstdOut()Get anOutputinstance that writes tothe standard output stream.static <T> Stringstring(T value, BiConsumer<T,Output> writer)static Output.ReadablestringBuilder()Create a newOutputthat writes to a new string builder.static OutputstringBuilder(int size)Create a newOutputthat writes to a new string builder of given capacity.default Writerwriter()Return aWriterthat writes to thisOutput.
-
-
-
Method Detail
-
output
static Output output(OutputStream stream)
Adapt anOutputStreamto the Output interface.- Parameters:
stream- theOutputStreamto adapt.- Returns:
- an
Outputinstance that writes to the suppliedOutputStream.
-
output
static Output.Readable output(StringBuilder builder)
Adapt aStringBuilderto the Output interface.- Parameters:
builder- theStringBuilderto adapt.- Returns:
- a readable
Outputinstance that writes to the suppliedStringBuilder.
-
output
static Output.Readable output(StringBuffer buffer)
Adapt aStringBufferto the Output interface.- Parameters:
buffer- theStringBufferto adapt.- Returns:
- a readable
Outputinstance that writes to the suppliedStringBuffer.
-
output
static Output output(CharBuffer buffer)
Adapt aCharBufferto the Output interface.- Parameters:
buffer- theCharBufferto adapt.- Returns:
- an
Outputinstance that writes to the suppliedCharBuffer.
-
output
static Output output(Path path)
Create anOutputthat writes to the specified file.- Parameters:
path- the file to write to.- Returns:
- an
Outputinstance that writes to the specified file.
-
stringBuilder
static Output.Readable stringBuilder()
Create a newOutputthat writes to a new string builder. The returned object can be used to retrieve the constructed string.- Returns:
- a new readable
Outputinstance.
-
stringBuilder
static Output stringBuilder(int size)
Create a newOutputthat writes to a new string builder of given capacity. The returned object can be used to retrieve the constructed string.- Parameters:
size- the initial capacity of the internal string builder.- Returns:
- a new readable
Outputinstance.
-
stdOut
static Output stdOut()
Get anOutputinstance that writes tothe standard output stream.- Returns:
- an
Outputinstance that writes tostandard out.
-
stdErr
static Output stdErr()
Get anOutputinstance that writes tothe standard error output stream.- Returns:
- an
Outputinstance that writes tostandard error.
-
nowhere
static Output.Readable nowhere()
Get anOutputinstance that writes nowhere.The returned instance is readable in order to be usable in places where a readable output is required, although the result of reading from this instance is always as if nothing was written.
- Returns:
- an
Outputinstance that writes nowhere.
-
lineNumbers
static Output lineNumbers(Output output)
Wrap anOutputin another instance that prepends a line number to every line written.- Parameters:
output- the wrapped output, where numbered lines will be written to.- Returns:
- an
Outputinstance that prepends lines with line numbers.
-
multiplex
static Output multiplex(Output... output)
Combine multipleOutputinstances to one instance that writes to all combined instances.- Parameters:
output- the output instances to combine and write all output to.- Returns:
- an
Outputinstance that writes to all the supplied instances.
-
and
default Output and(Output output)
Fluent API for multiplexing multipleOutputinstances.- Parameters:
output- anOutputinstance to also write to.- Returns:
- an
Outputinstance that writes to boththisand the suppliedOutputinstance.
-
string
static <T> String string(T value, BiConsumer<T,Output> writer)
Convenience method for converting an object to aStringthrough the means of a method that knows how to write such an object to anOutput.- Type Parameters:
T- the type of the object to write.- Parameters:
value- the object to convert to aString.writer- the method that knows how to write the supplied object to anOutput.- Returns:
- the resulting
String. - See Also:
append(Object, BiConsumer)
-
lines
static String lines(String... lines)
Join several strings as lines into one string with a line separator in between.- Parameters:
lines- the lines to join.- Returns:
- a string that consists of the supplied lines.
-
repeat
default Output repeat(int cp, int times)
-
escape
default Output escape(CharSequence str, IntFunction<String> replacement)
Escape the code points of the given character sequence according to the given replacement function.The replacement function can (and is recommended to) be a partial function, only returning replacements for the code points that are to be escaped and returning
nullfor other code points.- Parameters:
str- the character sequence to escape the contents of.replacement- the replacement function that defines how to escape the code points that need escaping.- Returns:
- this
Outputinstance to allow invocation chaining.
-
escape
default Output escape(CharSequence str, int start, int end, IntFunction<String> replacement)
Escape the code points of the given character sequence according to the given replacement function.The replacement function can (and is recommended to) be a partial function, only returning replacements for the code points that are to be escaped and returning
nullfor other code points.- Parameters:
str- the character sequence to escape the contents of.start- the position in the given character sequence to start at.end- the position in the given character sequence to end at.replacement- the replacement function that defines how to escape the code points that need escaping.- Returns:
- this
Outputinstance to allow invocation chaining.
-
append
default <T> Output append(T value, BiConsumer<T,Output> writer)
Append an arbitrary object, formatted by a supplied method for writing the object.Sample usage of this method:
class Container { private Contained one, other; public @Override String toString() { return Output.stringBuilder().append( this, Container::write ).toString(); } public void write( Output output ) { output.append( "Container[one=" ).append( one, Contained::write ) .append( ", other=" ).append( other, Contained::write ).append( ']' ); } }- Type Parameters:
T- the type of the object to write.- Parameters:
value- the object to write.writer- the method that knows how to write the supplied object to anOutput.- Returns:
- this
Outputinstance to allow invocation chaining.
-
append
Output append(char x)
- Specified by:
appendin interfaceAppendable
-
append
default Output append(CharSequence str)
- Specified by:
appendin interfaceAppendable
-
append
default Output append(CharSequence str, int start, int end)
- Specified by:
appendin interfaceAppendable
-
append
default Output append(boolean x)
Append a boolean value.- Parameters:
x- thebooleanto append.- Returns:
- this
Outputinstance to allow invocation chaining.
-
append
default Output append(int x)
Append an integer.- Parameters:
x- theintto append.- Returns:
- this
Outputinstance to allow invocation chaining.
-
append
default Output append(long x)
Append a long integer.- Parameters:
x- thelongto append.- Returns:
- this
Outputinstance to allow invocation chaining.
-
append
default Output append(float x)
Append a floating-point number.- Parameters:
x- thefloatto append.- Returns:
- this
Outputinstance to allow invocation chaining.
-
append
default Output append(double x)
Append a double-precision floating-point number.- Parameters:
x- thedoubleto append.- Returns:
- this
Outputinstance to allow invocation chaining.
-
appendCodePoint
default Output appendCodePoint(int codePoint)
Appends the string representation of thecodePointargument to this sequence.- Parameters:
codePoint- a Unicode code point- Returns:
- this
Outputinstance to allow invocation chaining. - Throws:
IllegalArgumentException- if the specifiedcodePointisn't a valid Unicode code point.- See Also:
the corresponding method of StringBuilder
-
append
default Output append(String str)
Append a string.- Parameters:
str- theStringto append.- Returns:
- this
Outputinstance to allow invocation chaining.
-
append
default Output append(char[] str)
Append an array of characters.- Parameters:
str- thechar[]to append.- Returns:
- this
Outputinstance to allow invocation chaining.
-
append
default Output append(char[] str, int offset, int len)
Append a range from an array of characters.- Parameters:
str- thechar[]to append.offset- the position in the supplied array to start from.len- the number of characters from the supplied array to append.- Returns:
- this
Outputinstance to allow invocation chaining.
-
printLines
default Output printLines(String text, String separator)
Append a specified string, with a given separator inserted between each line in the string.- Parameters:
text- theStringto append.separator- theStringto insert between each line of the string.- Returns:
- this
Outputinstance to allow invocation chaining.
-
println
default Output println()
Append a line separator.- Returns:
- this
Outputinstance to allow invocation chaining. - See Also:
System.lineSeparator()
-
println
default Output println(boolean x)
Append a boolean value and a line separator.- Parameters:
x- thebooleanto append.- Returns:
- this
Outputinstance to allow invocation chaining.
-
println
default Output println(char x)
Append the specified character and a line separator.- Parameters:
x- thecharto append.- Returns:
- this
Outputinstance to allow invocation chaining.
-
println
default Output println(int x)
Append an integer and a line separator.- Parameters:
x- theintto append.- Returns:
- this
Outputinstance to allow invocation chaining.
-
println
default Output println(long x)
Append a long integer and a line separator.- Parameters:
x- thelongto append.- Returns:
- this
Outputinstance to allow invocation chaining.
-
println
default Output println(float x)
Append a floating-point number and a line separator.- Parameters:
x- thefloatto append.- Returns:
- this
Outputinstance to allow invocation chaining.
-
println
default Output println(double x)
Append a double-precision floating-point number and a line separator.- Parameters:
x- thedoubleto append.- Returns:
- this
Outputinstance to allow invocation chaining.
-
println
default Output println(char[] str)
Append an array of characters and a line separator.- Parameters:
str- thechar[]to append.- Returns:
- this
Outputinstance to allow invocation chaining.
-
println
default Output println(String str)
Append a string and a line separator.- Parameters:
str- theStringto append.- Returns:
- this
Outputinstance to allow invocation chaining.
-
format
default Output format(String format, Object... args)
Appends a formatted string to this output stream using the specified format string and arguments.- Parameters:
format- the format string.args- Arguments referenced by the format specifiers in the format string.- Returns:
- this
Outputinstance to allow invocation chaining. - Throws:
IllegalFormatException- If a format string contains an illegal syntax, a format specifier that is incompatible with the given arguments, insufficient arguments given the format string, or other illegal conditions.- See Also:
Formatter.format(String, Object...)
-
format
default Output format(Locale l, String format, Object... args)
Appends a formatted string to this output stream using the specified format string and arguments.- Parameters:
l- The locale to apply during formatting. Iflisnullthen no localization is applied.format- the format string.args- Arguments referenced by the format specifiers in the format string.- Returns:
- this
Outputinstance to allow invocation chaining. - Throws:
IllegalFormatException- If a format string contains an illegal syntax, a format specifier that is incompatible with the given arguments, insufficient arguments given the format string, or other illegal conditions.- See Also:
Formatter.format(Locale, String, Object...)
-
flush
default void flush()
Flushes the underlying output.
-
close
default void close()
- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable
-
writer
default Writer writer()
Return aWriterthat writes to thisOutput.- Returns:
- a
Writerthat writes to thisOutput.
-
codePointAt
static int codePointAt(CharSequence str, int index)
Get the Unicode code point of the specified character sequence at the specified index.This method improves the corresponding method in
Characterby delegating to methods on specificCharSequenceimplementations if present.- Parameters:
str- the character sequence to get the code point from.index- the index in the character sequence the get the code point at.- Returns:
- the code point value of the character(s) at the
indexof the character sequence. - Throws:
IndexOutOfBoundsException- if theindexargument is negative or not less than the length of the contents of the character sequence.- See Also:
Character.codePointAt(CharSequence, int)
-
getChars
static void getChars(CharSequence source, int begin, int end, char[] target, int off)
Copies characters from the sourceCharSequenceinto the destination character array.This method delegates to methods on specific
CharSequenceimplementations if present.- Parameters:
source- the character sequence to copy characters from.begin- the index of the first character in the sequence to copy.end- the index after the last character in the sequence to copy.target- the destination array.off- the start offset in the destination array.- Throws:
IndexOutOfBoundsException- If any of the following is true:srcBeginis negative.srcBeginis greater thansrcEndsrcEndis greater than the length of the character sequencedstBeginis negativedstBegin+(srcEnd-srcBegin)is larger thandst.length
- See Also:
the corresponding method in String
-
-