Class SimpleTupleFormat

java.lang.Object
org.apache.commons.geometry.core.internal.SimpleTupleFormat

public class SimpleTupleFormat extends Object
Class for performing simple formatting and parsing of real number tuples.
  • Field Details

    • DEFAULT_SEPARATOR

      private static final String DEFAULT_SEPARATOR
      Default value separator string.
      See Also:
    • SPACE

      private static final String SPACE
      Space character.
      See Also:
    • 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 String separator
      String separating tuple values.
    • prefix

      private final String prefix
      String used to signal the start of a tuple; may be null.
    • suffix

      private final String suffix
      String used to signal the end of a tuple; may be null.
  • Constructor Details

    • SimpleTupleFormat

      public SimpleTupleFormat(String prefix, 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 tuple
      suffix - String used to signal the end of a tuple; if null, no string is expected at the end of the tuple
    • SimpleTupleFormat

      protected SimpleTupleFormat(String separator, String prefix, 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 tuple
      suffix - String used to signal the end of a tuple; if null, no string is expected at the end of the tuple
  • Method Details

    • getSeparator

      public String getSeparator()
      Return the string used to separate tuple values.
      Returns:
      the value separator string
    • getPrefix

      public 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 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 String format(double a)
      Return a tuple string with the given value.
      Parameters:
      a - value
      Returns:
      1-tuple string
    • format

      public String format(double a1, double a2)
      Return a tuple string with the given values.
      Parameters:
      a1 - first value
      a2 - second value
      Returns:
      2-tuple string
    • format

      public String format(double a1, double a2, double a3)
      Return a tuple string with the given values.
      Parameters:
      a1 - first value
      a2 - second value
      a3 - third value
      Returns:
      3-tuple string
    • format

      public String format(double a1, double a2, double a3, double a4)
      Return a tuple string with the given values.
      Parameters:
      a1 - first value
      a2 - second value
      a3 - third value
      a4 - fourth value
      Returns:
      4-tuple string
    • parse

      public <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. The function output is returned.
      Type Parameters:
      T - function return type
      Parameters:
      str - the string to be parsed
      fn - function that will be passed the parsed tuple values
      Returns:
      object returned by fn
      Throws:
      IllegalArgumentException - if the input string format is invalid
    • parse

      public <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. The function output is returned.
      Type Parameters:
      T - function return type
      Parameters:
      str - the string to be parsed
      fn - function that will be passed the parsed tuple values
      Returns:
      object returned by fn
      Throws:
      IllegalArgumentException - if the input string format is invalid
    • parse

      public <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. The function output is returned.
      Type Parameters:
      T - function return type
      Parameters:
      str - the string to be parsed
      fn - function that will be passed the parsed tuple values
      Returns:
      object returned by fn
      Throws:
      IllegalArgumentException - if the input string format is invalid
    • readPrefix

      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. An exception is thrown if the prefix is not found. Does nothing if the prefix is null.
      Parameters:
      str - the string being parsed
      pos - 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

      private double readTupleValue(String str, 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 parsed
      pos - 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

      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. An exception is thrown if the suffix is not found. Does nothing if the suffix is null.
      Parameters:
      str - the string being parsed
      pos - 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

      private void endParse(String str, 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 parsed
      pos - the current parsing position
      Throws:
      IllegalArgumentException - if extra non-whitespace content is found past the current parsing position
    • consumeWhitespace

      private void consumeWhitespace(String str, ParsePosition pos)
      Advance pos past any whitespace characters in str, starting at the current parse position index.
      Parameters:
      str - the input string
      pos - the current parse position
    • matchSequence

      private boolean matchSequence(String str, String seq, ParsePosition pos)
      Return a boolean indicating whether or not the input string str contains the string seq at the given parse index. If the match succeeds, the index of pos 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 against
      seq - the sequence to look for in str
      pos - the parse position indicating the index in str to attempt the match
      Returns:
      true if str contains exactly the same characters as seq at pos; otherwise, false
    • readSequence

      private void readSequence(String str, String seq, ParsePosition pos)
      Read the string given by seq from the given position in str. Throws an IllegalArgumentException if the sequence is not found at that position.
      Parameters:
      str - the string to match against
      seq - the sequence to look for in str
      pos - the parse position indicating the index in str to attempt the match
      Throws:
      IllegalArgumentException - if str does not contain the characters from seq at position pos
    • 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 IllegalArgumentException parseFailure(String msg, String str, ParsePosition pos)
      Return an IllegalArgumentException representing a parsing failure.
      Parameters:
      msg - the error message
      str - the string being parsed
      pos - 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 an IllegalArgumentException representing a parsing failure.
      Parameters:
      msg - the error message
      str - the string being parsed
      pos - the current parse position
      cause - the original cause of the error
      Returns:
      an exception signaling a parse failure