Order of commands preserved in command provider implementation

Minor correction in the CLIPrompter to avoid null pointer exception when
cancel on promptChoice

Signed-off-by: Emmanuel Bigeon <emmanuel@bigeon.fr>
This commit is contained in:
Emmanuel Bigeon 2016-06-13 15:49:05 -04:00
parent eed6f43aea
commit caa00f2a61
2 changed files with 12 additions and 6 deletions

View File

@ -36,8 +36,8 @@
* Created on: Aug 6, 2014 */ * Created on: Aug 6, 2014 */
package fr.bigeon.gclc.command; package fr.bigeon.gclc.command;
import java.util.HashSet; import java.util.ArrayList;
import java.util.Set; import java.util.List;
import fr.bigeon.gclc.exception.CommandRunException; import fr.bigeon.gclc.exception.CommandRunException;
import fr.bigeon.gclc.exception.InvalidCommandName; import fr.bigeon.gclc.exception.InvalidCommandName;
@ -53,12 +53,12 @@ public class CommandProvider implements ICommandProvider {
/** The space character */ /** The space character */
private static final String SPACE = " "; //$NON-NLS-1$ private static final String SPACE = " "; //$NON-NLS-1$
/** The commands map */ /** The commands map */
protected final Set<ICommand> commands; protected final List<ICommand> commands;
/** Create a command provider */ /** Create a command provider */
public CommandProvider() { public CommandProvider() {
super(); super();
commands = new HashSet<>(); commands = new ArrayList<>();
} }
/* (non-Javadoc) /* (non-Javadoc)
@ -70,6 +70,9 @@ public class CommandProvider implements ICommandProvider {
if (name == null || name.startsWith(MINUS) || name.contains(SPACE)) { if (name == null || name.startsWith(MINUS) || name.contains(SPACE)) {
throw new InvalidCommandName(); throw new InvalidCommandName();
} }
if (commands.contains(value)) {
return true;
}
return commands.add(value); return commands.add(value);
} }

View File

@ -162,8 +162,11 @@ public class CLIPrompter {
public static <U, T> T promptChoice(ConsoleManager manager, List<U> choices, public static <U, T> T promptChoice(ConsoleManager manager, List<U> choices,
Map<U, T> choicesMap, String message, Map<U, T> choicesMap, String message,
String cancel) throws IOException { String cancel) throws IOException {
return choicesMap.get(choices.get( Integer res = promptChoice(manager, choices, message, cancel);
promptChoice(manager, choices, message, cancel).intValue())); if (res == null) {
return null;
}
return choicesMap.get(choices.get(res.intValue()));
} }
/** @param manager the manager /** @param manager the manager