Class SafeDecimalParser

java.lang.Object
org.jruby.util.SafeDecimalParser
Direct Known Subclasses:
SafeDoubleParser, SafeFloatParser

class SafeDecimalParser extends Object
  • Field Details

    • TWO

      protected static final BigDecimal TWO
      Constant 2
    • LOWER

      protected static final BigDecimal LOWER
      Lower allowed value
    • UPPER

      protected static final BigDecimal UPPER
      Upper allowed value
    • MIDDLE

      protected static final BigDecimal MIDDLE
      The middle of the bad interval - used for rounding bad values
    • UPPER_DOUBLE

      private static final Double UPPER_DOUBLE
      Upper allowed value as Double
    • LOWER_DOUBLE

      private static final Double LOWER_DOUBLE
      Lower allowed value as Double
    • SUSPICIOUS_DIGITS

      private static final String SUSPICIOUS_DIGITS
      Digit sequence to trigger the slow path
      See Also:
  • Constructor Details

    • SafeDecimalParser

      SafeDecimalParser()
  • Method Details

    • isSuspicious

      protected static final boolean isSuspicious(String s)
      Heuristic test if we should look closer at the value
      Parameters:
      s - The non-null input String
      Returns:
      true if the value is suspicious, false otherwise
    • decimalValueOf

      protected static final Double decimalValueOf(String s)
      Safe parsing of a String into a Double
      Parameters:
      s - The input String, can be null
      Returns:
      The Double value
    • decimalValue

      protected static final double decimalValue(Number number)
      Safe way of getting the double value
      prevents BigDecimal from calling Double.parseDouble()
      Parameters:
      number -
      Returns:
      the double value
    • decimalValue

      protected static final double decimalValue(BigDecimal bigDecimal)
      Safe way of getting the double value
      Prevents BigDecimal from calling Double.parseDouble()
      Parameters:
      bigDecimal -
      Returns:
      the double value
    • parseSafely

      private static final Double parseSafely(String s)
      Slow parsing of a suspicious value

      Rounding takes place if the value is inside the bad interval

      Parameters:
      s - The non-null input String
      Returns:
      the double value
    • digits

      private static final String digits(String s)
      Extract the digits from a numeric string
      Parameters:
      s - The non-null String value
      Returns:
      A String containing only the digits
    • isDangerous

      private static final boolean isDangerous(BigDecimal bd)
      Tests if the value is in the dangerous interval
      Parameters:
      bd - The big decimal value
      Returns:
      true if the value is dangerous, false otherwise