java.lang.Object
com.adventure.commands.AbstractCommand
- All Implemented Interfaces:
Command
- Direct Known Subclasses:
AbstractRandomDecisionCommand,CmdBack,CmdClear,CmdDeleteGame,CmdDrop,CmdEquip,CmdFight,CmdHelp,CmdListGames,CmdLoadGame,CmdLook,CmdMove,CmdNewGame,CmdPick,CmdRun,CmdSaveGame,CmdShow,CmdWai,DummyCmd
Abstract command that contains basic functionality for a generic command.
-
Field Summary
FieldsModifier and TypeFieldDescriptionEventual parameters for the command.static final intInterval for busy waiting regarding safe input read.protected ApplicationContextCurrent application context.protected InputStreamStream used to receive user input.protected static final org.apache.logging.log4j.LoggerLogger.protected booleanFlag that indicates if the main thread requested the command to terminate.protected PrintWriterWriter for output. -
Constructor Summary
ConstructorsConstructorDescriptionDefault constructor.AbstractCommand(ApplicationContext context) Default constructor. -
Method Summary
Modifier and TypeMethodDescriptionprotected booleanMethod used to ask yes/no response from a user.protected booleancorrectArgumentsNumber(int argumentsNumber) Checks whether the number of arguments is correct or notprotected voiddisableDummyCommands(String[] commandNames) Disable all commands and enable again the commands disabled with the method "enableDummyCommands"protected voidSave and disable all enabled commandsprotected voidenableDummyCommands(String[] commandNames) Disable all commands and enable a list of dummy commands with a specific namegetArgs()Arguments getter.Gets command context.Input stream getter.Gets possible args for current command.Writer getter.voidkill()Requests command termination.protected voidRe-enable previously enabled commandsprotected StringRead method used for non-blocking read operations.protected StringRead method used for non-blocking read (entire line) operations.voidArguments setter.voidsetContext(ApplicationContext context) Context setter.voidsetInputStream(InputStream inputStream) Input stream setter.voidWriter setter.
-
Field Details
-
writer
Writer for output. -
inputStream
Stream used to receive user input. -
args
Eventual parameters for the command. -
context
Current application context. -
shouldTerminate
protected boolean shouldTerminateFlag that indicates if the main thread requested the command to terminate. -
BUSY_WAITING_QUANTUM
public static final int BUSY_WAITING_QUANTUMInterval for busy waiting regarding safe input read. Busy waiting is used to avoid blocking calls and allow thread termination when requested.- See Also:
-
logger
protected static final org.apache.logging.log4j.Logger loggerLogger.
-
-
Constructor Details
-
AbstractCommand
public AbstractCommand()Default constructor. Binds input stream and output stream to stdin and stdout. -
AbstractCommand
Default constructor. Binds input stream and output stream to stdin and stdout.
-
-
Method Details
-
getPossibleArgs
Gets possible args for current command.- Specified by:
getPossibleArgsin interfaceCommand- Returns:
- A list of possible arguments for the command.
- Throws:
GameStorageException- If some error occur while calculating possible args.
-
getWriter
Writer getter. -
getInputStream
Input stream getter.- Specified by:
getInputStreamin interfaceCommand- Returns:
- Input stream.
-
getArgs
Arguments getter. -
getContext
Description copied from interface:CommandGets command context.- Specified by:
getContextin interfaceCommand- Returns:
- Application context.
-
setWriter
Writer setter. -
setInputStream
Input stream setter.- Specified by:
setInputStreamin interfaceCommand- Parameters:
inputStream- Input stream.
-
setArgs
Arguments setter. -
setContext
Context setter.- Specified by:
setContextin interfaceCommand- Parameters:
context- Application context.
-
kill
public void kill()Description copied from interface:CommandRequests command termination. This ONLY asks the command to terminate, but termination depends on the correct implementation of the command. If the command was executing then it will throw InterruptedException. -
safeReadNext
Read method used for non-blocking read operations. Non-blocking implementation of Scanner.next().- Returns:
- Next valid string token.
- Throws:
InterruptedException- If the command gets a kill request.
-
safeReadNextLine
Read method used for non-blocking read (entire line) operations. Non-blocking implementation of Scanner.nextLine().- Returns:
- Text line.
- Throws:
InterruptedException- If the command gets a kill request.
-
askConfirmation
Method used to ask yes/no response from a user.- Returns:
- Response (true = yes, false = no)
- Throws:
InterruptedException- If the command gets a kill request.
-
correctArgumentsNumber
protected boolean correctArgumentsNumber(int argumentsNumber) Checks whether the number of arguments is correct or not- Parameters:
argumentsNumber- number of arguments expected- Returns:
- True if the number of arguments is correct, False otherwise
-
enableDummyCommands
Disable all commands and enable a list of dummy commands with a specific name- Parameters:
commandNames- String[] array of dummy commands names
-
disableDummyCommands
Disable all commands and enable again the commands disabled with the method "enableDummyCommands"- Parameters:
commandNames- String[] array of dummy commands names that will be unregistered
-
disableSaveAll
protected void disableSaveAll()Save and disable all enabled commands -
reEnableSaved
protected void reEnableSaved()Re-enable previously enabled commands
-