所有类


java.text
类 ChoiceFormat

java.lang.Object
  继承者 java.text.Format
      继承者 java.text.NumberFormat
          继承者 java.text.ChoiceFormat
所有已实现的接口:
Serializable, Cloneable

public class ChoiceFormat
   
   
   
   
   
extends NumberFormat

ChoiceFormat 允许将格式应用到某个范围的数。它通常用于在 MessageFormat 中处理复数。使用按升序排列的 double 列表指定 choice,列表中每一项都指定一个到下一项的半开区间:

当且仅当 limit[j] <= X < limit[j+1] 时,X 匹配 j
 
如果不匹配,则根据数 (X) 的是太小还是太大,选择使用第一个或最后一个索引。如果 limit 数组不按升序排列,则格式化的结果将不正确。ChoiceFormat 还接受 \u221E(等同于无穷大 (INF))。

注:ChoiceFormat 不同于其他 Format 类,因为 ChoiceFormat 对象是通过构造方法创建的(而不是通过 getInstance 样式工厂方法)。工厂方法不是必需的,因为 ChoiceFormat 不要求针对给定语言环境进行任何复杂的设置。事实上,ChoiceFormat 并不实现任何特定于语言环境的行为。

创建 ChoiceFormat 时,必须指定一个 format 数组和一个 limit 数组。这些数组的长度必须相同。例如,

  • limits = {1,2,3,4,5,6,7}
    formats = {"Sun","Mon","Tue","Wed","Thur","Fri","Sat"}
  • limits = {0, 1, ChoiceFormat.nextDouble(1)}
    formats = {"no files", "one file", "many files"}
    nextDouble 可用于获得下一个更大的 double 值,以形成半开区间。)

以下是一个显示格式化和分析的简单例子:

 double[] limits = {1,2,3,4,5,6,7};
 String[] monthNames = {"Sun","Mon","Tue","Wed","Thur","Fri","Sat"};
 ChoiceFormat form = new ChoiceFormat(limits, monthNames);
 ParsePosition status = new ParsePosition(0);
 for (double i = 0.0; i <= 8.0; ++i) {
     status.setIndex(0);
     System.out.println(i + " -> " + form.format(i) + " -> "
                              + form.parse(form.format(i),status));
 }
 
以下是一个较复杂的带模式格式的例子:
 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.getInstance()};
 MessageFormat pattform = new MessageFormat("There {0} on {1}");
 pattform.setFormats(testFormats);
 Object[] testArgs = {null, "ADisk", null};
 for (int i = 0; i < 4; ++i) {
     testArgs[0] = new Integer(i);
     testArgs[2] = testArgs[0];
     System.out.println(pattform.format(testArgs));
 }
 

为 ChoiceFormat 对象指定一个模式是相当直接的。例如:

 ChoiceFormat fmt = new ChoiceFormat(
      "-1#is negative| 0#is zero or fraction | 1#is one |1.0<is 1+ |2#is two |2<is more than 2.");
 System.out.println("Formatter Pattern : " + fmt.toPattern());

 System.out.println("Format with -INF : " + fmt.format(Double.NEGATIVE_INFINITY));
 System.out.println("Format with -1.0 : " + fmt.format(-1.0));
 System.out.println("Format with 0 : " + fmt.format(0));
 System.out.println("Format with 0.9 : " + fmt.format(0.9));
 System.out.println("Format with 1.0 : " + fmt.format(1));
 System.out.println("Format with 1.5 : " + fmt.format(1.5));
 System.out.println("Format with 2 : " + fmt.format(2));
 System.out.println("Format with 2.1 : " + fmt.format(2.1));
 System.out.println("Format with NaN : " + fmt.format(Double.NaN));
 System.out.println("Format with +INF : " + fmt.format(Double.POSITIVE_INFINITY));
 
输出结果类似如下:
 
 
 
 
 
 
Format with -INF : is negative Format with -1.0 : is negative Format with 0 : is zero or fraction Format with 0.9 : is zero or fraction Format with 1.0 : is one Format with 1.5 : is 1+ Format with 2 : is two Format with 2.1 : is more than 2. Format with NaN : is negative Format with +INF : is more than 2.

Synchronization

Choice 格式不是同步的。建议为每个线程创建单独的格式实例。如果多个线程并发访问一个格式,则它必须保持外部同步。

