All Packages  Class Hierarchy  This Package  Previous  Next  Index

Class java.text.ChoiceFormat

java.lang.Object
   |
   +----java.text.Format
           |
           +----java.text.NumberFormat
                   |
                   +----java.text.ChoiceFormat

public class ChoiceFormat
extends NumberFormat
A ChoiceFormat allows you to attach a format to a range of numbers It is generally used in a MessageFormat for doing things like plurals. The choice is specified with a ascending list of doubles, where each item specifies a half-open interval up to the next item:
     X matches j just in case limit[j] <= X < limit[j+1]
 
If there is no match, then either the first or last index is used, depending on whether the number is too low or too high. The length of the array of formats must be the same as the length of the array of limits. For example,
     {1,2,3,4,5,6,7},
          {"Sun","Mon","Tue","Wed","Thur","Fri","Sat"}
     {0, 1, ChoiceFormat.nextDouble(0)},
          {"no files", "one file", "many files"}
 
(nextDouble can be used to get the next higher double, to make the half-open interval.)

Here is a simple example that shows formatting and parsing:

     double[] limits = {1,2,3,4,5,6,7};
     String[] monthNames = {"Sun","Mon","Tue","Wed","Thur","Fri","Sat"};
     ChoiceFormat form = new ChoiceFormat(limits, monthNames);
     ParseStatus status = new ParseStatus();
     for (double i = 0.0; i <= 8.0; ++i) {
         status.startAt = 0;
         System.out.println(i + " -> " + form.format(i) + " -> "
                              + form.parse(form.format(i),status));
     }
 
Here is a more complex example, with a pattern format.
     double[] filelimits = {0,1,2};
     String[] filepart = {"are no files","is one file","are %2 files"};
     ChoiceFormat fileform = new ChoiceFormat(filelimits, filepart);
     Format[] testFormats = {fileform, null, NumberFormat.getDefault()};
     MessageFormat pattform
         = new MessageFormat("There %0 on %1",testFormats);
     Object[] testArgs = {null, "ADisk", null};
     for (int i = 0; i < 4; ++i) {
         testArgs[0] = new Integer(i);
         testArgs[2] = testArgs[0];
         System.out.print(pattform.getPattern() + " -> ");
         System.out.println(pattform.format(testArgs));
     }
 

See Also:
DecimalFormat, MessageFormat

Constructor Index

 o ChoiceFormat(double[], Object[])
Sets the limits and the corresponding formats.

Method Index

 o format(double, StringBuffer, FormatStatus)
Specialization of format.
 o format(long, StringBuffer, FormatStatus)
Specialization of format.
 o getFormats()
Get the formats passed in the constructor.
 o getLimits()
Get the limits passed in the constructor.
 o nextDouble(double)
Finds the least double greater than d.
 o nextDouble(double, boolean)
 o parse(String, ParseStatus)
Returns a Long if possible (e.g.
 o previousDouble(double)
Finds the greatest double less than d.
 o setChoices(double[], Object[])
Set the choices to be used in formatting.

Constructors

 o ChoiceFormat
  public ChoiceFormat(double limits[],
                      Object formats[])
Sets the limits and the corresponding formats.

See Also:
setChoices

Methods

 o setChoices
  public void setChoices(double limits[],
                         Object formats[])
Set the choices to be used in formatting.

Parameters:
limits - contains the top value that you want parsed with that format,and should be in ascending sorted order. When formatting X, the choice will be the i, where limit[i] <= X < limit[i+1].
formats - are the formats you want to use for each limit. They can be either Format objects or Strings. When formatting with object Y, if the object is a NumberFormat, then ((NumberFormat) Y).format(X) is called. Otherwise Y.toString() is called.
 o getLimits
  public double[] getLimits()
Get the limits passed in the constructor.

Returns:
the limits.
 o getFormats
  public Object[] getFormats()
Get the formats passed in the constructor.

Returns:
the formats.
 o format
  public StringBuffer format(long number,
                             StringBuffer toAppendTo,
                             FormatStatus status)
Specialization of format.

Overrides:
format in class NumberFormat
 o format
  public StringBuffer format(double number,
                             StringBuffer toAppendTo,
                             FormatStatus status)
Specialization of format.

Overrides:
format in class NumberFormat
 o parse
  public Number parse(String text,
                      ParseStatus status)
Returns a Long if possible (e.g.

Overrides:
parse in class NumberFormat
 o nextDouble
  public final static double nextDouble(double d)
Finds the least double greater than d. If nan, returns same value.

Used to make half-open intervals.

See Also:
previousDouble
 o previousDouble
  public final static double previousDouble(double d)
Finds the greatest double less than d. If nan, returns same value.

See Also:
nextDouble
 o nextDouble
  public static double nextDouble(double d,
                                  boolean positive)

All Packages  Class Hierarchy  This Package  Previous  Next  Index