jdk/src/java.base/share/classes/java/util/Formattable.java
author mchung
Fri, 22 May 2015 16:43:39 -0700
changeset 30789 9eca83469588
parent 29015 82642b0f0945
child 32108 aa5490a167ee
permissions -rw-r--r--
8074431: Remove native2ascii tool Reviewed-by: erikj, alanb, okutsu, mfang, naoto
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
23010
6dadb192ad81 8029235: Update copyright year to match last edit in jdk8 jdk repository for 2013
lana
parents: 18774
diff changeset
     2
 * Copyright (c) 2003, 2013, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package java.util;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * The <tt>Formattable</tt> interface must be implemented by any class that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * needs to perform custom formatting using the <tt>'s'</tt> conversion
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * specifier of {@link java.util.Formatter}.  This interface allows basic
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * control for formatting arbitrary objects.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * For example, the following class prints out different representations of a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * stock's name depending on the flags and length constraints:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 *
29015
82642b0f0945 8073347: javadoc of Formattable messed up by JDK-8019857
bpb
parents: 25859
diff changeset
    39
 * <pre> {@code
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 *   import java.nio.CharBuffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 *   import java.util.Formatter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 *   import java.util.Formattable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 *   import java.util.Locale;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 *   import static java.util.FormattableFlags.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 *
29015
82642b0f0945 8073347: javadoc of Formattable messed up by JDK-8019857
bpb
parents: 25859
diff changeset
    46
 *   ...
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 *   public class StockName implements Formattable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 *       private String symbol, companyName, frenchCompanyName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 *       public StockName(String symbol, String companyName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 *                        String frenchCompanyName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 *           ...
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 *       }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 *       ...
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 *       public void formatTo(Formatter fmt, int f, int width, int precision) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 *           StringBuilder sb = new StringBuilder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 *           // decide form of name
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 *           String name = companyName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 *           if (fmt.locale().equals(Locale.FRANCE))
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 *               name = frenchCompanyName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 *           boolean alternate = (f & ALTERNATE) == ALTERNATE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 *           boolean usesymbol = alternate || (precision != -1 && precision < 10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 *           String out = (usesymbol ? symbol : name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 *           // apply precision
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 *           if (precision == -1 || out.length() < precision) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 *               // write it all
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 *               sb.append(out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 *           } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 *               sb.append(out.substring(0, precision - 1)).append('*');
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 *           }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 *           // apply width and justification
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 *           int len = sb.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 *           if (len < width)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 *               for (int i = 0; i < width - len; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 *                   if ((f & LEFT_JUSTIFY) == LEFT_JUSTIFY)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 *                       sb.append(' ');
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 *                   else
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 *                       sb.insert(0, ' ');
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 *           fmt.format(sb.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 *       }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
 *       public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
 *           return String.format("%s - %s", symbol, companyName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
 *       }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
 *   }
29015
82642b0f0945 8073347: javadoc of Formattable messed up by JDK-8019857
bpb
parents: 25859
diff changeset
    92
 * }</pre>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
 * <p> When used in conjunction with the {@link java.util.Formatter}, the above
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
 * class produces the following output for various format strings.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
 *
29015
82642b0f0945 8073347: javadoc of Formattable messed up by JDK-8019857
bpb
parents: 25859
diff changeset
    97
 * <pre> {@code
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
 *   Formatter fmt = new Formatter();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
 *   StockName sn = new StockName("HUGE", "Huge Fruit, Inc.",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
 *                                "Fruit Titanesque, Inc.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
 *   fmt.format("%s", sn);                   //   -> "Huge Fruit, Inc."
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
 *   fmt.format("%s", sn.toString());        //   -> "HUGE - Huge Fruit, Inc."
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
 *   fmt.format("%#s", sn);                  //   -> "HUGE"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
 *   fmt.format("%-10.8s", sn);              //   -> "HUGE      "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
 *   fmt.format("%.12s", sn);                //   -> "Huge Fruit,*"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
 *   fmt.format(Locale.FRANCE, "%25s", sn);  //   -> "   Fruit Titanesque, Inc."
29015
82642b0f0945 8073347: javadoc of Formattable messed up by JDK-8019857
bpb
parents: 25859
diff changeset
   107
 * }</pre>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
 * <p> Formattables are not necessarily safe for multithreaded access.  Thread
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
 * safety is optional and may be enforced by classes that extend and implement
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
 * this interface.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
 * <p> Unless otherwise specified, passing a <tt>null</tt> argument to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
 * any method in this interface will cause a {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
 * NullPointerException} to be thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
 * @since  1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
public interface Formattable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * Formats the object using the provided {@link Formatter formatter}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     * @param  formatter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     *         The {@link Formatter formatter}.  Implementing classes may call
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     *         {@link Formatter#out() formatter.out()} or {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     *         Formatter#locale() formatter.locale()} to obtain the {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     *         Appendable} or {@link Locale} used by this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     *         <tt>formatter</tt> respectively.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * @param  flags
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     *         The flags modify the output format.  The value is interpreted as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     *         a bitmask.  Any combination of the following flags may be set:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     *         {@link FormattableFlags#LEFT_JUSTIFY}, {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     *         FormattableFlags#UPPERCASE}, and {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     *         FormattableFlags#ALTERNATE}.  If no flags are set, the default
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     *         formatting of the implementing class will apply.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * @param  width
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     *         The minimum number of characters to be written to the output.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     *         If the length of the converted value is less than the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     *         <tt>width</tt> then the output will be padded by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     *         <tt>'&nbsp;&nbsp;'</tt> until the total number of characters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     *         equals width.  The padding is at the beginning by default.  If
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     *         the {@link FormattableFlags#LEFT_JUSTIFY} flag is set then the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     *         padding will be at the end.  If <tt>width</tt> is <tt>-1</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     *         then there is no minimum.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * @param  precision
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     *         The maximum number of characters to be written to the output.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     *         The precision is applied before the width, thus the output will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     *         be truncated to <tt>precision</tt> characters even if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     *         <tt>width</tt> is greater than the <tt>precision</tt>.  If
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     *         <tt>precision</tt> is <tt>-1</tt> then there is no explicit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     *         limit on the number of characters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * @throws  IllegalFormatException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     *          If any of the parameters are invalid.  For specification of all
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     *          possible formatting errors, see the <a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     *          href="../util/Formatter.html#detail">Details</a> section of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     *          formatter class specification.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    void formatTo(Formatter formatter, int flags, int width, int precision);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
}