public class DefaultCommandBuilder extends Object implements CommandBuilder
CommandBuilder
. Builds a NoOp
command if no command is joined
to the command chain
of this builder.Constructor and Description |
---|
DefaultCommandBuilder()
|
DefaultCommandBuilder(ExecutionContextFactory contextFactory)
Constructor using the specified factory to create contexts for command
execution.
|
Modifier and Type | Method and Description |
---|---|
CommandBuilder |
apply(LineOperation operation)
Adds a new command based on the specified operation and adds it to the
chain of commands held by this builder.
|
Command<?> |
build()
Builds the composite command and returns it.
|
ExecutionContextFactory |
getContextFactory()
Returns the context factory used to create contexts for command
execution.
|
CommandBuilder |
join(Command<?> command)
Adds the specified command to the chain of commands held by this builder.
|
CommandBuilder |
reset()
Resets this command builder to its initial state.
|
void |
toDevNull()
Executes the composite command and does not write the result anywhere.
|
int |
toExitValue()
Executes the composite command returns its exit value, 0 if the command
completes successfully and another command specific error value different
from zero if the command throws an
ExitValueException . |
void |
toFile(File file)
Executes the composite command and writes the result to the given file.
|
void |
toFile(String file)
Executes the composite command and writes the result to the given file.
|
List<Line> |
toLineList()
Executes the composite command and returns the result as a list
containing the output lines.
|
Stream<Line> |
toLineStream()
Executes the composite command and returns the result as a stream
containing the output lines.
|
void |
toOutput(Output output)
Executes the composite command and writes the result to the given output.
|
void |
toOutputStream(OutputStream stream)
Executes the composite command and writes the result to the given stream.
|
void |
toStdOut()
Executes the composite command and writes the result to the standard
output.
|
String |
toString()
Returns a string representation of the composite command that would be
returned by
CommandBuilder.build() . |
List<String> |
toStringList()
Executes the composite command and returns the result as a list
containing the output lines without line ending.
|
String |
toStringResult()
Executes the composite command and returns the result as string.
|
Stream<String> |
toStringStream()
Executes the composite command and returns the result as a stream
containing the output lines without line endings.
|
void |
toWriter(Writer writer)
Executes the composite command and writes the result using the given
writer.
|
public DefaultCommandBuilder()
NoOp
command if no
command is joined
to this builder's command chain.
Uses a DefaultExecutionContext
to execute commands.public DefaultCommandBuilder(ExecutionContextFactory contextFactory)
NoOp
command which
will be replaced by the first command joined
to
this builder's command chain.contextFactory
- the factory used to create execution contexts that are passed
to the execute method when a command is executedpublic ExecutionContextFactory getContextFactory()
public CommandBuilder join(Command<?> command)
CommandBuilder
grep(..)
methods to join a grep
command.
If this is the fist joined command, the builder builds this
command and returns or executes it if CommandBuilder.build()
or
toXXX(..)
is called, respectively. If the command argument joins
already existing commands, the last command in the chain redirects its
output to the standard input of the specified command.
join
in interface CommandBuilder
command
- the command to join the last previously joined commandpublic CommandBuilder apply(LineOperation operation)
CommandBuilder
If this is the fist joined command, the builder builds the command
created from the operation and returns or executes it if CommandBuilder.build()
or toXXX(..)
is called, respectively. If the command argument joins
already existing commands, the last command in the chain redirects its
output to the standard input of the specified command.
apply
in interface CommandBuilder
operation
- the operation on which the added command is basedpublic CommandBuilder reset()
CommandBuilder
NoOp
command. New commands can be joined
subsequently to create a new command chain.reset
in interface CommandBuilder
public Command<?> build()
CommandBuilder
This method is rarely used by application code. Usually one of the toXXX(..) methods is invoked to execute the command and return the output directly. To get a string representation of the built command, the command's toString() method can be used.
build
in interface CommandBuilder
public String toString()
CommandBuilder
CommandBuilder.build()
. A composite command string looks for
instance like this:
"echo -messages [Hello WORLD] | grep -matchString world -ignoreCase"
Use To.toStringResult()
instead to execute the command and return
the output as a string.
toString
in interface CommandBuilder
toString
in class Object
public void toStdOut()
To
public List<Line> toLineList()
To
toLineList
in interface To
public List<String> toStringList()
To
toStringList
in interface To
public Stream<Line> toLineStream()
To
toLineStream
in interface To
public Stream<String> toStringStream()
To
toStringStream
in interface To
public void toOutput(Output output)
To
public void toFile(String file)
To
public void toFile(File file)
To
public void toOutputStream(OutputStream stream)
To
toOutputStream
in interface To
stream
- the target output streampublic void toWriter(Writer writer)
To
public void toDevNull()
To
public String toStringResult()
To
To return a representation of the command with its arguments without
executing the command, toString()
can be used
instead.
toStringResult
in interface To
public int toExitValue()
To
ExitValueException
.toExitValue
in interface To
Copyright © 2024. All rights reserved.