Interface CommandInput


@API(status=EXPERIMENTAL) public interface CommandInput
  • Field Details

    • BOOLEAN_STRICT

      static final List<String> BOOLEAN_STRICT
    • BOOLEAN_LIBERAL

      static final List<String> BOOLEAN_LIBERAL
    • BOOLEAN_LIBERAL_TRUE

      static final List<String> BOOLEAN_LIBERAL_TRUE
  • Method Details

    • of

      static @NonNull CommandInput of(@NonNull String input)
      Returns a new CommandInput instance from the given input.
      Parameters:
      input - the input string
      Returns:
      the command input instance
    • of

      Returns a new CommandInput instance from the given input.
      Parameters:
      input - the input string
      Returns:
      the command input instance
    • empty

      static @NonNull CommandInput empty()
      Returns a new CommandInput with an empty string as the input.
      Returns:
      the command input instance
    • input

      Returns the complete input string.
      Returns:
      the complete input
    • cursor

      @SideEffectFree @org.checkerframework.checker.index.qual.NonNegative int cursor()
      Returns the cursor position.

      This must always be non-negative, and less than length().

      Returns:
      the cursor position
    • length

      @Pure default @org.checkerframework.checker.index.qual.NonNegative int length()
      Returns the length of the input string.

      This should always be equal to the length of input().

      Returns:
      the length of the input string
    • remainingLength

      @SideEffectFree default @org.checkerframework.checker.index.qual.NonNegative int remainingLength()
      Returns the length of the remaining input.
      Returns:
      the length of the remaining input.
    • remainingTokens

      @SideEffectFree default @org.checkerframework.checker.index.qual.NonNegative int remainingTokens()
      Returns the number of remaining tokens.
      Returns:
      the number of remaining tokens
    • remainingInput

      @SideEffectFree default @NonNull String remainingInput()
      Returns the remaining input.
      Returns:
      the remaining input.
    • readInput

      @SideEffectFree default @NonNull String readInput()
      Returns the read input.
      Returns:
      the read input
    • appendString

      @NonNull CommandInput appendString(@NonNull String string)
      Suffixes the input with the given string and return a new command input containing the updated string.

      This does not modify this instance.

      Parameters:
      string - the string
      Returns:
      the command input with the appended string
    • hasRemainingInput

      @SideEffectFree default boolean hasRemainingInput()
      Returns whether there is any remaining input left to read.
      Returns:
      whether there is any remaining input
    • isEmpty

      @SideEffectFree default boolean isEmpty()
      Returns whether there is nothing left to read. This does not ignore whitespace.
      Returns:
      whether there is nothing left to read.
    • isEmpty

      @SideEffectFree default boolean isEmpty(boolean ignoreWhitespace)
      Returns whether there is nothing left to read.
      Parameters:
      ignoreWhitespace - whether to ignore whitespace
      Returns:
      whether there is nothing left to read.
    • hasRemainingInput

      @SideEffectFree default boolean hasRemainingInput(boolean ignoreWhitespace)
      Returns whether there is anything left to read.
      Parameters:
      ignoreWhitespace - whether to ignore whitespace
      Returns:
      whether there is anything left to read.
    • moveCursor

      void moveCursor(int chars)
      Moves the cursor chars positions.
      Parameters:
      chars - the number of characters to move the cursor
      Throws:
      CommandInput.CursorOutOfBoundsException - If chars exceeds remainingLength()
    • cursor

      @NonNull CommandInput cursor(@org.checkerframework.checker.index.qual.NonNegative int position)
      Sets the cursor position
      Parameters:
      position - the new position
      Returns:
      this
    • peekString

      @SideEffectFree default @NonNull String peekString(@org.checkerframework.checker.index.qual.NonNegative int chars)
      Reads chars characters of the remaining input without moving the cursor.
      Parameters:
      chars - the number of characters to read
      Returns:
      the read characters
      Throws:
      CommandInput.CursorOutOfBoundsException - If chars exceeds remainingLength()
    • read

      default @NonNull String read(@org.checkerframework.checker.index.qual.NonNegative int chars)
      Reads chars characters of the remaining input and move the cursor chars characters.
      Parameters:
      chars - the number of characters to read
      Returns:
      the read characters
      Throws:
      CommandInput.CursorOutOfBoundsException - If chars exceeds remainingLength()
    • peek

      @SideEffectFree default char peek()
      Reads the character at the cursor without moving the cursor.
      Returns:
      the character at the cursor.
      Throws:
      CommandInput.CursorOutOfBoundsException - If the cursor has exceeded the input
    • read

      default char read()
      Reads the character at the cursor and move the cursor one character.
      Returns:
      the character at the cursor.
    • peekString

      default @NonNull String peekString()
      Peeks until the next whitespace is encountered, skipping leading whitespace.
      Returns:
      the peeked string
    • readStringSkipWhitespace

      default @NonNull String readStringSkipWhitespace(boolean preserveSingleSpace)
      Reads until the next whitespace is encountered. Any trailing whitespace will be skipped.
      Parameters:
      preserveSingleSpace - whether a single trailing space should be preserved
      Returns:
      the read string
    • readStringSkipWhitespace

      default @NonNull String readStringSkipWhitespace()
      Reads until the next whitespace is encountered. Any trailing whitespace will be skipped.
      Returns:
      the read string
    • readString

      default @NonNull String readString()
      Skips initial whitespace and reads until the next whitespace is encountered.
      Returns:
      the read string
    • readUntil

      default @NonNull String readUntil(char separator)
      Reads until the separator, and then preserves it.
      Parameters:
      separator - the separator to read until
      Returns:
      the read string
    • readUntilAndSkip

      default @NonNull String readUntilAndSkip(char separator)
      Reads until the separator, and then skips the separator.
      Parameters:
      separator - the separator to read until
      Returns:
      the read string
    • skipWhitespace

      default @NonNull CommandInput skipWhitespace(int maxSpaces, boolean preserveSingleSpace)
      Skips maxSpaces of whitespace characters at the head of the input.
      Parameters:
      maxSpaces - maximum number of spaces of consume
      preserveSingleSpace - whether a single whitespace at the tail of the input should be ignored
      Returns:
      this
    • skipWhitespace

      default @NonNull CommandInput skipWhitespace(int maxSpaces)
      Skips maxSpaces of whitespace characters at the head of the input.
      Parameters:
      maxSpaces - maximum number of spaces of consume
      Returns:
      this
    • skipWhitespace

      default @NonNull CommandInput skipWhitespace(boolean preserveSingleSpace)
      Skips any whitespace characters at the head of the input.
      Parameters:
      preserveSingleSpace - whether a single space should be ignored
      Returns:
      this
    • skipWhitespace

      default @NonNull CommandInput skipWhitespace()
      Skips any whitespace characters at the head of the input.
      Returns:
      this
    • hasNonWhitespace

      default boolean hasNonWhitespace()
      Returns whether the remaining input contains any non-whitespace characters.
      Returns:
      whether the remaining input contains any non-whitespace characters
    • isValidByte

      @SideEffectFree default boolean isValidByte(byte min, byte max)
      Returns whether peekString() contain a valid Byte within the given range.
      Parameters:
      min - the min value
      max - the max value
      Returns:
      whether the input until the next whitespace contains a valid Byte
    • isValidByte

      @SideEffectFree default boolean isValidByte(@NonNull ByteRange range)
      Returns whether peekString() contain a valid Byte within the given range.
      Parameters:
      range - range of accepted numbers
      Returns:
      whether the input until the next whitespace contains a valid Byte
    • readByte

      default byte readByte()
      Reads the string until the next whitespace and parses in into a Byte.
      Returns:
      the parsed byte
      Throws:
      NumberFormatException - If the string cannot be parsed into a Byte.
    • isValidShort

      @SideEffectFree default boolean isValidShort(short min, short max)
      Returns whether peekString() contain a valid Short within the given range.
      Parameters:
      min - the min value
      max - the max value
      Returns:
      whether the input until the next whitespace contains a valid Short
    • isValidShort

      @SideEffectFree default boolean isValidShort(@NonNull ShortRange range)
      Returns whether peekString() contain a valid Short within the given range.
      Parameters:
      range - range of accepted numbers
      Returns:
      whether the input until the next whitespace contains a valid Short
    • readShort

      default short readShort()
      Reads the string until the next whitespace and parses in into a Short.
      Returns:
      the parsed short
      Throws:
      NumberFormatException - If the string cannot be parsed into a Short.
    • isValidInteger

      @SideEffectFree default boolean isValidInteger(int min, int max)
      Returns whether peekString() contain a valid Integer within the given range.
      Parameters:
      min - the min value
      max - the max value
      Returns:
      whether the input until the next whitespace contains a valid Integer
    • isValidInteger

      @SideEffectFree default boolean isValidInteger(@NonNull IntRange range)
      Returns whether peekString() contain a valid Integer within the given range.
      Parameters:
      range - range of accepted numbers
      Returns:
      whether the input until the next whitespace contains a valid Integer
    • readInteger

      default int readInteger()
      Reads the string until the next whitespace and parses in into a Integer.
      Returns:
      the parsed integer
      Throws:
      NumberFormatException - If the string cannot be parsed into a Integer.
    • readInteger

      default int readInteger(int radix)
      Reads the string until the next whitespace and parses in into a Integer.
      Parameters:
      radix - radix
      Returns:
      the parsed integer
      Throws:
      NumberFormatException - If the string cannot be parsed into a Integer.
    • isValidLong

      @SideEffectFree default boolean isValidLong(long min, long max)
      Returns whether peekString() contain a valid Long within the given range.
      Parameters:
      min - the min value
      max - the max value
      Returns:
      whether the input until the next whitespace contains a valid Long
    • isValidLong

      @SideEffectFree default boolean isValidLong(@NonNull LongRange range)
      Returns whether peekString() contain a valid Long within the given range.
      Parameters:
      range - range of accepted numbers
      Returns:
      whether the input until the next whitespace contains a valid Long
    • readLong

      default long readLong()
      Reads the string until the next whitespace and parses in into a Long.
      Returns:
      the parsed long
      Throws:
      NumberFormatException - If the string cannot be parsed into a Long.
    • isValidDouble

      @SideEffectFree default boolean isValidDouble(double min, double max)
      Returns whether peekString() contain a valid Double within the given range.
      Parameters:
      min - the min value
      max - the max value
      Returns:
      whether the input until the next whitespace contains a valid Double
    • isValidDouble

      @SideEffectFree default boolean isValidDouble(@NonNull DoubleRange range)
      Returns whether peekString() contain a valid Double within the given range.
      Parameters:
      range - range of accepted numbers
      Returns:
      whether the input until the next whitespace contains a valid Double
    • readDouble

      default double readDouble()
      Reads the string until the next whitespace and parses in into a Double.
      Returns:
      the parsed double
      Throws:
      NumberFormatException - If the string cannot be parsed into a Double.
    • isValidFloat

      @SideEffectFree default boolean isValidFloat(float min, float max)
      Returns whether peekString() contain a valid Float within the given range.
      Parameters:
      min - the min value
      max - the max value
      Returns:
      whether the input until the next whitespace contains a valid Float
    • isValidFloat

      @SideEffectFree default boolean isValidFloat(@NonNull FloatRange range)
      Returns whether peekString() contain a valid Float within the given range.
      Parameters:
      range - range of accepted numbers
      Returns:
      whether the input until the next whitespace contains a valid Float
    • readFloat

      default float readFloat()
      Reads the string until the next whitespace and parses in into a Float.
      Returns:
      the parsed float
      Throws:
      NumberFormatException - If the string cannot be parsed into a Float.
    • isValidBoolean

      @SideEffectFree default boolean isValidBoolean(boolean liberal)
      Returns whether peekString() contain a valid Boolean within the given range.
      Parameters:
      liberal - whether non-strict boolean values like "yes", "no", "on" and "off" should be allowed
      Returns:
      whether the input until the next whitespace contains a valid Boolean
    • readBoolean

      default boolean readBoolean()
      Reads the string until the next whitespace and parses in into a Boolean.
      Returns:
      the parsed boolean
      Throws:
      IllegalArgumentException - If the string cannot be parsed into a Boolean.
    • lastRemainingToken

      default @NonNull String lastRemainingToken()
      Returns the last remaining token.

      If the string ends with a blank space, then an empty string is returned.

      .
      Returns:
      the last remaining token, or an empty string if none remains
    • lastRemainingCharacter

      default char lastRemainingCharacter()
      Returns the last remaining character.
      Returns:
      the last remaining character
      Throws:
      CommandInput.CursorOutOfBoundsException - if isEmpty() is true
    • copy

      Returns a copy of this instance.
      Returns:
      copy of this instance
    • difference

      default @NonNull String difference(@NonNull CommandInput that, boolean includeTrailingWhitespace)
      Returns the input that has been consumed by that input that has not been consumed by input.
      Parameters:
      that - the input to compare to
      includeTrailingWhitespace - whether to include trailing whitespace
      Returns:
      the difference in consumed input
    • difference

      default @NonNull String difference(@NonNull CommandInput that)
      Returns the input that has been consumed by that input that has not been consumed by input.
      Parameters:
      that - the input to compare to
      Returns:
      the difference in consumed input