|
Java プラットフォーム 1.2 |
|||||||||
前のクラス 次のクラス | フレームあり フレームなし | |||||||||
概要: 内部クラス | フィールド | コンストラクタ | メソッド | 詳細: フィールド | コンストラクタ | メソッド |
java.lang.Object | +--java.text.Format | +--java.text.NumberFormat | +--java.text.ChoiceFormat
ChoiceFormat
を使用すると、ある範囲の数値にフォーマットを追加することができます。これは通常、複数を処理するときに MessageFormat
で使用されます。この選択項目は double の昇順リストで指定され、それぞれの項目は、次の項目までの一方が開いた間隔を指定します。
一致するものがないと、数値 (X) が小さ過ぎるのか大き過ぎるのかによって、最初か最後のインデックスが使用されます。リミット配列が昇順でない場合、フォーマットの結果は正しくありません。ChoiceFormat はまた、X は、limit[j] <= X < limit[j+1] の場合にだけ j に一致する
¥¥u221E
を infinity(INF) と同等なものとして受け付けます。
注:
ChoiceFormat
は、ほかの Format
クラスとは次の点で異なります。ChoiceFormat
オブジェクトは、getInstance
スタイルファクトリメソッドではなく、コンストラクタで作成します。ChoiceFormat
では、指定されたロケールに対して複雑なセットアップは必要ないので、ファクトリメソッドは不要です。実際、ChoiceFormat
には、ロケール固有の動作は実装されません。
ChoiceFormat を作成する場合には、フォーマットの配列とリミットの配列を指定する必要があります。これらの配列の長さは同じでなければなりません。次に例を示します。
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.
DecimalFormat
,
MessageFormat
, 直列化された形式クラス java.text.NumberFormat から継承したフィールド |
FRACTION_FIELD,
INTEGER_FIELD |
コンストラクタの概要 | |
ChoiceFormat(double[] limits,
String[] formats)
指定されたリミットとそれに対応するフォーマットによりオブジェクトを構築します。 |
|
ChoiceFormat(String newPattern)
指定されたパターンに基づくリミットとそれに対応するフォーマットによりオブジェクトを構築します。 |
メソッドの概要 | |
void |
applyPattern(String newPattern)
パターンを設定します。 |
Object |
clone()
Cloneable をオーバーライドします。 |
boolean |
equals(Object obj)
2 つが等しいかどうかを比較します。 |
StringBuffer |
format(double number,
StringBuffer toAppendTo,
FieldPosition status)
|
StringBuffer |
format(long number,
StringBuffer toAppendTo,
FieldPosition status)
フォーマットの特殊化です。 |
Object[] |
getFormats()
コンストラクタの中で渡されるフォーマットを取得します。 |
double[] |
getLimits()
コンストラクタの中で渡されるリミットを取得します。 |
int |
hashCode()
メッセージフォーマットオブジェクトのハッシュコードを生成します。 |
static double |
nextDouble(double d)
d より大きな最小の double を見つけます。 |
static double |
nextDouble(double d,
boolean positive)
|
Number |
parse(String text,
ParsePosition status)
|
static double |
previousDouble(double d)
d より小さな最大の double を見つけます。 |
void |
setChoices(double[] limits,
String[] formats)
フォーマットの際に使用する選択項目を設定します。 |
String |
toPattern()
パターンを取得します。 |
クラス java.text.Format から継承したメソッド |
format,
parseObject |
クラス java.lang.Object から継承したメソッド |
finalize,
getClass,
notify,
notifyAll,
toString,
wait,
wait,
wait |
コンストラクタの詳細 |
public ChoiceFormat(String newPattern)
public ChoiceFormat(double[] limits, String[] formats)
setChoices(double[], java.lang.String[])
メソッドの詳細 |
public void applyPattern(String newPattern)
newPattern
- クラスの説明を参照public String toPattern()
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() が呼び出されるpublic double[] getLimits()
public Object[] getFormats()
public StringBuffer format(long number, StringBuffer toAppendTo, FieldPosition status)
format(double, StringBuffer, FieldPosition)
を呼び出します。したがって、サポートされる long の範囲は、double で格納できる範囲に限られます。これが実際の制限となることはありません。public StringBuffer format(double number, StringBuffer toAppendTo, FieldPosition status)
public Number parse(String text, ParsePosition status)
public static final double nextDouble(double d)
一方が開いた間隔を作るのに使用します。
previousDouble(double)
public static final double previousDouble(double d)
nextDouble(double)
public Object clone()
public int hashCode()
public boolean equals(Object obj)
public static double nextDouble(double d, boolean positive)
|
Java プラットフォーム 1.2 |
|||||||||
前のクラス 次のクラス | フレームあり フレームなし | |||||||||
概要: 内部クラス | フィールド | コンストラクタ | メソッド | 詳細: フィールド | コンストラクタ | メソッド |