Class NFRuleSet

java.lang.Object
com.ibm.icu.text.NFRuleSet

final class NFRuleSet extends Object
A collection of rules used by a RuleBasedNumberFormat to format and parse numbers. It is the responsibility of a RuleSet to select an appropriate rule for formatting a particular number and dispatch control to it, and to arbitrate between different rules when parsing a number.
  • Field Details

    • name

      private final String name
      The rule set's name
    • rules

      private NFRule[] rules
      The rule set's regular rules
    • nonNumericalRules

      final NFRule[] nonNumericalRules
      The rule set's non-numerical rules like negative, fractions, infinity and NaN
    • fractionRules

      LinkedList<NFRule> fractionRules
      These are a pile of fraction rules in declared order. They may have alternate ways to represent fractions.
    • NEGATIVE_RULE_INDEX

      static final int NEGATIVE_RULE_INDEX
      -x
      See Also:
    • IMPROPER_FRACTION_RULE_INDEX

      static final int IMPROPER_FRACTION_RULE_INDEX
      x.x
      See Also:
    • PROPER_FRACTION_RULE_INDEX

      static final int PROPER_FRACTION_RULE_INDEX
      0.x
      See Also:
    • DEFAULT_RULE_INDEX

      static final int DEFAULT_RULE_INDEX
      x.0
      See Also:
    • INFINITY_RULE_INDEX

      static final int INFINITY_RULE_INDEX
      Inf
      See Also:
    • NAN_RULE_INDEX

      static final int NAN_RULE_INDEX
      NaN
      See Also:
    • owner

      final RuleBasedNumberFormat owner
      The RuleBasedNumberFormat that owns this rule
    • isFractionRuleSet

      private boolean isFractionRuleSet
      True if the rule set is a fraction rule set. A fraction rule set is a rule set that is used to format the fractional part of a number. It is called from a >> substitution in another rule set's fraction rule, and is only called upon to format values between 0 and 1. A fraction rule set has different rule-selection behavior than a regular rule set.
    • isParseable

      private final boolean isParseable
      True if the rule set is parseable.
    • RECURSION_LIMIT

      private static final int RECURSION_LIMIT
      Limit of recursion. It's about a 64 bit number formatted in base 2.
      See Also:
  • Constructor Details

    • NFRuleSet

      public NFRuleSet(RuleBasedNumberFormat owner, String[] descriptions, int index) throws IllegalArgumentException
      Constructs a rule set.
      Parameters:
      owner - The formatter that owns this rule set
      descriptions - An array of Strings representing rule set descriptions. On exit, this rule set's entry in the array will have been stripped of its rule set name and any trailing whitespace.
      index - The index into "descriptions" of the description for the rule to be constructed
      Throws:
      IllegalArgumentException
  • Method Details

    • parseRules

      public void parseRules(String description)
      Construct the subordinate data structures used by this object. This function is called by the RuleBasedNumberFormat constructor after all the rule sets have been created to actually parse the description and build rules from it. Since any rule set can refer to any other rule set, we have to have created all of them before we can create anything else.
      Parameters:
      description - The textual description of this rule set
    • setNonNumericalRule

      void setNonNumericalRule(NFRule rule)
      Set one of the non-numerical rules.
      Parameters:
      rule - The rule to set.
    • setBestFractionRule

      private void setBestFractionRule(int originalIndex, NFRule newRule, boolean rememberRule)
      Determine the best fraction rule to use. Rules matching the decimal point from DecimalFormatSymbols become the main set of rules to use.
      Parameters:
      originalIndex - The index into nonNumericalRules
      newRule - The new rule to consider
      rememberRule - Should the new rule be added to fractionRules.
    • makeIntoFractionRuleSet

      public void makeIntoFractionRuleSet()
      Flags this rule set as a fraction rule set. This function is called during the construction process once we know this rule set is a fraction rule set. We don't know a rule set is a fraction rule set until we see it used somewhere. This function is not ad must not be called at any time other than during construction of a RuleBasedNumberFormat.
    • equals

      public boolean equals(Object that)
      Compares two rule sets for equality.
      Overrides:
      equals in class Object
      Parameters:
      that - The other rule set
      Returns:
      true if the two rule sets are functionally equivalent.
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • toString

      public String toString()
      Builds a textual representation of a rule set.
      Overrides:
      toString in class Object
      Returns:
      A textual representation of a rule set. This won't necessarily be the same description that the rule set was constructed with, but it will produce the same results.
    • isFractionSet

      public boolean isFractionSet()
      Says whether this rule set is a fraction rule set.
      Returns:
      true if this rule is a fraction rule set; false if it isn't
    • getName

      public String getName()
      Returns the rule set's name
      Returns:
      The rule set's name
    • isPublic

      public boolean isPublic()
      Return true if the rule set is public.
      Returns:
      true if the rule set is public
    • isParseable

      public boolean isParseable()
      Return true if the rule set can be used for parsing.
      Returns:
      true if the rule set can be used for parsing.
    • format

      public void format(long number, StringBuilder toInsertInto, int pos, int recursionCount)
      Formats a long. Selects an appropriate rule and dispatches control to it.
      Parameters:
      number - The number being formatted
      toInsertInto - The string where the result is to be placed
      pos - The position in toInsertInto where the result of this operation is to be inserted
    • format

      public void format(double number, StringBuilder toInsertInto, int pos, int recursionCount)
      Formats a double. Selects an appropriate rule and dispatches control to it.
      Parameters:
      number - The number being formatted
      toInsertInto - The string where the result is to be placed
      pos - The position in toInsertInto where the result of this operation is to be inserted
    • findRule

      NFRule findRule(double number)
      Selects an appropriate rule for formatting the number.
      Parameters:
      number - The number being formatted.
      Returns:
      The rule that should be used to format it
    • findNormalRule

      private NFRule findNormalRule(long number)
      If the value passed to findRule() is a positive integer, findRule() uses this function to select the appropriate rule. The result will generally be the rule with the highest base value less than or equal to the number. There is one exception to this: If that rule has two substitutions and a base value that is not an even multiple of its divisor, and the number itself IS an even multiple of the rule's divisor, then the result will be the rule that preceded the original result in the rule list. (This behavior is known as the "rollback rule", and is used to handle optional text: a rule with optional text is represented internally as two rules, and the rollback rule selects appropriate between them. This avoids things like "two hundred zero".)
      Parameters:
      number - The number being formatted
      Returns:
      The rule to use to format this number
    • findFractionRuleSetRule

      private NFRule findFractionRuleSetRule(double number)
      If this rule is a fraction rule set, this function is used by findRule() to select the most appropriate rule for formatting the number. Basically, the base value of each rule in the rule set is treated as the denominator of a fraction. Whichever denominator can produce the fraction closest in value to the number passed in is the result. If there's a tie, the earlier one in the list wins. (If there are two rules in a row with the same base value, the first one is used when the numerator of the fraction would be 1, and the second rule is used the rest of the time.
      Parameters:
      number - The number being formatted (which will always be a number between 0 and 1)
      Returns:
      The rule to use to format this number
    • lcm

      private static long lcm(long x, long y)
      Calculates the least common multiple of x and y.
    • parse

      public Number parse(String text, ParsePosition parsePosition, double upperBound, int nonNumericalExecutedRuleMask)
      Parses a string. Matches the string to be parsed against each of its rules (with a base value less than upperBound) and returns the value produced by the rule that matched the most characters in the source string.
      Parameters:
      text - The string to parse
      parsePosition - The initial position is ignored and assumed to be 0. On exit, this object has been updated to point to the first character position this rule set didn't consume.
      upperBound - Limits the rules that can be allowed to match. Only rules whose base values are strictly less than upperBound are considered.
      Returns:
      The numerical result of parsing this string. This will be the matching rule's base value, composed appropriately with the results of matching any of its substitutions. The object will be an instance of Long if it's an integral value; otherwise, it will be an instance of Double. This function always returns a valid object: If nothing matched the input string at all, this function returns Long.valueOf(0), and the parse position is left unchanged.
    • setDecimalFormatSymbols

      public void setDecimalFormatSymbols(DecimalFormatSymbols newSymbols)