|
JavaTM 2 Platform Std. Ed. v1.3 |
|||||||||
前のクラス 次のクラス | フレームあり フレームなし | |||||||||
概要: 内部クラス | フィールド | コンストラクタ | メソッド | 詳細: フィールド | コンストラクタ | メソッド |
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)
フォーマットされた double でパターンを返します。 |
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)
d (if positive == true) より大きな最小の double、または d (if positive == false) より小さな最大の double を見つけます。 |
Number |
parse(String text,
ParsePosition status)
入力テキストから Number を解析します。 |
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 で格納できる範囲に限られます。これが実際の制限となることはありません。NumberFormat
内の format
java.text.NumberFormat
からコピーされたタグ:Format.format(java.lang.Object)
public StringBuffer format(double number, StringBuffer toAppendTo, FieldPosition status)
NumberFormat
内の format
number
- フォーマットおよび置換される数値toAppendTo
- テキストが追加される位置status
- 使用できる状態が返されないことは無視するpublic Number parse(String text, ParsePosition status)
NumberFormat
内の parse
text
- ソーステキストstatus
- 入出力パラメータ。入力時には、status.index フィールドは解析されるソーステキストの最初の文字を示します。出口でエラーが発生しなかった場合は、status.index はソーステキスト内の解析されていない最初の文字に設定されます。出口でエラーが発生した場合は、status.index は変更されず、status.errorIndex は解析が失敗した原因となった文字の最初のインデックスに設定されます。public static final double nextDouble(double d)
一方が開いた間隔を作るのに使用します。
previousDouble(double)
public static final double previousDouble(double d)
nextDouble(double)
public Object clone()
NumberFormat
内の clone
java.lang.Object
からコピーされたタグ:CloneNotSupportedException
- オブジェクトのクラスが Cloneable
インタフェースをサポートしていない場合。clone
メソッドをオーバーライドしたサブクラスも、インスタンスを複製できないことを示すためにこの例外をスローすることがあるOutOfMemoryError
- 十分なメモリがない場合Cloneable
public int hashCode()
NumberFormat
内の hashCode
java.lang.Object
からコピーされたタグ:Object.equals(java.lang.Object)
,
Hashtable
public boolean equals(Object obj)
NumberFormat
内の equals
java.lang.Object
からコピーされたタグ:obj
- 比較対象の参照オブジェクトtrue
、そうでない場合は false
Boolean.hashCode()
,
Hashtable
public static double nextDouble(double d, boolean positive)
|
JavaTM 2 Platform Std. Ed. v1.3 |
|||||||||
前のクラス 次のクラス | フレームあり フレームなし | |||||||||
概要: 内部クラス | フィールド | コンストラクタ | メソッド | 詳細: フィールド | コンストラクタ | メソッド |
Java、Java 2D、JDBC は、米国およびその他の国における米国 Sun Microsystems, Inc. の商標もしくは登録商標です。
Copyright 1993-2000 Sun Microsystems, Inc. 901 San Antonio Road,
Palo Alto, California, 94303, U.S.A. All Rights Reserved.