Class ChoiceFormat
- All Implemented Interfaces:
Serializable,Cloneable
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.
Examples:
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:
"#" specifies an inclusive limit value;"<" specifies an exclusive limit value.
- See Also:
- Serialized Form
-
Nested Class Summary
Nested classes/interfaces inherited from class java.text.NumberFormat
NumberFormat.Field -
Field Summary
Fields inherited from class java.text.NumberFormat
FRACTION_FIELD, INTEGER_FIELD -
Constructor Summary
Constructors Constructor Description ChoiceFormat(double[] limits, String[] formats)Constructs a newChoiceFormatwith the specified double values and associated strings.ChoiceFormat(String template)Constructs a newChoiceFormatwith the strings and limits parsed from the specified pattern. -
Method Summary
Modifier and Type Method Description voidapplyPattern(String template)Parses the pattern to determine new strings and ranges for thisChoiceFormat.Objectclone()Returns a new instance ofChoiceFormatwith the same ranges and strings as thisChoiceFormat.booleanequals(Object object)Compares the specified object with thisChoiceFormat.StringBufferformat(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.StringBufferformat(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 thisChoiceFormat.double[]getLimits()Returns the limits of thisChoiceFormat.inthashCode()Returns an integer hash code for the receiver.static doublenextDouble(double value)Returns the double value which is closest to the specified double but larger.static doublenextDouble(double value, boolean increment)Returns the double value which is closest to the specified double but either larger or smaller as specified.Numberparse(String string, ParsePosition position)Parses a double from the specified string starting at the index specified byposition.static doublepreviousDouble(double value)Returns the double value which is closest to the specified double but smaller.voidsetChoices(double[] limits, String[] formats)Sets the double values and associated strings of this ChoiceFormat.StringtoPattern()Returns the pattern of thisChoiceFormatwhich specifies the ranges and their associated strings.Methods inherited from class java.text.NumberFormat
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, setRoundingModeMethods inherited from class java.text.Format
format, formatToCharacterIterator, parseObject
-
Constructor Details
-
ChoiceFormat
Constructs a newChoiceFormatwith the specified double values and associated strings. When callingformatwith a double valued, then the elementiinformatsis selected whereifulfillslimits[i] <= d < limits[i+1].The length of the
limitsandformatsarrays must be the same.- Parameters:
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 throughlimits. The lower bound of the associated range is at the same index as the string.
-
ChoiceFormat
Constructs a newChoiceFormatwith the strings and limits parsed from the specified pattern.- Parameters:
template- the pattern of strings and ranges.- Throws:
IllegalArgumentException- if an error occurs while parsing the pattern.
-
-
Method Details
-
applyPattern
Parses the pattern to determine new strings and ranges for thisChoiceFormat.- Parameters:
template- the pattern of strings and ranges.- Throws:
IllegalArgumentException- if an error occurs while parsing the pattern.
-
clone
Returns a new instance ofChoiceFormatwith the same ranges and strings as thisChoiceFormat.- Overrides:
clonein classNumberFormat- Returns:
- a shallow copy of this
ChoiceFormat. - See Also:
Cloneable
-
equals
Compares the specified object with thisChoiceFormat. The object must be an instance ofChoiceFormatand have the same limits and formats to be equal to this instance.- Overrides:
equalsin classNumberFormat- Parameters:
object- the object to compare with this instance.- Returns:
trueif the specified object is equal to this instance;falseotherwise.- See Also:
hashCode()
-
format
Appends the string associated with the range in which the specified double value fits to the specified string buffer.- Specified by:
formatin classNumberFormat- Parameters:
value- the double to format.buffer- the target string buffer to append the formatted value to.field- aFieldPositionwhich is ignored.- Returns:
- the string buffer.
-
format
Appends the string associated with the range in which the specified long value fits to the specified string buffer.- Specified by:
formatin classNumberFormat- Parameters:
value- the long to format.buffer- the target string buffer to append the formatted value to.field- aFieldPositionwhich is ignored.- Returns:
- the string buffer.
-
getFormats
Returns the strings associated with the ranges of thisChoiceFormat.- Returns:
- an array of format strings.
-
getLimits
public double[] getLimits()Returns the limits of thisChoiceFormat.- Returns:
- the array of doubles which make up the limits of this
ChoiceFormat.
-
hashCode
public int hashCode()Returns an integer hash code for the receiver. Objects which are equal return the same value for this method.- Overrides:
hashCodein classNumberFormat- Returns:
- the receiver's hash.
- See Also:
equals(java.lang.Object)
-
nextDouble
public static final double nextDouble(double value)Returns the double value which is closest to the specified double but larger.- Parameters:
value- a double value.- Returns:
- the next larger double value.
-
nextDouble
public static double nextDouble(double value, boolean increment)Returns the double value which is closest to the specified double but either larger or smaller as specified.- Parameters:
value- a double value.increment-trueto get the next larger value,falseto get the previous smaller value.- Returns:
- the next larger or smaller double value.
-
parse
Parses a double from the specified string starting at the index specified byposition. The string is compared to the strings of thisChoiceFormatand 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 theParsePositionpassed to this method is updated to the index following the parsed text.If one of the format strings of this
ChoiceFormatinstance is found instringstarting atposition.getIndex()then- the index in
positionis set to the index following the parsed text; - the
Doublecorresponding to the format string is returned.
If none of the format strings is found in
stringthen- the error index in
positionis set to the current index inposition; -
Double.NaNis returned.
- Specified by:
parsein classNumberFormat- Parameters:
string- the source string to parse.position- input/output parameter, specifies the start index instringfrom where to start parsing. See the Returns section for a description of the output values.- Returns:
- a Double resulting from the parse, or Double.NaN if there is an error
- the index in
-
previousDouble
public static final double previousDouble(double value)Returns the double value which is closest to the specified double but smaller.- Parameters:
value- a double value.- Returns:
- the next smaller double value.
-
setChoices
Sets the double values and associated strings of this ChoiceFormat. When callingformatwith a double valued, then the elementiinformatsis selected whereifulfillslimits[i] <= d < limits[i+1].The length of the
limitsandformatsarrays must be the same.- Parameters:
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 throughlimits. The lower bound of the associated range is at the same index as the string.
-
toPattern
Returns the pattern of thisChoiceFormatwhich specifies the ranges and their associated strings.- Returns:
- the pattern.
-