Class AbstractCommand

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

public abstract class AbstractCommand extends Object implements Command
Abstract command that contains basic functionality for a generic command.
  • Field Details

    • writer

      protected PrintWriter writer
      Writer for output.
    • inputStream

      protected InputStream inputStream
      Stream used to receive user input.
    • args

      protected List<String> args
      Eventual parameters for the command.
    • context

      protected ApplicationContext context
      Current application context.
    • shouldTerminate

      protected boolean shouldTerminate
      Flag that indicates if the main thread requested the command to terminate.
    • BUSY_WAITING_QUANTUM

      public static final int BUSY_WAITING_QUANTUM
      Interval 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 logger
      Logger.
  • Constructor Details

    • AbstractCommand

      public AbstractCommand()
      Default constructor. Binds input stream and output stream to stdin and stdout.
    • AbstractCommand

      public AbstractCommand(ApplicationContext context)
      Default constructor. Binds input stream and output stream to stdin and stdout.
  • Method Details

    • getPossibleArgs

      public abstract List<String> getPossibleArgs() throws GameStorageException
      Gets possible args for current command.
      Specified by:
      getPossibleArgs in interface Command
      Returns:
      A list of possible arguments for the command.
      Throws:
      GameStorageException - If some error occur while calculating possible args.
    • getWriter

      public Writer getWriter()
      Writer getter.
      Specified by:
      getWriter in interface Command
      Returns:
      Writer object.
    • getInputStream

      public InputStream getInputStream()
      Input stream getter.
      Specified by:
      getInputStream in interface Command
      Returns:
      Input stream.
    • getArgs

      public List<String> getArgs()
      Arguments getter.
      Specified by:
      getArgs in interface Command
      Returns:
      Arguments.
    • getContext

      public ApplicationContext getContext()
      Description copied from interface: Command
      Gets command context.
      Specified by:
      getContext in interface Command
      Returns:
      Application context.
    • setWriter

      public void setWriter(Writer out)
      Writer setter.
      Specified by:
      setWriter in interface Command
      Parameters:
      out - Output writer.
    • setInputStream

      public void setInputStream(InputStream inputStream)
      Input stream setter.
      Specified by:
      setInputStream in interface Command
      Parameters:
      inputStream - Input stream.
    • setArgs

      public void setArgs(List<String> args)
      Arguments setter.
      Specified by:
      setArgs in interface Command
      Parameters:
      args - Arguments.
    • setContext

      public void setContext(ApplicationContext context)
      Context setter.
      Specified by:
      setContext in interface Command
      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.
      Specified by:
      kill in interface Command
    • safeReadNext

      protected String safeReadNext() throws InterruptedException
      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

      protected String safeReadNextLine() throws InterruptedException
      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

      protected boolean askConfirmation() throws InterruptedException
      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

      protected void enableDummyCommands(String[] commandNames)
      Disable all commands and enable a list of dummy commands with a specific name
      Parameters:
      commandNames - String[] array of dummy commands names
    • disableDummyCommands

      protected void disableDummyCommands(String[] commandNames)
      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