Class NFRule


  • final class NFRule
    extends java.lang.Object
    A class representing a single rule in a RuleBasedNumberFormat. A rule inserts its text into the result string and then passes control to its substitutions, which do the same thing.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private long baseValue
      The rule's base value
      private char decimalPoint
      If this is a fraction rule, this is the decimal point from DecimalFormatSymbols to match.
      (package private) static int DEFAULT_RULE
      Special base value used to identify a default rule
      private short exponent
      The rule's exponent (the radix raised to the power of the exponent equals the rule's divisor)
      (package private) RuleBasedNumberFormat formatter
      The RuleBasedNumberFormat that owns this rule
      (package private) static int IMPROPER_FRACTION_RULE
      Special base value used to identify an improper fraction (x.x) rule
      (package private) static int INFINITY_RULE
      Special base value used to identify an infinity rule
      (package private) static int NAN_RULE
      Special base value used to identify a not a number rule
      (package private) static int NEGATIVE_NUMBER_RULE
      Special base value used to identify a negative-number rule
      (package private) static int PROPER_FRACTION_RULE
      Special base value used to identify a proper fraction (0.x) rule
      private int radix
      The rule's radix (the radix to the power of the exponent equals the rule's divisor)
      private static java.lang.String[] RULE_PREFIXES  
      private PluralFormat rulePatternFormat
      The rule's plural format when defined.
      private java.lang.String ruleText
      The rule's rule text.
      private NFSubstitution sub1
      The rule's first substitution (the one with the lower offset into the rule text)
      private NFSubstitution sub2
      The rule's second substitution (the one with the higher offset into the rule text)
      (package private) static java.lang.Long ZERO  
    • Constructor Summary

      Constructors 
      Constructor Description
      NFRule​(RuleBasedNumberFormat formatter, java.lang.String ruleText)
      Nominal constructor for NFRule.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      private boolean allIgnorable​(java.lang.String str)
      Checks to see whether a string consists entirely of ignorable characters.
      void doFormat​(double number, java.lang.StringBuilder toInsertInto, int pos, int recursionCount)
      Formats the number, and inserts the resulting text into toInsertInto.
      void doFormat​(long number, java.lang.StringBuilder toInsertInto, int pos, int recursionCount)
      Formats the number, and inserts the resulting text into toInsertInto.
      java.lang.Number doParse​(java.lang.String text, java.text.ParsePosition parsePosition, boolean isFractionRule, double upperBound, int nonNumericalExecutedRuleMask, int recursionCount)
      Attempts to parse the string with this rule.
      boolean equals​(java.lang.Object that)
      Tests two rules for equality.
      private short expectedExponent()
      This calculates the rule's exponent based on its radix and base value.
      private NFSubstitution extractSubstitution​(NFRuleSet owner, NFRule predecessor)
      Searches the rule's rule text for the first substitution token, creates a substitution based on it, and removes the token from the rule's rule text.
      private void extractSubstitutions​(NFRuleSet owner, java.lang.String ruleText, NFRule predecessor)
      Searches the rule's rule text for the substitution tokens, creates the substitutions, and removes the substitution tokens from the rule's rule text.
      private int[] findText​(java.lang.String str, java.lang.String key, PluralFormat pluralFormatKey, int startingAt)
      Searches a string for another string.
      long getBaseValue()
      Returns the rule's base value
      char getDecimalPoint()
      Returns the rule's base value
      long getDivisor()
      Returns the rule's divisor (the value that controls the behavior of its substitutions)
      int hashCode()  
      (package private) boolean hasModulusSubstitution()
      Internal function used by the rounding code in MultiplierSubstitution.
      private static int indexOfAnyRulePrefix​(java.lang.String ruleText)
      Searches the rule's rule text for any of the specified strings.
      static void makeRules​(java.lang.String description, NFRuleSet owner, NFRule predecessor, RuleBasedNumberFormat ownersOwner, java.util.List<NFRule> returnList)
      Creates one or more rules based on the description passed in.
      private java.lang.Number matchToDelimiter​(java.lang.String text, int startPos, double baseVal, java.lang.String delimiter, PluralFormat pluralFormatDelimiter, java.text.ParsePosition pp, NFSubstitution sub, double upperBound, int nonNumericalExecutedRuleMask, int recursionCount)
      Used by parse() to match a substitution and any following text.
      private java.lang.String parseRuleDescriptor​(java.lang.String description)
      This function parses the rule's rule descriptor (i.e., the base value and/or other tokens that precede the rule's rule text in the description) and sets the rule's base value, radix, and exponent according to the descriptor.
      (package private) static long power​(long base, short exponent)
      This is an equivalent to Math.pow that accurately works on 64-bit numbers
      private int prefixLength​(java.lang.String str, java.lang.String prefix)
      Used by stripPrefix() to match characters.
      (package private) void setBaseValue​(long newBaseValue)
      Sets the rule's base value, and causes the radix and exponent to be recalculated.
      void setDecimalFormatSymbols​(DecimalFormatSymbols newSymbols)  
      boolean shouldRollBack​(long number)
      Used by the owning rule set to determine whether to invoke the rollback rule (i.e., whether this rule or the one that precedes it in the rule set's list should be used to format the number)
      private java.lang.String stripPrefix​(java.lang.String text, java.lang.String prefix, java.text.ParsePosition pp)
      This function is used by parse() to match the text being parsed against a possible prefix string.
      java.lang.String toString()
      Returns a textual representation of the rule.
      • Methods inherited from class java.lang.Object

        clone, finalize, getClass, notify, notifyAll, wait, wait, wait
    • Field Detail

      • NEGATIVE_NUMBER_RULE

        static final int NEGATIVE_NUMBER_RULE
        Special base value used to identify a negative-number rule
        See Also:
        Constant Field Values
      • IMPROPER_FRACTION_RULE

        static final int IMPROPER_FRACTION_RULE
        Special base value used to identify an improper fraction (x.x) rule
        See Also:
        Constant Field Values
      • PROPER_FRACTION_RULE

        static final int PROPER_FRACTION_RULE
        Special base value used to identify a proper fraction (0.x) rule
        See Also:
        Constant Field Values
      • DEFAULT_RULE

        static final int DEFAULT_RULE
        Special base value used to identify a default rule
        See Also:
        Constant Field Values
      • INFINITY_RULE

        static final int INFINITY_RULE
        Special base value used to identify an infinity rule
        See Also:
        Constant Field Values
      • NAN_RULE

        static final int NAN_RULE
        Special base value used to identify a not a number rule
        See Also:
        Constant Field Values
      • ZERO

        static final java.lang.Long ZERO
      • baseValue

        private long baseValue
        The rule's base value
      • radix

        private int radix
        The rule's radix (the radix to the power of the exponent equals the rule's divisor)
      • exponent

        private short exponent
        The rule's exponent (the radix raised to the power of the exponent equals the rule's divisor)
      • decimalPoint

        private char decimalPoint
        If this is a fraction rule, this is the decimal point from DecimalFormatSymbols to match.
      • ruleText

        private java.lang.String ruleText
        The rule's rule text. When formatting a number, the rule's text is inserted into the result string, and then the text from any substitutions is inserted into the result string
      • rulePatternFormat

        private PluralFormat rulePatternFormat
        The rule's plural format when defined. This is not a substitution because it only works on the current baseValue. It's normally not used due to the overhead.
      • sub1

        private NFSubstitution sub1
        The rule's first substitution (the one with the lower offset into the rule text)
      • sub2

        private NFSubstitution sub2
        The rule's second substitution (the one with the higher offset into the rule text)
      • RULE_PREFIXES

        private static final java.lang.String[] RULE_PREFIXES
    • Constructor Detail

      • NFRule

        public NFRule​(RuleBasedNumberFormat formatter,
                      java.lang.String ruleText)
        Nominal constructor for NFRule. Most of the work of constructing an NFRule is actually performed by makeRules().
    • Method Detail

      • makeRules

        public static void makeRules​(java.lang.String description,
                                     NFRuleSet owner,
                                     NFRule predecessor,
                                     RuleBasedNumberFormat ownersOwner,
                                     java.util.List<NFRule> returnList)
        Creates one or more rules based on the description passed in.
        Parameters:
        description - The description of the rule(s).
        owner - The rule set containing the new rule(s).
        predecessor - The rule that precedes the new one(s) in "owner"'s rule list
        ownersOwner - The RuleBasedNumberFormat that owns the rule set that owns the new rule(s)
        returnList - One or more instances of NFRule are added and returned here
      • parseRuleDescriptor

        private java.lang.String parseRuleDescriptor​(java.lang.String description)
        This function parses the rule's rule descriptor (i.e., the base value and/or other tokens that precede the rule's rule text in the description) and sets the rule's base value, radix, and exponent according to the descriptor. (If the description doesn't include a rule descriptor, then this function sets everything to default values and the rule set sets the rule's real base value).
        Parameters:
        description - The rule's description
        Returns:
        If "description" included a rule descriptor, this is "description" with the descriptor and any trailing whitespace stripped off. Otherwise; it's "descriptor" unchanged.
      • extractSubstitutions

        private void extractSubstitutions​(NFRuleSet owner,
                                          java.lang.String ruleText,
                                          NFRule predecessor)
        Searches the rule's rule text for the substitution tokens, creates the substitutions, and removes the substitution tokens from the rule's rule text.
        Parameters:
        owner - The rule set containing this rule
        predecessor - The rule preceding this one in "owners" rule list
        ruleText - The rule text
      • extractSubstitution

        private NFSubstitution extractSubstitution​(NFRuleSet owner,
                                                   NFRule predecessor)
        Searches the rule's rule text for the first substitution token, creates a substitution based on it, and removes the token from the rule's rule text.
        Parameters:
        owner - The rule set containing this rule
        predecessor - The rule preceding this one in the rule set's rule list
        Returns:
        The newly-created substitution. This is never null; if the rule text doesn't contain any substitution tokens, this will be a NullSubstitution.
      • setBaseValue

        final void setBaseValue​(long newBaseValue)
        Sets the rule's base value, and causes the radix and exponent to be recalculated. This is used during construction when we don't know the rule's base value until after it's been constructed. It should not be used at any other time.
        Parameters:
        newBaseValue - The new base value for the rule.
      • expectedExponent

        private short expectedExponent()
        This calculates the rule's exponent based on its radix and base value. This will be the highest power the radix can be raised to and still produce a result less than or equal to the base value.
      • indexOfAnyRulePrefix

        private static int indexOfAnyRulePrefix​(java.lang.String ruleText)
        Searches the rule's rule text for any of the specified strings.
        Returns:
        The index of the first match in the rule's rule text (i.e., the first substring in the rule's rule text that matches _any_ of the strings in "strings"). If none of the strings in "strings" is found in the rule's rule text, returns -1.
      • equals

        public boolean equals​(java.lang.Object that)
        Tests two rules for equality.
        Overrides:
        equals in class java.lang.Object
        Parameters:
        that - The rule to compare this one against
        Returns:
        True if the two rules are functionally equivalent
      • hashCode

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

        public java.lang.String toString()
        Returns a textual representation of the rule. This won't necessarily be the same as the description that this rule was created with, but it will produce the same result.
        Overrides:
        toString in class java.lang.Object
        Returns:
        A textual description of the rule
      • getDecimalPoint

        public final char getDecimalPoint()
        Returns the rule's base value
        Returns:
        The rule's base value
      • getBaseValue

        public final long getBaseValue()
        Returns the rule's base value
        Returns:
        The rule's base value
      • getDivisor

        public long getDivisor()
        Returns the rule's divisor (the value that controls the behavior of its substitutions)
        Returns:
        The rule's divisor
      • hasModulusSubstitution

        boolean hasModulusSubstitution()
        Internal function used by the rounding code in MultiplierSubstitution.
      • doFormat

        public void doFormat​(long number,
                             java.lang.StringBuilder toInsertInto,
                             int pos,
                             int recursionCount)
        Formats the number, and inserts the resulting text into toInsertInto.
        Parameters:
        number - The number being formatted
        toInsertInto - The string where the resultant text should be inserted
        pos - The position in toInsertInto where the resultant text should be inserted
      • doFormat

        public void doFormat​(double number,
                             java.lang.StringBuilder toInsertInto,
                             int pos,
                             int recursionCount)
        Formats the number, and inserts the resulting text into toInsertInto.
        Parameters:
        number - The number being formatted
        toInsertInto - The string where the resultant text should be inserted
        pos - The position in toInsertInto where the resultant text should be inserted
      • power

        static long power​(long base,
                          short exponent)
        This is an equivalent to Math.pow that accurately works on 64-bit numbers
        Parameters:
        base - The base
        exponent - The exponent
        Returns:
        radix ** exponent
        See Also:
        Math.pow(double, double)
      • shouldRollBack

        public boolean shouldRollBack​(long number)
        Used by the owning rule set to determine whether to invoke the rollback rule (i.e., whether this rule or the one that precedes it in the rule set's list should be used to format the number)
        Parameters:
        number - The number being formatted
        Returns:
        True if the rule set should use the rule that precedes this one in its list; false if it should use this rule
      • doParse

        public java.lang.Number doParse​(java.lang.String text,
                                        java.text.ParsePosition parsePosition,
                                        boolean isFractionRule,
                                        double upperBound,
                                        int nonNumericalExecutedRuleMask,
                                        int recursionCount)
        Attempts to parse the string with this rule.
        Parameters:
        text - The string being parsed
        parsePosition - On entry, the value is ignored and assumed to be 0. On exit, this has been updated with the position of the first character not consumed by matching the text against this rule (if this rule doesn't match the text at all, the parse position if left unchanged (presumably at 0) and the function returns Long.valueOf(0)).
        isFractionRule - True if this rule is contained within a fraction rule set. This is only used if the rule has no substitutions.
        Returns:
        If this rule matched the text, this is the rule's base value combined appropriately with the results of parsing the substitutions. If nothing matched, this is Long.valueOf(0) and the parse position is left unchanged. The result will be an instance of Long if the result is an integer and Double otherwise. The result is never null.
      • stripPrefix

        private java.lang.String stripPrefix​(java.lang.String text,
                                             java.lang.String prefix,
                                             java.text.ParsePosition pp)
        This function is used by parse() to match the text being parsed against a possible prefix string. This function matches characters from the beginning of the string being parsed to characters from the prospective prefix. If they match, pp is updated to the first character not matched, and the result is the unparsed part of the string. If they don't match, the whole string is returned, and pp is left unchanged.
        Parameters:
        text - The string being parsed
        prefix - The text to match against
        pp - On entry, ignored and assumed to be 0. On exit, points to the first unmatched character (assuming the whole prefix matched), or is unchanged (if the whole prefix didn't match).
        Returns:
        If things match, this is the unparsed part of "text"; if they didn't match, this is "text".
      • matchToDelimiter

        private java.lang.Number matchToDelimiter​(java.lang.String text,
                                                  int startPos,
                                                  double baseVal,
                                                  java.lang.String delimiter,
                                                  PluralFormat pluralFormatDelimiter,
                                                  java.text.ParsePosition pp,
                                                  NFSubstitution sub,
                                                  double upperBound,
                                                  int nonNumericalExecutedRuleMask,
                                                  int recursionCount)
        Used by parse() to match a substitution and any following text. "text" is searched for instances of "delimiter". For each instance of delimiter, the intervening text is tested to see whether it matches the substitution. The longest match wins.
        Parameters:
        text - The string being parsed
        startPos - The position in "text" where we should start looking for "delimiter".
        baseVal - A partial parse result (often the rule's base value), which is combined with the result from matching the substitution
        delimiter - The string to search "text" for.
        pp - Ignored and presumed to be 0 on entry. If there's a match, on exit this will point to the first unmatched character.
        sub - If we find "delimiter" in "text", this substitution is used to match the text between the beginning of the string and the position of "delimiter." (If "delimiter" is the empty string, then this function just matches against this substitution and updates everything accordingly.)
        upperBound - When matching the substitution, it will only consider rules with base values lower than this value.
        Returns:
        If there's a match, this is the result of composing baseValue with the result of matching the substitution. Otherwise, this is Long.valueOf(0). It's never null. If the result is an integer, this will be an instance of Long; otherwise, it's an instance of Double.
      • prefixLength

        private int prefixLength​(java.lang.String str,
                                 java.lang.String prefix)
        Used by stripPrefix() to match characters. If lenient parse mode is off, this just calls startsWith(). If lenient parse mode is on, this function uses CollationElementIterators to match characters in the strings (only primary-order differences are significant in determining whether there's a match).
        Parameters:
        str - The string being tested
        prefix - The text we're hoping to see at the beginning of "str"
        Returns:
        If "prefix" is found at the beginning of "str", this is the number of characters in "str" that were matched (this isn't necessarily the same as the length of "prefix" when matching text with a collator). If there's no match, this is 0.
      • findText

        private int[] findText​(java.lang.String str,
                               java.lang.String key,
                               PluralFormat pluralFormatKey,
                               int startingAt)
        Searches a string for another string. If lenient parsing is off, this just calls indexOf(). If lenient parsing is on, this function uses CollationElementIterator to match characters, and only primary-order differences are significant in determining whether there's a match.
        Parameters:
        str - The string to search
        key - The string to search "str" for
        startingAt - The index into "str" where the search is to begin
        Returns:
        A two-element array of ints. Element 0 is the position of the match, or -1 if there was no match. Element 1 is the number of characters in "str" that matched (which isn't necessarily the same as the length of "key")
      • allIgnorable

        private boolean allIgnorable​(java.lang.String str)
        Checks to see whether a string consists entirely of ignorable characters.
        Parameters:
        str - The string to test.
        Returns:
        true if the string is empty of consists entirely of characters that the number formatter's collator says are ignorable at the primary-order level. false otherwise.
      • setDecimalFormatSymbols

        public void setDecimalFormatSymbols​(DecimalFormatSymbols newSymbols)