Class ScopedStringReader

java.lang.Object
net.luis.utils.io.reader.StringReader
net.luis.utils.io.reader.ScopedStringReader

public class ScopedStringReader extends StringReader
A utility class to read strings with scopes.
A scope is defined by two characters, an opening and a closing character.
  • Constructor Details

    • ScopedStringReader

      public ScopedStringReader(@NotNull @NotNull String string)
      Constructs a new scoped string reader with the given string.
      Parameters:
      string - The string to read from
      Throws:
      NullPointerException - If the string is null
    • ScopedStringReader

      public ScopedStringReader(@NotNull @NotNull Reader reader)
      Constructs a new scoped string reader with the given reader.
      The reader is closed after reading the string.
      Parameters:
      reader - The reader to read from
      Throws:
      NullPointerException - If the reader is null
      UncheckedIOException - If an I/O error occurs while reading the string
  • Method Details

    • readScope

      @NotNull public @NotNull String readScope(@NotNull @NotNull StringScope scope)
      Reads a string with the given scope.

      The scope is defined by an opening and a closing character.
      The method will read until the opened scope is closed.
      If there are nested scopes, the method will read until all scopes are closed.

      The opening and closing character are included in the returned string.
      If there are no more characters to read, an empty string is returned.

      Parameters:
      scope - The scope to use
      Returns:
      The read string
      Throws:
      NullPointerException - If the scope is null
      IllegalArgumentException - If the next character is not the opening character of the scope
      InvalidStringException - If the scope is invalid
    • readUntil

      @NotNull public @NotNull String readUntil(char terminator)
      Reads the string until the given terminator is found.
      The terminator and escape character ('\\') are read but not included in the result.

      If the terminator is found at the beginning or at the end of the string, an empty string is returned.
      If the terminator is found in a quoted part or in a scope, the terminator is ignored.
      If the terminator is not found, the rest of the string is returned.

      Overrides:
      readUntil in class StringReader
      Parameters:
      terminator - The terminator to read until
      Returns:
      The read string
      Throws:
      IllegalArgumentException - If the terminator is a backslash
      InvalidStringException - If the scope is invalid
    • readUntilInclusive

      @NotNull public @NotNull String readUntilInclusive(char terminator)
      Reads the string until the given terminator is found.
      The escape character ('\\') is read but not included in the result.

      If the terminator is found at the beginning or at the end of the string, an empty string is returned.
      If the terminator is found in a quoted part or in a scope, the terminator is ignored.
      If the terminator is not found, the rest of the string is returned.

      Overrides:
      readUntilInclusive in class StringReader
      Parameters:
      terminator - The terminator to read until
      Returns:
      The read string
      Throws:
      IllegalArgumentException - If the terminator is a backslash
      InvalidStringException - If the scope is invalid
    • readUntil

      @NotNull public @NotNull String readUntil(char @NotNull ... terminators)
      Reads the string until any of the given terminators is found.
      The terminators and escape character ('\\') are read but not included in the result.

      If any terminator is found at the beginning or at the end of the string, an empty string is returned.
      If any terminator is found in a quoted part or in a scope, the terminator is ignored.
      If none terminator is found, the rest of the string is returned.

      Overrides:
      readUntil in class StringReader
      Parameters:
      terminators - The terminators to read until
      Returns:
      The string which was read until the terminator
      Throws:
      IllegalArgumentException - If the terminators are empty or contain a backslash
    • readUntilInclusive

      @NotNull public @NotNull String readUntilInclusive(char @NotNull ... terminators)
      Reads the string until any of the given terminators is found.
      The escape character ('\\') is read but not included in the result.

      If any terminator is found at the beginning or at the end of the string, an empty string is returned.
      If any terminator is found in a quoted part or in a scope, the terminator is ignored.
      If none terminator is found, the rest of the string is returned.

      Overrides:
      readUntilInclusive in class StringReader
      Parameters:
      terminators - The terminators to read until
      Returns:
      The string which was read until the terminator
      Throws:
      IllegalArgumentException - If the terminators are empty or contain a backslash
    • readUntil

      @Internal @NotNull protected @NotNull String readUntil(@NotNull @NotNull Predicate<Character> predicate, boolean inclusive)
      Internal method to read the string until the given predicate is true.
      Overrides:
      readUntil in class StringReader
      Parameters:
      predicate - The predicate to match the characters
      inclusive - Whether the character which matches the predicate should be included in the result or not
      Returns:
      The string which was read until the predicate is true
      See Also:
    • readUntil

      @NotNull public @NotNull String readUntil(@NotNull @NotNull String terminator, boolean caseSensitive)
      Reads the string until the given terminator string is found.
      The terminator string and escape character ('\\') are read but not included in the result.

      If the terminator string is found at the beginning or at the end of the string, an empty string is returned.
      If the terminator string is found in a quoted part or in a scope, the terminator is ignored.
      If the terminator string is not found, the rest of the string is returned.

      Overrides:
      readUntil in class StringReader
      Parameters:
      terminator - The terminating string to read until
      caseSensitive - Whether the terminator string should be case-sensitive or not
      Returns:
      The string which was read until the terminator
      Throws:
      NullPointerException - If the terminator string is null
      IllegalArgumentException - If the terminator string is empty or contains a backslash
    • readUntilInclusive

      @NotNull public @NotNull String readUntilInclusive(@NotNull @NotNull String terminator, boolean caseSensitive)
      Reads the string until the given terminator string is found.
      The escape character ('\\') is read but not included in the result.

      If the terminator string is found at the beginning or at the end of the string, an empty string is returned.
      If the terminator string is found in a quoted part or in a scope, the terminator is ignored.
      If the terminator string is not found, the rest of the string is returned.

      Overrides:
      readUntilInclusive in class StringReader
      Parameters:
      terminator - The terminating string to read until
      caseSensitive - Whether the terminator string should be case-sensitive or not
      Returns:
      The string which was read until the terminator
      Throws:
      NullPointerException - If the terminator string is null
      IllegalArgumentException - If the terminator string is empty or contains a backslash
    • readUntil

      @Internal @NotNull protected @NotNull String readUntil(@NotNull @NotNull String terminator, boolean caseSensitive, boolean inclusive)
      Internal method to read the string until the terminating string is found.
      Overrides:
      readUntil in class StringReader
      Parameters:
      terminator - The terminator string to read until
      caseSensitive - Whether the terminator string should be case-sensitive or not
      inclusive - Whether the terminator string should be included in the result or not
      Returns:
      The string which was read until the equals predicate is true
      Throws:
      NullPointerException - If the terminator string is null
      See Also:
    • readCollection

      @NotNull private <T> @NotNull Collection<T> readCollection(@NotNull @NotNull StringScope scope, @NotNull @NotNull Function<ScopedStringReader,T> parser)
      Reads a collection like object from the string.

      The collection is defined by an opening and a closing character.
      If there are no more characters to read, an empty collection is returned.
      Whitespace characters are ignored.

      The elements of the collection are separated by a comma.
      The elements are parsed by the given parser.

      Type Parameters:
      T - The type of the elements
      Parameters:
      scope - The scope with the opening and closing character
      parser - The parser to parse the elements
      Returns:
      The read collection
      Throws:
      NullPointerException - If the scope or parser is null
      IllegalArgumentException - If an error occurs while reading the collection
    • readList

      @NotNull public <T> @NotNull List<T> readList(@NotNull @NotNull Function<ScopedStringReader,T> parser)
      Reads a list from the string.
      The list is defined by square brackets.
      Type Parameters:
      T - The type of the elements
      Parameters:
      parser - The parser to parse the elements
      Returns:
      The read list as an ArrayList
      Throws:
      NullPointerException - If the parser is null
      InvalidStringException - If an error occurs while reading the list
      See Also:
    • readSet

      @NotNull public <T> @NotNull Set<T> readSet(@NotNull @NotNull Function<ScopedStringReader,T> parser)
      Reads a set from the string.
      The set is defined by parentheses.

      The order of the elements is not guaranteed.
      Duplicates are ignored.

      Type Parameters:
      T - The type of the elements
      Parameters:
      parser - The parser to parse the elements
      Returns:
      The read set as an HashSet
      Throws:
      NullPointerException - If the parser is null
      InvalidStringException - If an error occurs while reading the set
      See Also:
    • readMap

      @NotNull public <K, V> @NotNull Map<K,V> readMap(@NotNull @NotNull Function<ScopedStringReader,K> keyParser, @NotNull @NotNull Function<ScopedStringReader,V> valueParser)
      Reads a map from the string.
      The map is defined by curly brackets.

      The key and value are separated by an equal sign ('=').
      The entries are separated by a comma.

      If a key is defined multiple times, the last value is used.

      Type Parameters:
      K - The type of the keys
      V - The type of the values
      Parameters:
      keyParser - The parser to parse the keys
      valueParser - The parser to parse the values
      Returns:
      The read map as a HashMap
      Throws:
      NullPointerException - If the key or value parser is null
      InvalidStringException - If an error occurs while reading the map
      See Also: