Skip to content

Instantly share code, notes, and snippets.

@Exerosis
Created February 16, 2018 14:28
Show Gist options
  • Save Exerosis/6afbd12292271667ce0c18477b6e75ec to your computer and use it in GitHub Desktop.
Save Exerosis/6afbd12292271667ce0c18477b6e75ec to your computer and use it in GitHub Desktop.
interface Builder {
interface Buffer<Return> extends Output<Return> {
Return __build(OutputStream err,
OutputStream out,
boolean redirect,
Number buffer) throws IOException;
@Override
default Return __build(OutputStream err,
OutputStream out,
boolean redirect) throws IOException {
return __build(err, out, redirect || err == null && out == null, 1024);
}
default Output<Return> buffer(Number buffer) {
return (err, out, redirect) -> __build(err, out, redirect, buffer);
}
}
interface Output<Return> extends Error<Return> {
Return __build(OutputStream err,
OutputStream out,
boolean redirect) throws IOException;
@Override
default Return __build(OutputStream err,
boolean redirect) throws IOException {
return __build(err, null, redirect);
}
default Error<Return> pipeOutput(OutputStream out) {
return (err, redirect) -> __build(err, out, redirect);
}
}
interface Error<Return> {
Return __build(OutputStream err,
boolean redirect) throws IOException;
default Return pipeErrors(OutputStream err) throws IOException {
return __build(err, false);
}
default Return redirectErrors() throws IOException {
return __build(null, true);
}
default Return ignoreErrors() throws IOException {
return __build(null, false);
}
}
}
public static Builder.Buffer<CommandLine> create(String command) {
return (err, out, redirect, buffer) ->
new CommandLine(command, err, out, redirect, buffer.intValue());
}
private CommandLine(String command,
OutputStream out,
OutputStream err,
boolean redirect,
int buffer) throws IOException {
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment