ChoiceFormatNameChoiceFormatSynopsis
DescriptionThe ChoiceFormat class is a concrete subclass of NumberFormat that maps numerical ranges to strings, or formats. ChoiceFormat objects are used most often by MessageFormat objects to handle plurals, verb agreement, and other such issues. The ranges in a ChoiceFormat are specified as an ascending array of double values, where each number is the bottom end of a range. A value is mapped to a format when it falls within the range for that format. If the value does not fall in any of the ranges, it is mapped to the first or the last format, depending on whether the value is too low or too high. For example, consider the following code:
double[] limits = {1, 10, 100}; String[] labels = {"small", "medium", "large"} ChoiceFormat cf = new ChoiceFormat(limits, labels); Any number greater than or equal to one and less than 10 is mapped to the format "small". Any number greater than or equal to 10 and less than 100 is mapped to "medium". Numbers greater than or equal to 100 are mapped to "large". Furthermore, numbers less than one are also mapped to "small". The nextDouble() and previousDouble() methods can generate double values that are higher or lower than other double values. These methods provide another way to specify the limits used by a ChoiceFormat object. As shown above, you can create a ChoiceFormat object by specifying the limits and formats in two separate arrays. You can also create a ChoiceFormat object using a pattern string that specifies the limits and formats. The string is of the form:
[limit1]#[format1]|[limit2]#[format2]|... A < character can be used in place of the # to indicate that the next higher number, as determined by nextDouble(), should be used as the limit. The toPattern() method can be used to generate the pattern string for an existing ChoiceFormat object. Note that you create ChoiceFormat objects directly, rather than through factory methods. This is because ChoiceFormat does not implement any locale-specific behavior. To produce properly internationalized output, the formats for a ChoiceFormat should come from a ResourceBundle instead of being embedded in the code. Class Summary
public class java.text.ChoiceFormat extends java.text.NumberFormat { // Constructors public ChoiceFormat(String newPattern); public ChoiceFormat(double[] limits, String[] formats); // Class Methods public static final double nextDouble(double d); public static double nextDouble(double d, boolean positive); public static final double previousDouble(double d); // Instance Methods public void applyPattern(String newPattern); public Object clone(); public boolean equals(Object obj); public StringBuffer format(double number, StringBuffer toAppendTo, FieldPosition status); public StringBuffer format(long number, StringBuffer toAppendTo, FieldPosition status); public Object[] getFormats(); public double[] getLimits(); public int hashCode(); public Number parse(String text, ParsePosition status); public void setChoices(double[] limits, String[] formats); public String toPattern(); } ConstructorsChoiceFormatpublic ChoiceFormat(String newPattern)
public ChoiceFormat(double[] limits, String[] formats)
Class MethodsnextDoublepublic static final double nextDouble(double d)
public static double nextDouble(double d, boolean positive)
previousDoublepublic static final double previousDouble(double d)
Instance MethodsapplyPatternpublic void applyPattern(String newPattern)
clonepublic Object clone()
equalspublic boolean equals(Object obj)
format
public StringBuffer format(double number, StringBuffer toAppendTo, FieldPosition status)
public StringBuffer format(long number, StringBuffer toAppendTo, FieldPosition status)
getFormatspublic Object[] getFormats()
getLimitspublic double[] getLimits()
hashCodepublic int hashCode()
parsepublic Number parse(String text, ParsePosition status)
setChoicespublic void setChoices(double[] limits, String[] formats)
toPatternpublic String toPattern()
Inherited Methods
See AlsoFieldPosition, MessageFormat, Number, NumberFormat, ParsePosition, ResourceBundle, String, StringBuffer | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|