public class ChoiceFormat extends NumberFormat
MessageFormat class to handle plurals in
messages. ChoiceFormat enables users to attach a format to a range of
numbers. The choice is specified with an ascending list of doubles, where
each item specifies a half-open interval up to the next item as in the
following: X matches j if and only if limit[j] <= X < limit[j+1].
If there is no match, then either the first or last index is used. The first or last index is used depending on whether the number is too low or too high. The length of the format array must be the same as the length of the limits array.
double[] limits = {1, 2, 3, 4, 5, 6, 7};
String[] fmts = {"Sun", "Mon", "Tue", "Wed", "Thur", "Fri", "Sat"};
double[] limits2 = {0, 1, ChoiceFormat.nextDouble(1)};
String[] fmts2 = {"no files", "one file", "many files"};
ChoiceFormat.nextDouble(double) allows to get the double following the one passed to the method. This is used to create half open intervals.
ChoiceFormat objects also may be converted to and from patterns.
The conversion can be done programmatically, as in the example above, or
by using a pattern like the following:
"1#Sun|2#Mon|3#Tue|4#Wed|5#Thur|6#Fri|7#Sat" "0#are no files|1#is one file|1<are many files"
where:
NumberFormat.FieldFRACTION_FIELD, INTEGER_FIELD| Constructor and Description |
|---|
ChoiceFormat(double[] limits,
String[] formats)
Constructs a new
ChoiceFormat with the specified double values
and associated strings. |
ChoiceFormat(String template)
Constructs a new
ChoiceFormat with the strings and limits parsed
from the specified pattern. |
| Modifier and Type | Method and Description |
|---|---|
void |
applyPattern(String template)
Parses the pattern to determine new strings and ranges for this
ChoiceFormat. |
Object |
clone()
Returns a new instance of
ChoiceFormat with the same ranges and
strings as this ChoiceFormat. |
boolean |
equals(Object object)
Compares the specified object with this
ChoiceFormat. |
StringBuffer |
format(double value,
StringBuffer buffer,
FieldPosition field)
Appends the string associated with the range in which the specified
double value fits to the specified string buffer.
|
StringBuffer |
format(long value,
StringBuffer buffer,
FieldPosition field)
Appends the string associated with the range in which the specified long
value fits to the specified string buffer.
|
Object[] |
getFormats()
Returns the strings associated with the ranges of this
ChoiceFormat. |
double[] |
getLimits()
Returns the limits of this
ChoiceFormat. |
int |
hashCode()
Returns an integer hash code for the receiver.
|
static double |
nextDouble(double value)
Equivalent to
Math.nextUp(double). |
static double |
nextDouble(double value,
boolean increment)
Equivalent to
Math.nextUp(double) if increment == true, and
Math.nextAfter(double, double) with direction == Double.NEGATIVE_INFINITY
otherwise. |
Number |
parse(String string,
ParsePosition position)
Parses a double from the specified string starting at the index specified
by
position. |
static double |
previousDouble(double value)
Equivalent to
Math.nextAfter(double, double) with
direction == Double.NEGATIVE_INFINITY. |
void |
setChoices(double[] limits,
String[] formats)
Sets the double values and associated strings of this ChoiceFormat.
|
String |
toPattern()
Returns the pattern of this
ChoiceFormat which specifies the
ranges and their associated strings. |
format, format, format, getAvailableLocales, getCurrency, getCurrencyInstance, getCurrencyInstance, getInstance, getInstance, getIntegerInstance, getIntegerInstance, getMaximumFractionDigits, getMaximumIntegerDigits, getMinimumFractionDigits, getMinimumIntegerDigits, getNumberInstance, getNumberInstance, getPercentInstance, getPercentInstance, getRoundingMode, isGroupingUsed, isParseIntegerOnly, parse, parseObject, setCurrency, setGroupingUsed, setMaximumFractionDigits, setMaximumIntegerDigits, setMinimumFractionDigits, setMinimumIntegerDigits, setParseIntegerOnly, setRoundingModeformat, formatToCharacterIterator, parseObjectpublic ChoiceFormat(double[] limits,
String[] formats)
ChoiceFormat with the specified double values
and associated strings. When calling
format with a double
value d, then the element i in formats is
selected where i fulfills limits[i] <= d < limits[i+1].
The length of the limits and formats arrays must be the
same.
limits - an array of doubles in ascending order. The lowest and highest
possible values are negative and positive infinity.formats - the strings associated with the ranges defined through limits. The lower bound of the associated range is at the
same index as the string.public ChoiceFormat(String template)
ChoiceFormat with the strings and limits parsed
from the specified pattern.template - the pattern of strings and ranges.IllegalArgumentException - if an error occurs while parsing the pattern.public void applyPattern(String template)
ChoiceFormat.template - the pattern of strings and ranges.IllegalArgumentException - if an error occurs while parsing the pattern.public Object clone()
ChoiceFormat with the same ranges and
strings as this ChoiceFormat.clone in class NumberFormatChoiceFormat.Cloneablepublic boolean equals(Object object)
ChoiceFormat. The object
must be an instance of ChoiceFormat and have the same limits and
formats to be equal to this instance.equals in class NumberFormatobject - the object to compare with this instance.true if the specified object is equal to this instance;
false otherwise.hashCode()public StringBuffer format(double value, StringBuffer buffer, FieldPosition field)
format in class NumberFormatvalue - the double to format.buffer - the target string buffer to append the formatted value to.field - a FieldPosition which is ignored.public StringBuffer format(long value, StringBuffer buffer, FieldPosition field)
format in class NumberFormatvalue - the long to format.buffer - the target string buffer to append the formatted value to.field - a FieldPosition which is ignored.public Object[] getFormats()
ChoiceFormat.public double[] getLimits()
ChoiceFormat.ChoiceFormat.public int hashCode()
hashCode in class NumberFormatequals(java.lang.Object)public static final double nextDouble(double value)
Math.nextUp(double).public static double nextDouble(double value,
boolean increment)
Math.nextUp(double) if increment == true, and
Math.nextAfter(double, double) with direction == Double.NEGATIVE_INFINITY
otherwise.public Number parse(String string, ParsePosition position)
position. The string is compared to the strings of this
ChoiceFormat and if a match occurs then the lower bound of the
corresponding range in the limits array is returned. If the string is
successfully parsed then the index of the ParsePosition passed to
this method is updated to the index following the parsed text.
If one of the format strings of this ChoiceFormat instance is
found in string starting at position.getIndex() then
position is set to the index following the
parsed text;
Double corresponding to the format
string is returned.
If none of the format strings is found in string then
position is set to the current index in
position;Double.NaN is returned.
parse in class NumberFormatstring - the source string to parse.position - input/output parameter, specifies the start index in string from where to start parsing. See the Returns
section for a description of the output values.public static final double previousDouble(double value)
Math.nextAfter(double, double) with
direction == Double.NEGATIVE_INFINITY.public void setChoices(double[] limits,
String[] formats)
format with
a double value d, then the element i in formats
is selected where i fulfills
limits[i] <= d < limits[i+1].
The length of the limits and formats arrays must be the
same.
limits - an array of doubles in ascending order. The lowest and highest
possible values are negative and positive infinity.formats - the strings associated with the ranges defined through limits. The lower bound of the associated range is at the
same index as the string.public String toPattern()
ChoiceFormat which specifies the
ranges and their associated strings.