Top Description Fields Constructors Methods
com.ibm.icu.text

pack-priv final Class NFRule

extends Object
Class Inheritance
Imports
java.text.FieldPosition, .ParsePosition, java.util.List, .Objects, com.ibm.icu.impl.PatternProps

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

Modifier and TypeField and 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.

pack-priv static final 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)

private final RuleBasedNumberFormat
formatter

The RuleBasedNumberFormat that owns this rule

pack-priv static final int
IMPROPER_FRACTION_RULE

Special base value used to identify an improper fraction (x.x) rule

pack-priv static final int
INFINITY_RULE

Special base value used to identify an infinity rule

pack-priv static final int
NAN_RULE

Special base value used to identify a not a number rule

pack-priv static final int
NEGATIVE_NUMBER_RULE

Special base value used to identify a negative-number rule

pack-priv static final 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 final String[]
private PluralFormat
rulePatternFormat

The rule's plural format when defined.

private 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)

pack-priv static final Long

Constructor Summary

AccessConstructor and Description
public
NFRule(RuleBasedNumberFormat formatter, String ruleText)

Nominal constructor for NFRule.

Method Summary

Modifier and TypeMethod and Description
private boolean

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.
allIgnorable
(String
The string to test.
str
)

Checks to see whether a string consists entirely of ignorable characters.

public void
doFormat(long
The number being formatted
number
,
StringBuilder
The string where the resultant text should be inserted
toInsertInto
,
int
The position in toInsertInto where the resultant text should be inserted
pos
,
int recursionCount)

Formats the number, and inserts the resulting text into toInsertInto.

public void
doFormat(double
The number being formatted
number
,
StringBuilder
The string where the resultant text should be inserted
toInsertInto
,
int
The position in toInsertInto where the resultant text should be inserted
pos
,
int recursionCount)

Formats the number, and inserts the resulting text into toInsertInto.

public Number

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 new Long(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.
doParse
(String
The string being parsed
text
,
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 new Long(0)).
parsePosition
,
boolean
True if this rule is contained within a fraction rule set. This is only used if the rule has no substitutions.
isFractionRule
,
double upperBound, int nonNumericalExecutedRuleMask)

Attempts to parse the string with this rule.

public boolean

Returns:

True if the two rules are functionally equivalent
equals
(Object
The rule to compare this one against
that
)

Overrides java.lang.Object.equals.

Tests two rules for equality.
private short
expectedExponent()

This calculates the rule's exponent based on its radix and base value.

private NFSubstitution

Returns:

