src/java.base/share/classes/java/text/Format.java
author jboes
Fri, 20 Sep 2019 11:07:52 +0100
changeset 58242 94bb65cb37d3
parent 57956 e0b8b019d2f5
child 58288 48e480e56aad
permissions -rw-r--r--
8230648: Replace @exception tag with @throws in java.base Summary: Minor coding style update of javadoc tag in any file in java.base Reviewed-by: prappo, lancea
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
54206
003cc64366da 8220249: fix headings in java.compiler
jjg
parents: 47216
diff changeset
     2
 * Copyright (c) 1996, 2019, 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
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * (C) Copyright Taligent, Inc. 1996, 1997 - All Rights Reserved
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * (C) Copyright IBM Corp. 1996 - 1998 - All Rights Reserved
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 *   The original version of this source code and documentation is copyrighted
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * and owned by Taligent, Inc., a wholly-owned subsidiary of IBM. These
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * materials are provided under terms of a License Agreement between Taligent
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * and Sun. This technology is protected by multiple US and International
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * patents. This notice and attribution to Taligent may not be removed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 *   Taligent is a registered trademark of Taligent, Inc.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
package java.text;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import java.io.Serializable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * <code>Format</code> is an abstract base class for formatting locale-sensitive
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * information such as dates, messages, and numbers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * <code>Format</code> defines the programming interface for formatting
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * locale-sensitive objects into <code>String</code>s (the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * <code>format</code> method) and for parsing <code>String</code>s back
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * into objects (the <code>parseObject</code> method).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * Generally, a format's <code>parseObject</code> method must be able to parse
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * any string formatted by its <code>format</code> method. However, there may
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * be exceptional cases where this is not possible. For example, a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * <code>format</code> method might create two adjacent integer numbers with
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * no separator in between, and in this case the <code>parseObject</code> could
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * not tell which digits belong to which number.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 *
54206
003cc64366da 8220249: fix headings in java.compiler
jjg
parents: 47216
diff changeset
    61
 * <h2>Subclassing</h2>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * The Java Platform provides three specialized subclasses of <code>Format</code>--
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * <code>DateFormat</code>, <code>MessageFormat</code>, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * <code>NumberFormat</code>--for formatting dates, messages, and numbers,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * respectively.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * Concrete subclasses must implement three methods:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 * <ol>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 * <li> <code>format(Object obj, StringBuffer toAppendTo, FieldPosition pos)</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 * <li> <code>formatToCharacterIterator(Object obj)</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 * <li> <code>parseObject(String source, ParsePosition pos)</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 * </ol>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 * These general methods allow polymorphic parsing and formatting of objects
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 * and are used, for example, by <code>MessageFormat</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 * Subclasses often also provide additional <code>format</code> methods for
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 * specific input types as well as <code>parse</code> methods for specific
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 * result types. Any <code>parse</code> method that does not take a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 * <code>ParsePosition</code> argument should throw <code>ParseException</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 * when no text in the required format is at the beginning of the input text.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 * Most subclasses will also implement the following factory methods:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 * <ol>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 * <li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
 * <code>getInstance</code> for getting a useful format object appropriate
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
 * for the current locale
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
 * <li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
 * <code>getInstance(Locale)</code> for getting a useful format
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
 * object appropriate for the specified locale
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
 * </ol>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
 * In addition, some subclasses may also implement other
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
 * <code>getXxxxInstance</code> methods for more specialized control. For
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
 * example, the <code>NumberFormat</code> class provides
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
 * <code>getPercentInstance</code> and <code>getCurrencyInstance</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
 * methods for getting specialized number formatters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
 * Subclasses of <code>Format</code> that allow programmers to create objects
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
 * for locales (with <code>getInstance(Locale)</code> for example)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
 * must also implement the following class method:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
 * <blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
 * public static Locale[] getAvailableLocales()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
 * </blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
 * And finally subclasses may define a set of constants to identify the various
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
 * fields in the formatted output. These constants are used to create a FieldPosition
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
 * object which identifies what information is contained in the field and its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
 * position in the formatted result. These constants should be named
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
 * <code><em>item</em>_FIELD</code> where <code><em>item</em></code> identifies
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
 * the field. For examples of these constants, see <code>ERA_FIELD</code> and its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
 * friends in {@link DateFormat}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
 *
54206
003cc64366da 8220249: fix headings in java.compiler
jjg
parents: 47216
diff changeset
   118
 * <h3><a id="synchronization">Synchronization</a></h3>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
 * Formats are generally not synchronized.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
 * It is recommended to create separate format instances for each thread.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
 * If multiple threads access a format concurrently, it must be synchronized
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
 * externally.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
 * @see          java.text.ParsePosition
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
 * @see          java.text.FieldPosition
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
 * @see          java.text.NumberFormat
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
 * @see          java.text.DateFormat
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
 * @see          java.text.MessageFormat
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
 * @author       Mark Davis
