モジュール java.base
パッケージ java.text

クラスChoiceFormat

  • すべての実装されたインタフェース:
    Serializable, Cloneable


    public class ChoiceFormat
    extends NumberFormat
    ChoiceFormatを使用すると、ある範囲の数値にフォーマットを追加することができます。 これは通常、複数を処理するときにMessageFormatで使用されます。 この選択項目はdoubleの昇順リストで指定され、それぞれの項目は、次の項目までの一方が開いた間隔を指定します。
     X matches j if and only if limit[j] ≤ X < limit[j+1]
     
    一致するものがない場合、数値(X)が小さすぎるのか大きすぎるのかによって、最初または最後のインデックスが使用されます。 リミット配列が昇順でない場合、フォーマットの結果は正しくならない。 ChoiceFormatはまた、\u221Eをinfinity(INF)と同等なものとして受け付けます。

    注: ChoiceFormatは、他のFormatクラスとは次の点で異なります。ChoiceFormatオブジェクトは、getInstanceスタイル・ファクトリ・メソッドではなく、コンストラクタで作成します。 ChoiceFormatでは、指定されたロケールに対して複雑なセット・アップは必要ないので、ファクトリ・メソッドは不要です。 実際、ChoiceFormatには、ロケール固有の動作は実装されません。

    ChoiceFormatを作成する場合には、フォーマットの配列とリミットの配列を指定する必要があります。 これらの配列の長さは同じである必要があります。 次に例を示します。

    • 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[] dayOfWeekNames = {"Sun","Mon","Tue","Wed","Thur","Fri","Sat"};
     ChoiceFormat form = new ChoiceFormat(limits, dayOfWeekNames);
     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フォーマットは同期化されません。 スレッドごとに別のフォーマット・インスタンスを作成することをお薦めします。 複数のスレッドがフォーマットに並行してアクセスする場合は、外部的に同期化する必要があります。

    導入されたバージョン:
    1.1
    関連項目:
    DecimalFormatMessageFormat直列化された形式
    • コンストラクタの詳細

      • ChoiceFormat

        public ChoiceFormat​(String newPattern)
        指定されたパターンに基づくリミットとそれに対応するフォーマットによりオブジェクトを構築します。
        パラメータ:
        newPattern - 新しいパターン文字列
        例外:
        NullPointerExcpetion - newPatternnullの場合
        関連項目:
        applyPattern(java.lang.String)
      • ChoiceFormat

        public ChoiceFormat​(double[] limits,
                            String[] formats)
        指定されたリミットとそれに対応するフォーマットによりオブジェクトを構築します。
        パラメータ:
        limits - 昇順のリミット
        formats - 対応するフォーマット文字列
        例外:
        NullPointerException - limitsまたはformatsnullの場合
        関連項目:
        setChoices(double[], java.lang.String[])
    • メソッドの詳細

      • applyPattern

        public void applyPattern​(String newPattern)
        パターンを設定します。
        パラメータ:
        newPattern - クラスの説明を参照。
        例外:
        NullPointerException - newPatternnullの場合
      • toPattern

        public String toPattern​()
        パターンを取得します。
        戻り値:
        パターン文字列
      • setChoices

        public void setChoices​(double[] limits,
                               String[] formats)
        フォーマットの際に使用する選択項目を設定します。
        パラメータ:
        limits - そのフォーマットで解析する1番大きい値。これは昇順でなければならない。 Xをフォーマットする場合、limit[i] ≤ X < limit[i+1]であれば、選択項目はiになる。 リミット配列が昇順でない場合、フォーマットの結果は正しくならない。
        formats - それぞれのリミットに対して使用するフォーマット。 これは、Formatオブジェクトか文字列である。 オブジェクトYでフォーマットする場合、オブジェクトがNumberFormatであれば、((NumberFormat) Y).format(X)が呼び出される。 そうでなければ、Y.toString()が呼び出される。
        例外:
        NullPointerException - limitsまたはformatsnullの場合
      • getLimits

        public double[] getLimits​()
        コンストラクタの中で渡されるリミットを取得します。
        戻り値:
        リミット。
      • getFormats

        public Object[] getFormats​()
        コンストラクタの中で渡されるフォーマットを取得します。
        戻り値:
        フォーマット。
      • format

        public StringBuffer format​(long number,
                                   StringBuffer toAppendTo,
                                   FieldPosition status)
        フォーマットの特殊化です。 このメソッドは実際にはformat(double, StringBuffer, FieldPosition)を呼び出します。したがって、サポートされるlongの範囲は、doubleで格納できる範囲に限られます。 これが実際の制限となることはありません。
        定義:
        format、クラス: NumberFormat
        パラメータ:
        number - フォーマットするlong数値
        toAppendTo - フォーマット後のテキストを付加するStringBuffer
        status - フィールドの位置
        戻り値:
        フォーマットされたStringBuffer
        関連項目:
        Format.format(java.lang.Object)
      • format

        public StringBuffer format​(double number,
                                   StringBuffer toAppendTo,
                                   FieldPosition status)
        フォーマットされたdoubleでパターンを返します。
        定義:
        format、クラス: NumberFormat
        パラメータ:
        number - フォーマットおよび置換される数値。
        toAppendTo - テキストが追加される位置。
        status - 使用できる状態が返されないことは無視する。
        戻り値:
        フォーマットされたStringBuffer
        例外:
        NullPointerException - toAppendTonullの場合
        関連項目:
        Format.format(java.lang.Object)
      • parse

        public Number parse​(String text,
                            ParsePosition status)
        入力テキストからNumberを解析します。
        定義:
        parse、クラス: NumberFormat
        パラメータ:
        text - ソース・テキスト。
        status - 入出力パラメータ。 入力時には、status.indexフィールドは解析されるソース・テキストの最初の文字を示す。 出口でエラーが発生しなかった場合は、status.indexはソース・テキスト内の解析されていない最初の文字に設定される。 出口でエラーが発生した場合は、status.indexは変更されず、status.errorIndexは解析が失敗した原因となった文字の最初のインデックスに設定される。
        戻り値:
        解析された数値を表すNumber。
        例外:
        NullPointerException - statusnullの場合、またはtextnullであり、選択文字列のリストが空でない場合。
        関連項目:
        NumberFormat.isParseIntegerOnly(), Format.parseObject(java.lang.String, java.text.ParsePosition)
      • nextDouble

        public static final double nextDouble​(double d)
        dより大きな最小のdoubleを見つけます。 NaNの場合は、同じ値を返します。

        一方が開いた間隔を作るのに使用します。

        パラメータ:
        d - 参照値
        戻り値:
        dより大きな最小のdouble
        関連項目:
        previousDouble(double)
      • previousDouble

        public static final double previousDouble​(double d)
        dより小さな最大のdoubleを見つけます。 NaNの場合は、同じ値を返します。
        パラメータ:
        d - 参照値
        戻り値:
        dより小さな最大のdouble
        関連項目:
        nextDouble(double)
      • clone

        public Object clone​()
        Cloneableをオーバーライドします。
        オーバーライド:
        clone、クラス: NumberFormat
        戻り値:
        このインスタンスの複製。
        関連項目:
        Cloneable
      • equals

        public boolean equals​(Object obj)
        2つの間の等価性比較
        オーバーライド:
        equals、クラス: NumberFormat
        パラメータ:
        obj - 比較対象の参照オブジェクト。
        戻り値:
        このオブジェクトがobj引数と同じである場合はtrue、それ以外の場合はfalse
        関連項目:
        Object.hashCode()HashMap
      • nextDouble

        public static double nextDouble​(double d,
                                        boolean positive)
        d (positivetrueの場合)より大きな最小のdouble、またはd (positivefalseの場合)より小さな最大のdoubleを見つけます。 NaNの場合は、同じ値を返します。 これらのメンバー関数が、Double.longBitsToDouble(long)、Double.doubleToLongBits(double)、Double.isNaN(double)ではない場合、浮動小数点のフラグに影響しません。
        パラメータ:
        d - 参照値
        positive - 最小のdoubleが必要な場合はtrue、そうでない場合はfalse
        戻り値:
        最小または最大のdouble値