Class SimpleTupleFormat
- java.lang.Object
-
- org.apache.commons.geometry.core.internal.SimpleTupleFormat
-
public class SimpleTupleFormat extends java.lang.Object
Class for performing simple formatting and parsing of real number tuples.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private static class
SimpleTupleFormat.TupleParseException
Exception class for errors occurring during tuple parsing.
-
Field Summary
Fields Modifier and Type Field Description private static SimpleTupleFormat
DEFAULT_INSTANCE
Static instance configured with default values.private static java.lang.String
DEFAULT_SEPARATOR
Default value separator string.private java.lang.String
prefix
String used to signal the start of a tuple; may be null.private java.lang.String
separator
String separating tuple values.private static java.lang.String
SPACE
Space character.private java.lang.String
suffix
String used to signal the end of a tuple; may be null.
-
Constructor Summary
Constructors Modifier Constructor Description SimpleTupleFormat(java.lang.String prefix, java.lang.String suffix)
Constructs a new instance with the default string separator (a comma) and the given prefix and suffix.protected
SimpleTupleFormat(java.lang.String separator, java.lang.String prefix, java.lang.String suffix)
Simple constructor.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description private void
consumeWhitespace(java.lang.String str, java.text.ParsePosition pos)
Advancepos
past any whitespace characters instr
, starting at the current parse position index.private void
endParse(java.lang.String str, java.text.ParsePosition pos)
End a parse operation by ensuring that all non-whitespace characters in the string have been parsed.java.lang.String
format(double a)
Return a tuple string with the given value.java.lang.String
format(double a1, double a2)
Return a tuple string with the given values.java.lang.String
format(double a1, double a2, double a3)
Return a tuple string with the given values.java.lang.String
format(double a1, double a2, double a3, double a4)
Return a tuple string with the given values.static SimpleTupleFormat
getDefault()
Return an instance configured with default values.java.lang.String
getPrefix()
Return the string used to signal the start of a tuple.java.lang.String
getSeparator()
Return the string used to separate tuple values.java.lang.String
getSuffix()
Returns the string used to signal the end of a tuple.private boolean
matchSequence(java.lang.String str, java.lang.String seq, java.text.ParsePosition pos)
Return a boolean indicating whether or not the input stringstr
contains the stringseq
at the given parse index.<T> T
parse(java.lang.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(java.lang.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(java.lang.String str, DoubleFunction3N<T> fn)
Parse the given string as a 3-tuple and passes the parsed values to the given function.private static java.lang.IllegalArgumentException
parseFailure(java.lang.String msg, java.lang.String str, java.text.ParsePosition pos)
Return anIllegalArgumentException
representing a parsing failure.private static java.lang.IllegalArgumentException
parseFailure(java.lang.String msg, java.lang.String str, java.text.ParsePosition pos, java.lang.Throwable cause)
Return anIllegalArgumentException
representing a parsing failure.private void
readPrefix(java.lang.String str, java.text.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(java.lang.String str, java.lang.String seq, java.text.ParsePosition pos)
Read the string given byseq
from the given position instr
.private void
readSuffix(java.lang.String str, java.text.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(java.lang.String str, java.text.ParsePosition pos)
Read and return a tuple value from the current position in the given string.
-
-
-
Field Detail
-
DEFAULT_SEPARATOR
private static final java.lang.String DEFAULT_SEPARATOR
Default value separator string.- See Also:
- Constant Field Values
-
SPACE
private static final java.lang.String SPACE
Space character.- See Also:
- Constant Field Values
-
DEFAULT_INSTANCE
private static final SimpleTupleFormat DEFAULT_INSTANCE
Static instance configured with default values. Tuples in this format are enclosed by parentheses and separated by commas.
-
separator
private final java.lang.String separator
String separating tuple values.
-
prefix
private final java.lang.String prefix
String used to signal the start of a tuple; may be null.
-
suffix
private final java.lang.String suffix
String used to signal the end of a tuple; may be null.
-
-
Constructor Detail
-
SimpleTupleFormat
public SimpleTupleFormat(java.lang.String prefix, java.lang.String suffix)
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
protected SimpleTupleFormat(java.lang.String separator, java.lang.String prefix, java.lang.String suffix)
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 Detail
-
getSeparator
public java.lang.String getSeparator()
Return the string used to separate tuple values.- Returns:
- the value separator string
-
getPrefix
public java.lang.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
public java.lang.String 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
public java.lang.String format(double a)
Return a tuple string with the given value.- Parameters:
a
- value- Returns:
- 1-tuple string
-
format
public java.lang.String format(double a1, double a2)
Return a tuple string with the given values.- Parameters:
a1
- first valuea2
- second value- Returns:
- 2-tuple string
-
format
public java.lang.String format(double a1, double a2, double a3)
Return a tuple string with the given values.- Parameters:
a1
- first valuea2
- second valuea3
- third value- Returns:
- 3-tuple string
-
format
public java.lang.String format(double a1, double a2, double a3, double a4)
Return a tuple string with the given values.- Parameters:
a1
- first valuea2
- second valuea3
- third valuea4
- fourth value- Returns:
- 4-tuple string
-
parse
public <T> T parse(java.lang.String str, DoubleFunction1N<T> fn)
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:
java.lang.IllegalArgumentException
- if the input string format is invalid
-
parse
public <T> T parse(java.lang.String str, DoubleFunction2N<T> fn)
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:
java.lang.IllegalArgumentException
- if the input string format is invalid
-
parse
public <T> T parse(java.lang.String str, DoubleFunction3N<T> fn)
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:
java.lang.IllegalArgumentException
- if the input string format is invalid
-
readPrefix
private void readPrefix(java.lang.String str, java.text.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. 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:
java.lang.IllegalArgumentException
- if the configured prefix is not null and is not found at the current parsing position, ignoring preceding whitespace
-
readTupleValue
private double readTupleValue(java.lang.String str, java.text.ParsePosition pos)
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:
java.lang.IllegalArgumentException
- if the configured prefix is not null and is not found at the current parsing position, ignoring preceding whitespace
-
readSuffix
private void readSuffix(java.lang.String str, java.text.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. 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:
java.lang.IllegalArgumentException
- if the configured suffix is not null and is not found at the current parsing position, ignoring preceding whitespace
-
endParse
private void endParse(java.lang.String str, java.text.ParsePosition pos)
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:
java.lang.IllegalArgumentException
- if extra non-whitespace content is found past the current parsing position
-
consumeWhitespace
private void consumeWhitespace(java.lang.String str, java.text.ParsePosition pos)
Advancepos
past any whitespace characters instr
, starting at the current parse position index.- Parameters:
str
- the input stringpos
- the current parse position
-
matchSequence
private boolean matchSequence(java.lang.String str, java.lang.String seq, java.text.ParsePosition pos)
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
private void readSequence(java.lang.String str, java.lang.String seq, java.text.ParsePosition pos)
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:
java.lang.IllegalArgumentException
- ifstr
does not contain the characters fromseq
at positionpos
-
getDefault
public static SimpleTupleFormat 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
private static java.lang.IllegalArgumentException parseFailure(java.lang.String msg, java.lang.String str, java.text.ParsePosition pos)
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 java.lang.IllegalArgumentException parseFailure(java.lang.String msg, java.lang.String str, java.text.ParsePosition pos, java.lang.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
-
-