The newly-created substitution. This is never null; if the rule text doesn't contain any substitution tokens, this will be a NullSubstitution.
extractSubstitution
(NFRuleSet
The rule set containing this rule
owner
,
NFRule
The rule preceding this one in the rule set's rule list
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
The rule set containing this rule
owner
,
String
The rule text
ruleText
,
NFRule
The rule preceding this one in "owners" rule list
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[]

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")
findText
(String
The string to search
str
,
String
The string to search "str" for
key
,
PluralFormat pluralFormatKey, int
The index into "str" where the search is to begin
startingAt
)

Searches a string for another string.

public final long

Returns:

The rule's base value
getBaseValue
()

Returns the rule's base value

public final char

Returns:

The rule's base value
getDecimalPoint
()

Returns the rule's base value

public long

Returns:

The rule's divisor
getDivisor
()

Returns the rule's divisor (the value that controls the behavior of its substitutions)

public int
hashCode()

Overrides java.lang.Object.hashCode.

Returns a hash code value for the object.
private static int

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.
indexOfAnyRulePrefix
(String ruleText)

Searches the rule's rule text for any of the specified strings.

public static void
makeRules(String
The description of the rule(s).
description
,
NFRuleSet
The rule set containing the new rule(s).
owner
,
NFRule
The rule that precedes the new one(s) in "owner"'s rule list
predecessor
,
RuleBasedNumberFormat
The RuleBasedNumberFormat that owns the rule set that owns the new rule(s)
ownersOwner
,
List<NFRule>
One or more instances of NFRule are added and returned here
returnList
)

Creates one or more rules based on the description passed in.

private Number

Returns:

If there's a match, this is the result of composing baseValue with the result of matching the substitution. Otherwise, this is new Long(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.
matchToDelimiter
(String
The string being parsed
text
,
int
The position in "text" where we should start looking for "delimiter".
startPos
,
double
A partial parse result (often the rule's base value), which is combined with the result from matching the substitution
baseVal
,
String
The string to search "text" for.
delimiter
,
PluralFormat pluralFormatDelimiter, ParsePosition
Ignored and presumed to be 0 on entry. If there's a match, on exit this will point to the first unmatched character.
pp
,
NFSubstitution
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.)
sub
,
double
When matching the substitution, it will only consider rules with base values lower than this value.
upperBound
,
int nonNumericalExecutedRuleMask)

Used by parse() to match a substitution and any following text.

private String

Returns:

If "description" included a rule descriptor, this is "description" with the descriptor and any trailing whitespace stripped off. Otherwise; it's "descriptor" unchanged.
parseRuleDescriptor
(String
The rule's description
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.

pack-priv static long

Returns:

radix ** exponent
power
(long
The base
base
,
short
The exponent
exponent
)

This is an equivalent to Math.pow that accurately works on 64-bit numbers

private int

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.
prefixLength
(String
The string being tested
str
,
String
The text we're hoping to see at the beginning of "str"
prefix
)

Used by stripPrefix() to match characters.

pack-priv final void
setBaseValue(long
The new base value for the rule.
newBaseValue
)

Sets the rule's base value, and causes the radix and exponent to be recalculated.

public void
public boolean

Returns:

True if the rule set should use the rule that precedes this one in its list; false if it should use this rule
shouldRollBack
(long
The number being formatted
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 String

Returns:

If things match, this is the unparsed part of "text"; if they didn't match, this is "text".
stripPrefix
(String
The string being parsed
text
,
String
The text to match against
prefix
,
ParsePosition
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).
pp
)

This function is used by parse() to match the text being parsed against a possible prefix string.

public String

Returns:

A textual description of the rule
toString
()

Overrides java.lang.Object.toString.

Returns a textual representation of the rule.
Inherited from java.lang.Object:
clonefinalizegetClassnotifynotifyAllwaitwaitwait

Field Detail

baseValueback to summary
private long baseValue

The rule's base value

decimalPointback to summary
private char decimalPoint

If this is a fraction rule, this is the decimal point from DecimalFormatSymbols to match.

DEFAULT_RULEback to summary
pack-priv static final int DEFAULT_RULE

Special base value used to identify a default rule

exponentback to summary
private short exponent

The rule's exponent (the radix raised to the power of the exponent equals the rule's divisor)

formatterback to summary
private final RuleBasedNumberFormat formatter

The RuleBasedNumberFormat that owns this rule

IMPROPER_FRACTION_RULEback to summary
pack-priv static final int IMPROPER_FRACTION_RULE

Special base value used to identify an improper fraction (x.x) rule

INFINITY_RULEback to summary
pack-priv static final int INFINITY_RULE

Special base value used to identify an infinity rule

NAN_RULEback to summary
pack-priv static final int NAN_RULE

Special base value used to identify a not a number rule

NEGATIVE_NUMBER_RULEback to summary
pack-priv static final int NEGATIVE_NUMBER_RULE

Special base value used to identify a negative-number rule

PROPER_FRACTION_RULEback to summary
pack-priv static final int PROPER_FRACTION_RULE

Special base value used to identify a proper fraction (0.x) rule

radixback to summary
private int radix

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

RULE_PREFIXESback to summary
private static final String[] RULE_PREFIXES
rulePatternFormatback to summary
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.

ruleTextback to summary
private 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

sub1back to summary
private NFSubstitution sub1

The rule's first substitution (the one with the lower offset into the rule text)

sub2back to summary
private NFSubstitution sub2

The rule's second substitution (the one with the higher offset into the rule text)

ZEROback to summary
pack-priv static final Long ZERO

Constructor Detail

NFRuleback to summary
public NFRule(RuleBasedNumberFormat formatter, String ruleText)

Nominal constructor for NFRule. Most of the work of constructing an NFRule is actually performed by makeRules().

Method Detail

allIgnorableback to summary
private boolean allIgnorable(String str)

Checks to see whether a string consists entirely of ignorable characters.

Parameters
str:String

The string to test.

Returns:boolean

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.

doFormatback to summary
public void doFormat(long number, StringBuilder toInsertInto, int pos, int recursionCount)

Formats the number, and inserts the resulting text into toInsertInto.

Parameters
number:long

The number being formatted

toInsertInto:StringBuilder

The string where the resultant text should be inserted

pos:int

The position in toInsertInto where the resultant text should be inserted

doFormatback to summary
public void doFormat(double number, StringBuilder toInsertInto, int pos, int recursionCount)

Formats the number, and inserts the resulting text into toInsertInto.

Parameters
number:double

The number being formatted

toInsertInto:StringBuilder

The string where the resultant text should be inserted

pos:int

The position in toInsertInto where the resultant text should be inserted

doParseback to summary
public Number doParse(String text, ParsePosition parsePosition, boolean isFractionRule, double upperBound, int nonNumericalExecutedRuleMask)

Attempts to parse the string with this rule.

Parameters
text:String

The string being parsed

parsePosition: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 new Long(0)).

isFractionRule:boolean

True if this rule is contained within a fraction rule set. This is only used if the rule has no substitutions.

