Java : ChoiceFormat - API使用例
ChoiceFormat (Java SE 18 & JDK 18) の使用例まとめです。
だいたいのメソッドを網羅済みです。
API仕様のおともにどうぞ。
概要
ChoiceFormat クラスは、数値の範囲ごとに対応する文字列へとフォーマットします。
また、ChoiceFormat は Locale (言語、国・地域) には依存していません。
final double[] limits = {-1.0, 0.0, ChoiceFormat.nextDouble(0.0)};
System.out.println(Arrays.toString(limits)); // [-1.0, 0.0, 4.9E-324]
final String[] formats = {"Negative!", "Zero!", "Positive!"};
final var format = new ChoiceFormat(limits, formats);
// x < limits[0] ... x < -1.0 ... Negative!
// limits[0] <= x < limits[1] ... -1.0 <= x < 0.0 ... Negative!
// limits[1] <= x < limits[2] ... 0.0 <= x < 4.9E-314 ... Zero!
// limits[2] <= x ... 4.9E-314 <= x ... Positive!
System.out.println(format.format(-0.123)); // Negative!
System.out.println(format.format(-456.0)); // Negative!
System.out.println(format.format(0.0)); // Zero!
System.out.println(format.format(789.0)); // Positive!
NumberFormatで宣言されたフィールド
FRACTION_FIELD, INTEGER_FIELD
「Java API 使用例 : NumberFormat」をご参照ください。
コンストラクタ
ChoiceFormat (double[] limits, String[] formats)
final double[] limits = {-1.0, 0.0, ChoiceFormat.nextDouble(0.0)};
System.out.println(Arrays.toString(limits)); // [-1.0, 0.0, 4.9E-324]
final String[] formats = {"Negative!", "Zero!", "Positive!"};
final var format = new ChoiceFormat(limits, formats);
// x < limits[0] ... x < -1.0 ... Negative!
// limits[0] <= x < limits[1] ... -1.0 <= x < 0.0 ... Negative!
// limits[1] <= x < limits[2] ... 0.0 <= x < 4.9E-314 ... Zero!
// limits[2] <= x ... 4.9E-314 <= x ... Positive!
System.out.println(format.format(-0.123)); // Negative!
System.out.println(format.format(-456.0)); // Negative!
System.out.println(format.format(0.0)); // Zero!
System.out.println(format.format(789.0)); // Positive!
ChoiceFormat (String newPattern)
final var pattern = "0#no files| 1#one file| 1<many files";
final var format = new ChoiceFormat(pattern);
System.out.println(format.format(0.0)); // no files
System.out.println(format.format(0.5)); // no files
System.out.println(format.format(1.0)); // one file
System.out.println(format.format(1.5)); // many files
System.out.println(format.format(2.0)); // many files
メソッド
void applyPattern (String newPattern)
final var format = new ChoiceFormat("1#one| 2#two");
System.out.println(format.toPattern()); // 1.0#one|2.0#two
System.out.println(format.format(1.0)); // one
System.out.println(format.format(2.0)); // two
format.applyPattern("1#first|2#second");
System.out.println(format.toPattern()); // 1.0#first|2.0#second
System.out.println(format.format(1.0)); // first
System.out.println(format.format(2.0)); // second
Object clone ()
final var format = new ChoiceFormat("1#one| 2#two");
System.out.println(format.toPattern()); // 1.0#one|2.0#two
if (format.clone() instanceof ChoiceFormat cloned) {
System.out.println(format.toPattern()); // 1.0#one|2.0#two
System.out.println(format.equals(cloned)); // true
}
boolean equals (Object obj)
final var format1 = new ChoiceFormat("1#one| 2#two");
final var format2 = new ChoiceFormat("1#one| 2#two");
final var format3 = new ChoiceFormat("1#first|2#second");
System.out.println(format1.equals(format2)); // true
System.out.println(format1.equals(format3)); // false
System.out.println(format2.equals(format3)); // false
StringBuffer format (double number, StringBuffer toAppendTo, FieldPosition status)
final var number = 2.0;
final var format = new ChoiceFormat("1#one| 2#two");
System.out.println(format.format(number)); // two
final var sb = new StringBuffer("num = ");
System.out.println(sb); // num =
final var status = new FieldPosition(NumberFormat.INTEGER_FIELD);
final var ret = format.format(number, sb, status);
System.out.println(ret); // num = two
// status は有効な値を返しません。
System.out.println(status.getBeginIndex()); // 0
System.out.println(status.getEndIndex()); // 0
StringBuffer format (long number, StringBuffer toAppendTo, FieldPosition status)
このメソッドは format((double)number, toAppendTo, status) と同等です。
API使用例はそちらをご参照ください。
Object[] getFormats ()
final var format = new ChoiceFormat(
new double[]{1.0, 2.0},
new String[]{"one", "two"}
);
System.out.println(Arrays.toString(format.getLimits())); // [1.0, 2.0]
System.out.println(Arrays.toString(format.getFormats())); // [one, two]
System.out.println(format.format(1.0)); // one
System.out.println(format.format(2.0)); // two
format.setChoices(
new double[]{1.0, 2.0, 3.0},
new String[]{"first", "second", "third"}
);
System.out.println(Arrays.toString(format.getLimits())); // [1.0, 2.0, 3.0]
System.out.println(Arrays.toString(format.getFormats())); // [first, second, third]
System.out.println(format.format(1.0)); // first
System.out.println(format.format(2.0)); // second
System.out.println(format.format(3.0)); // third
double[] getLimits ()
このメソッドの使用例は、getFormats() にまとめて記載しました。
そちらのAPI使用例をご参照ください。
int hashCode ()
final var format = new ChoiceFormat(
new double[]{1.0, 2.0},
new String[]{"one", "two"}
);
System.out.println(format.hashCode()); // 115278
final var format = new ChoiceFormat("1#first|2#second");
System.out.println(format.hashCode()); // -906279818
static final double nextDouble (double d)
final var ret1 = ChoiceFormat.nextDouble(-1.0);
System.out.println(ret1); // -0.9999999999999999
final var ret2 = ChoiceFormat.nextDouble(0.0);
System.out.println(ret2); // 4.9E-324
final var ret3 = ChoiceFormat.nextDouble(1.0);
System.out.println(ret3); // 1.0000000000000002
static double nextDouble (double d, boolean positive)
final var ret1 = ChoiceFormat.nextDouble(-1.0, true);
System.out.println(ret1); // -0.9999999999999999
final var ret2 = ChoiceFormat.nextDouble(-1.0, false);
System.out.println(ret2); // -1.0000000000000002
final var ret1 = ChoiceFormat.nextDouble(0.0, true);
System.out.println(ret1); // 4.9E-324
final var ret2 = ChoiceFormat.nextDouble(0.0, false);
System.out.println(ret2); // -4.9E-324
final var ret1 = ChoiceFormat.nextDouble(1.0, true);
System.out.println(ret1); // 1.0000000000000002
final var ret2 = ChoiceFormat.nextDouble(1.0, false);
System.out.println(ret2); // 0.9999999999999999
Number parse (String text, ParsePosition status)
final var format = new ChoiceFormat(
new double[]{1.0, 2.0},
new String[]{"one", "two"}
);
final var source = "one two";
final var pos = new ParsePosition(0);
final var ret1 = format.parse(source, pos);
System.out.println(ret1); // 1.0
System.out.println(pos.getIndex()); // 3
pos.setIndex(pos.getIndex() + 1);
final var ret2 = format.parse(source, pos);
System.out.println(ret2); // 2.0
System.out.println(pos.getIndex()); // 7
final var ret3 = format.parse(source, pos);
System.out.println(ret3); // NaN
static final double previousDouble (double d)
final var ret1 = ChoiceFormat.previousDouble(-1.0);
System.out.println(ret1); // -1.0000000000000002
final var ret2 = ChoiceFormat.previousDouble(0.0);
System.out.println(ret2); // -4.9E-324
final var ret3 = ChoiceFormat.previousDouble(1.0);
System.out.println(ret3); // 0.9999999999999999
void setChoices (double[] limits, String[] formats)
このメソッドの使用例は、getFormats() にまとめて記載しました。
そちらのAPI使用例をご参照ください。
String toPattern ()
このメソッドの使用例は、applyPattern(String newPattern) にまとめて記載しました。
そちらのAPI使用例をご参照ください。
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, parse, parseObject, setCurrency, setGroupingUsed, setMaximumFractionDigits, setMaximumIntegerDigits, setMinimumFractionDigits, setMinimumIntegerDigits, setParseIntegerOnly, setRoundingMode
「Java API 使用例 : NumberFormat」をご参照ください。
Formatで宣言されたメソッド
format, formatToCharacterIterator, parseObject
「Java API 使用例 : Format」をご参照ください。