Adding javadoc and code compliance

Signed-off-by: Emmanuel Bigeon <emmanuel@bigeon.fr>
This commit is contained in:
2017-11-13 22:01:33 -05:00
parent fce2b01914
commit 83c02f82ec
46 changed files with 1051 additions and 1002 deletions

View File

@@ -56,15 +56,14 @@ import fr.bigeon.gclc.manager.ConsoleInput;
import fr.bigeon.gclc.manager.ConsoleOutput;
/**
* <p>
* A {@link ConsoleApplication} is an application that require the user to input
* commands.
* <p>
* A typical use case is the following:
*
* <pre>
* {@link ConsoleOutput} out = new {@link fr.bigeon.gclc.manager.SystemConsoleOutput SystemConsoleOutput}();
* {@link ConsoleInput} in = new {@link fr.bigeon.gclc.manager.SystemConsoleInput SystemConsoleInput}();
* {@link ConsoleOutput} out = new {@link fr.bigeon.gclc.manager.StreamConsoleOutput StreamConsoleOutput}();
* {@link ConsoleInput} in = new {@link fr.bigeon.gclc.manager.StreamConsoleInput StreamConsoleInput}();
* {@link ConsoleApplication} app = new {@link ConsoleApplication}(out, in, "welcome", "see you latter")};
* app.{@link ConsoleApplication#add(ICommand) add}("my_command", new {@link ICommand MyCommand()});
* app.{@link ConsoleApplication#start() start()};
@@ -97,7 +96,7 @@ public final class ConsoleApplication implements ICommandProvider {
private final List<CommandRequestListener> listeners = new ArrayList<>();
/** Create a console application.
*
*
* @param out the output
* @param in the input
* @param welcome the welcoming message
@@ -117,7 +116,9 @@ public final class ConsoleApplication implements ICommandProvider {
return root.add(cmd);
}
/** @param listener the command listener */
/** Add a listener for command requests.
*
* @param listener the command listener */
public void addListener(final CommandRequestListener listener) {
listeners.add(listener);
}
@@ -132,17 +133,7 @@ public final class ConsoleApplication implements ICommandProvider {
root.executeSub(output, input, command, args);
}
/* (non-Javadoc)
* @see fr.bigeon.gclc.command.ICommandProvider#executeSub(java.lang.String, java.lang.String[])
*/
@Deprecated
@Override
public void executeSub(final String command,
final String... args) throws CommandRunException {
executeSub(out, in, command, args);
}
/** Signify to the application that no command should be inputed anymore */
/** Signify to the application that no command should be inputed anymore. */
public void exit() {
LOGGER.fine("Request exiting application..."); //$NON-NLS-1$
running = false;
@@ -156,7 +147,12 @@ public final class ConsoleApplication implements ICommandProvider {
return root.get(command);
}
/** @param cmd the command
/** Interpret a command line.
* <p>
* This method will split the command in its part and execute the command
* with {@link #executeSub(ConsoleOutput, ConsoleInput, String, String...)}.
*
* @param cmd the command
* @throws IOException if the command could not be parsed */
public void interpretCommand(final String cmd) throws IOException {
List<String> args;
@@ -183,12 +179,16 @@ public final class ConsoleApplication implements ICommandProvider {
}
}
/** @return the running status */
/** Test if the application is running.
*
* @return the running status */
public boolean isRunning() {
return running;
}
/** @param listener the command listener to remove */
/** Remove a listener from this application.
*
* @param listener the command listener to remove */
public void removeListener(final CommandRequestListener listener) {
listeners.remove(listener);
}
@@ -223,7 +223,7 @@ public final class ConsoleApplication implements ICommandProvider {
}
}
/** Start the application */
/** Start the application. */
public void start() {
try {
running = true;

View File

@@ -51,15 +51,17 @@ import fr.bigeon.gclc.exception.CommandParsingException;
* @author Emmanuel Bigeon */
public final class GCLCConstants {
/** The escaping character */
/** The escaping character. */
private static final char ESCAPING_CHAR = getSystemEscapingChar();
/** Hide utility class constructor */
/** Hide utility class constructor. */
private GCLCConstants() {
// utility class
}
/** @param cmd the command to parse
/** Get the end of a string argument.
*
* @param cmd the command to parse
* @param startIndex the starting point of the parsing
* @param index the index of the current position
* @return the argument
@@ -73,12 +75,16 @@ public final class GCLCConstants {
return cmd.substring(startIndex + 1, index - 1);
}
/** @return the escaping character */
/** Get the excaping character.
*
* @return the escaping character */
private static char getSystemEscapingChar() {
return '\\';
}
/** @param arg the string to remove excaping character from
/** Remove escaping characters from the string.
*
* @param arg the string to remove excaping character from
* @return the string without escape character */
private static String removeEscaped(final String arg) {
final StringBuilder builder = new StringBuilder();
@@ -93,7 +99,7 @@ public final class GCLCConstants {
return builder.toString();
}
/** Splits a command in the diferrent arguments
/** Splits a command in the diferrent arguments.
*
* @param cmd the command to split in its parts
* @return the list of argument preceded by the command name

View File

@@ -42,10 +42,10 @@ import java.io.IOException;
import fr.bigeon.gclc.manager.ConsoleOutput;
/**
/** A command to execute.
* <p>
* A command to execute. It is mandatory that it has a name and that name cannot
* start with minus character or contain spaces.
* It is mandatory that it has a name and that name cannot start with minus
* character or contain spaces.
* <p>
* A command can be executed, with parameters that will be provided as an array
* of strings.
@@ -68,16 +68,24 @@ public abstract class Command implements ICommand {
/** The linux end of line character. */
private static final String EOL_LINUX = "\n"; //$NON-NLS-1$
/** The name of the command */
/** The name of the command. */
protected final String name;
/** @param name the command name */
/** Create the command.
*
* @param name the command name */
public Command(final String name) {
super();
this.name = name;
}
/** @return a brief description of the command */
/** Get the brief part of the command help.
* <p>
* This method may be overriden by implementations to improve the help
* content. The default behavior is to print the tip.
*
* @return a brief description of the command
* @see Command#help(ConsoleOutput, String...) */
protected String brief() {
return " " + tip(); //$NON-NLS-1$
}
@@ -89,9 +97,21 @@ public abstract class Command implements ICommand {
return name;
}
/* (non-Javadoc)
* @see fr.bigeon.gclc.command.ICommand#help(fr.bigeon.gclc.ConsoleManager,
* java.lang.String) */
/** Prints a help content for this command to the console output.
* <p>
* This help is following the given format:
*
* <pre>
* [Command name]
* [brief message]
*
* Usage:
* [Usage pattern]
*
* [Usage details]
* </pre>
*
* @see fr.bigeon.gclc.command.ICommand#help(ConsoleOutput, String...) */
@Override
public final void help(final ConsoleOutput manager,
final String... args) throws IOException {
@@ -111,18 +131,16 @@ public abstract class Command implements ICommand {
}
}
/**
/** This method return the detail of the help.
* <p>
* This method return the detail of the help. It immediatly follows the
* {@link #usagePattern() usage pattern}.
* It immediatly follows the {@link #usagePattern() usage pattern}.
*
* @return the detailed help (should end with end of line or be empty) */
protected abstract String usageDetail();
/**
/** This prints the usage pattern for the command.
* <p>
* This prints the usage pattern for the command. It follows the brief
* introduction on the command ({@link #brief()})
* It follows the brief introduction on the command ({@link #brief()})
*
* @return the usage pattern */
protected String usagePattern() {

View File

@@ -44,29 +44,27 @@ import fr.bigeon.gclc.exception.InvalidCommandName;
import fr.bigeon.gclc.i18n.Messages;
import fr.bigeon.gclc.manager.ConsoleInput;
import fr.bigeon.gclc.manager.ConsoleOutput;
import fr.bigeon.gclc.manager.EmptyInput;
import fr.bigeon.gclc.manager.SinkOutput;
/**
* <p>
* A command provider is a map of key word to command to execute
/** A command provider is a map of key word to command to execute.
*
* @author Emmanuel BIGEON */
public class CommandProvider implements ICommandProvider {
/** The minus character */
/** The minus character. */
private static final String MINUS = "-"; //$NON-NLS-1$
/** The space character */
/** The space character. */
private static final String SPACE = " "; //$NON-NLS-1$
/** The commands map */
/** The commands map. */
protected final List<ICommand> commands;
/** Create a command provider */
/** Create a command provider. */
public CommandProvider() {
super();
commands = new ArrayList<>();
}
/** @param name the command name
/** Test the command name validity.
*
* @param name the command name
* @throws InvalidCommandName if the name is invalid */
private static void testCommandName(final String name) throws InvalidCommandName {
if (name == null || name.isEmpty() || name.startsWith(MINUS) ||
@@ -94,6 +92,11 @@ public class CommandProvider implements ICommandProvider {
return commands.add(value);
}
/* (non-Javadoc)
* @see
* fr.bigeon.gclc.command.ICommandProvider#executeSub(fr.bigeon.gclc.manager
* .ConsoleOutput, fr.bigeon.gclc.manager.ConsoleInput, java.lang.String,
* java.lang.String[]) */
@Override
public final void executeSub(final ConsoleOutput out, final ConsoleInput in,
final String cmd,
@@ -108,20 +111,6 @@ public class CommandProvider implements ICommandProvider {
Messages.getString("CommandProvider.unrecognized", cmd), null); //$NON-NLS-1$
}
@Deprecated
@Override
public final void executeSub(final String cmd,
final String... args) throws CommandRunException {
for (final ICommand command : commands) {
if (command.getCommandName().equals(cmd)) {
command.execute(SinkOutput.INSTANCE, EmptyInput.INSTANCE, args);
return;
}
}
throw new CommandRunException(
Messages.getString("CommandProvider.unrecognized", cmd), null); //$NON-NLS-1$
}
/* (non-Javadoc)
* @see fr.bigeon.gclc.command.ICommandProvider#get(java.lang.String) */
@Override

View File

@@ -45,29 +45,32 @@ import fr.bigeon.gclc.manager.ConsoleInput;
import fr.bigeon.gclc.manager.ConsoleOutput;
import fr.bigeon.gclc.prompt.CLIPrompterMessages;
/** <p>
* A command to exit a {@link ConsoleApplication}.
/** A command to exit a {@link ConsoleApplication}.
*
* @author Emmanuel BIGEON */
public final class ExitCommand implements ICommand {
/** The exit command manual message key */
public class ExitCommand implements ICommand {
/** The exit command manual message key. */
private static final String EXIT_MAN = "exit.man"; //$NON-NLS-1$
/** The tip of the exit command */
/** The tip of the exit command. */
private static final String EXIT = "exit.tip"; //$NON-NLS-1$
/** The application that will be exited when this command runs */
/** The application that will be exited when this command runs. */
private final ConsoleApplication app;
/** The exit command name */
/** The exit command name. */
private final String name;
/** @param name the name of the command
/** Create the exiting command.
*
* @param name the name of the command
* @param app the application to exit */
public ExitCommand(final String name, final ConsoleApplication app) {
this.name = name;
this.app = app;
}
/** The actions to take before exiting */
public void beforeExit() {
/** The actions to take before exiting.
* <p>
* This method is intended to be overriden by sub classes. */
protected void beforeExit() {
// Do nothing by default
}

View File

@@ -53,10 +53,12 @@ import fr.bigeon.gclc.prompt.CLIPrompterMessages;
* @author Emmanuel BIGEON */
public final class HelpExecutor extends Command {
/** The command to execute the help of */
/** The command to execute the help of. */
private final ICommand cmd;
/** @param cmdName the command name
/** Create the help command.
*
* @param cmdName the command name
* @param cmd the command to execute the help of */
public HelpExecutor(final String cmdName,
final ICommand cmd) {
@@ -75,8 +77,8 @@ public final class HelpExecutor extends Command {
}
/* (non-Javadoc)
* @see fr.bigeon.gclc.command.ICommand#execute(fr.bigeon.gclc.manager.ConsoleOutput, fr.bigeon.gclc.manager.ConsoleInput, java.lang.String[])
*/
* @see fr.bigeon.gclc.command.ICommand#execute(ConsoleOutput, ConsoleInput,
* String[]) */
@Override
public void execute(final ConsoleOutput out, final ConsoleInput in,
final String... args) throws CommandRunException {

View File

@@ -44,7 +44,7 @@ import fr.bigeon.gclc.exception.CommandRunException;
import fr.bigeon.gclc.manager.ConsoleInput;
import fr.bigeon.gclc.manager.ConsoleOutput;
/** The contract of commands
/** The contract of commands.
* <p>
* This interface describe the contract of commands
*
@@ -64,25 +64,15 @@ public interface ICommand {
String getCommandName();
/** This prints the help associated to this command.
* <p>
* The default behavior is to print:
*
* <pre>
* [Command name]
* [brief message]
*
* Usage:
* [Usage pattern]
*
* [Usage details]
* </pre>
*
* @param output the output to print the data
* @param args the arguments called with the help
* @throws IOException if the manager was closed */
void help(ConsoleOutput output, String... args) throws IOException;
/** @return a tip on the command */
/** Get a tip (brief helping message) for the command.
*
* @return a tip on the command */
String tip();
}

View File

@@ -41,63 +41,44 @@ import fr.bigeon.gclc.exception.InvalidCommandName;
import fr.bigeon.gclc.manager.ConsoleInput;
import fr.bigeon.gclc.manager.ConsoleOutput;
/** <p>
* An ICommadProvider is a provider of commands that can register commands under
* some keywords.
/** An ICommadProvider is a provider of commands that can register commands
* under some keywords.
*
* @author Emmanuel BIGEON */
public interface ICommandProvider {
/** <p>
* Adds a command to this provider, if no command was associated with the
* given key
/** Adds a command to this provider, if no command was associated with the
* given key.
*
* @param value the command to execute
* @return if the command was added
* @throws InvalidCommandName if the command name is invalid */
boolean add(ICommand value) throws InvalidCommandName;
/**
/** Execute the command with the given name.
* <p>
* This method executes the command with the given name found. If no command
* with this name is found, an error command is usually executed. If there
* are several commands with the same name, the behavior is unspecified.
* Depending on the implementation, it may run an error command or prompt
* the user for a choice.
*
* If no command with this name is found, an error command is usually
* executed. If there are several commands with the same name, the behavior
* is unspecified. Depending on the implementation, it may run an error
* command or prompt the user for a choice.
*
* @param out the output
* @param in the input
* @param command the name of the command the user wishes to execute
* @param args the arguments for the command
* @throws CommandRunException if the command failed to run */
void executeSub(ConsoleOutput out, ConsoleInput in,
String command,
String... args) throws CommandRunException;
void executeSub(ConsoleOutput out, ConsoleInput in, String command,
String... args) throws CommandRunException;
/** <p>
* This method executes the command with the given name found. If no command
* with this name is found, an error command is usually executed. If there
* are several commands with the same name, the behavior is unspecified.
* Depending on the implementation, it may run an error command or prompt
* the user for a choice.
*
* @param command the name of the command the user wishes to execute
* @param args the arguments for the command
* @throws CommandRunException if the command failed to run */
@Deprecated
void executeSub(String command,
String... args) throws CommandRunException;
/**
/** Get the command with the given name.
* <p>
* This method provide the command with the given name found. If no command
* with this name is found, an error command is usually returned. If there
* are several commands with the same name, the behavior is unspecified.
* Depending on the implementation, it may return an error command or the
* first command with this name found.
* If no command with this name is found, an error command is usually
* returned. If there are several commands with the same name, the behavior
* is unspecified. Depending on the implementation, it may return an error
* command or the first command with this name found.
*
* @param command the name of the command the user wishes to execute
* @return the command to execute */
ICommand get(String command);
}
}

View File

@@ -38,7 +38,6 @@
*/
package fr.bigeon.gclc.command;
import fr.bigeon.gclc.exception.CommandRunException;
import fr.bigeon.gclc.manager.ConsoleInput;
import fr.bigeon.gclc.manager.ConsoleOutput;
@@ -49,20 +48,22 @@ import fr.bigeon.gclc.manager.ConsoleOutput;
* @author Emmanuel Bigeon */
public final class MockCommand implements ICommand {
/** The command name */
/** The command name. */
private final String name;
/** @param name the command name */
/** Create the command.
*
* @param name the command name */
public MockCommand(final String name) {
this.name = name;
}
/* (non-Javadoc)
* @see fr.bigeon.gclc.command.ICommand#execute(fr.bigeon.gclc.manager.ConsoleOutput, fr.bigeon.gclc.manager.ConsoleInput, java.lang.String[])
*/
* @see fr.bigeon.gclc.command.ICommand#execute(ConsoleOutput, ConsoleInput,
* String[]) */
@Override
public void execute(final ConsoleOutput out, final ConsoleInput in,
final String... args) throws CommandRunException {
final String... args) {
//
}

View File

@@ -55,7 +55,7 @@ import fr.bigeon.gclc.exception.CommandRunExceptionType;
import fr.bigeon.gclc.manager.ConsoleInput;
import fr.bigeon.gclc.manager.ConsoleOutput;
/** A command that will laucnh a series of command from a file
/** A command that will launch a series of command from a file.
* <p>
* This command will read a file and execute each non empty non commented line
* as a command of the application.
@@ -63,18 +63,20 @@ import fr.bigeon.gclc.manager.ConsoleOutput;
* @author Emmanuel Bigeon */
public final class ScriptExecution extends Command {
/** The tab character */
/** The tab character. */
private static final String TAB = "\t"; //$NON-NLS-1$
/** the space character */
/** the space character. */
private static final String SPACE = " "; //$NON-NLS-1$
/** The application */
/** The application. */
private final ConsoleApplication application;
/** The commenting prefix */
/** The commenting prefix. */
private final String commentPrefix;
/** The charset for files */
/** The charset for files. */
private final Charset charset;
/** @param name the name of the command
/** Create the script command.
*
* @param name the name of the command
* @param application the application
* @param commentPrefix the comment prefix in the script files
* @param charset the charset to use for files */
@@ -86,7 +88,9 @@ public final class ScriptExecution extends Command {
this.charset = charset;
}
/** @param args the arguments
/** Check the arguments.
*
* @param args the arguments
* @throws CommandRunException if the arguments were not the ones
* expected */
private void checkArgs(final String[] args) throws CommandRunException {
@@ -153,7 +157,9 @@ public final class ScriptExecution extends Command {
e, this);
}
/** @param cmd the line
/** Read a line of the script.
*
* @param cmd the line
* @param params the formatting parameters
* @return the command if it is indeed one, null otherwise
* @throws CommandRunException if the line stqrted with a space character */
@@ -161,10 +167,11 @@ public final class ScriptExecution extends Command {
final Object[] params) throws CommandRunException {
if (cmd.startsWith(SPACE) || cmd.startsWith(TAB)) {
throw new CommandRunException(
"Invalid command in script (line starts with space character)", //$NON-NLS-1$
"Invalid line in script (line starts with space character)", //$NON-NLS-1$
this);
}
if (cmd.isEmpty() || cmd.startsWith(commentPrefix)) {
// Comment line
return null;
}
return MessageFormat.format(cmd, params);

View File

@@ -52,7 +52,7 @@ import fr.bigeon.gclc.manager.ConsoleOutput;
* @author Emmanuel BIGEON */
public final class SubedCommand extends CommandProvider implements ICommand {
/** The tab character */
/** The tab character. */
private static final String TAB = "\t"; //$NON-NLS-1$
/** The command to execute when this command is called with no sub
* arguments.
@@ -64,7 +64,9 @@ public final class SubedCommand extends CommandProvider implements ICommand {
/** The name of the command */
private final String name;
/** @param name the name of the command */
/** Create the command that defines sub commands.
*
* @param name the name of the command */
public SubedCommand(final String name) {
super();
this.name = name;
@@ -72,7 +74,9 @@ public final class SubedCommand extends CommandProvider implements ICommand {
tip = null;
}
/** @param name the name of the command
/** Create the command that defines sub commands.
*
* @param name the name of the command
* @param noArgCommand the command to execute when no extra parameter are
* provided */
public SubedCommand(final String name, final ICommand noArgCommand) {
@@ -82,7 +86,9 @@ public final class SubedCommand extends CommandProvider implements ICommand {
tip = null;
}
/** @param name the name of the command
/** Create the command that defines sub commands.
*
* @param name the name of the command
* @param noArgCommand the command to execute
* @param tip the help tip associated */
public SubedCommand(final String name, final ICommand noArgCommand,
@@ -93,7 +99,9 @@ public final class SubedCommand extends CommandProvider implements ICommand {
this.tip = tip;
}
/** @param name the name of the command
/** Create the command that defines sub commands.
*
* @param name the name of the command
* @param tip the help tip associated */
public SubedCommand(final String name, final String tip) {
super();

View File

@@ -38,27 +38,33 @@
*/
package fr.bigeon.gclc.exception;
/** An exception raised during command parsing
/** An exception raised during command parsing.
*
* @author Emmanuel Bigeon */
public class CommandParsingException extends Exception {
/** svuid */
/** svuid. */
private static final long serialVersionUID = 1L;
/** @param message an explaination
* @param cause the cause */
public CommandParsingException(String message, Throwable cause) {
super(message, cause);
}
/** @param message an explaination */
public CommandParsingException(String message) {
/** Create the exception with a message.
*
* @param message the message */
public CommandParsingException(final String message) {
super(message);
}
/** @param cause the cause */
public CommandParsingException(Throwable cause) {
/** Create the exception with a message and a cause.
*
* @param message the message
* @param cause the cause */
public CommandParsingException(final String message, final Throwable cause) {
super(message, cause);
}
/** Create the exception with a message.
*
* @param cause the cause */
public CommandParsingException(final Throwable cause) {
super(cause);
}

View File

@@ -40,44 +40,49 @@ package fr.bigeon.gclc.exception;
import fr.bigeon.gclc.command.ICommand;
/** <p>
* An exception thrown when a command failed to run correctly.
/** An exception thrown when a command failed to run correctly.
*
* @author Emmanuel BIGEON */
public final class CommandRunException extends Exception {
/**
*
*/
/** the SVUID. */
private static final long serialVersionUID = 1L;
/** The type of run exception */
/** The type of run exception. */
private final CommandRunExceptionType type;
/** The command that caused the error */
private final transient ICommand source;
/** The command that caused the error. */
private final ICommand source;
/** @param type the type of exception
/** Create the exception.
*
* @param type the type of exception
* @param message the message
* @param source the source */
public CommandRunException(final CommandRunExceptionType type, final String message,
final ICommand source) {
public CommandRunException(final CommandRunExceptionType type,
final String message, final ICommand source) {
super(message);
this.type = type;
this.source = source;
}
/** @param type the type of exception
/** Create the exception with a cause.
*
* @param type the type of exception
* @param message a message
* @param cause the cause
* @param source the source */
public CommandRunException(final CommandRunExceptionType type, final String message,
final Throwable cause, final ICommand source) {
public CommandRunException(final CommandRunExceptionType type,
final String message, final Throwable cause,
final ICommand source) {
super(message, cause);
this.type = type;
this.source = source;
}
/** @param message a message
/** Create the exception with type
* {@link CommandRunExceptionType#EXECUTION}.
*
* @param message a message
* @param source the source */
public CommandRunException(final String message, final ICommand source) {
super(message);
@@ -85,7 +90,10 @@ public final class CommandRunException extends Exception {
this.source = source;
}
/** @param message a message
/** Create the exception with type {@link CommandRunExceptionType#EXECUTION}
* and a cause.
*
* @param message a message
* @param cause the cause
* @param source the source */
public CommandRunException(final String message, final Throwable cause,
@@ -106,12 +114,16 @@ public final class CommandRunException extends Exception {
return super.getLocalizedMessage();
}
/** @return the source */
/** Get the exception raising command.
*
* @return the source */
public ICommand getSource() {
return source;
}
/** @return the type */
/** Get the exception type.
*
* @return the type */
public CommandRunExceptionType getType() {
return type;
}

View File

@@ -38,14 +38,14 @@
*/
package fr.bigeon.gclc.exception;
/** The command run exception possible types
/** The command run exception possible types.
*
* @author Emmanuel Bigeon */
public enum CommandRunExceptionType {
/** Type of exception due to a wrong usage */
/** Type of exception due to a wrong usage. */
USAGE,
/** Type of exception due to a problem in execution */
/** Type of exception due to a problem in execution. */
EXECUTION,
/** Type of exception due to the impossibility to interact with user */
/** Type of exception due to the impossibility to interact with user. */
INTERACTION;
}

View File

@@ -38,36 +38,39 @@
*/
package fr.bigeon.gclc.exception;
/** <p>
* Exception sent from the application when a command is added but the name of
* the command is already used
/** Exception sent from the application when a command is added but the name of
* the command is already used.
*
* @author Emmanuel BIGEON */
public class InvalidCommandName extends Exception {
/**
*
*/
/** the SVUID. */
private static final long serialVersionUID = 1L;
/** Default constructor */
/** Default constructor. */
public InvalidCommandName() {
super();
}
/** @param message the message
* @param cause the cause */
public InvalidCommandName(String message, Throwable cause) {
super(message, cause);
}
/** @param message the message */
public InvalidCommandName(String message) {
/** Create the exception with a message.
*
* @param message the message */
public InvalidCommandName(final String message) {
super(message);
}
/** @param cause the cause */
public InvalidCommandName(Throwable cause) {
/** Create the exception with a message and a cause.
*
* @param message the message
* @param cause the cause */
public InvalidCommandName(final String message, final Throwable cause) {
super(message, cause);
}
/** Create the exception with a cause.
*
* @param cause the cause */
public InvalidCommandName(final Throwable cause) {
super(cause);
}

View File

@@ -47,24 +47,28 @@ package fr.bigeon.gclc.exception;
* @author Emmanuel Bigeon */
public class InvalidParameterException extends Exception {
/**
*
*/
/** the SVUID. */
private static final long serialVersionUID = 1L;
/** @param message the message
* @param cause the cause */
public InvalidParameterException(String message, Throwable cause) {
super(message, cause);
}
/** @param message the message */
public InvalidParameterException(String message) {
/** Create the exception with a message.
*
* @param message the message */
public InvalidParameterException(final String message) {
super(message);
}
/** @param cause the cause */
public InvalidParameterException(Throwable cause) {
/** Create the exception with a message and a cause.
*
* @param message the message
* @param cause the cause */
public InvalidParameterException(final String message, final Throwable cause) {
super(message, cause);
}
/** Create the exception with a cause.
*
* @param cause the cause */
public InvalidParameterException(final Throwable cause) {
super(cause);
}

View File

@@ -0,0 +1,8 @@
/**
* gclc:fr.bigeon.gclc.exception.package-info.java
* Created on: Nov 13, 2017
*/
/** Exceptions package.
*
* @author Emmanuel Bigeon */
package fr.bigeon.gclc.exception;

View File

@@ -48,23 +48,23 @@ import java.util.logging.Logger;
*
* @author Emmanuel Bigeon */
public final class Messages {
/** The resource bundle name */
/** The resource bundle name. */
private static final String BUNDLE_NAME = "fr.bigeon.gclc.l10n.messages"; //$NON-NLS-1$
/** The resource bundle */
/** The resource bundle. */
private static final ResourceBundle RESOURCE_BUNDLE = ResourceBundle
.getBundle(BUNDLE_NAME);
/** The class logger */
/** The class logger. */
private static final Logger LOGGER = Logger
.getLogger(Messages.class.getName());
/** Utility class */
/** Utility class. */
private Messages() {
// Utility class
}
/** Get formatted internationalized messages
/** Get formatted internationalized messages.
*
* @param key the message key
* @param args the formatting arguments

View File

@@ -0,0 +1,8 @@
/**
* gclc:fr.bigeon.gclc.i18n.package-info.java
* Created on: Nov 13, 2017
*/
/** Internationalization package.
*
* @author Emmanuel Bigeon */
package fr.bigeon.gclc.i18n;

View File

@@ -46,7 +46,7 @@ import java.io.InterruptedIOException;
* @author Emmanuel Bigeon */
public interface ConsoleInput extends AutoCloseable {
/** Closes the manager.
/** Closes the input.
*
* @throws IOException if the close raised an exception */
@Override
@@ -57,7 +57,7 @@ public interface ConsoleInput extends AutoCloseable {
* @return the prompt prefix */
String getPrompt();
/** Indicate to the manager that is should interrompt the prompting, if
/** Indicate to the input that is should interrompt the prompting, if
* possible.
* <p>
* The pending {@link #prompt()} or {@link #prompt(String)} operations

View File

@@ -40,38 +40,37 @@ package fr.bigeon.gclc.manager;
import java.io.IOException;
/**
* <p>
* TODO
/** A console output definition.
*
* @author Emmanuel Bigeon
*
*/
* @author Emmanuel Bigeon */
public interface ConsoleOutput extends AutoCloseable {
/* (non-Javadoc)
* @see java.lang.AutoCloseable#close() */
@Override
void close() throws IOException;
/** Print a string.
/** Test if the output is closed.
*
* @return if the manager is closed. */
boolean isClosed();
/** Print a string.
*
* @param text the message to print (without line break at the end).
* @throws IOException if the manager is closed or could not read the
* prompt */
void print(String text) throws IOException;
/** Prints an end of line
*
/** Prints an end of line.
*
* @throws IOException if the manager is closed or could not read the
* prompt */
void println() throws IOException;
/** @param message the message to print
/** Print a string followed by an end of line.
* <p>
* This is the same as calling successively {@link #print(String)} and
* {@link #println()}.
*
* @param message the message to print
* @throws IOException if the manager is closed or could not read the
* prompt */
void println(String message) throws IOException;
/** @return if the manager is closed. */
boolean isClosed();
}

View File

@@ -38,8 +38,6 @@
*/
package fr.bigeon.gclc.manager;
import java.io.IOException;
/** A console input that return empty to all prompting.
*
* @author Emmanuel Bigeon */
@@ -54,7 +52,7 @@ public final class EmptyInput implements ConsoleInput {
/* (non-Javadoc)
* @see fr.bigeon.gclc.manager.ConsoleInput#close() */
@Override
public void close() throws IOException {
public void close() {
//
}
@@ -82,21 +80,21 @@ public final class EmptyInput implements ConsoleInput {
/* (non-Javadoc)
* @see fr.bigeon.gclc.manager.ConsoleInput#prompt() */
@Override
public String prompt() throws IOException {
public String prompt() {
return ""; //$NON-NLS-1$
}
/* (non-Javadoc)
* @see fr.bigeon.gclc.manager.ConsoleInput#prompt(long) */
@Override
public String prompt(final long timeout) throws IOException {
public String prompt(final long timeout) {
return ""; //$NON-NLS-1$
}
/* (non-Javadoc)
* @see fr.bigeon.gclc.manager.ConsoleInput#prompt(java.lang.String) */
@Override
public String prompt(final String message) throws IOException {
public String prompt(final String message) {
return ""; //$NON-NLS-1$
}
@@ -105,7 +103,7 @@ public final class EmptyInput implements ConsoleInput {
* long) */
@Override
public String prompt(final String message,
final long timeout) throws IOException {
final long timeout) {
return ""; //$NON-NLS-1$
}

View File

@@ -46,33 +46,33 @@ import java.io.PipedOutputStream;
import java.io.PrintStream;
import java.nio.charset.StandardCharsets;
/** This console manager allows to enter commands and retrieve the output as an
/** This console input allows to enter commands and retrieve the output as an
* input.
* <p>
* This console manager is used to internally pilot an application. This can be
* This console input is used to internally pilot an application. This can be
* used to test application behavior.
*
* @author Emmanuel Bigeon */
public final class PipedConsoleInput
implements ConsoleInput {
/** The encoding between streams */
/** The encoding between streams. */
private static final String UTF_8 = "UTF-8"; //$NON-NLS-1$
/** THe inner manager */
private final SystemConsoleInput innerManager;
/** The stream to pipe commands into */
/** THe inner manager. */
private final StreamConsoleInput innerManager;
/** The stream to pipe commands into. */
private final PipedOutputStream commandInput;
/** The reader to get application return from */
/** The reader to get application return from. */
private final BufferedReader commandBuffOutput;
/** The stream to get application return from */
/** The stream to get application return from. */
private final PipedInputStream commandOutput;
/** The print writer for application to write return to */
/** The print writer for application to write return to. */
private final PrintStream outPrint;
/** The stream for the application to read commands from */
/** The stream for the application to read commands from. */
private final PipedInputStream in;
/** The writing thread */
/** The writing thread. */
private final WritingRunnable writing;
/** The reading thread */
/** The reading thread. */
private final ReadingRunnable reading;
/** Create a manager that will write and read through piped stream.
@@ -86,7 +86,7 @@ public final class PipedConsoleInput
commandBuffOutput = new BufferedReader(
new InputStreamReader(commandOutput, StandardCharsets.UTF_8));
outPrint = new PrintStream(out, true, UTF_8);
innerManager = new SystemConsoleInput(outPrint, in,
innerManager = new StreamConsoleInput(outPrint, in,
StandardCharsets.UTF_8);
writing = new WritingRunnable(commandInput, StandardCharsets.UTF_8);
reading = new ReadingRunnable(commandBuffOutput);
@@ -97,7 +97,12 @@ public final class PipedConsoleInput
th.start();
}
/** @return the content of the next line written by the application
/** Test if a content is available on the reading head.
* <p>
* If this method returns true, the next {@link #prompt()} operation should
* return immediatly.
*
* @return the content of the next line written by the application
* @throws IOException if the reading failed */
public boolean available() throws IOException {
return reading.hasMessage();
@@ -120,7 +125,12 @@ public final class PipedConsoleInput
return innerManager.getPrompt();
}
/** @param message the message
/** Wait for a specific message to arrive.
* <p>
* When this method returns, the message was appended to the data, it
* <em>may or may not</em> be the next line of data.
*
* @param message the message
* @return the thread to join to wait for message delivery
* @see fr.bigeon.gclc.manager.ReadingRunnable#getWaitForDelivery(java.lang.String) */
public Thread getWaitForDelivery(final String message) {
@@ -134,11 +144,15 @@ public final class PipedConsoleInput
innerManager.interruptPrompt();
}
/* (non-Javadoc)
* @see fr.bigeon.gclc.manager.ConsoleInput#isClosed() */
@Override
public boolean isClosed() {
return innerManager.isClosed();
}
/* (non-Javadoc)
* @see fr.bigeon.gclc.manager.ConsoleInput#prompt() */
@Override
public String prompt() throws IOException {
return innerManager
@@ -152,28 +166,41 @@ public final class PipedConsoleInput
return innerManager.prompt(timeout);
}
/* (non-Javadoc)
* @see fr.bigeon.gclc.manager.ConsoleInput#prompt(java.lang.String) */
@Override
public String prompt(final String message) throws IOException {
return innerManager.prompt(message + System.lineSeparator());
}
/* (non-Javadoc)
* @see fr.bigeon.gclc.manager.ConsoleInput#prompt(java.lang.String,
* long) */
@Override
public String prompt(final String message, final long timeout) throws IOException {
return innerManager.prompt(message + System.lineSeparator(), timeout);
}
/** @return the content of the next line written by the application
/** Read the next line in the input printed content.
* <p>
* This corresponds to the {@link #prompt(String)} messages.
*
* @return the content of the next line written by the application
* @throws IOException if the reading failed */
public String readNextLine() throws IOException {
return reading.getMessage();
}
/* (non-Javadoc)
* @see fr.bigeon.gclc.manager.ConsoleInput#setPrompt(java.lang.String) */
@Override
public void setPrompt(final String prompt) {
innerManager.setPrompt(prompt);
}
/** @param content the content to type to the application
/** Type a message in the input.
*
* @param content the content to type to the application
* @throws IOException if the typing failed */
public void type(final String content) throws IOException {
writing.addMessage(content);

View File

@@ -46,27 +46,26 @@ import java.io.PipedOutputStream;
import java.io.PrintStream;
import java.nio.charset.StandardCharsets;
/** This console manager allows to enter commands and retrieve the output as an
* input.
/** This console output allows to retrieve the output as an input.
* <p>
* This console manager is used to internally pilot an application. This can be
* This console output is used to internally pilot an application. This can be
* used to test application behavior.
*
* @author Emmanuel Bigeon */
public final class PipedConsoleOutput
implements ConsoleOutput {
/** The encoding between streams */
/** The encoding between streams. */
private static final String UTF_8 = "UTF-8"; //$NON-NLS-1$
/** THe inner manager */
private final SystemConsoleOutput innerManager;
/** The reader to get application return from */
/** THe inner manager. */
private final StreamConsoleOutput innerManager;
/** The reader to get application return from. */
private final BufferedReader commandBuffOutput;
/** The stream to get application return from */
/** The stream to get application return from. */
private final PipedInputStream commandOutput;
/** The print writer for application to write return to */
/** The print writer for application to write return to. */
private final PrintStream outPrint;
/** The reading thread */
/** The reading thread. */
private final ReadingRunnable reading;
/** Create a manager that will write and read through piped stream.
@@ -78,19 +77,23 @@ public final class PipedConsoleOutput
commandBuffOutput = new BufferedReader(
new InputStreamReader(commandOutput, StandardCharsets.UTF_8));
outPrint = new PrintStream(out, true, UTF_8);
innerManager = new SystemConsoleOutput(outPrint);
innerManager = new StreamConsoleOutput(outPrint);
reading = new ReadingRunnable(commandBuffOutput);
final Thread th = new Thread(reading, "read"); //$NON-NLS-1$
th.setDaemon(true);
th.start();
}
/** @return the content of the next line written by the application
/** Test if there is available data.
*
* @return the content of the next line written by the application
* @throws IOException if the reading failed */
public boolean available() throws IOException {
return reading.hasMessage();
}
/* (non-Javadoc)
* @see java.lang.AutoCloseable#close() */
@Override
public void close() throws IOException {
reading.setRunning(false);
@@ -100,34 +103,49 @@ public final class PipedConsoleOutput
commandOutput.close();
}
/** @param message the message
/** Wait for a specific message to arrive.
* <p>
* When this method returns, the message was appended to the data, it
* <em>may or may not</em> be the next line of data.
*
* @param message the message
* @return the thread to join to wait for message delivery
* @see fr.bigeon.gclc.manager.ReadingRunnable#getWaitForDelivery(java.lang.String) */
public Thread getWaitForDelivery(final String message) {
return reading.getWaitForDelivery(message);
}
/* (non-Javadoc)
* @see fr.bigeon.gclc.manager.ConsoleOutput#isClosed() */
@Override
public boolean isClosed() {
return innerManager.isClosed();
}
/* (non-Javadoc)
* @see fr.bigeon.gclc.manager.ConsoleOutput#print(java.lang.String) */
@Override
public void print(final String object) throws IOException {
innerManager.print(object);
}
/* (non-Javadoc)
* @see fr.bigeon.gclc.manager.ConsoleOutput#println() */
@Override
public void println() throws IOException {
innerManager.println();
}
/* (non-Javadoc)
* @see fr.bigeon.gclc.manager.ConsoleOutput#println(java.lang.String) */
@Override
public void println(final String object) throws IOException {
innerManager.println(object);
}
/** @return the content of the next line written by the application
/** Read the next line of data.
*
* @return the content of the next line written by the application
* @throws IOException if the reading failed */
public String readNextLine() throws IOException {
return reading.getMessage();

View File

@@ -53,20 +53,22 @@ import java.util.logging.Logger;
* @author Emmanuel Bigeon */
public final class ReadingRunnable implements Runnable {
/** The runnable to wait for notification on an object
/** The runnable to wait for arrival of a message in the queue.
*
* @author Emmanuel Bigeon */
private final class ToWaitRunnable implements Runnable {
/** The Object */
/** The Object. */
private final Object obj;
/** The locking object */
/** The locking object. */
private final Object start;
/** The message */
/** The message. */
private final String message;
/** The started status */
/** The started status. */
private boolean started = false;
/** @param obj the object to lock on
/** Create the waiting runnable.
*
* @param obj the object to lock on
* @param start the object to notify when ready to wait
* @param message the message to wait for */
public ToWaitRunnable(final Object obj, final Object start,
@@ -76,13 +78,17 @@ public final class ReadingRunnable implements Runnable {
this.message = message;
}
/** @return the started */
/** Test if the waiting runnable is started.
*
* @return the started */
public boolean isStarted() {
synchronized (start) {
return started;
}
}
/* (non-Javadoc)
* @see java.lang.Runnable#run() */
@SuppressWarnings("synthetic-access")
@Override
public void run() {
@@ -107,34 +113,36 @@ public final class ReadingRunnable implements Runnable {
}
}
/** The thread intteruption logging message */
/** The thread intteruption logging message. */
private static final String THREAD_INTERRUPTION_EXCEPTION = "Thread interruption exception."; //$NON-NLS-1$
/** The closed pipe message */
/** The closed pipe message. */
private static final String CLOSED_PIPE = "Closed pipe"; //$NON-NLS-1$
/** Wait timeout */
/** Wait timeout. */
private static final long TIMEOUT = 1000;
/** Class logger */
/** Class logger. */
private static final Logger LOGGER = Logger
.getLogger(ReadingRunnable.class.getName());
/** Read messages */
/** Read messages. */
private final Deque<String> messages = new ArrayDeque<>();
/** the reader */
/** the reader. */
private final BufferedReader reader;
/** the state of this runnable */
/** the state of this runnable. */
private boolean running = true;
/** Synchro object */
/** Synchro object. */
private final Object lock = new Object();
/** The waiting status for a message */
/** The waiting status for a message. */
private boolean waiting;
/** The blocker for a given message */
/** The blocker for a given message. */
private final Map<String, Object> messageBlocker = new HashMap<>();
/** The lock */
private final Object messageBlockerLock = new Object();
/** The message being delivered */
/** The message being delivered. */
private String delivering;
/** @param reader the input to read from */
/** Create a reading runnable.
*
* @param reader the input to read from */
public ReadingRunnable(final BufferedReader reader) {
super();
this.reader = reader;
@@ -154,7 +162,9 @@ public final class ReadingRunnable implements Runnable {
return res;
}
/** @return the next read message
/** Get the next message.
*
* @return the next read message
* @throws IOException if the pipe is closed */
public String getMessage() throws IOException {
synchronized (lock) {
@@ -180,7 +190,9 @@ public final class ReadingRunnable implements Runnable {
}
}
/** @param timeout the read time out
/** Get the next message, but wait only a given time for it.
*
* @param timeout the read time out
* @return The next message that was in the input
* @throws IOException if the input was closed */
public String getNextMessage(final long timeout) throws IOException {
@@ -206,7 +218,9 @@ public final class ReadingRunnable implements Runnable {
}
}
/** @param message the message
/** Get a waiting thread for a specific message delivery.
*
* @param message the message
* @return the thread to join to wait for message delivery */
public Thread getWaitForDelivery(final String message) {
synchronized (messageBlockerLock) {
@@ -235,7 +249,9 @@ public final class ReadingRunnable implements Runnable {
}
}
/** @return if a message is waiting
/** Test if some data is available.
*
* @return if a message is waiting
* @throws IOException if the pipe is closed */
public boolean hasMessage() throws IOException {
synchronized (lock) {
@@ -246,7 +262,8 @@ public final class ReadingRunnable implements Runnable {
}
}
/** Interrupts the wait on the next message by providing an empty message */
/** Interrupts the wait on the next message by providing an empty
* message. */
public void interrupt() {
synchronized (lock) {
if (waiting) {
@@ -256,14 +273,18 @@ public final class ReadingRunnable implements Runnable {
}
}
/** @return the running */
/** Test if this element is still running.
*
* @return the running */
public boolean isRunning() {
synchronized (lock) {
return running;
}
}
/** @param message the message */
/** Notify the arrival of a given message.
*
* @param message the message */
private void notifyMessage(final String message) {
synchronized (messageBlockerLock) {
delivering = message;
@@ -313,7 +334,9 @@ public final class ReadingRunnable implements Runnable {
}
}
/** @param running the running to set */
/** Set the running status for this reading runnable.
*
* @param running the running to set */
public void setRunning(final boolean running) {
synchronized (lock) {
this.running = running;

View File

@@ -43,7 +43,7 @@ import java.io.IOException;
/** A console output that absorbs every message.
*
* @author Emmanuel Bigeon */
public class SinkOutput implements ConsoleOutput {
public final class SinkOutput implements ConsoleOutput {
/** The sink output. */
public static final ConsoleOutput INSTANCE = new SinkOutput();

View File

@@ -50,38 +50,40 @@ import java.nio.charset.Charset;
* The default constructor will use the system standart input and output.
*
* @author Emmanuel BIGEON */
public final class SystemConsoleInput implements ConsoleInput {
public final class StreamConsoleInput implements ConsoleInput {
/** The default prompt */
/** The default prompt. */
public static final String DEFAULT_PROMPT = "> "; //$NON-NLS-1$
/** The command prompt. It can be changed. */
private String prompt = DEFAULT_PROMPT;
/** The print stream */
/** The print stream. */
private final PrintStream out;
/** The input stream */
/** The input stream. */
private final BufferedReader in;
/** If the manager is closed */
/** If the manager is closed. */
private boolean closed = false;
/** The prompting thread */
/** The prompting thread. */
private final Thread promptThread;
/** The reading runnable */
/** The reading runnable. */
private final ReadingRunnable reading;
/** This default constructor relies on the system defined standart output
* and input stream. */
public SystemConsoleInput() {
public StreamConsoleInput() {
this(System.out, System.in, Charset.defaultCharset());
}
/** @param out the output stream
/** Create the stream base console input.
*
* @param out the output stream
* @param in the input stream
* @param charset the charset for the input */
public SystemConsoleInput(final PrintStream out, final InputStream in,
public StreamConsoleInput(final PrintStream out, final InputStream in,
final Charset charset) {
super();
this.out = out;
@@ -92,7 +94,9 @@ public final class SystemConsoleInput implements ConsoleInput {
promptThread.start();
}
/** @throws IOException if the stream was closed */
/** Check that the console input is not closed.
*
* @throws IOException if the stream was closed */
private void checkOpen() throws IOException {
if (closed) {
throw new IOException();
@@ -109,7 +113,8 @@ public final class SystemConsoleInput implements ConsoleInput {
in.close();
}
/** @return the prompt */
/* (non-Javadoc)
* @see fr.bigeon.gclc.manager.ConsoleInput#getPrompt() */
@Override
public String getPrompt() {
return prompt;
@@ -118,10 +123,10 @@ public final class SystemConsoleInput implements ConsoleInput {
/** Beware, in this implementation this is the same as closing the manager.
*
* @see fr.bigeon.gclc.manager.ConsoleInput#interruptPrompt() */
@Override
public void interruptPrompt() {
reading.interrupt();
}
@Override
public void interruptPrompt() {
reading.interrupt();
}
/* (non-Javadoc)
* @see fr.bigeon.gclc.manager.ConsoleManager#isClosed() */
@@ -156,13 +161,15 @@ public final class SystemConsoleInput implements ConsoleInput {
/* (non-Javadoc)
* @see fr.bigeon.gclc.ConsoleManager#prompt(java.lang.String) */
@Override
public String prompt(final String message, final long timeout) throws IOException {
public String prompt(final String message,
final long timeout) throws IOException {
checkOpen();
out.print(message);
return reading.getNextMessage(timeout);
}
/** @param prompt the prompt to set */
/* (non-Javadoc)
* @see fr.bigeon.gclc.manager.ConsoleInput#setPrompt(java.lang.String) */
@Override
public void setPrompt(final String prompt) {
this.prompt = prompt;

View File

@@ -46,30 +46,29 @@ import java.io.PrintStream;
* The default constructor will use the system standart input and output.
*
* @author Emmanuel BIGEON */
public final class SystemConsoleOutput implements ConsoleOutput {
/** The default prompt */
public static final String DEFAULT_PROMPT = "> "; //$NON-NLS-1$
/** The print stream */
public final class StreamConsoleOutput implements ConsoleOutput {
/** The print stream. */
private final PrintStream out;
/** If the manager is closed */
/** If the manager is closed. */
private boolean closed = false;
/** This default constructor relies on the system defined standart output
* and input stream. */
public SystemConsoleOutput() {
public StreamConsoleOutput() {
this(System.out);
}
/** @param out the output stream */
public SystemConsoleOutput(final PrintStream out) {
/** Create a print stream based console output.
*
* @param out the output stream */
public StreamConsoleOutput(final PrintStream out) {
super();
this.out = out;
}
/** @throws IOException if the stream was closed */
/** Check the open status.
*
* @throws IOException if the stream was closed */
private void checkOpen() throws IOException {
if (closed) {
throw new IOException();
@@ -79,7 +78,7 @@ public final class SystemConsoleOutput implements ConsoleOutput {
/* (non-Javadoc)
* @see fr.bigeon.gclc.manager.ConsoleManager#close() */
@Override
public void close() throws IOException {
public void close() {
closed = true;
}

View File

@@ -54,24 +54,26 @@ import java.util.logging.Logger;
* @author Emmanuel Bigeon */
public final class WritingRunnable implements Runnable {
/** Wait timeout */
/** Wait timeout. */
private static final long TIMEOUT = 1000;
/** Class logger */
/** Class logger. */
private static final Logger LOGGER = Logger
.getLogger(WritingRunnable.class.getName());
/** Messages to write */
/** Messages to write. */
private final Deque<String> messages = new ArrayDeque<>();
/** Stream to write to */
/** Stream to write to. */
private final OutputStream outPrint;
/** The charset */
/** The charset. */
private final Charset charset;
/** Runnable state */
/** Runnable state. */
private boolean running = true;
/** Synchro object */
/** Synchro object. */
private final Object lock = new Object();
/** @param outPrint the output to print to
/** Create the writing runnable.
*
* @param outPrint the output to print to
* @param charset the charset of the stream */
public WritingRunnable(final OutputStream outPrint, final Charset charset) {
super();
@@ -79,9 +81,11 @@ public final class WritingRunnable implements Runnable {
this.charset = charset;
}
/** @param message the message
/** Add a message in the queue.
*
* @param message the message
* @throws IOException if the pipe is closed */
public void addMessage(final String message) throws IOException {
public synchronized void addMessage(final String message) throws IOException {
synchronized (lock) {
if (!running) {
throw new IOException("Closed pipe"); //$NON-NLS-1$
@@ -91,7 +95,9 @@ public final class WritingRunnable implements Runnable {
}
}
/** @return the running */
/** Test if the message is running.
*
* @return the running */
public boolean isRunning() {
synchronized (lock) {
return running;
@@ -102,40 +108,53 @@ public final class WritingRunnable implements Runnable {
* @see java.lang.Runnable#run() */
@Override
public void run() {
while (running) {
synchronized (lock) {
while (messages.isEmpty()) {
try {
lock.wait(TIMEOUT);
} catch (final InterruptedException e) {
LOGGER.log(Level.SEVERE,
"Thread interruption exception.", e); //$NON-NLS-1$
Thread.currentThread().interrupt();
}
waitNextMessage();
if (!running) {
return;
}
}
final String message = messages.poll();
final ByteBuffer buff = charset
.encode(message + System.lineSeparator());
if (buff.hasArray()) {
try {
outPrint.write(buff.array());
} catch (final IOException e) {
LOGGER.log(Level.SEVERE, "Unable to write to stream", //$NON-NLS-1$
e);
}
}
writeMessage();
}
}
}
/** @param running the running to set */
public void setRunning(final boolean running) {
/** Set the running status.
*
* @param running the running to set */
public synchronized void setRunning(final boolean running) {
synchronized (lock) {
this.running = running;
}
}
/** Wait for next message. */
private void waitNextMessage() {
try {
lock.wait(TIMEOUT);
} catch (final InterruptedException e) {
if (running) {
LOGGER.log(Level.SEVERE,
"Thread interruption exception.", e); //$NON-NLS-1$
}
Thread.currentThread().interrupt();
}
}
/** Write next message to output. */
private void writeMessage() {
final String message = messages.poll();
final ByteBuffer buff = charset
.encode(message + System.lineSeparator());
if (buff.hasArray()) {
try {
outPrint.write(buff.array());
} catch (final IOException e) {
LOGGER.log(Level.SEVERE, "Unable to write to stream", //$NON-NLS-1$
e);
}
}
}
}

View File

@@ -0,0 +1,8 @@
/**
* gclc:fr.bigeon.gclc.manager.package-info.java
* Created on: Nov 13, 2017
*/
/** The console input and output definitions.
*
* @author Emmanuel Bigeon */
package fr.bigeon.gclc.manager;

View File

@@ -46,44 +46,42 @@ import java.util.logging.Logger;
import fr.bigeon.gclc.manager.ConsoleInput;
import fr.bigeon.gclc.manager.ConsoleOutput;
/**
* <p>
* The {@link CLIPrompter} class is a utility class that provides method to
/** The {@link CLIPrompter} class is a utility class that provides method to
* prompt the user.
*
* @author Emmanuel BIGEON */
public final class CLIPrompter {
/**
*
*/
/** The zero integer. */
private static final Integer ZERO = Integer.valueOf(0);
/** message key for format error in prompting a choice */
/** message key for format error in prompting a choice. */
private static final String PROMPTCHOICE_FORMATERR = "promptchoice.formaterr"; //$NON-NLS-1$
/** message key for out of bound error in prompting a choice */
/** message key for out of bound error in prompting a choice. */
private static final String PROMPTCHOICE_OUTOFBOUNDS = "promptchoice.outofbounds"; //$NON-NLS-1$
/** message key for first form of no in prompting a choice */
/** message key for first form of no in prompting a choice. */
private static final String PROMPTBOOL_CHOICES_NO1 = "promptbool.choices.no1"; //$NON-NLS-1$
/** message key for first form of yes in prompting a choice */
/** message key for first form of yes in prompting a choice. */
private static final String PROMPTBOOL_CHOICES_YES1 = "promptbool.choices.yes1"; //$NON-NLS-1$
@SuppressWarnings("javadoc")
/** Message key for boolean choosing. */
private static final String BOOL_CHOICES = "promptbool.choices"; //$NON-NLS-1$
@SuppressWarnings("javadoc")
/** Message key for the list end of prompt symbol. */
private static final String LIST_DISP_KEY = "promptlist.exit.dispkey"; //$NON-NLS-1$
@SuppressWarnings("javadoc")
/** Message key for the line prompt. */
private static final String PROMPT = "prompt.lineprompt"; //$NON-NLS-1$
@SuppressWarnings("javadoc")
/** Message key for the separation of choices selection. */
private static final String LIST_CHOICE_SEP = "promptlist.multi.sepkey"; //$NON-NLS-1$
/** The class logger */
/** The class logger. */
private static final Logger LOGGER = Logger
.getLogger(CLIPrompter.class.getName());
/** Utility class */
/** Utility class. */
private CLIPrompter() {
// Utility class
}
/** @param val the string to parse
/** Add a user choice to the list.
*
* @param val the string to parse
* @param chs the list of integers
* @param index the max index of choice
* @return if the parsing was done correctly */
@@ -102,7 +100,9 @@ public final class CLIPrompter {
return false;
}
/** @param output the manager
/** List the choices on the output.
*
* @param output the manager
* @param choices the choices
* @param cancel the cancel option if it exists
* @return the number of choices plus one (or the number of choices if there
@@ -121,7 +121,9 @@ public final class CLIPrompter {
return Integer.valueOf(index - 1);
}
/** @param manager the manager
/** Prompt for a boolean value.
*
* @param manager the manager
* @param input the input
* @param message the prompting message
* @return the choice
@@ -159,7 +161,9 @@ public final class CLIPrompter {
.getString("promptbool.choices.yes2")); //$NON-NLS-1$
}
/** @param manager the manager
/** Prompt for a choice.
*
* @param manager the manager
* @param input the input
* @param keys the keys to be printed
* @param choices the real choices
@@ -168,21 +172,23 @@ public final class CLIPrompter {
* @param <U> the type of elements
* @return the choice
* @throws IOException if the manager was closed */
@SuppressWarnings("boxing")
public static <U> U promptChoice(final ConsoleOutput manager,
final ConsoleInput input,
final List<String> keys,
final List<U> choices,
final String message,
final String cancel) throws IOException {
final Integer index = promptChoice(manager, input, keys, message, cancel);
final Integer index = promptChoice(manager, input, keys, message,
cancel);
if (index == null) {
return null;
}
return choices.get(index);
return choices.get(index.intValue());
}
/** @param manager the manager
/** Prompt for a choice.
*
* @param manager the manager
* @param input the input
* @param <U> The choices labels type
* @param <T> The real choices objects
@@ -206,7 +212,9 @@ public final class CLIPrompter {
return choicesMap.get(choices.get(res.intValue()));
}
/** @param manager the manager
/** Prompt for a choice.
*
* @param manager the manager
* @param input the input
* @param <U> the type of choices
* @param choices the list of choices
@@ -222,14 +230,12 @@ public final class CLIPrompter {
manager.println(message);
final Integer index = listChoices(manager, choices, cancel);
String result;
boolean keepOn = true;
int r = -1;
while (keepOn) {
while (true) {
result = input.prompt(CLIPrompterMessages.getString(PROMPT));
try {
r = Integer.parseInt(result);
if (r >= 0 && r <= index.intValue()) {
keepOn = false;
break;
}
manager.println(CLIPrompterMessages
@@ -237,7 +243,6 @@ public final class CLIPrompter {
} catch (final NumberFormatException e) {
LOGGER.log(Level.FINER,
"Unrecognized number. Prompting user again.", e); //$NON-NLS-1$
keepOn = true;
manager.println(CLIPrompterMessages
.getString(PROMPTCHOICE_FORMATERR, ZERO, index));
}
@@ -249,7 +254,9 @@ public final class CLIPrompter {
return Integer.valueOf(r);
}
/** @param manager the manager
/** Prompt for a choice.
*
* @param manager the manager
* @param input the input
* @param <U> The choices labels type
* @param <T> The real choices objects
@@ -264,11 +271,13 @@ public final class CLIPrompter {
final String message,
final String cancel) throws IOException {
return promptChoice(manager, input,
new ArrayList<>(choicesMap.keySet()),
choicesMap, message, cancel);
new ArrayList<>(choicesMap.keySet()), choicesMap, message,
cancel);
}
/** @param manager the manager
/** Prompt for an integer.
*
* @param manager the manager
* @param message the prompt message
* @return the integer
* @throws IOException if the manager was closed */
@@ -294,7 +303,7 @@ public final class CLIPrompter {
return r;
}
/** This methods prompt the user for a list of elements
/** This methods prompt the user for a list of elements.
*
* @param manager the manager
* @param input the input
@@ -308,7 +317,7 @@ public final class CLIPrompter {
CLIPrompterMessages.getString("promptlist.exit.defaultkey")); //$NON-NLS-1$
}
/** This methods prompt the user for a list of elements
/** This methods prompt the user for a list of elements.
*
* @param manager the manager
* @param input the input
@@ -373,7 +382,9 @@ public final class CLIPrompter {
return res.toString();
}
/** @param manager the manager
/** Prompt the user to select zero or more elements from a list.
*
* @param manager the manager
* @param input the input
* @param keys the keys to be printed
* @param choices the real choices
@@ -386,7 +397,8 @@ public final class CLIPrompter {
final List<String> keys,
final List<U> choices,
final String message) throws IOException {
final List<Integer> indices = promptMultiChoice(manager, input, keys, message);
final List<Integer> indices = promptMultiChoice(manager, input, keys,
message);
final List<U> userChoices = new ArrayList<>();
for (final Integer integer : indices) {
userChoices.add(choices.get(integer.intValue()));
@@ -394,7 +406,9 @@ public final class CLIPrompter {
return userChoices;
}
/** @param manager the manager
/** Prompt the user to select zero or more elements from a list.
*
* @param manager the manager
* @param input the input
* @param <U> The choices labels type
* @param <T> The real choices objects
@@ -417,7 +431,9 @@ public final class CLIPrompter {
return userChoices;
}
/** @param manager the manager
/** Prompt the user to select zero or more elements from a list.
*
* @param manager the manager
* @param input the input
* @param <U> the type of choices
* @param choices the list of choices
@@ -462,7 +478,9 @@ public final class CLIPrompter {
return chs;
}
/** @param manager the manager
/** Prompt the user to select zero or more elements from a list.
*
* @param manager the manager
* @param input the input
* @param <U> The choices labels type
* @param <T> The real choices objects
@@ -475,11 +493,12 @@ public final class CLIPrompter {
final Map<U, T> choicesMap,
final String message) throws IOException {
return promptMultiChoice(manager, input,
new ArrayList<>(choicesMap.keySet()),
choicesMap, message);
new ArrayList<>(choicesMap.keySet()), choicesMap, message);
}
/** @param manager the manager
/** Prompt the user for a non empty text.
*
* @param manager the manager
* @param prompt the prompting message
* @param reprompt the prompting message after empty input
* @return the non empty input

View File

@@ -44,28 +44,25 @@ import java.util.ResourceBundle;
import java.util.logging.Level;
import java.util.logging.Logger;
/** <p>
* Utility class for the messages of the CLIPrompter
/** Utility class for the messages of the CLIPrompter.
*
* @author Emmanuel BIGEON */
public final class CLIPrompterMessages {
/** The resource name */
/** The resource name. */
private static final String BUNDLE_NAME = "fr.bigeon.gclc.messages"; //$NON-NLS-1$
/** The resource */
/** The resource. */
private static final ResourceBundle RESOURCE_BUNDLE = ResourceBundle
.getBundle(BUNDLE_NAME);
/** The logger */
/** The logger. */
private static final Logger LOGGER = Logger
.getLogger(CLIPrompterMessages.class.getName());
/** Utility class */
/** Utility class. */
private CLIPrompterMessages() {
// Utility constructor
}
/** Return the formatted message corresponding to the given key
/** Return the formatted message corresponding to the given key.
*
* @param key the message's key
* @param args the arguments

View File

@@ -51,14 +51,14 @@ import fr.bigeon.gclc.manager.PipedConsoleOutput;
* @author Emmanuel Bigeon */
public abstract class AOutputForwardRunnable implements Runnable {
/** The class logger */
/** The class logger. */
private static final Logger LOGGER = Logger
.getLogger(AOutputForwardRunnable.class.getName());
/** The default timeout (one tenth of second). */
private static final long DEFAULT_TIMEOUT = 100;
/** The manager. */
private final PipedConsoleOutput manager;
/** The timeout */
/** The timeout. */
private final long timeout;
/** Create a forwarding runnable.
@@ -90,12 +90,18 @@ public abstract class AOutputForwardRunnable implements Runnable {
this.timeout = timeout;
}
/** @param m the line to forward */
/** Do forward the line.
*
* @param m the line to forward */
protected abstract void forwardLine(String m);
/** @return if the thread should keep running */
/** Test if the runable is still running.
*
* @return if the thread should keep running */
protected abstract boolean isRunning();
/* (non-Javadoc)
* @see java.lang.Runnable#run() */
@Override
public final void run() {
try {

View File

@@ -46,19 +46,22 @@ import java.util.List;
* @author Emmanuel BIGEON */
public final class PrintUtils {
/** The continuation dot string */
/** The continuation dot string. */
private static final String CONT_DOT = "..."; //$NON-NLS-1$
/** The continuation dot string length */
/** The continuation dot string length. */
private static final int CONT_DOT_LENGTH = CONT_DOT.length();
/** The empty string constant */
/** The empty string constant. */
private static final String EMPTY = ""; //$NON-NLS-1$
/** Utility class */
/** Utility class. */
private PrintUtils() {
// Utility class
}
/** @param text the text to print
/** Print the text possibly cutting it if it goes over the authorized length
* and adding a mark of continuation.
*
* @param text the text to print
* @param nbCharacters the number of characters of the resulting text
* @param indicateTooLong if an indication shell be given that the text
* didn't fit
@@ -83,7 +86,9 @@ public final class PrintUtils {
return res.toString();
}
/** @param description the element to wrap in lines
/** Wrap the text, cutting at spaces.
*
* @param description the element to wrap in lines
* @param i the length of the wrap
* @return the list of resulting strings */
public static List<String> wrap(final String description, final int i) {

View File

@@ -0,0 +1,8 @@
/**
* gclc:fr.bigeon.gclc.tools.package-info.java
* Created on: Nov 13, 2017
*/
/** Tool classes.
*
* @author Emmanuel Bigeon */
package fr.bigeon.gclc.tools;