SensibleProgram
public abstract class AbstractProgram extends Object implements Program
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 AbstractProgram() { @Override public int run() throws Exception { System.out.println("hello, world"); return 0; } }.runAndExitVm(); } }
This class is thread-safe.
Modifier and Type | Class | Description |
---|---|---|
static class |
AbstractProgram.Option |
Options controlling the behavior of an
AbstractProgram . |
EXIT_FAILURE, EXIT_SUCCESS
Modifier | Constructor | Description |
---|---|---|
protected |
AbstractProgram() |
Constructs an instance with an
Object critical section lock, an
error stream that is a PrintWriter on the system standard error
stream without buffering, without automatic flushing, and using the
default character encoding, and an empty options set. |
protected |
AbstractProgram(Object lock) |
Constructs an instance with the specified critical section lock, an error
stream that is a
PrintWriter on the system standard error stream
without buffering, without automatic flushing, and using the default
character encoding, and an empty options set. |
protected |
AbstractProgram(Object lock,
PrintWriter err,
Set<AbstractProgram.Option> options) |
Constructs an instance with the specified critical section lock, error
stream, and options set.
|
Modifier and Type | Method | Description |
---|---|---|
int |
exitStatus(Throwable t) |
Returns an exit status for the specified exception.
|
void |
flush() |
Flushes any buffered output to underlying standard I/O destinations.
|
protected Object |
lock() |
Returns the object on which this program's critical sections synchronize.
|
void |
printError(Throwable t) |
Prints an error for the specified exception.
|
void |
runAndExitVm() |
Runs this program and exits the VM with the exit status of this program.
|
protected AbstractProgram()
Object
critical section lock, an
error stream that is a PrintWriter
on the system standard error
stream without buffering, without automatic flushing, and using the
default character encoding, and an empty options set.protected AbstractProgram(Object lock)
PrintWriter
on the system standard error stream
without buffering, without automatic flushing, and using the default
character encoding, and an empty options set.lock
- the object on which this program's critical sections
synchronizeNullPointerException
- if lock
is null
protected AbstractProgram(Object lock, PrintWriter err, Set<AbstractProgram.Option> options)
lock
- the object on which this program's critical sections
synchronizeerr
- the stream where error output should be written; this program
assumes ownership of the stream; the client should not maintain
references to it and must not use or modify it in any way; if the
stream is thread-safe, then it is safe for the client to use it,
but output to it by this program cannot be guaranteed to be
correct unless the client maintains a reference to the
lock
object and synchronizes on it when using the streamoptions
- the set of options controlling how this program behaves;
this program assumes ownership of the set; the client should not
maintain references to it and must not modify it in any wayNullPointerException
- if lock
, err
, or
options
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
lock()
and does the following
while synchronized on the returned object: prints a stack trace
of the specified exception to the error stream and flushes it.
If the specified exception is a ProgramDieException
, a
stack trace is not printed, and instead the exception's message,
if not null
and not empty, is printed followed by the
platform's line separator string. If this program was created
with the LOCALIZED
option, the
localized
message of the ProgramDieException
is used; otherwise
the non-localized
message is used.t
- the exception for which an error should be printedpublic int exitStatus(Throwable t)
Clients may ignore any exception thrown by this method.
exitStatus
in interface Program
Program.EXIT_FAILURE
unless
the specified exception is a ProgramDieException
, in
which case its exit
status is returned.t
- the exception for which an exit status should be returnedpublic void flush()
public void runAndExitVm()
Programs.runAndExitVm(Program)
with this program as the
argument.protected Object lock()