Added System command description and added recommendation ability
Additionally removed ability to decide whether exceptions thrown by SystemCommands should be rethrown as a mentally superior team member intervened that it would be useless.
This commit is contained in:
		| @@ -37,17 +37,21 @@ public final class SystemCommand { | |||||||
| 	 */ | 	 */ | ||||||
| 	protected final Function<String[], Void> action; | 	protected final Function<String[], Void> action; | ||||||
|  |  | ||||||
|  | 	protected final String description; | ||||||
|  |  | ||||||
| 	/** | 	/** | ||||||
| 	 * Constructs a new {@code NoArgSystemCommand} that takes no arguments. | 	 * Constructs a new {@code NoArgSystemCommand} that takes no arguments. | ||||||
| 	 * | 	 * | ||||||
| 	 * @param action            the action that should be performed | 	 * @param action            the action that should be performed | ||||||
| 	 * @param numberOfArguments the amount of arguments that need to be parsed for | 	 * @param numberOfArguments the amount of arguments that need to be parsed for | ||||||
| 	 *                          the underlying function | 	 *                          the underlying function | ||||||
|  | 	 * @param description       the description of this {@code SystemCommand} | ||||||
| 	 * @since Envoy Client v0.1-beta | 	 * @since Envoy Client v0.1-beta | ||||||
| 	 */ | 	 */ | ||||||
| 	public SystemCommand(Function<String[], Void> action, int numberOfArguments) { | 	public SystemCommand(Function<String[], Void> action, int numberOfArguments, String description) { | ||||||
| 		this.numberOfArguments	= numberOfArguments; | 		this.numberOfArguments	= numberOfArguments; | ||||||
| 		this.action				= action; | 		this.action				= action; | ||||||
|  | 		this.description		= description; | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	/** | 	/** | ||||||
| @@ -62,4 +66,10 @@ public final class SystemCommand { | |||||||
| 	 * @since Envoy Client v0.1-beta | 	 * @since Envoy Client v0.1-beta | ||||||
| 	 */ | 	 */ | ||||||
| 	public int getNumberOfArguments() { return numberOfArguments; } | 	public int getNumberOfArguments() { return numberOfArguments; } | ||||||
|  |  | ||||||
|  | 	/** | ||||||
|  | 	 * @return the description | ||||||
|  | 	 * @since Envoy Client v0.1-beta | ||||||
|  | 	 */ | ||||||
|  | 	public String getDescription() { return description; } | ||||||
| } | } | ||||||
|   | |||||||
| @@ -1,16 +1,14 @@ | |||||||
| package envoy.client.data.commands; | package envoy.client.data.commands; | ||||||
|  |  | ||||||
| import java.util.Arrays; | import java.util.*; | ||||||
| import java.util.HashMap; |  | ||||||
| import java.util.Optional; |  | ||||||
| import java.util.function.Function; | import java.util.function.Function; | ||||||
| import java.util.logging.Level; | import java.util.logging.Level; | ||||||
|  | import java.util.stream.Collectors; | ||||||
|  |  | ||||||
| import envoy.util.EnvoyLog; | import envoy.util.EnvoyLog; | ||||||
|  |  | ||||||
| /** | /** | ||||||
|  * This class stores all {@link SystemCommand}s used. |  * This class stores all {@link SystemCommand}s used. | ||||||
|  * |  | ||||||
|  * <p> |  * <p> | ||||||
|  * Project: <strong>envoy-client</strong><br> |  * Project: <strong>envoy-client</strong><br> | ||||||
|  * File: <strong>SystemCommandsMap.java</strong><br> |  * File: <strong>SystemCommandsMap.java</strong><br> | ||||||
| @@ -23,8 +21,7 @@ import envoy.util.EnvoyLog; | |||||||
|  */ |  */ | ||||||
| public class SystemCommandsMap { | public class SystemCommandsMap { | ||||||
|  |  | ||||||
| 	private final HashMap<String, SystemCommand>	systemCommands	= new HashMap<>(); | 	private final HashMap<String, SystemCommand> systemCommands = new HashMap<>(); | ||||||
| 	private boolean									rethrowFailure	= true; |  | ||||||
|  |  | ||||||
| 	/** | 	/** | ||||||
| 	 * @param command           the string that must be inputted to execute the | 	 * @param command           the string that must be inputted to execute the | ||||||
| @@ -35,7 +32,20 @@ public class SystemCommandsMap { | |||||||
| 	 * @since Envoy Client v0.1-beta | 	 * @since Envoy Client v0.1-beta | ||||||
| 	 */ | 	 */ | ||||||
| 	public void addCommand(String command, Function<String[], Void> action, int numberOfArguments) { | 	public void addCommand(String command, Function<String[], Void> action, int numberOfArguments) { | ||||||
| 		systemCommands.put(command, new SystemCommand(action, numberOfArguments)); | 		systemCommands.put(command, new SystemCommand(action, numberOfArguments, "")); | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	/** | ||||||
|  | 	 * @param command           the string that must be inputted to execute the | ||||||
|  | 	 *                          given action | ||||||
|  | 	 * @param action            the action that should be performed | ||||||
|  | 	 * @param numberOfArguments the amount of arguments that need to be parsed for | ||||||
|  | 	 *                          the underlying function | ||||||
|  | 	 * @param description       the description of this {@link SystemCommand} | ||||||
|  | 	 * @since Envoy Client v0.1-beta | ||||||
|  | 	 */ | ||||||
|  | 	public void addCommand(String command, Function<String[], Void> action, int numberOfArguments, String description) { | ||||||
|  | 		systemCommands.put(command, new SystemCommand(action, numberOfArguments, description)); | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	/** | 	/** | ||||||
| @@ -49,6 +59,20 @@ public class SystemCommandsMap { | |||||||
| 	 */ | 	 */ | ||||||
| 	public void addNoArgCommand(String command, Function<String[], Void> action) { addCommand(command, action, 0); } | 	public void addNoArgCommand(String command, Function<String[], Void> action) { addCommand(command, action, 0); } | ||||||
|  |  | ||||||
|  | 	/** | ||||||
|  | 	 * Adds a new {@link SystemCommand} to the map that does not depend upon | ||||||
|  | 	 * arguments in the given String | ||||||
|  | 	 * | ||||||
|  | 	 * @param command     the string that must be inputted to execute the given | ||||||
|  | 	 *                    action | ||||||
|  | 	 * @param action      the action that should be performed. To see why this | ||||||
|  | 	 *                    Function takes a {@code String[]}, see | ||||||
|  | 	 *                    {@link SystemCommand} | ||||||
|  | 	 * @param description the description of this {@link SystemCommand} | ||||||
|  | 	 * @since Envoy Client v0.1-beta | ||||||
|  | 	 */ | ||||||
|  | 	public void addNoArgCommand(String command, Function<String[], Void> action, String description) { addCommand(command, action, 0); } | ||||||
|  |  | ||||||
| 	/** | 	/** | ||||||
| 	 * @param command           the string that must be inputted to execute the | 	 * @param command           the string that must be inputted to execute the | ||||||
| 	 *                          given action | 	 *                          given action | ||||||
| @@ -60,7 +84,7 @@ public class SystemCommandsMap { | |||||||
| 	 * @since Envoy Client v0.1-beta | 	 * @since Envoy Client v0.1-beta | ||||||
| 	 */ | 	 */ | ||||||
| 	public void add(String command, Function<String[], Void> action, int numberOfArguments) { | 	public void add(String command, Function<String[], Void> action, int numberOfArguments) { | ||||||
| 		systemCommands.put(command, new SystemCommand(action, numberOfArguments)); | 		systemCommands.put(command, new SystemCommand(action, numberOfArguments, "")); | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	/** | 	/** | ||||||
| @@ -84,7 +108,6 @@ public class SystemCommandsMap { | |||||||
| 	 * @since Envoy Client v0.1-beta | 	 * @since Envoy Client v0.1-beta | ||||||
| 	 */ | 	 */ | ||||||
| 	public Optional<SystemCommand> checkPresent(String input) { | 	public Optional<SystemCommand> checkPresent(String input) { | ||||||
| 		// TODO: Is String.indexOf inclusive or exclusive? |  | ||||||
| 		final var firstWord = input.substring(0, input.indexOf(" ")); | 		final var firstWord = input.substring(0, input.indexOf(" ")); | ||||||
| 		return Optional.ofNullable(systemCommands.get(firstWord)); | 		return Optional.ofNullable(systemCommands.get(firstWord)); | ||||||
| 	} | 	} | ||||||
| @@ -113,14 +136,9 @@ public class SystemCommandsMap { | |||||||
| 			// Splitting the String so that the leading command including the first " " is | 			// Splitting the String so that the leading command including the first " " is | ||||||
| 			// removed and only as many following words as allowed by the system command | 			// removed and only as many following words as allowed by the system command | ||||||
| 			// persist | 			// persist | ||||||
| 			// TODO: Is String.indexOf inclusive or exclusive? |  | ||||||
| 			final var remainingString = input.substring(input.indexOf(" ") + 1); | 			final var remainingString = input.substring(input.indexOf(" ") + 1); | ||||||
| 			// TODO: Is Arrays.copyOfRange inclusive or exclusive in the "to" parameter? |  | ||||||
| 			// TODO: Current implementation will fail in certain cases, i.e. two characters | 			// TODO: Current implementation will fail in certain cases, i.e. two characters | ||||||
| 			// behind each other (" "), not enough words, ... | 			// behind each other (" "), not enough words, ... | ||||||
| 			// TODO: Another way of failure is the fact that in the current implementation, |  | ||||||
| 			// it is assumed that a String is already present in finished form and not that |  | ||||||
| 			// it will be finished later on, or does it? |  | ||||||
| 			final var arguments = Arrays.copyOfRange(remainingString.split(" "), 0, systemCommand.getNumberOfArguments()); | 			final var arguments = Arrays.copyOfRange(remainingString.split(" "), 0, systemCommand.getNumberOfArguments()); | ||||||
| 			// Executing the function | 			// Executing the function | ||||||
| 			try { | 			try { | ||||||
| @@ -129,30 +147,46 @@ public class SystemCommandsMap { | |||||||
| 				EnvoyLog.getLogger(SystemCommandsMap.class).log(Level.WARNING, "The system command " + | 				EnvoyLog.getLogger(SystemCommandsMap.class).log(Level.WARNING, "The system command " + | ||||||
| 				// detected command | 				// detected command | ||||||
| 				input.substring(0, input.indexOf(" ")) + " threw an exception: ", e); | 				input.substring(0, input.indexOf(" ")) + " threw an exception: ", e); | ||||||
| 				if (rethrowFailure) throw e; |  | ||||||
| 			} | 			} | ||||||
| 		}); | 		}); | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	/** | 	/** | ||||||
| 	 * @return whether failures of {@link SystemCommand}s should be rethrown | 	 * Retrieves the recommendations based on the current command entered (i.e the | ||||||
|  | 	 * String "/exam" should be entered as "exam"). If none are present, nothing | ||||||
|  | 	 * will be done. | ||||||
|  | 	 * Otherwise the given function will be executed on the recommendations. | ||||||
|  | 	 * | ||||||
|  | 	 * @param input  the String between "/" and " " | ||||||
|  | 	 * @param action the action that should be taken for the recommendations, if any | ||||||
|  | 	 *               are present | ||||||
| 	 * @since Envoy Client v0.1-beta | 	 * @since Envoy Client v0.1-beta | ||||||
| 	 */ | 	 */ | ||||||
| 	public boolean isRethrowFailure() { return rethrowFailure; } | 	public void requestRecommendations(String input, Function<Set<String>, Void> action) { | ||||||
|  | 		final var partialCommand = input.substring(0, input.indexOf(" ")); | ||||||
| 	/** | 		// Get the expected commands | ||||||
| 	 * @param rethrowFailure whether failures of {@link SystemCommand}s should be | 		final var recommendations = recommendCommands(partialCommand); | ||||||
| 	 *                       rethrown | 		if (recommendations.isEmpty()) return; | ||||||
| 	 * @return this instance of a {@code SystemCommandsMap} | 		// Execute the given action | ||||||
| 	 * @since Envoy Client v0.1-beta | 		else action.apply(recommendations); | ||||||
| 	 */ |  | ||||||
| 	public SystemCommandsMap setRethrowFailure(boolean rethrowFailure) { |  | ||||||
| 		this.rethrowFailure = rethrowFailure; |  | ||||||
| 		return this; |  | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	/** | 	/** | ||||||
| 	 * @return the systemCommands | 	 * Recommends commands based upon the currently entered input.<br> | ||||||
|  | 	 * In the current implementation, all we check is whether a key contains this | ||||||
|  | 	 * input. This might be updated later on. | ||||||
|  | 	 * | ||||||
|  | 	 * @param partialCommand the partially entered command | ||||||
|  | 	 * @return a set of all commands that match this input | ||||||
|  | 	 */ | ||||||
|  | 	private Set<String> recommendCommands(String partialCommand) { | ||||||
|  | 		// current implementation only looks if input is contained within a command, | ||||||
|  | 		// might be updated | ||||||
|  | 		return systemCommands.keySet().stream().filter(command -> command.contains(partialCommand)).collect(Collectors.toSet()); | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	/** | ||||||
|  | 	 * @return all {@link SystemCommand}s used with the underlying command as key | ||||||
| 	 * @since Envoy Client v0.1-beta | 	 * @since Envoy Client v0.1-beta | ||||||
| 	 */ | 	 */ | ||||||
| 	public HashMap<String, SystemCommand> getSystemCommands() { return systemCommands; } | 	public HashMap<String, SystemCommand> getSystemCommands() { return systemCommands; } | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user
	 delvh
					delvh