A scope is defined by two characters, an opening and a closing character.
-
Constructor Summary
ConstructorsConstructorDescriptionScopedStringReader
(@NotNull Reader reader) Constructs a new scoped string reader with the given reader.
The reader is closed after reading the string.ScopedStringReader
(@NotNull String string) Constructs a new scoped string reader with the given string. -
Method Summary
Modifier and TypeMethodDescriptionprivate <T> @NotNull Collection
<T> readCollection
(@NotNull StringScope scope, @NotNull Function<ScopedStringReader, T> parser) Reads a collection like object from the string.<T> @NotNull List
<T> readList
(@NotNull Function<ScopedStringReader, T> parser) Reads a list from the string.
The list is defined by square brackets.<K,
V> @NotNull Map <K, V> readMap
(@NotNull Function<ScopedStringReader, K> keyParser, @NotNull Function<ScopedStringReader, V> valueParser) Reads a map from the string.
The map is defined by curly brackets.@NotNull String
readScope
(@NotNull StringScope scope) Reads a string with the given scope.<T> @NotNull Set
<T> readSet
(@NotNull Function<ScopedStringReader, T> parser) Reads a set from the string.
The set is defined by parentheses.@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.@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.@NotNull String
Reads the string until the given terminator string is found.
The terminator string and escape character ('\\') are read but not included in the result.protected @NotNull String
Internal method to read the string until the terminating string is found.protected @NotNull String
Internal method to read the string until the given predicate is true.@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.@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.@NotNull String
readUntilInclusive
(@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.Methods inherited from class net.luis.utils.io.reader.StringReader
canRead, canRead, canRead, equals, getIndex, getString, hashCode, mark, peek, read, read, readBigDecimal, readBigInteger, readBoolean, readByte, readDouble, readExpected, readExpected, readFloat, readInt, readLine, readLong, readNumber, readQuotedString, readRemaining, readShort, readString, readUnquotedString, readUnquotedString, reset, skip, skip, skip, skip, skipWhitespaces
-
Constructor Details
-
ScopedStringReader
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
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 nullUncheckedIOException
- If an I/O error occurs while reading the string
-
-
Method Details
-
readScope
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 nullIllegalArgumentException
- If the next character is not the opening character of the scopeInvalidStringException
- If the scope is invalid
-
readUntil
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 classStringReader
- Parameters:
terminator
- The terminator to read until- Returns:
- The read string
- Throws:
IllegalArgumentException
- If the terminator is a backslashInvalidStringException
- If the scope is invalid
-
readUntilInclusive
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 classStringReader
- Parameters:
terminator
- The terminator to read until- Returns:
- The read string
- Throws:
IllegalArgumentException
- If the terminator is a backslashInvalidStringException
- If the scope is invalid
-
readUntil
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 classStringReader
- 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
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 classStringReader
- 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 classStringReader
- Parameters:
predicate
- The predicate to match the charactersinclusive
- 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 classStringReader
- Parameters:
terminator
- The terminating string to read untilcaseSensitive
- 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 nullIllegalArgumentException
- 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 classStringReader
- Parameters:
terminator
- The terminating string to read untilcaseSensitive
- 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 nullIllegalArgumentException
- 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 classStringReader
- Parameters:
terminator
- The terminator string to read untilcaseSensitive
- Whether the terminator string should be case-sensitive or notinclusive
- 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 characterparser
- The parser to parse the elements- Returns:
- The read collection
- Throws:
NullPointerException
- If the scope or parser is nullIllegalArgumentException
- 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 nullInvalidStringException
- 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 nullInvalidStringException
- 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 keysV
- The type of the values- Parameters:
keyParser
- The parser to parse the keysvalueParser
- The parser to parse the values- Returns:
- The read map as a
HashMap
- Throws:
NullPointerException
- If the key or value parser is nullInvalidStringException
- If an error occurs while reading the map- See Also:
-