Skip to content

Commit

Permalink
Merge pull request #543 from mattirn/commandRegistry-improvment
Browse files Browse the repository at this point in the history
CommandRegistry interface: removed execute() method
  • Loading branch information
mattirn committed Jun 6, 2020
2 parents 04eaadf + f13b695 commit 497f10c
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 88 deletions.
22 changes: 8 additions & 14 deletions console/src/main/java/org/jline/console/CommandMethods.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,34 +15,28 @@
import org.jline.reader.Completer;

public class CommandMethods {
Consumer<CommandInput> execute;
Function<CommandInput, Object> executeFunction;
Function<CommandInput, ?> execute;
Function<String, List<Completer>> compileCompleter;

public CommandMethods(Function<CommandInput, Object> execute, Function<String, List<Completer>> compileCompleter) {
this.executeFunction = execute;
public CommandMethods(Function<CommandInput, ?> execute, Function<String, List<Completer>> compileCompleter) {
this.execute = execute;
this.compileCompleter = compileCompleter;
}

public CommandMethods(Consumer<CommandInput> execute, Function<String, List<Completer>> compileCompleter) {
this.execute = execute;
this.execute = (CommandInput i) -> {
execute.accept(i);
return null;
};
this.compileCompleter = compileCompleter;
}

public Consumer<CommandInput> execute() {
public Function<CommandInput, ?> execute() {
return execute;
}

public Function<CommandInput, Object> executeFunction() {
return executeFunction;
}

public Function<String, List<Completer>> compileCompleter() {
return compileCompleter;
}

public boolean isConsumer() {
return execute != null;
}

}
27 changes: 3 additions & 24 deletions console/src/main/java/org/jline/console/CommandRegistry.java
Original file line number Diff line number Diff line change
Expand Up @@ -106,39 +106,18 @@ default CmdDesc commandDescription(List<String> args) {
CmdDesc commandDescription(String command);

/**
* Execute a command that have only string parameters and options. Implementation of the method is required
* when aggregating command registries using SystemRegistry.
* @param session the data of the current command session
* @param command the name of the command
* @param args arguments of the command
* @return result of the command execution
* @throws Exception in case of error
*/
default Object execute(CommandSession session, String command, String[] args) throws Exception {
throw new IllegalArgumentException("CommandRegistry method execute(String command, String[] args) is not implemented!");
}

/**
* Execute a command. If command has other than string parameters a custom implementation is required.
* This method will be called only when we have ConsoleEngine in SystemRegistry.
* Execute a command.
* @param session the data of the current command session
* @param command the name of the command
* @param args arguments of the command
* @return result of the command execution
* @throws Exception in case of error
*/
default Object invoke(CommandSession session, String command, Object... args) throws Exception {
String[] _args = new String[args.length];
for (int i = 0; i < args.length; i++) {
if (!(args[i] instanceof String)) {
throw new IllegalArgumentException();
}
_args[i] = args[i].toString();
}
return execute(session, command, _args);
throw new IllegalStateException("CommandRegistry method invoke(session, command, ... args) is not implemented!");
}

public static class CommandSession {
static class CommandSession {
private final Terminal terminal;
private final InputStream in;
private final PrintStream out;
Expand Down
9 changes: 0 additions & 9 deletions console/src/main/java/org/jline/console/SystemRegistry.java
Original file line number Diff line number Diff line change
Expand Up @@ -102,15 +102,6 @@ public interface SystemRegistry extends CommandRegistry, ConsoleOptionGetter {
*/
Terminal terminal();

/**
* Execute command with arguments
* @param command command to be executed
* @param args arguments of the command
* @return command execution result
* @throws Exception in case of error
*/
Object execute(String command, String[] args) throws Exception;

/**
* Execute command with arguments
* @param command command to be executed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import org.jline.console.CommandInput;
import org.jline.console.CommandMethods;
import org.jline.console.CommandRegistry;
import org.jline.console.CommandRegistry.CommandSession;
import org.jline.reader.impl.completer.SystemCompleter;
import org.jline.utils.AttributedString;
import org.jline.utils.AttributedStringBuilder;
Expand All @@ -25,7 +24,7 @@
*
* @author <a href="mailto:matti.rintanikkola@gmail.com">Matti Rinta-Nikkola</a>
*/
public abstract class AbstractCommandRegistry {
public abstract class AbstractCommandRegistry implements CommandRegistry {
private CmdRegistry cmdRegistry;
private Exception exception;

Expand Down Expand Up @@ -65,31 +64,11 @@ public void registerCommands(Map<String,CommandMethods> commandExecute) {
cmdRegistry = new NameCmdRegistry(commandExecute);
}

public Object execute(CommandRegistry.CommandSession session, String command, String[] args) throws Exception {
exception = null;
getCommandMethods(command).execute().accept(new CommandInput(command, args, session));
if (exception != null) {
throw exception;
}
return null;
}

@Override
public Object invoke(CommandSession session, String command, Object... args) throws Exception {
Object out = null;
exception = null;
CommandMethods methods = getCommandMethods(command);
if (methods.isConsumer()) {
String[] _args = new String[args.length];
for (int i = 0; i < args.length; i++) {
if (!(args[i] instanceof String)) {
throw new IllegalArgumentException();
}
_args[i] = args[i].toString();
}
methods.execute().accept(new CommandInput(command, _args, session));
} else {
out = methods.executeFunction().apply(new CommandInput(command, args, session));
}
Object out = methods.execute().apply(new CommandInput(command, args, session));
if (exception != null) {
throw exception;
}
Expand All @@ -100,14 +79,17 @@ public void saveException(Exception exception) {
this.exception = exception;
}

@Override
public boolean hasCommand(String command) {
return cmdRegistry.hasCommand(command);
}

@Override
public Set<String> commandNames() {
return cmdRegistry.commandNames();
}

@Override
public Map<String, String> commandAliases() {
return cmdRegistry.commandAliases();
}
Expand All @@ -120,6 +102,7 @@ public void alias(String alias, String command) {
cmdRegistry.alias(alias, command);
}

@Override
public SystemCompleter compileCompleters() {
return cmdRegistry.compileCompleters();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -810,7 +810,7 @@ public Object invoke(CommandRegistry.CommandSession session, String command, Obj
exception = null;
Object out = null;
if (hasCommand(command)) {
out = getCommandMethods(command).executeFunction().apply(new CommandInput(command, args, session));
out = getCommandMethods(command).execute().apply(new CommandInput(command, args, session));
} else {
String[] _args = new String[args.length];
for (int i = 0; i < args.length; i++) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -387,23 +387,11 @@ public Object invoke(String command, Object... args) throws Exception {
return out;
}

@Override
public Object execute(String command, String[] args) throws Exception {
Object out = null;
int id = registryId(command);
if (id > -1) {
out = commandRegistries[id].execute(commandSession(), command, args);
} else if (isLocalCommand(command)) {
out = localExecute(command, args);
}
return out;
}

private Object localExecute(String command, Object[] args) throws Exception {
if (!isLocalCommand(command)) {
throw new IllegalArgumentException();
}
Object out = commandExecute.get(command).executeFunction()
Object out = commandExecute.get(command).execute()
.apply(new CommandInput(command, args, commandSession()));
if (exception != null) {
throw exception;
Expand Down
6 changes: 3 additions & 3 deletions console/src/test/java/org/jline/example/Console.java
Original file line number Diff line number Diff line change
Expand Up @@ -165,13 +165,13 @@ public SystemCompleter compileCompleters() {
return out;
}

public Object execute(CommandRegistry.CommandSession session, String command, String[] args) throws Exception {
public Object invoke(CommandSession session, String command, Object... args) throws Exception {
exception = null;
commandExecute.get(command(command)).execute().accept(new CommandInput(command, args, session));
Object out = commandExecute.get(command(command)).execute().apply(new CommandInput(command, args, session));
if (exception != null) {
throw exception;
}
return null;
return out;
}

public CmdDesc commandDescription(String command) {
Expand Down

0 comments on commit 497f10c

Please sign in to comment.