Returns:Number

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 new Long(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.

equalsback to summary
public boolean equals(Object that)

Overrides java.lang.Object.equals.

Tests two rules for equality.

Parameters
that:Object

The rule to compare this one against

Returns:boolean

True if the two rules are functionally equivalent

Annotations
@Override
expectedExponentback to summary
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.

extractSubstitutionback to summary
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:NFRuleSet

The rule set containing this rule

predecessor:NFRule

The rule preceding this one in the rule set's rule list

Returns:NFSubstitution

The newly-created substitution. This is never null; if the rule text doesn't contain any substitution tokens, this will be a NullSubstitution.

extractSubstitutionsback to summary
private void extractSubstitutions(NFRuleSet owner, 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:NFRuleSet

The rule set containing this rule

ruleText:String

The rule text

predecessor:NFRule

The rule preceding this one in "owners" rule list

findTextback to summary
private int[] findText(String str, 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:String

The string to search

key:String

The string to search "str" for

startingAt:int

The index into "str" where the search is to begin

Returns:int[]

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")

getBaseValueback to summary
public final long getBaseValue()

Returns the rule's base value

Returns:long

The rule's base value

getDecimalPointback to summary
public final char getDecimalPoint()

Returns the rule's base value

Returns:char

The rule's base value

getDivisorback to summary
public long getDivisor()

Returns the rule's divisor (the value that controls the behavior of its substitutions)

Returns:long

The rule's divisor

hashCodeback to summary
public int hashCode()

Overrides java.lang.Object.hashCode.

Doc from java.lang.Object.hashCode.

Returns a hash code value for the object. This method is supported for the benefit of hash tables such as those provided by java.util.HashMap.

The general contract of hashCode is:

  • Whenever it is invoked on the same object more than once during an execution of a Java application, the hashCode method must consistently return the same integer, provided no information used in equals comparisons on the object is modified. This integer need not remain consistent from one execution of an application to another execution of the same application.
  • If two objects are equal according to the equals method, then calling the hashCode method on each of the two objects must produce the same integer result.
  • It is not required that if two objects are unequal according to the equals method, then calling the hashCode method on each of the two objects must produce distinct integer results. However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hash tables.
Returns:int

a hash code value for this object.

Annotations
@Override
indexOfAnyRulePrefixback to summary
private static int indexOfAnyRulePrefix(String ruleText)

Searches the rule's rule text for any of the specified strings.

Returns:int

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.

makeRulesback to summary
public static void makeRules(String description, NFRuleSet owner, NFRule predecessor, RuleBasedNumberFormat ownersOwner, List<NFRule> returnList)

Creates one or more rules based on the description passed in.

Parameters
description:String

The description of the rule(s).

owner:NFRuleSet

The rule set containing the new rule(s).

predecessor:NFRule

The rule that precedes the new one(s) in "owner"'s rule list

ownersOwner:RuleBasedNumberFormat

The RuleBasedNumberFormat that owns the rule set that owns the new rule(s)

returnList:List<NFRule>

One or more instances of NFRule are added and returned here

matchToDelimiterback to summary
private Number matchToDelimiter(String text, int startPos, double baseVal, String delimiter, PluralFormat pluralFormatDelimiter, ParsePosition pp, NFSubstitution sub, double upperBound, int nonNumericalExecutedRuleMask)

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:String

The string being parsed

startPos:int

The position in "text" where we should start looking for "delimiter".

baseVal:double

A partial parse result (often the rule's base value), which is combined with the result from matching the substitution

delimiter:String

The string to search "text" for.

pp:ParsePosition

Ignored and presumed to be 0 on entry. If there's a match, on exit this will point to the first unmatched character.

sub:NFSubstitution

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:double

When matching the substitution, it will only consider rules with base values lower than this value.

Returns:Number

If there's a match, this is the result of composing baseValue with the result of matching the substitution. Otherwise, this is new Long(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.

parseRuleDescriptorback to summary
private String parseRuleDescriptor(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:String

The rule's description

Returns:String

If "description" included a rule descriptor, this is "description" with the descriptor and any trailing whitespace stripped off. Otherwise; it's "descriptor" unchanged.

powerback to summary
pack-priv static long power(long base, short exponent)

This is an equivalent to Math.pow that accurately works on 64-bit numbers

Parameters
base:long

The base

exponent:short

The exponent

Returns:long

radix ** exponent

See Also
Math#pow(double, double)
prefixLengthback to summary
private int prefixLength(String str, 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:String

The string being tested

prefix:String

The text we're hoping to see at the beginning of "str"

Returns:int

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.

setBaseValueback to summary
pack-priv 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:long

The new base value for the rule.

setDecimalFormatSymbolsback to summary
public void setDecimalFormatSymbols(DecimalFormatSymbols newSymbols)
shouldRollBackback to summary
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:long

The number being formatted

Returns:boolean

True if the rule set should use the rule that precedes this one in its list; false if it should use this rule

stripPrefixback to summary
private String stripPrefix(String text, String prefix, 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:String

The string being parsed

prefix:String

The text to match against

pp:ParsePosition

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:String

If things match, this is the unparsed part of "text"; if they didn't match, this is "text".

toStringback to summary
public String toString()

Overrides java.lang.Object.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.

Returns:String

A textual description of the rule

Annotations
@Override