另请参见:
DecimalFormat, MessageFormat, 序列化表格

嵌套类摘要
 
从类 java.text.NumberFormat 继承的嵌套类/接口
NumberFormat.Field
 
字段摘要
 
从类 java.text.NumberFormat 继承的字段
FRACTION_FIELD, INTEGER_FIELD
 
构造方法摘要
ChoiceFormat(double[] limits, String[] formats)
          根据 limit 和相应的 format 构造。
ChoiceFormat(String newPattern)
          根据 limit 和相应的 format(基于模式)构造。
 
方法摘要
 voidapplyPattern(String newPattern)
          设置模式。
 Objectclone()
          重写 Cloneable
 booleanequals(Object obj)
          两个对象之间的相等性比较。
 StringBufferformat(double number, StringBuffer toAppendTo, FieldPosition status)
          返回带已格式化 double 的模式。
 StringBufferformat(long number, StringBuffer toAppendTo, FieldPosition status)
          特殊格式。
 Object[]getFormats()
          获得传入构造方法的 format。
 double[]getLimits()
          获得传入构造方法的 limit。
 inthashCode()
          为消息格式对象生成哈希码。
static doublenextDouble(double d)
          查找大于 d 的最小 double 值。
static doublenextDouble(double d, boolean positive)
          查找大于 d 的最小 double 值(如果 positive == true),或者小于 d 的最大值(如果 positive == false)。
 Numberparse(String text, ParsePosition status)
          分析输入文本中的一个 Number。
static doublepreviousDouble(double d)
          查找小于 d 的最大 double 值。
 voidsetChoices(double[] limits, String[] formats)
          设置要在格式化中使用的 choice。
 StringtoPattern()
          得到模式。
 
从类 java.text.NumberFormat 继承的方法
format, format, format, getAvailableLocales, getCurrency, getCurrencyInstance, getCurrencyInstance, getInstance, getInstance, getIntegerInstance, getIntegerInstance, getMaximumFractionDigits, getMaximumIntegerDigits, getMinimumFractionDigits, getMinimumIntegerDigits, getNumberInstance, getNumberInstance, getPercentInstance, getPercentInstance, isGroupingUsed, isParseIntegerOnly, parse, parseObject, setCurrency, setGroupingUsed, setMaximumFractionDigits, setMaximumIntegerDigits, setMinimumFractionDigits, setMinimumIntegerDigits, setParseIntegerOnly
 
从类 java.text.Format 继承的方法
format, formatToCharacterIterator, parseObject
 
从类 java.lang.Object 继承的方法
finalize, getClass, notify, notifyAll, toString, wait, wait, wait
 

构造方法详细信息

ChoiceFormat

public ChoiceFormat(String newPattern)
根据 limit 和相应的 format(基于模式)构造。

另请参见:
applyPattern(java.lang.String)

ChoiceFormat

public ChoiceFormat(double[] limits,
                    String[] formats)
根据 limit 和相应的 format 构造。

另请参见:
setChoices(double[], java.lang.String[])
方法详细信息

applyPattern

public void applyPattern(String newPattern)
设置模式。

参数:
newPattern - 参见类描述。

toPattern

public String toPattern()
得到模式。


setChoices

public void setChoices(double[] limits,
                       String[] formats)
设置要在格式化中使用的 choice。

参数:
limits - 包含要使用该格式进行分析的最大值,应该按升序排列。格式化 X 时,如果 limit[i] <= X < limit[i+1],则 choice 为 i。如果 limit 数组不按升序排列,则格式化的结果将不正确。
formats - 要为每个 limit 使用的格式。格式可以是 Format 对象或 String。用对象 Y 格式化时,如果该对象是一个 NumberFormat,则调用 ((NumberFormat) Y).format(X)。否则调用 Y.toString()。

getLimits

public double[] getLimits()
获得传入构造方法的 limit。

返回:
limit。

getFormats

public Object[] getFormats()
获得传入构造方法的 format。

返回:
format。

format

public StringBuffer format(long number,
                           StringBuffer toAppendTo,
                           FieldPosition status)
特殊格式。此方法真正调用 format(double, StringBuffer, FieldPosition),这样,受支持的 long 范围只等于 double 存储的范围。这不是一个实用的限制。

指定者:
NumberFormat 中的 format
另请参见:
Format.format(java.lang.Object)