45434
4582657c7260 8181082: class-level since tag issues in java.base & java.datatransfer module
mli
parents: 44844
diff changeset
   132
 * @since 1.1
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
public abstract class Format implements Serializable, Cloneable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
57956
e0b8b019d2f5 8229997: Apply java.io.Serial annotations in java.base
darcy
parents: 54206
diff changeset
   136
    @java.io.Serial
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    private static final long serialVersionUID = -299282585814624189L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * Sole constructor.  (For invocation by subclass constructors, typically
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * implicit.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    protected Format() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * Formats an object to produce a string. This is equivalent to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * <blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * {@link #format(Object, StringBuffer, FieldPosition) format}<code>(obj,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     *         new StringBuffer(), new FieldPosition(0)).toString();</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     * </blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * @param obj    The object to format
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * @return       Formatted string.
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 57956
diff changeset
   155
     * @throws    IllegalArgumentException if the Format cannot format the given
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     *            object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    public final String format (Object obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        return format(obj, new StringBuffer(), new FieldPosition(0)).toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     * Formats an object and appends the resulting text to a given string
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     * buffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * If the <code>pos</code> argument identifies a field used by the format,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     * then its indices are set to the beginning and end of the first such
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * field encountered.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     * @param obj    The object to format
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * @param toAppendTo    where the text is to be appended
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     * @param pos    A <code>FieldPosition</code> identifying a field
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     *               in the formatted text
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     * @return       the string buffer passed in as <code>toAppendTo</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     *               with formatted text appended
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 57956
diff changeset
   175
     * @throws    NullPointerException if <code>toAppendTo</code> or
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     *            <code>pos</code> is null
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 57956
diff changeset
   177
     * @throws    IllegalArgumentException if the Format cannot format the given
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     *            object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    public abstract StringBuffer format(Object obj,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                    StringBuffer toAppendTo,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                    FieldPosition pos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     * Formats an Object producing an <code>AttributedCharacterIterator</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     * You can use the returned <code>AttributedCharacterIterator</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     * to build the resulting String, as well as to determine information
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     * about the resulting String.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     * Each attribute key of the AttributedCharacterIterator will be of type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     * <code>Field</code>. It is up to each <code>Format</code> implementation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     * to define what the legal values are for each attribute in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     * <code>AttributedCharacterIterator</code>, but typically the attribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     * key is also used as the attribute value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     * <p>The default implementation creates an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     * <code>AttributedCharacterIterator</code> with no attributes. Subclasses
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     * that support fields should override this and create an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     * <code>AttributedCharacterIterator</code> with meaningful attributes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     *
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 57956
diff changeset
   200
     * @throws    NullPointerException if obj is null.
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 57956
diff changeset
   201
     * @throws    IllegalArgumentException when the Format cannot format the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     *            given object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     * @param obj The object to format
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     * @return AttributedCharacterIterator describing the formatted value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
    public AttributedCharacterIterator formatToCharacterIterator(Object obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        return createAttributedCharacterIterator(format(obj));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     * Parses text from a string to produce an object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     * The method attempts to parse text starting at the index given by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     * <code>pos</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     * If parsing succeeds, then the index of <code>pos</code> is updated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     * to the index after the last character used (parsing does not necessarily
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     * use all characters up to the end of the string), and the parsed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     * object is returned. The updated <code>pos</code> can be used to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     * indicate the starting point for the next call to this method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     * If an error occurs, then the index of <code>pos</code> is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     * changed, the error index of <code>pos</code> is set to the index of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     * the character where the error occurred, and null is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     * @param source A <code>String</code>, part of which should be parsed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     * @param pos A <code>ParsePosition</code> object with index and error
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     *            index information as described above.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     * @return An <code>Object</code> parsed from the string. In case of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     *         error, returns null.
34328
0534cb944d0e 8073211: javadoc of Format parseObject methods should specify NullPointerExceptions
okutsu
parents: 25859
diff changeset
   230
     * @throws NullPointerException if {@code source} or {@code pos} is null.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
    public abstract Object parseObject (String source, ParsePosition pos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     * Parses text from the beginning of the given string to produce an object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     * The method may not use the entire text of the given string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     * @param source A <code>String</code> whose beginning should be parsed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     * @return An <code>Object</code> parsed from the string.
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 57956
diff changeset
   240
     * @throws    ParseException if the beginning of the specified string
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     *            cannot be parsed.
34328
0534cb944d0e 8073211: javadoc of Format parseObject methods should specify NullPointerExceptions
okutsu
parents: 25859
diff changeset
   242
     * @throws NullPointerException if {@code source} is null.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
    public Object parseObject(String source) throws ParseException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        ParsePosition pos = new ParsePosition(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        Object result = parseObject(source, pos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        if (pos.index == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
            throw new ParseException("Format.parseObject(String) failed",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
                pos.errorIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     * Creates and returns a copy of this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     * @return a clone of this instance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
    public Object clone() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
            return super.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        } catch (CloneNotSupportedException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
            // will never happen
10419
12c063b39232 7084245: Update usages of InternalError to use exception chaining
sherman
parents: 5506
diff changeset
   264
            throw new InternalError(e);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
    // Convenience methods for creating AttributedCharacterIterators from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
    // different parameters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     * Creates an <code>AttributedCharacterIterator</code> for the String
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     * <code>s</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     * @param s String to create AttributedCharacterIterator from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     * @return AttributedCharacterIterator wrapping s
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
    AttributedCharacterIterator createAttributedCharacterIterator(String s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        AttributedString as = new AttributedString(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        return as.getIterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
    /**
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 19054
diff changeset
   287
     * Creates an <code>AttributedCharacterIterator</code> containing the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     * concatenated contents of the passed in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     * <code>AttributedCharacterIterator</code>s.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     * @param iterators AttributedCharacterIterators used to create resulting
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     *                  AttributedCharacterIterators
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     * @return AttributedCharacterIterator wrapping passed in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     *         AttributedCharacterIterators
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
    AttributedCharacterIterator createAttributedCharacterIterator(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
                       AttributedCharacterIterator[] iterators) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        AttributedString as = new AttributedString(iterators);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        return as.getIterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     * Returns an AttributedCharacterIterator with the String
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
     * <code>string</code> and additional key/value pair <code>key</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
     * <code>value</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
     * @param string String to create AttributedCharacterIterator from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     * @param key Key for AttributedCharacterIterator
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     * @param value Value associated with key in AttributedCharacterIterator
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     * @return AttributedCharacterIterator wrapping args
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
    AttributedCharacterIterator createAttributedCharacterIterator(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
                      String string, AttributedCharacterIterator.Attribute key,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
                      Object value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        AttributedString as = new AttributedString(string);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        as.addAttribute(key, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
        return as.getIterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
     * Creates an AttributedCharacterIterator with the contents of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
     * <code>iterator</code> and the additional attribute <code>key</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
     * <code>value</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
     * @param iterator Initial AttributedCharacterIterator to add arg to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
     * @param key Key for AttributedCharacterIterator
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
     * @param value Value associated with key in AttributedCharacterIterator
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
     * @return AttributedCharacterIterator wrapping args
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
    AttributedCharacterIterator createAttributedCharacterIterator(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
              AttributedCharacterIterator iterator,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
              AttributedCharacterIterator.Attribute key, Object value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
        AttributedString as = new AttributedString(iterator);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
        as.addAttribute(key, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
        return as.getIterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
     * Defines constants that are used as attribute keys in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
     * <code>AttributedCharacterIterator</code> returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
     * from <code>Format.formatToCharacterIterator</code> and as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
     * field identifiers in <code>FieldPosition</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
    public static class Field extends AttributedCharacterIterator.Attribute {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
        // Proclaim serial compatibility with 1.4 FCS
57956
e0b8b019d2f5 8229997: Apply java.io.Serial annotations in java.base
darcy
parents: 54206
diff changeset
   353
        @java.io.Serial
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
        private static final long serialVersionUID = 276966692217360283L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
         * Creates a Field with the specified name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
         * @param name Name of the attribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
        protected Field(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
            super(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
     * FieldDelegate is notified by the various <code>Format</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
     * implementations as they are formatting the Objects. This allows for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
     * storage of the individual sections of the formatted String for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
     * later use, such as in a <code>FieldPosition</code> or for an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
     * <code>AttributedCharacterIterator</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
     * Delegates should NOT assume that the <code>Format</code> will notify
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
     * the delegate of fields in any particular order.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
     *
14014
da3648e13e67 8000269: Cleanup javadoc warnings
alanb
parents: 10419
diff changeset
   377
     * @see FieldPosition#getFieldDelegate
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
     * @see CharacterIteratorFieldDelegate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
    interface FieldDelegate {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
         * Notified when a particular region of the String is formatted. This
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
         * method will be invoked if there is no corresponding integer field id
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
         * matching <code>attr</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
         * @param attr Identifies the field matched
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
         * @param value Value associated with the field
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
         * @param start Beginning location of the field, will be >= 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
         * @param end End of the field, will be >= start and <= buffer.length()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
         * @param buffer Contains current formatted value, receiver should
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
         *        NOT modify it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
        public void formatted(Format.Field attr, Object value, int start,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
                              int end, StringBuffer buffer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
         * Notified when a particular region of the String is formatted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
         * @param fieldID Identifies the field by integer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
         * @param attr Identifies the field matched
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
         * @param value Value associated with the field
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
         * @param start Beginning location of the field, will be >= 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
         * @param end End of the field, will be >= start and <= buffer.length()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
         * @param buffer Contains current formatted value, receiver should
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
         *        NOT modify it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
        public void formatted(int fieldID, Format.Field attr, Object value,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
                              int start, int end, StringBuffer buffer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
}