T
- the type of this program's UIpublic abstract class SensibleProgram<T extends SensibleUi> extends AbstractProgram
Sensible behavior is achieved via a SensibleUi
.
The only method that must be overridden in a subclass is
run()
; this is the entry point of the program.
This program synchronizes critical sections on the object returned by
lock()
; subclasses that need to synchronize their own
critical sections on the same object can obtain it via this method.
Here's an example "Hello World" program:
public class HelloWorld { public static void main(String[] args) { new SensibleProgram<SensibleUi>(DefaultConfigurableSensibleUi.create()) { @Override public int run() throws Exception { ui().write("%s%n", "hello, world"); return 0; } }.runAndExitVm(); } }
This class is thread-safe.
AbstractProgram.Option
EXIT_FAILURE, EXIT_SUCCESS
Constructor | Description |
---|---|
SensibleProgram(Object lock,
T ui) |
Constructs an instance with the specified critical section lock and UI.
|
SensibleProgram(T ui) |
Constructs an instance with an
Object critical section lock and
the specified UI. |
Modifier and Type | Method | Description |
---|---|---|
void |
flush() |
Flushes any buffered output to underlying standard I/O destinations.
|
void |
printError(Throwable t) |
Prints an error for the specified exception.
|
protected T |
ui() |
Returns the user interface of this program.
|
protected void |
ui(T ui) |
Sets the user interface of this program.
|
exitStatus, lock, runAndExitVm
public SensibleProgram(T ui)
Object
critical section lock and
the specified UI.ui
- the user interface of this program; this program assumes
ownership of the UI; the client should not maintain references to
it and must not use or modify it in any wayNullPointerException
- if ui
is null
public SensibleProgram(Object lock, T ui)
lock
- the object on which this program's critical sections
synchronizeui
- the user interface of this program; this program assumes
ownership of the UI; the client should not maintain references to
it and must not use or modify it in any wayNullPointerException
- if lock
or ui
is null
public void printError(Throwable t)
The error is considered to be printed fully (e.g., flushed) when the call returns.
Clients may ignore any exception thrown by this method.
printError
in interface Program
printError
in class AbstractProgram
InterruptedException
, InterruptedIOException
, or
ClosedByInterruptException
. Otherwise, it calls
lock()
and does the following while synchronized
on the returned object: prints the exception by calling the UI's
error(String,
Object...)
method with an empty string as the first argument
and the exception as the second argument and flushes the UI by
calling the UI's flush()
method.t
- the exception for which an error should be printedStandardIoException
- if an error occurs while writing to an
underlying standard I/O destinationpublic void flush()
flush
in interface Flushable
flush
in interface Program
flush
in class AbstractProgram
lock()
and does the
following while synchronized on the returned object: flushes the
UI by calling the UI's flush()
method.StandardIoException
- if an error occurs while flushing an
underlying standard I/O destinationprotected T ui()
lock()
and does the
following while synchronized on the returned object: returns
this program's UI.protected void ui(T ui)
lock()
and does the
following while synchronized on the returned object: sets this
program's UI.ui
- the user interface of this program; this program assumes
ownership of the UI; the client should not maintain references to
it and must not use or modify it in any wayNullPointerException
- if ui
is null