クラスChoiceFormat
- すべての実装されたインタフェース:
Serializable
,Cloneable
ChoiceFormat
は、数値の範囲にフォーマットをアタッチできるNumberFormat
の具象サブクラスです。 通常、複数形を処理するためにMessageFormat
で使用されます。 この選択項目はdoubleの昇順リストで指定され、それぞれの項目は、次の項目までの一方が開いた間隔を指定します。
一致するものがない場合、数値(X)が小さすぎるのか大きすぎるのかによって、最初または最後のインデックスが使用されます。 リミット配列が昇順でない場合、フォーマットの結果は正しくならない。 ChoiceFormatはまた、X matches j if and only if limit[j] ≤ X < limit[j+1]
\u221E
をinfinity(INF)と同等なものとして受け付けます。
ノート: ChoiceFormat
は、他のFormat
クラスとは次の点で異なります。ChoiceFormat
オブジェクトは、getInstance
スタイル・ファクトリ・メソッドではなく、コンストラクタで作成します。 ChoiceFormat
では、指定されたロケールに対して複雑なセット・アップは必要ないので、ファクトリ・メソッドは不要です。 実際、ChoiceFormat
には、ロケール固有の動作は実装されません。
Patterns
ChoiceFormat
パターンの構文は次のとおりです。
ノート:関係≤は<=と同等ではありません
- Pattern:
- SubPattern *("|" SubPattern)
- SubPattern:
- 関係書式の制限
- ノート: 追加の各SubPatternには、リミット・リレーションの昇順の間隔が必要です
- 制限:
- 数値 / "∞" / "-∞"
- 番号:
- ["-"] *(Digit) 1*(Decimal / Digit) *(Digit) [指数]
- Decimal:
- 1*(Digit ".") / 1*("." Digit)
- Digit:
- 0 - 9
- Exponent:
- *(Digit)桁ExponentSymbol桁 *(Digit)
- ExponentSymbol:
- "e" / "E"
- 関連:
- "#" / "<" / "≤"
- 書式:
- 特殊パターン文字「|」以外の任意の文字
「フォーマット」パターン内で予約済の特殊パターン文字を使用するには、単一引用符で囲む必要があります。 たとえば、new ChoiceFormat("1#'|'foo'|'").format(1)
は"|foo|"
を返します。 1行に2つの一重引用符を使用して、リテラル一重引用符を生成します。 たとえば、new ChoiceFormat("1# ''one'' ").format(1)
は" 'one' "
を返します。
使用方法
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を取得して、一方が開いた間隔を作るのに使用する)
次に、値を書式設定および解析するための配列を含むChoiceFormatを構成する例を示します。
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));
}
Stringパターンを使用して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(fmt.format(Double.NEGATIVE_INFINITY)); // outputs "is negative"
System.out.println(fmt.format(-1.0)); // outputs "is negative"
System.out.println(fmt.format(0)); // outputs "is zero or fraction"
System.out.println(fmt.format(0.9)); // outputs "is zero or fraction"
System.out.println(fmt.format(1)); // outputs "is one"
System.out.println(fmt.format(1.5)); // outputs "is 1+"
System.out.println(fmt.format(2)); // outputs "is two"
System.out.println(fmt.format(2.1)); // outputs "is more than 2."
System.out.println(fmt.format(Double.NaN)); // outputs "is negative"
System.out.println(fmt.format(Double.POSITIVE_INFINITY)); // outputs "is more than 2."
より高度なパターンの場合、ChoiceFormat
をMessageFormat
とともに使用して、単数形および複数形の正確な形式を生成できます。
MessageFormat msgFmt = new MessageFormat("The disk \"{0}\" contains {1}.");
double[] fileLimits = {0,1,2};
String[] filePart = {"no files","one file","{1,number} files"};
ChoiceFormat fileChoices = new ChoiceFormat(fileLimits, filePart);
msgFmt.setFormatByArgumentIndex(1, fileChoices);
Object[] args = {"MyDisk", 1273};
System.out.println(msgFmt.format(args));
fileCount
にさまざまな値を設定した場合の出力結果を次に示します。
The disk "MyDisk" contains no files. The disk "MyDisk" contains one file. The disk "MyDisk" contains 1,273 files.
ChoiceFormat
パターン内のMessageFormat
パターンに関する注意事項は、MessageFormat
を参照してください。
Synchronization
choiceフォーマットは同期化されません。 スレッドごとに別のフォーマット・インスタンスを作成することをお薦めします。 複数のスレッドがフォーマットに並行してアクセスする場合は、外部的に同期化する必要があります。
- APIのノート:
- サブクラスは、すべての不適切な場合に
IllegalArgumentException
をスローすることで、より一貫性のあるパターン検証を実行できます。 不正なパターンに関するこの実装の動作については、Implementation Note
を参照してください。このクラスは、使用しない
NumberFormat
からインスタンス・メソッドを継承します。サブクラスは、このようなメソッドに対してUnsupportedOperationException
をオーバーライドおよびスローできます。 - 実装上のノート:
- パターンが正しくないと、この実装は例外をスローするか、成功して不正な部分を破棄する可能性があります。
NumberFormatException
は、limit
を数値として解析できない場合にスローされ、SubPattern
がないか、間隔が昇順でない場合にIllegalArgumentException
がスローされます。 不正な部分を破棄すると、limits
およびformats
が空であるChoiceFormatが発生する可能性があります。 - 導入されたバージョン:
- 1.1
- 関連項目:
-
ネストされたクラスのサマリー
クラスjava.text.NumberFormatで宣言されたネストされたクラス/インタフェース
NumberFormat.Field, NumberFormat.Style
-
フィールドのサマリー
クラスjava.text.NumberFormatで宣言されたフィールド
FRACTION_FIELD, INTEGER_FIELD
-
コンストラクタのサマリー
コンストラクタコンストラクタ説明ChoiceFormat
(double[] limits, String[] formats) 指定されたリミットとそれに対応するフォーマットによりオブジェクトを構築します。ChoiceFormat
(String newPattern) パターンに基づいて制限および対応する形式を持つChoiceFormatを構築します。 -
メソッドのサマリー
修飾子と型メソッド説明void
applyPattern
(String newPattern) 指定されたパターンをこのChoiceFormatオブジェクトに適用します。clone()
Cloneableをオーバーライドします。boolean
指定されたオブジェクトをこのChoiceFormat
と比較し、等しいかどうかを確認します。format
(double number, StringBuffer toAppendTo, FieldPosition status) フォーマットされたdoubleでパターンを返します。format
(long number, StringBuffer toAppendTo, FieldPosition status) フォーマットの特殊化です。Object[]
このChoiceFormatのフォーマットを返します。double[]
このChoiceFormatの制限を返します。int
hashCode()
このChoiceFormat
のハッシュ・コードを返します。static final double
nextDouble
(double d) d
より大きな最小のdoubleを見つけます。static double
nextDouble
(double d, boolean positive) d
(positive
がtrue
の場合)より大きな最小のdouble、またはd
(positive
がfalse
の場合)より小さな最大のdoubleを見つけます。parse
(String text, ParsePosition status) 入力テキストからNumberを解析します。static final double
previousDouble
(double d) d
より小さな最大のdoubleを見つけます。void
setChoices
(double[] limits, String[] formats) フォーマットの際に使用する選択項目を設定します。このChoiceFormatオブジェクトのlimits
およびformats
を表すパターンstring
を返します。toString()
デバッグ用に、このChoiceFormat
を識別する文字列を返します。クラスjava.text.NumberFormatで宣言されたメソッド
format, format, format, getAvailableLocales, getCompactNumberInstance, getCompactNumberInstance, getCurrency, getCurrencyInstance, getCurrencyInstance, getInstance, getInstance, getIntegerInstance, getIntegerInstance, getMaximumFractionDigits, getMaximumIntegerDigits, getMinimumFractionDigits, getMinimumIntegerDigits, getNumberInstance, getNumberInstance, getPercentInstance, getPercentInstance, getRoundingMode, isGroupingUsed, isParseIntegerOnly, isStrict, parse, parseObject, setCurrency, setGroupingUsed, setMaximumFractionDigits, setMaximumIntegerDigits, setMinimumFractionDigits, setMinimumIntegerDigits, setParseIntegerOnly, setRoundingMode, setStrict
クラスjava.text.Formatで宣言されたメソッド
format, formatToCharacterIterator, parseObject
-
コンストラクタの詳細
-
ChoiceFormat
public ChoiceFormat(String newPattern) パターンに基づいて制限および対応する形式を持つChoiceFormatを構築します。 ChoiceFormatパターンの構文およびエラーに関連する注意事項は、「パターン」セクションにあります。ChoiceFormat(double[], String[])
とは異なり、limits
が昇順でない場合、このコンストラクタはIllegalArgumentException
をスローします。- パラメータ:
newPattern
- 新しいパターン文字列- 例外:
NullPointerException
-newPattern
がnull
の場合IllegalArgumentException
-newPattern
がパターン構文に違反している場合- 関連項目:
-
ChoiceFormat
public ChoiceFormat(double[] limits, String[] formats) 指定されたリミットとそれに対応するフォーマットによりオブジェクトを構築します。- パラメータ:
limits
- 昇順のリミットformats
- 対応するフォーマット文字列- 例外:
NullPointerException
-limits
またはformats
がnull
の場合IllegalArgumentException
-limits
とformats
の長さが等しくない場合- 関連項目:
-
-
メソッドの詳細
-
applyPattern
public void applyPattern(String newPattern) 指定されたパターンをこのChoiceFormatオブジェクトに適用します。 ChoiceFormatパターンの構文およびエラーに関連する注意事項は、「パターン」セクションにあります。setChoices(double[], String[])
とは異なり、limits
が昇順でない場合、このメソッドはIllegalArgumentException
をスローします。- パラメータ:
newPattern
- パターン文字列- 例外:
NullPointerException
-newPattern
がnull
の場合IllegalArgumentException
-newPattern
がパターン構文に違反している場合- 関連項目:
-
toPattern
public String toPattern()このChoiceFormatオブジェクトのlimits
およびformats
を表すパターンstring
を返します。 戻されるstring
は、applyPattern(String)
またはChoiceFormat(String)
のいずれかに渡される同じ入力string
であることは保証されません。- 戻り値:
- このChoiceFormatオブジェクトの
limits
およびformats
を表すパターンstring
- 関連項目:
-
setChoices
public void setChoices(double[] limits, String[] formats) フォーマットの際に使用する選択項目を設定します。- パラメータ:
limits
- そのフォーマットで解析する1番大きい値。これは昇順でなければならない。 Xをフォーマットする場合、limit[i] ≤ X < limit[i+1]であれば、選択項目はiになる。 リミット配列が昇順でない場合、フォーマットの結果は正しくならない。formats
- それぞれのリミットに対して使用するフォーマット。- 例外:
NullPointerException
-limits
またはformats
がnull
の場合IllegalArgumentException
-limits
とformats
の長さが等しくない場合
-
getLimits
public double[] getLimits()このChoiceFormatの制限を返します。- 戻り値:
- このChoiceFormatの制限
-
getFormats
-
format
public StringBuffer format(long number, StringBuffer toAppendTo, FieldPosition status) フォーマットの特殊化です。 このメソッドは、実際にformat(double, StringBuffer, FieldPosition)
をコールします。 したがって、サポートされているlongの範囲は、doubleで格納できる範囲とのみ等しくなります。 これが実際の制限となることはありません。- 定義:
format
、クラスNumberFormat
- パラメータ:
number
- フォーマットおよび置換される数値。toAppendTo
- テキストが追加される位置。status
- 使用できる状態が返されないことは無視する。- 戻り値:
- フォーマットされたStringBuffer
- 例外:
ArrayIndexOutOfBoundsException
- このChoiceFormatのlimits
またはformats
が空の場合NullPointerException
-toAppendTo
がnull
の場合- 関連項目:
-
format
public StringBuffer format(double number, StringBuffer toAppendTo, FieldPosition status) フォーマットされたdoubleでパターンを返します。- 定義:
format
、クラスNumberFormat
- パラメータ:
number
- フォーマットおよび置換される数値。toAppendTo
- テキストが追加される位置。status
- 使用できる状態が返されないことは無視する。- 戻り値:
- フォーマットされたStringBuffer
- 例外:
ArrayIndexOutOfBoundsException
- このChoiceFormatのlimits
またはformats
が空の場合NullPointerException
-toAppendTo
がnull
の場合- 関連項目:
-
parse
public Number parse(String text, ParsePosition status) 入力テキストからNumberを解析します。- 定義:
parse
、クラスNumberFormat
- パラメータ:
text
- ソース・テキスト。status
- 入出力パラメータ。 入力時には、status.indexフィールドは解析されるソース・テキストの最初の文字を示す。 出口でエラーが発生しなかった場合は、status.indexはソース・テキスト内の解析されていない最初の文字に設定される。 出口でエラーが発生した場合は、status.indexは変更されず、status.errorIndexは解析が失敗した原因となった文字の最初のインデックスに設定される。- 戻り値:
- 解析された数値を表すNumber。
- 例外:
NullPointerException
-status
がnull
の場合、またはtext
がnull
で、選択文字列のリストが空でない場合。- 関連項目:
-
nextDouble
public static final double nextDouble(double d) d
より大きな最小のdoubleを見つけます。NaN
の場合は、同じ値を返します。一方が開いた間隔を作るのに使用します。
- 実装上のノート:
- これは、
Math.nextUp(d)
の呼出しと同等です。 - パラメータ:
d
- 参照値- 戻り値:
d
より大きい最小倍精度値- 関連項目:
-
nextDouble
public static double nextDouble(double d, boolean positive) d
(positive
がtrue
の場合)より大きな最小のdouble、またはd
(positive
がfalse
の場合)より小さな最大のdoubleを見つけます。NaN
の場合は、同じ値を返します。- 実装上のノート:
- これは、
positive ? Math.nextUp(d) : Math.nextDown(d)
の呼出しと同等です。 - パラメータ:
d
- 参照値positive
- 最小のdoubleが必要な場合はtrue
、そうでない場合はfalse
- 戻り値:
- 最小または最大のdouble値
-
previousDouble
public static final double previousDouble(double d) d
より小さな最大のdoubleを見つけます。NaN
の場合は、同じ値を返します。- 実装上のノート:
- これは、
Math.nextDown(d)
の呼出しと同等です。 - パラメータ:
d
- 参照値- 戻り値:
d
より小さな最大のdouble- 関連項目:
-
clone
-
hashCode
public int hashCode()このChoiceFormat
のハッシュ・コードを返します。- オーバーライド:
hashCode
、クラスNumberFormat
- 実装要件:
- このメソッドは、
getFormats()
およびgetLimits()
によって返される値を使用してハッシュ・コード値を計算します。 - 戻り値:
- この
ChoiceFormat
のハッシュ・コード - 関連項目:
-
toString
-
equals
public boolean equals(Object obj) 指定されたオブジェクトをこのChoiceFormat
と比較し、等しいかどうかを確認します。 オブジェクトがChoiceFormat
でもあり、2つの書式で値が同じ書式である場合、trueを返します。- オーバーライド:
equals
、クラスNumberFormat
- 実装要件:
- このメソッドは、
instanceof
ではなく、getClass()
に基づくクラス・アイデンティティの概念を使用して等価チェックを実行します。 したがって、サブクラスのequalsメソッドでは、このクラスのインスタンスはサブクラスのインスタンスと等しく比較されません。 - パラメータ:
obj
- 等価性のために比較されるオブジェクト- 戻り値:
- 指定されたオブジェクトがこの
ChoiceFormat
と等しい場合はtrue
- 関連項目:
-