Skip to content

Instantly share code, notes, and snippets.

@Exerosis
Created February 16, 2018 14:37
Show Gist options
  • Save Exerosis/e6f03e0c12181a39490c93a709d0820b to your computer and use it in GitHub Desktop.
Save Exerosis/e6f03e0c12181a39490c93a709d0820b 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, 1024);
}
default Output<Return> buffer(Number buffer) {
return (err, out, redirect) -> __build(err, out, redirect, buffer);
}
}
interface Output<Return> extends Error.Ignore<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 Redirect<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);
}
interface Redirect<Return> extends Error<Return> {
default Return redirectErrors() throws IOException {
return __build(null, true);
}
}
interface Ignore<Return> extends Error<Return> {
default Return ignoreErrors() throws IOException {
return __build(null, true);
}
}
}
}
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