Package com.ibm.icu.text
Class NFRuleSet
java.lang.Object
com.ibm.icu.text.NFRuleSet
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
FieldsModifier and TypeFieldDescription(package private) static final int
x.0(package private) LinkedList
<NFRule> These are a pile of fraction rules in declared order.(package private) static final int
x.x(package private) static final int
Infprivate boolean
True if the rule set is a fraction rule set.private final boolean
True if the rule set is parseable.private final String
The rule set's name(package private) static final int
NaN(package private) static final int
-x(package private) final NFRule[]
The rule set's non-numerical rules like negative, fractions, infinity and NaN(package private) final RuleBasedNumberFormat
The RuleBasedNumberFormat that owns this rule(package private) static final int
0.xprivate static final int
Limit of recursion.private NFRule[]
The rule set's regular rules -
Constructor Summary
ConstructorsConstructorDescriptionNFRuleSet
(RuleBasedNumberFormat owner, String[] descriptions, int index) Constructs a rule set. -
Method Summary
Modifier and TypeMethodDescriptionboolean
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, StringBuilder toInsertInto, int pos, int recursionCount) Formats a double.void
format
(long number, StringBuilder toInsertInto, int pos, int recursionCount) Formats a long.getName()
Returns the rule set's nameint
hashCode()
boolean
Says whether this rule set is a fraction rule set.boolean
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
Flags this rule set as a fraction rule set.parse
(String text, ParsePosition parsePosition, double upperBound, int nonNumericalExecutedRuleMask, int recursionCount) Parses a string.void
parseRules
(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.toString()
Builds a textual representation of a rule set.
-
Field Details
-
name
The rule set's name -
rules
The rule set's regular rules -
nonNumericalRules
The rule set's non-numerical rules like negative, fractions, infinity and NaN -
fractionRules
LinkedList<NFRule> fractionRulesThese 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_INDEXx.x- See Also:
-
PROPER_FRACTION_RULE_INDEX
static final int PROPER_FRACTION_RULE_INDEX0.x- See Also:
-
DEFAULT_RULE_INDEX
static final int DEFAULT_RULE_INDEXx.0- See Also:
-
INFINITY_RULE_INDEX
static final int INFINITY_RULE_INDEXInf- See Also:
-
NAN_RULE_INDEX
static final int NAN_RULE_INDEXNaN- See Also:
-
owner
The RuleBasedNumberFormat that owns this rule -
isFractionRuleSet
private boolean isFractionRuleSetTrue 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 isParseableTrue if the rule set is parseable. -
RECURSION_LIMIT
private static final int RECURSION_LIMITLimit 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 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:
IllegalArgumentException
-
-
Method Details
-
parseRules
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
Set one of the non-numerical rules.- Parameters:
rule
- The rule to set.
-
setBestFractionRule
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
Compares two rule sets for equality. -
hashCode
public int hashCode() -
toString
Builds a textual representation of a rule set. -
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
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
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
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
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
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
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, int recursionCount) 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
-