< prev index next >
src/java.base/share/classes/java/util/Formatter.java
Print this page
*** 1,7 ****
/*
! * Copyright (c) 2003, 2017, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
--- 1,7 ----
/*
! * Copyright (c) 2003, 2018, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
*** 282,296 ****
* denoted by an upper-case character (i.e. {@code 'B'}, {@code 'H'},
* {@code 'S'}, {@code 'C'}, {@code 'X'}, {@code 'E'}, {@code 'G'},
* {@code 'A'}, and {@code 'T'}) are the same as those for the corresponding
* lower-case conversion characters except that the result is converted to
* upper case according to the rules of the prevailing {@link java.util.Locale
! * Locale}. The result is equivalent to the following invocation of {@link
! * String#toUpperCase(Locale)}
*
- * <pre>
- * out.toUpperCase(Locale.getDefault(Locale.Category.FORMAT)) </pre>
*
* <table class="striped">
* <caption style="display:none">genConv</caption>
* <thead>
* <tr><th scope="col" style="vertical-align:bottom"> Conversion
--- 282,296 ----
* denoted by an upper-case character (i.e. {@code 'B'}, {@code 'H'},
* {@code 'S'}, {@code 'C'}, {@code 'X'}, {@code 'E'}, {@code 'G'},
* {@code 'A'}, and {@code 'T'}) are the same as those for the corresponding
* lower-case conversion characters except that the result is converted to
* upper case according to the rules of the prevailing {@link java.util.Locale
! * Locale}. If there is no explicit locale specified, either at the
! * construction of the instance or as a parameter to its method
! * invocation, then the {@link java.util.Locale.Category#FORMAT default locale}
! * is used.
*
*
* <table class="striped">
* <caption style="display:none">genConv</caption>
* <thead>
* <tr><th scope="col" style="vertical-align:bottom"> Conversion
*** 707,721 ****
* <p> Conversions denoted by an upper-case character (i.e. {@code 'B'},
* {@code 'H'}, {@code 'S'}, {@code 'C'}, {@code 'X'}, {@code 'E'},
* {@code 'G'}, {@code 'A'}, and {@code 'T'}) are the same as those for the
* corresponding lower-case conversion characters except that the result is
* converted to upper case according to the rules of the prevailing {@link
! * java.util.Locale Locale}. The result is equivalent to the following
! * invocation of {@link String#toUpperCase(Locale)}
! *
! * <pre>
! * out.toUpperCase(Locale.getDefault(Locale.Category.FORMAT)) </pre>
*
* <h4><a id="dgen">General</a></h4>
*
* <p> The following general conversions may be applied to any argument type:
*
--- 707,720 ----
* <p> Conversions denoted by an upper-case character (i.e. {@code 'B'},
* {@code 'H'}, {@code 'S'}, {@code 'C'}, {@code 'X'}, {@code 'E'},
* {@code 'G'}, {@code 'A'}, and {@code 'T'}) are the same as those for the
* corresponding lower-case conversion characters except that the result is
* converted to upper case according to the rules of the prevailing {@link
! * java.util.Locale Locale}. If there is no explicit locale specified,
! * either at the construction of the instance or as a parameter to its method
! * invocation, then the {@link java.util.Locale.Category#FORMAT default locale}
! * is used.
*
* <h4><a id="dgen">General</a></h4>
*
* <p> The following general conversions may be applied to any argument type:
*
*** 2895,2914 ****
case Conversion.HEXADECIMAL_FLOAT:
printFloat(arg, l);
break;
case Conversion.CHARACTER:
case Conversion.CHARACTER_UPPER:
! printCharacter(arg);
break;
case Conversion.BOOLEAN:
! printBoolean(arg);
break;
case Conversion.STRING:
printString(arg, l);
break;
case Conversion.HASHCODE:
! printHashCode(arg);
break;
case Conversion.LINE_SEPARATOR:
a.append(System.lineSeparator());
break;
case Conversion.PERCENT_SIGN:
--- 2894,2913 ----
case Conversion.HEXADECIMAL_FLOAT:
printFloat(arg, l);
break;
case Conversion.CHARACTER:
case Conversion.CHARACTER_UPPER:
! printCharacter(arg, l);
break;
case Conversion.BOOLEAN:
! printBoolean(arg, l);
break;
case Conversion.STRING:
printString(arg, l);
break;
case Conversion.HASHCODE:
! printHashCode(arg, l);
break;
case Conversion.LINE_SEPARATOR:
a.append(System.lineSeparator());
break;
case Conversion.PERCENT_SIGN:
*** 2919,2929 ****
}
}
private void printInteger(Object arg, Locale l) throws IOException {
if (arg == null)
! print("null");
else if (arg instanceof Byte)
print(((Byte)arg).byteValue(), l);
else if (arg instanceof Short)
print(((Short)arg).shortValue(), l);
else if (arg instanceof Integer)
--- 2918,2928 ----
}
}
private void printInteger(Object arg, Locale l) throws IOException {
if (arg == null)
! print("null", l);
else if (arg instanceof Byte)
print(((Byte)arg).byteValue(), l);
else if (arg instanceof Short)
print(((Short)arg).shortValue(), l);
else if (arg instanceof Integer)
*** 2936,2946 ****
failConversion(c, arg);
}
private void printFloat(Object arg, Locale l) throws IOException {
if (arg == null)
! print("null");
else if (arg instanceof Float)
print(((Float)arg).floatValue(), l);
else if (arg instanceof Double)
print(((Double)arg).doubleValue(), l);
else if (arg instanceof BigDecimal)
--- 2935,2945 ----
failConversion(c, arg);
}
private void printFloat(Object arg, Locale l) throws IOException {
if (arg == null)
! print("null", l);
else if (arg instanceof Float)
print(((Float)arg).floatValue(), l);
else if (arg instanceof Double)
print(((Double)arg).doubleValue(), l);
else if (arg instanceof BigDecimal)
*** 2949,2959 ****
failConversion(c, arg);
}
private void printDateTime(Object arg, Locale l) throws IOException {
if (arg == null) {
! print("null");
return;
}
Calendar cal = null;
// Instead of Calendar.setLenient(true), perhaps we should
--- 2948,2958 ----
failConversion(c, arg);
}
private void printDateTime(Object arg, Locale l) throws IOException {
if (arg == null) {
! print("null", l);
return;
}
Calendar cal = null;
// Instead of Calendar.setLenient(true), perhaps we should
*** 2980,2992 ****
// Use the provided locale so that invocations of
// localizedMagnitude() use optimizations for null.
print(cal, c, l);
}
! private void printCharacter(Object arg) throws IOException {
if (arg == null) {
! print("null");
return;
}
String s = null;
if (arg instanceof Character) {
s = ((Character)arg).toString();
--- 2979,2991 ----
// Use the provided locale so that invocations of
// localizedMagnitude() use optimizations for null.
print(cal, c, l);
}
! private void printCharacter(Object arg, Locale l) throws IOException {
if (arg == null) {
! print("null", l);
return;
}
String s = null;
if (arg instanceof Character) {
s = ((Character)arg).toString();
*** 3009,3019 ****
else
throw new IllegalFormatCodePointException(i);
} else {
failConversion(c, arg);
}
! print(s);
}
private void printString(Object arg, Locale l) throws IOException {
if (arg instanceof Formattable) {
Formatter fmt = Formatter.this;
--- 3008,3018 ----
else
throw new IllegalFormatCodePointException(i);
} else {
failConversion(c, arg);
}
! print(s, l);
}
private void printString(Object arg, Locale l) throws IOException {
if (arg instanceof Formattable) {
Formatter fmt = Formatter.this;
*** 3022,3063 ****
((Formattable)arg).formatTo(fmt, f.valueOf(), width, precision);
} else {
if (f.contains(Flags.ALTERNATE))
failMismatch(Flags.ALTERNATE, 's');
if (arg == null)
! print("null");
else
! print(arg.toString());
}
}
! private void printBoolean(Object arg) throws IOException {
String s;
if (arg != null)
s = ((arg instanceof Boolean)
? ((Boolean)arg).toString()
: Boolean.toString(true));
else
s = Boolean.toString(false);
! print(s);
}
! private void printHashCode(Object arg) throws IOException {
String s = (arg == null
? "null"
: Integer.toHexString(arg.hashCode()));
! print(s);
}
! private void print(String s) throws IOException {
if (precision != -1 && precision < s.length())
s = s.substring(0, precision);
if (f.contains(Flags.UPPERCASE))
! s = s.toUpperCase(Locale.getDefault(Locale.Category.FORMAT));
appendJustified(a, s);
}
private Appendable appendJustified(Appendable a, CharSequence cs) throws IOException {
if (width == -1) {
return a.append(cs);
}
boolean padRight = f.contains(Flags.LEFT_JUSTIFY);
--- 3021,3067 ----
((Formattable)arg).formatTo(fmt, f.valueOf(), width, precision);
} else {
if (f.contains(Flags.ALTERNATE))
failMismatch(Flags.ALTERNATE, 's');
if (arg == null)
! print("null", l);
else
! print(arg.toString(), l);
}
}
! private void printBoolean(Object arg, Locale l) throws IOException {
String s;
if (arg != null)
s = ((arg instanceof Boolean)
? ((Boolean)arg).toString()
: Boolean.toString(true));
else
s = Boolean.toString(false);
! print(s, l);
}
! private void printHashCode(Object arg, Locale l) throws IOException {
String s = (arg == null
? "null"
: Integer.toHexString(arg.hashCode()));
! print(s, l);
}
! private void print(String s, Locale l) throws IOException {
if (precision != -1 && precision < s.length())
s = s.substring(0, precision);
if (f.contains(Flags.UPPERCASE))
! s = toUpperCaseWithLocale(s, l);
appendJustified(a, s);
}
+ private String toUpperCaseWithLocale(String s, Locale l) {
+ return s.toUpperCase(Objects.requireNonNullElse(l,
+ Locale.getDefault(Locale.Category.FORMAT)));
+ }
+
private Appendable appendJustified(Appendable a, CharSequence cs) throws IOException {
if (width == -1) {
return a.append(cs);
}
boolean padRight = f.contains(Flags.LEFT_JUSTIFY);
*** 3274,3284 ****
sb.append(f.contains(Flags.UPPERCASE) ? "0X" : "0x");
if (f.contains(Flags.ZERO_PAD)) {
trailingZeros(sb, width - len);
}
if (f.contains(Flags.UPPERCASE))
! s = s.toUpperCase(Locale.getDefault(Locale.Category.FORMAT));
sb.append(s);
}
// justify based on width
appendJustified(a, sb);
--- 3278,3288 ----
sb.append(f.contains(Flags.UPPERCASE) ? "0X" : "0x");
if (f.contains(Flags.ZERO_PAD)) {
trailingZeros(sb, width - len);
}
if (f.contains(Flags.UPPERCASE))
! s = toUpperCaseWithLocale(s, l);
sb.append(s);
}
// justify based on width
appendJustified(a, sb);
*** 3349,3359 ****
}
if (f.contains(Flags.ZERO_PAD)) {
trailingZeros(sb, width - len);
}
if (f.contains(Flags.UPPERCASE))
! s = s.toUpperCase(Locale.getDefault(Locale.Category.FORMAT));
sb.append(s);
}
// trailing sign indicator
trailingSign(sb, (value.signum() == -1));
--- 3353,3363 ----
}
if (f.contains(Flags.ZERO_PAD)) {
trailingZeros(sb, width - len);
}
if (f.contains(Flags.UPPERCASE))
! s = toUpperCaseWithLocale(s, l);
sb.append(s);
}
// trailing sign indicator
trailingSign(sb, (value.signum() == -1));
*** 3948,3958 ****
StringBuilder sb = new StringBuilder();
print(sb, t, c, l);
// justify based on width
if (f.contains(Flags.UPPERCASE)) {
! appendJustified(a, sb.toString().toUpperCase(Locale.getDefault(Locale.Category.FORMAT)));
} else {
appendJustified(a, sb);
}
}
--- 3952,3962 ----
StringBuilder sb = new StringBuilder();
print(sb, t, c, l);
// justify based on width
if (f.contains(Flags.UPPERCASE)) {
! appendJustified(a, toUpperCaseWithLocale(sb.toString(), l));
} else {
appendJustified(a, sb);
}
}
*** 4130,4141 ****
print(sb, t, DateTime.SECOND, l).append(' ');
// this may be in wrong place for some locales
StringBuilder tsb = new StringBuilder();
print(tsb, t, DateTime.AM_PM, l);
! sb.append(tsb.toString().toUpperCase(Objects.requireNonNullElse(l,
! Locale.getDefault(Locale.Category.FORMAT))));
break;
}
case DateTime.DATE_TIME: { // 'c' (Sat Nov 04 12:02:33 EST 1999)
char sep = ' ';
print(sb, t, DateTime.NAME_OF_DAY_ABBREV, l).append(sep);
--- 4134,4144 ----
print(sb, t, DateTime.SECOND, l).append(' ');
// this may be in wrong place for some locales
StringBuilder tsb = new StringBuilder();
print(tsb, t, DateTime.AM_PM, l);
! sb.append(toUpperCaseWithLocale(tsb.toString(), l));
break;
}
case DateTime.DATE_TIME: { // 'c' (Sat Nov 04 12:02:33 EST 1999)
char sep = ' ';
print(sb, t, DateTime.NAME_OF_DAY_ABBREV, l).append(sep);
*** 4169,4179 ****
private void print(TemporalAccessor t, char c, Locale l) throws IOException {
StringBuilder sb = new StringBuilder();
print(sb, t, c, l);
// justify based on width
if (f.contains(Flags.UPPERCASE)) {
! appendJustified(a, sb.toString().toUpperCase(Locale.getDefault(Locale.Category.FORMAT)));
} else {
appendJustified(a, sb);
}
}
--- 4172,4182 ----
private void print(TemporalAccessor t, char c, Locale l) throws IOException {
StringBuilder sb = new StringBuilder();
print(sb, t, c, l);
// justify based on width
if (f.contains(Flags.UPPERCASE)) {
! appendJustified(a, toUpperCaseWithLocale(sb.toString(), l));
} else {
appendJustified(a, sb);
}
}
*** 4371,4382 ****
print(sb, t, DateTime.MINUTE, l).append(sep);
print(sb, t, DateTime.SECOND, l).append(' ');
// this may be in wrong place for some locales
StringBuilder tsb = new StringBuilder();
print(tsb, t, DateTime.AM_PM, l);
! sb.append(tsb.toString().toUpperCase(Objects.requireNonNullElse(l,
! Locale.getDefault(Locale.Category.FORMAT))));
break;
}
case DateTime.DATE_TIME: { // 'c' (Sat Nov 04 12:02:33 EST 1999)
char sep = ' ';
print(sb, t, DateTime.NAME_OF_DAY_ABBREV, l).append(sep);
--- 4374,4384 ----
print(sb, t, DateTime.MINUTE, l).append(sep);
print(sb, t, DateTime.SECOND, l).append(' ');
// this may be in wrong place for some locales
StringBuilder tsb = new StringBuilder();
print(tsb, t, DateTime.AM_PM, l);
! sb.append(toUpperCaseWithLocale(tsb.toString(), l));
break;
}
case DateTime.DATE_TIME: { // 'c' (Sat Nov 04 12:02:33 EST 1999)
char sep = ' ';
print(sb, t, DateTime.NAME_OF_DAY_ABBREV, l).append(sep);
< prev index next >