Class NFSubstitution

    • Field Summary

      Fields 
      Modifier and Type Field Description
      private static long MAX_INT64_IN_DOUBLE  
      (package private) DecimalFormat numberFormat
      The DecimalFormat this substitution uses to format its result, or null.
      (package private) int pos
      The substitution's position in the rule text of the rule that owns it
      (package private) NFRuleSet ruleSet
      The rule set this substitution uses to format its result, or null.
    • Constructor Summary

      Constructors 
      Constructor Description
      NFSubstitution​(int pos, NFRuleSet ruleSet, java.lang.String description)
      Base constructor for substitutions.
    • Method Summary

      All Methods Static Methods Instance Methods Abstract Methods Concrete Methods 
      Modifier and Type Method Description
      abstract double calcUpperBound​(double oldUpperBound)
      Calculates an upper bound when searching for a rule that matches this substitution.
      abstract double composeRuleValue​(double newRuleValue, double oldRuleValue)
      Derives a new value from the two values passed in.
      java.lang.Number doParse​(java.lang.String text, java.text.ParsePosition parsePosition, double baseValue, double upperBound, boolean lenientParse, int nonNumericalExecutedRuleMask)
      Parses a string using the rule set or DecimalFormat belonging to this substitution.
      void doSubstitution​(double number, java.lang.StringBuilder toInsertInto, int position, int recursionCount)
      Performs a mathematical operation on the number, formats it using either ruleSet or decimalFormat, and inserts the result into toInsertInto.
      void doSubstitution​(long number, java.lang.StringBuilder toInsertInto, int position, int recursionCount)
      Performs a mathematical operation on the number, formats it using either ruleSet or decimalFormat, and inserts the result into toInsertInto.
      boolean equals​(java.lang.Object that)
      Compares two substitutions for equality
      int getPos()
      Returns the substitution's position in the rule that owns it.
      int hashCode()  
      boolean isModulusSubstitution()
      Returns true if this is a modulus substitution.
      static NFSubstitution makeSubstitution​(int pos, NFRule rule, NFRule rulePredecessor, NFRuleSet ruleSet, RuleBasedNumberFormat formatter, java.lang.String description)
      Parses the description, creates the right kind of substitution, and initializes it based on the description.
      void setDecimalFormatSymbols​(DecimalFormatSymbols newSymbols)  
      void setDivisor​(int radix, short exponent)
      Set's the substitution's divisor.
      (package private) abstract char tokenChar()
      Returns the character used in the textual representation of substitutions of this type.
      java.lang.String toString()
      Returns a textual description of the substitution
      abstract double transformNumber​(double number)
      Subclasses override this function to perform some kind of mathematical operation on the number.
      abstract long transformNumber​(long number)
      Subclasses override this function to perform some kind of mathematical operation on the number.
      • Methods inherited from class java.lang.Object

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

      • pos

        final int pos
        The substitution's position in the rule text of the rule that owns it
      • ruleSet

        final NFRuleSet ruleSet
        The rule set this substitution uses to format its result, or null. (Either this or numberFormat has to be non-null.)
      • numberFormat

        final DecimalFormat numberFormat
        The DecimalFormat this substitution uses to format its result, or null. (Either this or ruleSet has to be non-null.)
    • Constructor Detail

      • NFSubstitution

        NFSubstitution​(int pos,
                       NFRuleSet ruleSet,
                       java.lang.String description)
        Base constructor for substitutions. This constructor sets up the fields which are common to all substitutions.
        Parameters:
        pos - The substitution's position in the owning rule's rule text
        ruleSet - The rule set that owns this substitution
        description - The substitution descriptor (i.e., the text inside the token characters)
    • Method Detail

      • makeSubstitution

        public static NFSubstitution makeSubstitution​(int pos,
                                                      NFRule rule,
                                                      NFRule rulePredecessor,
                                                      NFRuleSet ruleSet,
                                                      RuleBasedNumberFormat formatter,
                                                      java.lang.String description)
        Parses the description, creates the right kind of substitution, and initializes it based on the description.
        Parameters:
        pos - The substitution's position in the rule text of the rule that owns it.
        rule - The rule containing this substitution
        rulePredecessor - The rule preceding the one that contains this substitution in the rule set's rule list (this is used only for >>> substitutions).
        ruleSet - The rule set containing the rule containing this substitution
        formatter - The RuleBasedNumberFormat that ultimately owns this substitution
        description - The description to parse to build the substitution (this is just the substring of the rule's description containing the substitution token itself)
        Returns:
        A new substitution constructed according to the description
      • setDivisor

        public void setDivisor​(int radix,
                               short exponent)
        Set's the substitution's divisor. Used by NFRule.setBaseValue(). A no-op for all substitutions except multiplier and modulus substitutions.
        Parameters:
        radix - The radix of the divisor
        exponent - The exponent of the divisor
      • equals

        public boolean equals​(java.lang.Object that)
        Compares two substitutions for equality
        Overrides:
        equals in class java.lang.Object
        Parameters:
        that - The substitution to compare this one to
        Returns:
        true if the two substitutions are functionally equivalent
      • hashCode

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

        public java.lang.String toString()
        Returns a textual description of the substitution
        Overrides:
        toString in class java.lang.Object
        Returns:
        A textual description of the substitution. This might not be identical to the description it was created from, but it'll produce the same result.
      • doSubstitution

        public void doSubstitution​(long number,
                                   java.lang.StringBuilder toInsertInto,
                                   int position,
                                   int recursionCount)
        Performs a mathematical operation on the number, formats it using either ruleSet or decimalFormat, and inserts the result into toInsertInto.
        Parameters:
        number - The number being formatted.
        toInsertInto - The string we insert the result into
        position - The position in toInsertInto where the owning rule's rule text begins (this value is added to this substitution's position to determine exactly where to insert the new text)
      • doSubstitution

        public void doSubstitution​(double number,
                                   java.lang.StringBuilder toInsertInto,
                                   int position,
                                   int recursionCount)
        Performs a mathematical operation on the number, formats it using either ruleSet or decimalFormat, and inserts the result into toInsertInto.
        Parameters:
        number - The number being formatted.
        toInsertInto - The string we insert the result into
        position - The position in toInsertInto where the owning rule's rule text begins (this value is added to this substitution's position to determine exactly where to insert the new text)
      • transformNumber

        public abstract long transformNumber​(long number)
        Subclasses override this function to perform some kind of mathematical operation on the number. The result of this operation is formatted using the rule set or DecimalFormat that this substitution refers to, and the result is inserted into the result string.
        Parameters:
        number - The number being formatted
        Returns:
        The result of performing the opreration on the number
      • transformNumber

        public abstract double transformNumber​(double number)
        Subclasses override this function to perform some kind of mathematical operation on the number. The result of this operation is formatted using the rule set or DecimalFormat that this substitution refers to, and the result is inserted into the result string.
        Parameters:
        number - The number being formatted
        Returns:
        The result of performing the opreration on the number
      • doParse

        public java.lang.Number doParse​(java.lang.String text,
                                        java.text.ParsePosition parsePosition,
                                        double baseValue,
                                        double upperBound,
                                        boolean lenientParse,
                                        int nonNumericalExecutedRuleMask)
        Parses a string using the rule set or DecimalFormat belonging to this substitution. If there's a match, a mathematical operation (the inverse of the one used in formatting) is performed on the result of the parse and the value passed in and returned as the result. The parse position is updated to point to the first unmatched character in the string.
        Parameters:
        text - The string to parse
        parsePosition - On entry, ignored, but assumed to be 0. On exit, this is updated to point to the first unmatched character (or 0 if the substitution didn't match)
        baseValue - A partial parse result that should be combined with the result of this parse
        upperBound - When searching the rule set for a rule matching the string passed in, only rules with base values lower than this are considered
        lenientParse - If true and matching against rules fails, the substitution will also try matching the text against numerals using a default-constructed NumberFormat. If false, no extra work is done. (This value is false whenever the formatter isn't in lenient-parse mode, but is also false under some conditions even when the formatter _is_ in lenient-parse mode.)
        Returns:
        If there's a match, this is the result of composing baseValue with whatever was returned from matching the characters. This will be either a Long or a Double. If there's no match this is Long.valueOf(0) (not null), and parsePosition is left unchanged.
      • composeRuleValue

        public abstract double composeRuleValue​(double newRuleValue,
                                                double oldRuleValue)
        Derives a new value from the two values passed in. The two values are typically either the base values of two rules (the one containing the substitution and the one matching the substitution) or partial parse results derived in some other way. The operation is generally the inverse of the operation performed by transformNumber().
        Parameters:
        newRuleValue - The value produced by matching this substitution
        oldRuleValue - The value that was passed to the substitution by the rule that owns it
        Returns:
        A third value derived from the other two, representing a partial parse result
      • calcUpperBound

        public abstract double calcUpperBound​(double oldUpperBound)
        Calculates an upper bound when searching for a rule that matches this substitution. Rules with base values greater than or equal to upperBound are not considered.
        Parameters:
        oldUpperBound - The current upper-bound setting. The new upper bound can't be any higher.
      • getPos

        public final int getPos()
        Returns the substitution's position in the rule that owns it.
        Returns:
        The substitution's position in the rule that owns it.
      • tokenChar

        abstract char tokenChar()
        Returns the character used in the textual representation of substitutions of this type. Used by toString().
        Returns:
        This substitution's token character.
      • isModulusSubstitution

        public boolean isModulusSubstitution()
        Returns true if this is a modulus substitution. (We didn't do this with instanceof partially because it causes source files to proliferate and partially because we have to port this to C++.)
        Returns:
        true if this object is an instance of ModulusSubstitution
      • setDecimalFormatSymbols

        public void setDecimalFormatSymbols​(DecimalFormatSymbols newSymbols)