< prev index next >

src/java.base/share/classes/java/util/Formatter.java

Print this page

        

@@ -1,7 +1,7 @@
 /*
- * Copyright (c) 2003, 2017, Oracle and/or its affiliates. All rights reserved.
+ * 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,15 +282,15 @@
  * 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)}
+ * 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.
  *
- * <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

@@ -707,15 +707,14 @@
  * <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>
+ * 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,20 +2894,20 @@
             case Conversion.HEXADECIMAL_FLOAT:
                 printFloat(arg, l);
                 break;
             case Conversion.CHARACTER:
             case Conversion.CHARACTER_UPPER:
-                printCharacter(arg);
+                printCharacter(arg, l);
                 break;
             case Conversion.BOOLEAN:
-                printBoolean(arg);
+                printBoolean(arg, l);
                 break;
             case Conversion.STRING:
                 printString(arg, l);
                 break;
             case Conversion.HASHCODE:
-                printHashCode(arg);
+                printHashCode(arg, l);
                 break;
             case Conversion.LINE_SEPARATOR:
                 a.append(System.lineSeparator());
                 break;
             case Conversion.PERCENT_SIGN:

@@ -2919,11 +2918,11 @@
             }
         }
 
         private void printInteger(Object arg, Locale l) throws IOException {
             if (arg == null)
-                print("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,11 +2935,11 @@
                 failConversion(c, arg);
         }
 
         private void printFloat(Object arg, Locale l) throws IOException {
             if (arg == null)
-                print("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,11 +2948,11 @@
                 failConversion(c, arg);
         }
 
         private void printDateTime(Object arg, Locale l) throws IOException {
             if (arg == null) {
-                print("null");
+                print("null", l);
                 return;
             }
             Calendar cal = null;
 
             // Instead of Calendar.setLenient(true), perhaps we should

@@ -2980,13 +2979,13 @@
             // Use the provided locale so that invocations of
             // localizedMagnitude() use optimizations for null.
             print(cal, c, l);
         }
 
-        private void printCharacter(Object arg) throws IOException {
+        private void printCharacter(Object arg, Locale l) throws IOException {
             if (arg == null) {
-                print("null");
+                print("null", l);
                 return;
             }
             String s = null;
             if (arg instanceof Character) {
                 s = ((Character)arg).toString();

@@ -3009,11 +3008,11 @@
                 else
                     throw new IllegalFormatCodePointException(i);
             } else {
                 failConversion(c, arg);
             }
-            print(s);
+            print(s, l);
         }
 
         private void printString(Object arg, Locale l) throws IOException {
             if (arg instanceof Formattable) {
                 Formatter fmt = Formatter.this;

@@ -3022,42 +3021,47 @@
                 ((Formattable)arg).formatTo(fmt, f.valueOf(), width, precision);
             } else {
                 if (f.contains(Flags.ALTERNATE))
                     failMismatch(Flags.ALTERNATE, 's');
                 if (arg == null)
-                    print("null");
+                    print("null", l);
                 else
-                    print(arg.toString());
+                    print(arg.toString(), l);
             }
         }
 
-        private void printBoolean(Object arg) throws IOException {
+        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);
+            print(s, l);
         }
 
-        private void printHashCode(Object arg) throws IOException {
+        private void printHashCode(Object arg, Locale l) throws IOException {
             String s = (arg == null
                         ? "null"
                         : Integer.toHexString(arg.hashCode()));
-            print(s);
+            print(s, l);
         }
 
-        private void print(String s) throws IOException {
+        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 = s.toUpperCase(Locale.getDefault(Locale.Category.FORMAT));
+                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,11 +3278,11 @@
                     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));
+                    s = toUpperCaseWithLocale(s, l);
                 sb.append(s);
             }
 
             // justify based on width
             appendJustified(a, sb);

@@ -3349,11 +3353,11 @@
                 }
                 if (f.contains(Flags.ZERO_PAD)) {
                     trailingZeros(sb, width - len);
                 }
                 if (f.contains(Flags.UPPERCASE))
-                    s = s.toUpperCase(Locale.getDefault(Locale.Category.FORMAT));
+                    s = toUpperCaseWithLocale(s, l);
                 sb.append(s);
             }
 
             // trailing sign indicator
             trailingSign(sb, (value.signum() == -1));

@@ -3948,11 +3952,11 @@
             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)));
+                appendJustified(a, toUpperCaseWithLocale(sb.toString(), l));
             } else {
                 appendJustified(a, sb);
             }
         }
 

@@ -4130,12 +4134,11 @@
                 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))));
+                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,11 +4172,11 @@
         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)));
+                appendJustified(a, toUpperCaseWithLocale(sb.toString(), l));
             } else {
                 appendJustified(a, sb);
             }
         }
 

@@ -4371,12 +4374,11 @@
                     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))));
+                    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 >