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 int
Interval for busy waiting regarding safe input read.protected ApplicationContext
Current application context.protected InputStream
Stream used to receive user input.protected static final org.apache.logging.log4j.Logger
Logger.protected boolean
Flag that indicates if the main thread requested the command to terminate.protected PrintWriter
Writer for output. -
Constructor Summary
ConstructorsConstructorDescriptionDefault constructor.AbstractCommand
(ApplicationContext context) Default constructor. -
Method Summary
Modifier and TypeMethodDescriptionprotected boolean
Method used to ask yes/no response from a user.protected boolean
correctArgumentsNumber
(int argumentsNumber) Checks whether the number of arguments is correct or notprotected void
disableDummyCommands
(String[] commandNames) Disable all commands and enable again the commands disabled with the method "enableDummyCommands"protected void
Save and disable all enabled commandsprotected void
enableDummyCommands
(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.void
kill()
Requests command termination.protected void
Re-enable previously enabled commandsprotected String
Read method used for non-blocking read operations.protected String
Read method used for non-blocking read (entire line) operations.void
Arguments setter.void
setContext
(ApplicationContext context) Context setter.void
setInputStream
(InputStream inputStream) Input stream setter.void
Writer 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:
getPossibleArgs
in 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:
getInputStream
in interfaceCommand
- Returns:
- Input stream.
-
getArgs
Arguments getter. -
getContext
Description copied from interface:Command
Gets command context.- Specified by:
getContext
in interfaceCommand
- Returns:
- Application context.
-
setWriter
Writer setter. -
setInputStream
Input stream setter.- Specified by:
setInputStream
in interfaceCommand
- Parameters:
inputStream
- Input stream.
-
setArgs
Arguments setter. -
setContext
Context setter.- Specified by:
setContext
in interfaceCommand
- Parameters:
context
- Application context.
-
kill
public void kill()Description copied from interface:Command
Requests 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
-