Package com.ibm.icu.text
Class NFRuleSet
- java.lang.Object
-
- com.ibm.icu.text.NFRuleSet
-
final class NFRuleSet extends java.lang.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 Summary
Fields Modifier and Type Field Description (package private) static int
DEFAULT_RULE_INDEX
x.0(package private) java.util.LinkedList<NFRule>
fractionRules
These are a pile of fraction rules in declared order.(package private) static int
IMPROPER_FRACTION_RULE_INDEX
x.x(package private) static int
INFINITY_RULE_INDEX
Infprivate boolean
isFractionRuleSet
True if the rule set is a fraction rule set.private boolean
isParseable
True if the rule set is parseable.private java.lang.String
name
The rule set's name(package private) static int
NAN_RULE_INDEX
NaN(package private) static int
NEGATIVE_RULE_INDEX
-x(package private) NFRule[]
nonNumericalRules
The rule set's non-numerical rules like negative, fractions, infinity and NaN(package private) RuleBasedNumberFormat
owner
The RuleBasedNumberFormat that owns this rule(package private) static int
PROPER_FRACTION_RULE_INDEX
0.xprivate static int
RECURSION_LIMIT
Limit of recursion.private NFRule[]
rules
The rule set's regular rules
-
Constructor Summary
Constructors Constructor Description NFRuleSet(RuleBasedNumberFormat owner, java.lang.String[] descriptions, int index)
Constructs a rule set.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description boolean
equals(java.lang.Object that)
Compares two rule sets for equality.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.private NFRule
findNormalRule(long number)
If the value passed to findRule() is a positive integer, findRule() uses this function to select the appropriate rule.(package private) NFRule
findRule(double number)
Selects an appropriate rule for formatting the number.void
format(double number, java.lang.StringBuilder toInsertInto, int pos, int recursionCount)
Formats a double.void
format(long number, java.lang.StringBuilder toInsertInto, int pos, int recursionCount)
Formats a long.java.lang.String
getName()
Returns the rule set's nameint
hashCode()
boolean
isFractionSet()
Says whether this rule set is a fraction rule set.boolean
isParseable()
Return true if the rule set can be used for parsing.boolean
isPublic()
Return true if the rule set is public.private static long
lcm(long x, long y)
Calculates the least common multiple of x and y.void
makeIntoFractionRuleSet()
Flags this rule set as a fraction rule set.java.lang.Number
parse(java.lang.String text, java.text.ParsePosition parsePosition, double upperBound, int nonNumericalExecutedRuleMask)
Parses a string.void
parseRules(java.lang.String description)
Construct the subordinate data structures used by this object.private void
setBestFractionRule(int originalIndex, NFRule newRule, boolean rememberRule)
Determine the best fraction rule to use.void
setDecimalFormatSymbols(DecimalFormatSymbols newSymbols)
(package private) void
setNonNumericalRule(NFRule rule)
Set one of the non-numerical rules.java.lang.String
toString()
Builds a textual representation of a rule set.
-
-
-
Field Detail
-
name
private final java.lang.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
java.util.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:
- Constant Field Values
-
IMPROPER_FRACTION_RULE_INDEX
static final int IMPROPER_FRACTION_RULE_INDEX
x.x- See Also:
- Constant Field Values
-
PROPER_FRACTION_RULE_INDEX
static final int PROPER_FRACTION_RULE_INDEX
0.x- See Also:
- Constant Field Values
-
DEFAULT_RULE_INDEX
static final int DEFAULT_RULE_INDEX
x.0- See Also:
- Constant Field Values
-
INFINITY_RULE_INDEX
static final int INFINITY_RULE_INDEX
Inf- See Also:
- Constant Field Values
-
NAN_RULE_INDEX
static final int NAN_RULE_INDEX
NaN- See Also:
- Constant Field Values
-
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:
- Constant Field Values
-
-
Constructor Detail
-
NFRuleSet
public NFRuleSet(RuleBasedNumberFormat owner, java.lang.String[] descriptions, int index) throws java.lang.IllegalArgumentException
Constructs a rule set.- Parameters:
owner
- The formatter that owns this rule setdescriptions
- 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:
java.lang.IllegalArgumentException
-
-
Method Detail
-
parseRules
public void parseRules(java.lang.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 nonNumericalRulesnewRule
- The new rule to considerrememberRule
- 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(java.lang.Object that)
Compares two rule sets for equality.- Overrides:
equals
in classjava.lang.Object
- Parameters:
that
- The other rule set- Returns:
- true if the two rule sets are functionally equivalent.
-
hashCode
public int hashCode()
- Overrides:
hashCode
in classjava.lang.Object
-
toString
public java.lang.String toString()
Builds a textual representation of a rule set.- Overrides:
toString
in classjava.lang.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 java.lang.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, java.lang.StringBuilder toInsertInto, int pos, int recursionCount)
Formats a long. Selects an appropriate rule and dispatches control to it.- Parameters:
number
- The number being formattedtoInsertInto
- The string where the result is to be placedpos
- The position in toInsertInto where the result of this operation is to be inserted
-
format
public void format(double number, java.lang.StringBuilder toInsertInto, int pos, int recursionCount)
Formats a double. Selects an appropriate rule and dispatches control to it.- Parameters:
number
- The number being formattedtoInsertInto
- The string where the result is to be placedpos
- 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 java.lang.Number parse(java.lang.String text, java.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 parseparsePosition
- 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)
-
-