- java.lang.Object
-
- swim.codec.Output<T>
-
- Direct Known Subclasses:
OutputBuffer
public abstract class Output<T> extends Object
Non-blocking token stream writer.Output
enables incremental, interruptible writing of network protocols and data formats.Output tokens
Output tokens are modeled as primitive
int
s, commonly representing Unicode code points, or raw octets; eachOutput
implementation specifies the semantic type of its tokens.Output states
Output
is always in one of four states: continue, full, done, or error. The cont state indicates that the stream is ready to write a single token; the full state indicates that the stream is unable to write additional tokens at this time, but that the stream may logically resume at some point in the future; and the done state indicates that the stream has terminated, and thatbind
will return the output result; and the error state indicates that the stream has terminated abnormally.isCont()
returnstrue
when in the cont state;isFull()
returnstrue
when in the full state;isDone()
returnstrue
when in the done state; andisError()
returnstrue
when in the error state.Output results
An
Output
yields a value of typeT
, obtained via thebind()
method, representing some implementation defined result of writing the output. For example, anOutput<String>
implementation may–but is not required to–yield aString
containing all code points written to the output.Non-blocking behavior
Output
writers never block. AnOutput
that would otherwise block writing additional output instead enters the full state, signaling the output generator to back off producing the output, but to remain prepared to produce additional output in the future. AnOutput
enters the done state when it encounters the final enf of its output, signaling to the output generator to stop producing.Output settings
An output generator may alter the tokens it produces based on its
Output
'ssettings
. Uses include pretty printing and styling generated output.OutputSettings
subclasses can provide additional parameters understood by specialized output producers.Cloning
An
Output
may becloned
to branch the token stream in an implementation specified manner. Not allOutput
implementations support cloning.- See Also:
OutputSettings
,Writer
-
-
Constructor Summary
Constructors Constructor Description Output()
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description abstract T
bind()
Returns the implementation-defined result of writing the output.Output<T>
clone()
Returns an implementation-defined branch of the token stream.Output<T>
debug(Object object)
Writes the code points of the developer-readableDebug
string of the givenobject
.Output<T>
display(Object object)
Writes the code points of the human-readableDisplay
string of the givenobject
.static <T> Output<T>
done()
Returns anOutput
in the done state, that binds anull
result.static <T> Output<T>
done(OutputSettings settings)
Returns anOutput
in the done state, with the givensettings
.static <T> Output<T>
done(T value)
Returns anOutput
in the done state, that binds the givenvalue
.static <T> Output<T>
done(T value, OutputSettings settings)
Returns anOutput
in the done state, that binds the givenvalue
, with the givensettings
.static <T> Output<T>
error(Throwable error)
Returns anOutput
in the error state, with the given outputerror
.static <T> Output<T>
error(Throwable error, OutputSettings settings)
Returns anOutput
in the error state, with the given outputerror
andsettings
.Output<T>
flush()
Writes any internally buffered state to the underlying output stream.Output<T>
fork(Object condition)
Returns anOutput
equivalent to thisOutput
, but whose behavior may be altered by the given out-of-bandcondition
.static <T> Output<T>
full()
Returns anOutput
in the full state, that binds anull
result.static <T> Output<T>
full(OutputSettings settings)
Returns anOutput
in the full state, with the givensettings
.static <T> Output<T>
full(T value)
Returns anOutput
in the full state, that binds the givenvalue
.static <T> Output<T>
full(T value, OutputSettings settings)
Returns anOutput
in the full state, that binds the givenvalue
, with the givensettings
.abstract boolean
isCont()
Returnstrue
when the nextwrite(int)
will succeed.abstract boolean
isDone()
Returnstrue
when nowrite
will ever again suucced.abstract boolean
isError()
Returnstrue
when an immediatewrite
will fail due to an error with the token stream.abstract boolean
isFull()
Returnstrue
when an immediatewrite
will fail, but writes may succeed at some point in the future.abstract boolean
isPart()
Returnstrue
if this is a partialOutput
that will enter the full state when it is unable to write additional tokens.abstract Output<T>
isPart(boolean isPart)
Returns a partialOutput
equivalent to thisOutput
, ifisPart
istrue
; returns a finalOutput
equivalent to thisOutput
ifisPart
isfalse
.abstract OutputSettings
settings()
Returns theOutputSettings
used to configure the behavior of output producers that write to thisOutput
.abstract Output<T>
settings(OutputSettings settings)
Updates thesettings
associated with thisOutput
.Throwable
trap()
Returns the output error.abstract Output<T>
write(int token)
Writes a singletoken
to the stream, if thisOutput
is in the cont state.Output<T>
write(String string)
Writes the code points of the givenstring
.Output<T>
writeln()
Writes the code points of thesettings
'line separator
.Output<T>
writeln(String string)
Writes the code points of the givenstring
, followed by the code points of thesettings
'line separator
.
-
-
-
Method Detail
-
isCont
public abstract boolean isCont()
-
isFull
public abstract boolean isFull()
Returnstrue
when an immediatewrite
will fail, but writes may succeed at some point in the future. i.e. thisOutput
is in the full state.
-
isDone
public abstract boolean isDone()
Returnstrue
when nowrite
will ever again suucced. i.e. thisOutput
is in the done state.
-
isError
public abstract boolean isError()
Returnstrue
when an immediatewrite
will fail due to an error with the token stream. i.e. thisOutput
is in the error state. Whentrue
,trap()
will return the output error.
-
isPart
public abstract boolean isPart()
Returnstrue
if this is a partialOutput
that will enter the full state when it is unable to write additional tokens.
-
isPart
public abstract Output<T> isPart(boolean isPart)
Returns a partialOutput
equivalent to thisOutput
, ifisPart
istrue
; returns a finalOutput
equivalent to thisOutput
ifisPart
isfalse
. The caller's reference tothis
Output
should be replaced by the returnedOutput
.
-
write
public abstract Output<T> write(int token)
Writes a singletoken
to the stream, if thisOutput
is in the cont state. Returns anOutput
in the error state if thisOutput
is not in the cont state. The caller's reference tothis
Output
should be replaced by the returnedOutput
.
-
write
public Output<T> write(String string)
Writes the code points of the givenstring
. Assumes this is a UnicodeOutput
with sufficient capacity. Returns anOutput
in the error state if thisOutput
exits the cont state before the fullstring
has been writtem. The caller's reference tothis
Output
should be replaced by the returnedOutput
.
-
writeln
public Output<T> writeln(String string)
Writes the code points of the givenstring
, followed by the code points of thesettings
'line separator
. Assumes this is a UnicodeOutput
with sufficient capacity. Returns anOutput
in the error state if thisOutput
exits the cont state before the fullstring
and line separator has been written. The caller's reference tothis
Output
should be replaced by the returnedOutput
.
-
writeln
public Output<T> writeln()
Writes the code points of thesettings
'line separator
. Assumes this is a UnicodeOutput
with sufficient capacity. Returns anOutput
in the error state if thisOutput
exits the cont state before the full line separator has been written. The caller's reference tothis
Output
should be replaced by the returnedOutput
.
-
display
public Output<T> display(Object object)
Writes the code points of the human-readableDisplay
string of the givenobject
. Assumes this is a UnicodeOutput
with sufficient capacity. Returns anOutput
in the error state if thisOutput
exits the contt state before the full display string has been written. The caller's reference tothis
Output
should be replaced by the returnedOutput
.
-
debug
public Output<T> debug(Object object)
Writes the code points of the developer-readableDebug
string of the givenobject
. Assumes this is a UnicodeOutput
with sufficient capacity. Returns anOutput
in the error state if thisOutput
exits the contt state before the full debug string has been written. The caller's reference tothis
Output
should be replaced by the returnedOutput
.
-
flush
public Output<T> flush()
Writes any internally buffered state to the underlying output stream.
-
fork
public Output<T> fork(Object condition)
Returns anOutput
equivalent to thisOutput
, but whose behavior may be altered by the given out-of-bandcondition
. The caller's reference tothis
Output
should be replaced by the returnedOutput
.
-
bind
public abstract T bind()
Returns the implementation-defined result of writing the output.
-
trap
public Throwable trap()
Returns the output error. Only guaranteed to return an error when in the error state.- Throws:
OutputException
- if thisOutput
is not in the error state.
-
settings
public abstract OutputSettings settings()
Returns theOutputSettings
used to configure the behavior of output producers that write to thisOutput
.
-
settings
public abstract Output<T> settings(OutputSettings settings)
Updates thesettings
associated with thisOutput
.- Returns:
this
-
clone
public Output<T> clone()
Returns an implementation-defined branch of the token stream.- Overrides:
clone
in classObject
- Throws:
UnsupportedOperationException
- if thisOutput
cannot be cloned.
-
full
public static <T> Output<T> full()
Returns anOutput
in the full state, that binds anull
result.
-
full
public static <T> Output<T> full(OutputSettings settings)
Returns anOutput
in the full state, with the givensettings
.
-
full
public static <T> Output<T> full(T value)
Returns anOutput
in the full state, that binds the givenvalue
.
-
full
public static <T> Output<T> full(T value, OutputSettings settings)
Returns anOutput
in the full state, that binds the givenvalue
, with the givensettings
.
-
done
public static <T> Output<T> done()
Returns anOutput
in the done state, that binds anull
result.
-
done
public static <T> Output<T> done(OutputSettings settings)
Returns anOutput
in the done state, with the givensettings
.
-
done
public static <T> Output<T> done(T value)
Returns anOutput
in the done state, that binds the givenvalue
.
-
done
public static <T> Output<T> done(T value, OutputSettings settings)
Returns anOutput
in the done state, that binds the givenvalue
, with the givensettings
.
-
error
public static <T> Output<T> error(Throwable error)
Returns anOutput
in the error state, with the given outputerror
.
-
error
public static <T> Output<T> error(Throwable error, OutputSettings settings)
Returns anOutput
in the error state, with the given outputerror
andsettings
.
-
-