- java.lang.Object
-
- swim.codec.Output<T>
-
- Direct Known Subclasses:
OutputBuffer
public abstract class Output<T> extends Object
Non-blocking token stream writer.Outputenables incremental, interruptible writing of network protocols and data formats.Output tokens
Output tokens are modeled as primitive
ints, commonly representing Unicode code points, or raw octets; eachOutputimplementation specifies the semantic type of its tokens.Output states
Outputis 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 thatbindwill return the output result; and the error state indicates that the stream has terminated abnormally.isCont()returnstruewhen in the cont state;isFull()returnstruewhen in the full state;isDone()returnstruewhen in the done state; andisError()returnstruewhen in the error state.Output results
An
Outputyields 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 aStringcontaining all code points written to the output.Non-blocking behavior
Outputwriters never block. AnOutputthat 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. AnOutputenters 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.OutputSettingssubclasses can provide additional parameters understood by specialized output producers.Cloning
An
Outputmay beclonedto branch the token stream in an implementation specified manner. Not allOutputimplementations 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 Tbind()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-readableDebugstring of the givenobject.Output<T>display(Object object)Writes the code points of the human-readableDisplaystring of the givenobject.static <T> Output<T>done()Returns anOutputin the done state, that binds anullresult.static <T> Output<T>done(OutputSettings settings)Returns anOutputin the done state, with the givensettings.static <T> Output<T>done(T value)Returns anOutputin the done state, that binds the givenvalue.static <T> Output<T>done(T value, OutputSettings settings)Returns anOutputin the done state, that binds the givenvalue, with the givensettings.static <T> Output<T>error(Throwable error)Returns anOutputin the error state, with the given outputerror.static <T> Output<T>error(Throwable error, OutputSettings settings)Returns anOutputin the error state, with the given outputerrorandsettings.Output<T>flush()Writes any internally buffered state to the underlying output stream.Output<T>fork(Object condition)Returns anOutputequivalent to thisOutput, but whose behavior may be altered by the given out-of-bandcondition.static <T> Output<T>full()Returns anOutputin the full state, that binds anullresult.static <T> Output<T>full(OutputSettings settings)Returns anOutputin the full state, with the givensettings.static <T> Output<T>full(T value)Returns anOutputin the full state, that binds the givenvalue.static <T> Output<T>full(T value, OutputSettings settings)Returns anOutputin the full state, that binds the givenvalue, with the givensettings.abstract booleanisCont()Returnstruewhen the nextwrite(int)will succeed.abstract booleanisDone()Returnstruewhen nowritewill ever again suucced.abstract booleanisError()Returnstruewhen an immediatewritewill fail due to an error with the token stream.abstract booleanisFull()Returnstruewhen an immediatewritewill fail, but writes may succeed at some point in the future.abstract booleanisPart()Returnstrueif this is a partialOutputthat will enter the full state when it is unable to write additional tokens.abstract Output<T>isPart(boolean isPart)Returns a partialOutputequivalent to thisOutput, ifisPartistrue; returns a finalOutputequivalent to thisOutputifisPartisfalse.abstract OutputSettingssettings()Returns theOutputSettingsused to configure the behavior of output producers that write to thisOutput.abstract Output<T>settings(OutputSettings settings)Updates thesettingsassociated with thisOutput.Throwabletrap()Returns the output error.abstract Output<T>write(int token)Writes a singletokento the stream, if thisOutputis 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()
Returnstruewhen an immediatewritewill fail, but writes may succeed at some point in the future. i.e. thisOutputis in the full state.
-
isDone
public abstract boolean isDone()
Returnstruewhen nowritewill ever again suucced. i.e. thisOutputis in the done state.
-
isError
public abstract boolean isError()
Returnstruewhen an immediatewritewill fail due to an error with the token stream. i.e. thisOutputis in the error state. Whentrue,trap()will return the output error.
-
isPart
public abstract boolean isPart()
Returnstrueif this is a partialOutputthat will enter the full state when it is unable to write additional tokens.
-
isPart
public abstract Output<T> isPart(boolean isPart)
Returns a partialOutputequivalent to thisOutput, ifisPartistrue; returns a finalOutputequivalent to thisOutputifisPartisfalse. The caller's reference tothisOutputshould be replaced by the returnedOutput.
-
write
public abstract Output<T> write(int token)
Writes a singletokento the stream, if thisOutputis in the cont state. Returns anOutputin the error state if thisOutputis not in the cont state. The caller's reference tothisOutputshould be replaced by the returnedOutput.
-
write
public Output<T> write(String string)
Writes the code points of the givenstring. Assumes this is a UnicodeOutputwith sufficient capacity. Returns anOutputin the error state if thisOutputexits the cont state before the fullstringhas been writtem. The caller's reference tothisOutputshould 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 UnicodeOutputwith sufficient capacity. Returns anOutputin the error state if thisOutputexits the cont state before the fullstringand line separator has been written. The caller's reference tothisOutputshould be replaced by the returnedOutput.
-
writeln
public Output<T> writeln()
Writes the code points of thesettings'line separator. Assumes this is a UnicodeOutputwith sufficient capacity. Returns anOutputin the error state if thisOutputexits the cont state before the full line separator has been written. The caller's reference tothisOutputshould be replaced by the returnedOutput.
-
display
public Output<T> display(Object object)
Writes the code points of the human-readableDisplaystring of the givenobject. Assumes this is a UnicodeOutputwith sufficient capacity. Returns anOutputin the error state if thisOutputexits the contt state before the full display string has been written. The caller's reference tothisOutputshould be replaced by the returnedOutput.
-
debug
public Output<T> debug(Object object)
Writes the code points of the developer-readableDebugstring of the givenobject. Assumes this is a UnicodeOutputwith sufficient capacity. Returns anOutputin the error state if thisOutputexits the contt state before the full debug string has been written. The caller's reference tothisOutputshould 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 anOutputequivalent to thisOutput, but whose behavior may be altered by the given out-of-bandcondition. The caller's reference tothisOutputshould 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 thisOutputis not in the error state.
-
settings
public abstract OutputSettings settings()
Returns theOutputSettingsused to configure the behavior of output producers that write to thisOutput.
-
settings
public abstract Output<T> settings(OutputSettings settings)
Updates thesettingsassociated with thisOutput.- Returns:
this
-
clone
public Output<T> clone()
Returns an implementation-defined branch of the token stream.- Overrides:
clonein classObject- Throws:
UnsupportedOperationException- if thisOutputcannot be cloned.
-
full
public static <T> Output<T> full()
Returns anOutputin the full state, that binds anullresult.
-
full
public static <T> Output<T> full(OutputSettings settings)
Returns anOutputin the full state, with the givensettings.
-
full
public static <T> Output<T> full(T value)
Returns anOutputin the full state, that binds the givenvalue.
-
full
public static <T> Output<T> full(T value, OutputSettings settings)
Returns anOutputin the full state, that binds the givenvalue, with the givensettings.
-
done
public static <T> Output<T> done()
Returns anOutputin the done state, that binds anullresult.
-
done
public static <T> Output<T> done(OutputSettings settings)
Returns anOutputin the done state, with the givensettings.
-
done
public static <T> Output<T> done(T value)
Returns anOutputin the done state, that binds the givenvalue.
-
done
public static <T> Output<T> done(T value, OutputSettings settings)
Returns anOutputin the done state, that binds the givenvalue, with the givensettings.
-
error
public static <T> Output<T> error(Throwable error)
Returns anOutputin the error state, with the given outputerror.
-
error
public static <T> Output<T> error(Throwable error, OutputSettings settings)
Returns anOutputin the error state, with the given outputerrorandsettings.
-
-