Class SimpleTupleFormat
java.lang.Object
org.apache.commons.geometry.core.internal.SimpleTupleFormat
Class for performing simple formatting and parsing of real number tuples.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprivate static class
Exception class for errors occurring during tuple parsing. -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final SimpleTupleFormat
Static instance configured with default values.private static final String
Default value separator string.private final String
String used to signal the start of a tuple; may be null.private final String
String separating tuple values.private static final String
Space character.private final String
String used to signal the end of a tuple; may be null. -
Constructor Summary
ConstructorsModifierConstructorDescriptionSimpleTupleFormat
(String prefix, String suffix) Constructs a new instance with the default string separator (a comma) and the given prefix and suffix.protected
SimpleTupleFormat
(String separator, String prefix, String suffix) Simple constructor. -
Method Summary
Modifier and TypeMethodDescriptionprivate void
consumeWhitespace
(String str, ParsePosition pos) Advancepos
past any whitespace characters instr
, starting at the current parse position index.private void
endParse
(String str, ParsePosition pos) End a parse operation by ensuring that all non-whitespace characters in the string have been parsed.format
(double a) Return a tuple string with the given value.format
(double a1, double a2) Return a tuple string with the given values.format
(double a1, double a2, double a3) Return a tuple string with the given values.format
(double a1, double a2, double a3, double a4) Return a tuple string with the given values.static SimpleTupleFormat
Return an instance configured with default values.Return the string used to signal the start of a tuple.Return the string used to separate tuple values.Returns the string used to signal the end of a tuple.private boolean
matchSequence
(String str, String seq, ParsePosition pos) Return a boolean indicating whether or not the input stringstr
contains the stringseq
at the given parse index.<T> T
parse
(String str, DoubleFunction1N<T> fn) Parse the given string as a 1-tuple and passes the tuple values to the given function.<T> T
parse
(String str, DoubleFunction2N<T> fn) Parse the given string as a 2-tuple and passes the tuple values to the given function.<T> T
parse
(String str, DoubleFunction3N<T> fn) Parse the given string as a 3-tuple and passes the parsed values to the given function.private static IllegalArgumentException
parseFailure
(String msg, String str, ParsePosition pos) Return anIllegalArgumentException
representing a parsing failure.private static IllegalArgumentException
parseFailure
(String msg, String str, ParsePosition pos, Throwable cause) Return anIllegalArgumentException
representing a parsing failure.private void
readPrefix
(String str, ParsePosition pos) Read the configured prefix from the current position in the given string, ignoring any preceding whitespace, and advance the parsing position past the prefix sequence.private void
readSequence
(String str, String seq, ParsePosition pos) Read the string given byseq
from the given position instr
.private void
readSuffix
(String str, ParsePosition pos) Read the configured suffix from the current position in the given string, ignoring any preceding whitespace, and advance the parsing position past the suffix sequence.private double
readTupleValue
(String str, ParsePosition pos) Read and return a tuple value from the current position in the given string.
-
Field Details
-
DEFAULT_SEPARATOR
Default value separator string.- See Also:
-
SPACE
Space character.- See Also:
-
DEFAULT_INSTANCE
Static instance configured with default values. Tuples in this format are enclosed by parentheses and separated by commas. -
separator
String separating tuple values. -
prefix
String used to signal the start of a tuple; may be null. -
suffix
String used to signal the end of a tuple; may be null.
-
-
Constructor Details
-
SimpleTupleFormat
Constructs a new instance with the default string separator (a comma) and the given prefix and suffix.- Parameters:
prefix
- String used to signal the start of a tuple; if null, no string is expected at the start of the tuplesuffix
- String used to signal the end of a tuple; if null, no string is expected at the end of the tuple
-
SimpleTupleFormat
Simple constructor.- Parameters:
separator
- String used to separate tuple values; must not be null.prefix
- String used to signal the start of a tuple; if null, no string is expected at the start of the tuplesuffix
- String used to signal the end of a tuple; if null, no string is expected at the end of the tuple
-
-
Method Details
-
getSeparator
Return the string used to separate tuple values.- Returns:
- the value separator string
-
getPrefix
Return the string used to signal the start of a tuple. This value may be null.- Returns:
- the string used to begin each tuple or null
-
getSuffix
Returns the string used to signal the end of a tuple. This value may be null.- Returns:
- the string used to end each tuple or null
-
format
Return a tuple string with the given value.- Parameters:
a
- value- Returns:
- 1-tuple string
-
format
Return a tuple string with the given values.- Parameters:
a1
- first valuea2
- second value- Returns:
- 2-tuple string
-
format
Return a tuple string with the given values.- Parameters:
a1
- first valuea2
- second valuea3
- third value- Returns:
- 3-tuple string
-
format
Return a tuple string with the given values.- Parameters:
a1
- first valuea2
- second valuea3
- third valuea4
- fourth value- Returns:
- 4-tuple string
-
parse
Parse the given string as a 1-tuple and passes the tuple values to the given function. The function output is returned.- Type Parameters:
T
- function return type- Parameters:
str
- the string to be parsedfn
- function that will be passed the parsed tuple values- Returns:
- object returned by
fn
- Throws:
IllegalArgumentException
- if the input string format is invalid
-
parse
Parse the given string as a 2-tuple and passes the tuple values to the given function. The function output is returned.- Type Parameters:
T
- function return type- Parameters:
str
- the string to be parsedfn
- function that will be passed the parsed tuple values- Returns:
- object returned by
fn
- Throws:
IllegalArgumentException
- if the input string format is invalid
-
parse
Parse the given string as a 3-tuple and passes the parsed values to the given function. The function output is returned.- Type Parameters:
T
- function return type- Parameters:
str
- the string to be parsedfn
- function that will be passed the parsed tuple values- Returns:
- object returned by
fn
- Throws:
IllegalArgumentException
- if the input string format is invalid
-
readPrefix
Read the configured prefix from the current position in the given string, ignoring any preceding whitespace, and advance the parsing position past the prefix sequence. An exception is thrown if the prefix is not found. Does nothing if the prefix is null.- Parameters:
str
- the string being parsedpos
- the current parsing position- Throws:
IllegalArgumentException
- if the configured prefix is not null and is not found at the current parsing position, ignoring preceding whitespace
-
readTupleValue
Read and return a tuple value from the current position in the given string. An exception is thrown if a valid number is not found. The parsing position is advanced past the parsed number and any trailing separator.- Parameters:
str
- the string being parsedpos
- the current parsing position- Returns:
- the tuple value
- Throws:
IllegalArgumentException
- if the configured prefix is not null and is not found at the current parsing position, ignoring preceding whitespace
-
readSuffix
Read the configured suffix from the current position in the given string, ignoring any preceding whitespace, and advance the parsing position past the suffix sequence. An exception is thrown if the suffix is not found. Does nothing if the suffix is null.- Parameters:
str
- the string being parsedpos
- the current parsing position- Throws:
IllegalArgumentException
- if the configured suffix is not null and is not found at the current parsing position, ignoring preceding whitespace
-
endParse
End a parse operation by ensuring that all non-whitespace characters in the string have been parsed. An exception is thrown if extra content is found.- Parameters:
str
- the string being parsedpos
- the current parsing position- Throws:
IllegalArgumentException
- if extra non-whitespace content is found past the current parsing position
-
consumeWhitespace
Advancepos
past any whitespace characters instr
, starting at the current parse position index.- Parameters:
str
- the input stringpos
- the current parse position
-
matchSequence
Return a boolean indicating whether or not the input stringstr
contains the stringseq
at the given parse index. If the match succeeds, the index ofpos
is moved to the first character after the match. If the match does not succeed, the parse position is left unchanged.- Parameters:
str
- the string to match againstseq
- the sequence to look for instr
pos
- the parse position indicating the index instr
to attempt the match- Returns:
- true if
str
contains exactly the same characters asseq
atpos
; otherwise, false
-
readSequence
Read the string given byseq
from the given position instr
. Throws an IllegalArgumentException if the sequence is not found at that position.- Parameters:
str
- the string to match againstseq
- the sequence to look for instr
pos
- the parse position indicating the index instr
to attempt the match- Throws:
IllegalArgumentException
- ifstr
does not contain the characters fromseq
at positionpos
-
getDefault
Return an instance configured with default values. Tuples in this format are enclosed by parentheses and separated by commas. Ex:"(1.0)" "(1.0, 2.0)" "(1.0, 2.0, 3.0)"
- Returns:
- instance configured with default values
-
parseFailure
Return anIllegalArgumentException
representing a parsing failure.- Parameters:
msg
- the error messagestr
- the string being parsedpos
- the current parse position- Returns:
- an exception signaling a parse failure
-
parseFailure
private static IllegalArgumentException parseFailure(String msg, String str, ParsePosition pos, Throwable cause) Return anIllegalArgumentException
representing a parsing failure.- Parameters:
msg
- the error messagestr
- the string being parsedpos
- the current parse positioncause
- the original cause of the error- Returns:
- an exception signaling a parse failure
-