src/java.base/share/classes/java/text/ChoiceFormat.java
author chegar
Thu, 17 Oct 2019 20:54:25 +0100
branchdatagramsocketimpl-branch
changeset 58679 9c3209ff7550
parent 58678 9cf78a70fa4f
parent 58288 48e480e56aad
permissions -rw-r--r--
datagramsocketimpl-branch: merge with default
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: 51275
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.InvalidObjectException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import java.io.ObjectInputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import java.util.Arrays;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
/**
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
    47
 * A {@code ChoiceFormat} allows you to attach a format to a range of numbers.
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
    48
 * It is generally used in a {@code MessageFormat} for handling plurals.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * The choice is specified with an ascending list of doubles, where each item
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * specifies a half-open interval up to the next item:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * <blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * <pre>
19054
a64012cb49d6 8021108: Clean up doclint warnings and errors in java.text package
peytoia
parents: 18156
diff changeset
    53
 * X matches j if and only if limit[j] &le; X &lt; limit[j+1]
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * </blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * If there is no match, then either the first or last index is used, depending
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * on whether the number (X) is too low or too high.  If the limit array is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * in ascending order, the results of formatting will be incorrect.  ChoiceFormat
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * also accepts <code>&#92;u221E</code> as equivalent to infinity(INF).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * <strong>Note:</strong>
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
    63
 * {@code ChoiceFormat} differs from the other {@code Format}
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
    64
 * classes in that you create a {@code ChoiceFormat} object with a
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
    65
 * constructor (not with a {@code getInstance} style factory
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
    66
 * method). The factory methods aren't necessary because {@code ChoiceFormat}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * doesn't require any complex setup for a given locale. In fact,
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
    68
 * {@code ChoiceFormat} doesn't implement any locale specific behavior.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 * <p>
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
    71
 * When creating a {@code ChoiceFormat}, you must specify an array of formats
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 * and an array of limits. The length of these arrays must be the same.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 * For example,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 * <li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 *     <em>limits</em> = {1,2,3,4,5,6,7}<br>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 *     <em>formats</em> = {"Sun","Mon","Tue","Wed","Thur","Fri","Sat"}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 * <li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 *     <em>limits</em> = {0, 1, ChoiceFormat.nextDouble(1)}<br>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 *     <em>formats</em> = {"no files", "one file", "many files"}<br>
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
    81
 *     ({@code nextDouble} can be used to get the next higher double, to
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 *     make the half-open interval.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 * Here is a simple example that shows formatting and parsing:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
 * <blockquote>
19054
a64012cb49d6 8021108: Clean up doclint warnings and errors in java.text package
peytoia
parents: 18156
diff changeset
    88
 * <pre>{@code
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
 * double[] limits = {1,2,3,4,5,6,7};
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
 * String[] dayOfWeekNames = {"Sun","Mon","Tue","Wed","Thur","Fri","Sat"};
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
 * ChoiceFormat form = new ChoiceFormat(limits, dayOfWeekNames);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
 * ParsePosition status = new ParsePosition(0);
19054
a64012cb49d6 8021108: Clean up doclint warnings and errors in java.text package
peytoia
parents: 18156
diff changeset
    93
 * for (double i = 0.0; i <= 8.0; ++i) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
 *     status.setIndex(0);
19054
a64012cb49d6 8021108: Clean up doclint warnings and errors in java.text package
peytoia
parents: 18156
diff changeset
    95
 *     System.out.println(i + " -> " + form.format(i) + " -> "
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
 *                              + form.parse(form.format(i),status));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
 * }
19054
a64012cb49d6 8021108: Clean up doclint warnings and errors in java.text package
peytoia
parents: 18156
diff changeset
    98
 * }</pre>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
 * </blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
 * Here is a more complex example, with a pattern format:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
 * <blockquote>
19054
a64012cb49d6 8021108: Clean up doclint warnings and errors in java.text package
peytoia
parents: 18156
diff changeset
   102
 * <pre>{@code
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
 * double[] filelimits = {0,1,2};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
 * String[] filepart = {"are no files","is one file","are {2} files"};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
 * ChoiceFormat fileform = new ChoiceFormat(filelimits, filepart);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
 * Format[] testFormats = {fileform, null, NumberFormat.getInstance()};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
 * MessageFormat pattform = new MessageFormat("There {0} on {1}");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
 * pattform.setFormats(testFormats);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
 * Object[] testArgs = {null, "ADisk", null};
19054
a64012cb49d6 8021108: Clean up doclint warnings and errors in java.text package
peytoia
parents: 18156
diff changeset
   110
 * for (int i = 0; i < 4; ++i) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
 *     testArgs[0] = new Integer(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
 *     testArgs[2] = testArgs[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
 *     System.out.println(pattform.format(testArgs));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
 * }
19054
a64012cb49d6 8021108: Clean up doclint warnings and errors in java.text package
peytoia
parents: 18156
diff changeset
   115
 * }</pre>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
 * </blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
 * Specifying a pattern for ChoiceFormat objects is fairly straightforward.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
 * For example:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
 * <blockquote>
19054
a64012cb49d6 8021108: Clean up doclint warnings and errors in java.text package
peytoia
parents: 18156
diff changeset
   121
 * <pre>{@code
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
 * ChoiceFormat fmt = new ChoiceFormat(
19054
a64012cb49d6 8021108: Clean up doclint warnings and errors in java.text package
peytoia
parents: 18156
diff changeset
   123
 *      "-1#is negative| 0#is zero or fraction | 1#is one |1.0<is 1+ |2#is two |2<is more than 2.");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
 * System.out.println("Formatter Pattern : " + fmt.toPattern());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
 * System.out.println("Format with -INF : " + fmt.format(Double.NEGATIVE_INFINITY));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
 * System.out.println("Format with -1.0 : " + fmt.format(-1.0));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
 * System.out.println("Format with 0 : " + fmt.format(0));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
 * System.out.println("Format with 0.9 : " + fmt.format(0.9));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
 * System.out.println("Format with 1.0 : " + fmt.format(1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
 * System.out.println("Format with 1.5 : " + fmt.format(1.5));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
 * System.out.println("Format with 2 : " + fmt.format(2));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
 * System.out.println("Format with 2.1 : " + fmt.format(2.1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
 * System.out.println("Format with NaN : " + fmt.format(Double.NaN));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
 * System.out.println("Format with +INF : " + fmt.format(Double.POSITIVE_INFINITY));
19054
a64012cb49d6 8021108: Clean up doclint warnings and errors in java.text package
peytoia
parents: 18156
diff changeset
   136
 * }</pre>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
 * </blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
 * And the output result would be like the following:
9226
d0aa5fcc8743 7036842: HTML tag mismatch in API doc for ChoiceFormat
peytoia
parents: 5506
diff changeset
   139
 * <blockquote>
19054
a64012cb49d6 8021108: Clean up doclint warnings and errors in java.text package
peytoia
parents: 18156
diff changeset
   140
 * <pre>{@code
a64012cb49d6 8021108: Clean up doclint warnings and errors in java.text package
peytoia
parents: 18156
diff changeset
   141
 * Format with -INF : is negative
a64012cb49d6 8021108: Clean up doclint warnings and errors in java.text package
peytoia
parents: 18156
diff changeset
   142
 * Format with -1.0 : is negative
a64012cb49d6 8021108: Clean up doclint warnings and errors in java.text package
peytoia
parents: 18156
diff changeset
   143
 * Format with 0 : is zero or fraction
a64012cb49d6 8021108: Clean up doclint warnings and errors in java.text package
peytoia
parents: 18156
diff changeset
   144
 * Format with 0.9 : is zero or fraction
a64012cb49d6 8021108: Clean up doclint warnings and errors in java.text package
peytoia
parents: 18156
diff changeset
   145
 * Format with 1.0 : is one
a64012cb49d6 8021108: Clean up doclint warnings and errors in java.text package
peytoia
parents: 18156
diff changeset
   146
 * Format with 1.5 : is 1+
a64012cb49d6 8021108: Clean up doclint warnings and errors in java.text package
peytoia
parents: 18156
diff changeset
   147
 * Format with 2 : is two
a64012cb49d6 8021108: Clean up doclint warnings and errors in java.text package
peytoia
parents: 18156
diff changeset
   148
 * Format with 2.1 : is more than 2.
a64012cb49d6 8021108: Clean up doclint warnings and errors in java.text package
peytoia
parents: 18156
diff changeset
   149
 * Format with NaN : is negative
a64012cb49d6 8021108: Clean up doclint warnings and errors in java.text package
peytoia
parents: 18156
diff changeset
   150
 * Format with +INF : is more than 2.
a64012cb49d6 8021108: Clean up doclint warnings and errors in java.text package
peytoia
parents: 18156
diff changeset
   151
 * }</pre>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
 * </blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
 *
54206
003cc64366da 8220249: fix headings in java.compiler
jjg
parents: 51275
diff changeset
   154
 * <h2><a id="synchronization">Synchronization</a></h2>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
 * Choice formats are not synchronized.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
 * It is recommended to create separate format instances for each thread.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
 * If multiple threads access a format concurrently, it must be synchronized
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
 * externally.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
 * @see          DecimalFormat
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
 * @see          MessageFormat
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
 * @author       Mark Davis
45434
4582657c7260 8181082: class-level since tag issues in java.base & java.datatransfer module
mli
parents: 44844
diff changeset
   166
 * @since 1.1
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
public class ChoiceFormat extends NumberFormat {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    // Proclaim serial compatibility with 1.1 FCS
57956
e0b8b019d2f5 8229997: Apply java.io.Serial annotations in java.base
darcy
parents: 54206
diff changeset
   171
    @java.io.Serial
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    private static final long serialVersionUID = 1795184449645032964L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     * Sets the pattern.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     * @param newPattern See the class description.
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 57956
diff changeset
   177
     * @throws    NullPointerException if {@code newPattern}
43011
36edf207e84c 8169480: Inconsistencies across Format class hierarchy in their API spec and actual implementation of Exceptions
nishjain
parents: 37521
diff changeset
   178
     *            is {@code null}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    public void applyPattern(String newPattern) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        StringBuffer[] segments = new StringBuffer[2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        for (int i = 0; i < segments.length; ++i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
            segments[i] = new StringBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        double[] newChoiceLimits = new double[30];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        String[] newChoiceFormats = new String[30];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        int count = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        int part = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        double startValue = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        double oldStartValue = Double.NaN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        boolean inQuote = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        for (int i = 0; i < newPattern.length(); ++i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
            char ch = newPattern.charAt(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            if (ch=='\'') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
                // Check for "''" indicating a literal quote
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
                if ((i+1)<newPattern.length() && newPattern.charAt(i+1)==ch) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
                    segments[part].append(ch);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
                    ++i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
                    inQuote = !inQuote;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
            } else if (inQuote) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
                segments[part].append(ch);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
            } else if (ch == '<' || ch == '#' || ch == '\u2264') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
                if (segments[0].length() == 0) {
46893
4c5a4cd5b092 6609718: [Fmt-Ch] uninformative exception in ChoiceFormat.applyPattern(String)
nishjain
parents: 45434
diff changeset
   206
                    throw new IllegalArgumentException("Each interval must"
4c5a4cd5b092 6609718: [Fmt-Ch] uninformative exception in ChoiceFormat.applyPattern(String)
nishjain
parents: 45434
diff changeset
   207
                            + " contain a number before a format");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
                }
46893
4c5a4cd5b092 6609718: [Fmt-Ch] uninformative exception in ChoiceFormat.applyPattern(String)
nishjain
parents: 45434
diff changeset
   209
4c5a4cd5b092 6609718: [Fmt-Ch] uninformative exception in ChoiceFormat.applyPattern(String)
nishjain
parents: 45434
diff changeset
   210
                String tempBuffer = segments[0].toString();
4c5a4cd5b092 6609718: [Fmt-Ch] uninformative exception in ChoiceFormat.applyPattern(String)
nishjain
parents: 45434
diff changeset
   211
                if (tempBuffer.equals("\u221E")) {
4c5a4cd5b092 6609718: [Fmt-Ch] uninformative exception in ChoiceFormat.applyPattern(String)
nishjain
parents: 45434
diff changeset
   212
                    startValue = Double.POSITIVE_INFINITY;
4c5a4cd5b092 6609718: [Fmt-Ch] uninformative exception in ChoiceFormat.applyPattern(String)
nishjain
parents: 45434
diff changeset
   213
                } else if (tempBuffer.equals("-\u221E")) {
4c5a4cd5b092 6609718: [Fmt-Ch] uninformative exception in ChoiceFormat.applyPattern(String)
nishjain
parents: 45434
diff changeset
   214
                    startValue = Double.NEGATIVE_INFINITY;
4c5a4cd5b092 6609718: [Fmt-Ch] uninformative exception in ChoiceFormat.applyPattern(String)
nishjain
parents: 45434
diff changeset
   215
                } else {
49510
a7777856ae98 8200364: Remove unnecessary boxing via primitive wrapper valueOf(String) methods
martin
parents: 47216
diff changeset
   216
                    startValue = Double.parseDouble(tempBuffer);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
                }
46893
4c5a4cd5b092 6609718: [Fmt-Ch] uninformative exception in ChoiceFormat.applyPattern(String)
nishjain
parents: 45434
diff changeset
   218
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
                if (ch == '<' && startValue != Double.POSITIVE_INFINITY &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
                        startValue != Double.NEGATIVE_INFINITY) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
                    startValue = nextDouble(startValue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
                if (startValue <= oldStartValue) {
46893
4c5a4cd5b092 6609718: [Fmt-Ch] uninformative exception in ChoiceFormat.applyPattern(String)
nishjain
parents: 45434
diff changeset
   224
                    throw new IllegalArgumentException("Incorrect order of"
4c5a4cd5b092 6609718: [Fmt-Ch] uninformative exception in ChoiceFormat.applyPattern(String)
nishjain
parents: 45434
diff changeset
   225
                            + " intervals, must be in ascending order");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
                segments[0].setLength(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
                part = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
            } else if (ch == '|') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
                if (count == newChoiceLimits.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
                    newChoiceLimits = doubleArraySize(newChoiceLimits);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
                    newChoiceFormats = doubleArraySize(newChoiceFormats);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
                newChoiceLimits[count] = startValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
                newChoiceFormats[count] = segments[1].toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
                ++count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
                oldStartValue = startValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
                segments[1].setLength(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
                part = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
                segments[part].append(ch);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        // clean up last one
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        if (part == 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
            if (count == newChoiceLimits.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
                newChoiceLimits = doubleArraySize(newChoiceLimits);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
                newChoiceFormats = doubleArraySize(newChoiceFormats);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
            newChoiceLimits[count] = startValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
            newChoiceFormats[count] = segments[1].toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
            ++count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        choiceLimits = new double[count];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        System.arraycopy(newChoiceLimits, 0, choiceLimits, 0, count);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        choiceFormats = new String[count];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        System.arraycopy(newChoiceFormats, 0, choiceFormats, 0, count);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     * Gets the pattern.
19054
a64012cb49d6 8021108: Clean up doclint warnings and errors in java.text package
peytoia
parents: 18156
diff changeset
   262
     *
a64012cb49d6 8021108: Clean up doclint warnings and errors in java.text package
peytoia
parents: 18156
diff changeset
   263
     * @return the pattern string
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
    public String toPattern() {
24969
afa6934dd8e8 8041679: Replace uses of StringBuffer with StringBuilder within core library classes
psandoz
parents: 21278
diff changeset
   266
        StringBuilder result = new StringBuilder();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        for (int i = 0; i < choiceLimits.length; ++i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
            if (i != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
                result.append('|');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
            // choose based upon which has less precision
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
            // approximate that by choosing the closest one to an integer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
            // could do better, but it's not worth it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
            double less = previousDouble(choiceLimits[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
            double tryLessOrEqual = Math.abs(Math.IEEEremainder(choiceLimits[i], 1.0d));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
            double tryLess = Math.abs(Math.IEEEremainder(less, 1.0d));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
            if (tryLessOrEqual < tryLess) {
27957
24b4e6082f19 8055723: Replace concat String to append in StringBuilder parameters (dev)
weijun
parents: 25859
diff changeset
   279
                result.append(choiceLimits[i]);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
                result.append('#');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
                if (choiceLimits[i] == Double.POSITIVE_INFINITY) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
                    result.append("\u221E");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
                } else if (choiceLimits[i] == Double.NEGATIVE_INFINITY) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
                    result.append("-\u221E");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
                } else {
27957
24b4e6082f19 8055723: Replace concat String to append in StringBuilder parameters (dev)
weijun
parents: 25859
diff changeset
   287
                    result.append(less);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
                result.append('<');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
            // Append choiceFormats[i], using quotes if there are special characters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
            // Single quotes themselves must be escaped in either case.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
            String text = choiceFormats[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
            boolean needQuote = text.indexOf('<') >= 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
                || text.indexOf('#') >= 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
                || text.indexOf('\u2264') >= 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
                || text.indexOf('|') >= 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
            if (needQuote) result.append('\'');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
            if (text.indexOf('\'') < 0) result.append(text);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
                for (int j=0; j<text.length(); ++j) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
                    char c = text.charAt(j);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
                    result.append(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
                    if (c == '\'') result.append(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
            if (needQuote) result.append('\'');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        return result.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
     * Constructs with limits and corresponding formats based on the pattern.
19054
a64012cb49d6 8021108: Clean up doclint warnings and errors in java.text package
peytoia
parents: 18156
diff changeset
   314
     *
a64012cb49d6 8021108: Clean up doclint warnings and errors in java.text package
peytoia
parents: 18156
diff changeset
   315
     * @param newPattern the new pattern string
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 57956
diff changeset
   316
     * @throws    NullPointerException if {@code newPattern} is
43011
36edf207e84c 8169480: Inconsistencies across Format class hierarchy in their API spec and actual implementation of Exceptions
nishjain
parents: 37521
diff changeset
   317
     *            {@code null}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     * @see #applyPattern
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
    public ChoiceFormat(String newPattern)  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        applyPattern(newPattern);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
     * Constructs with the limits and the corresponding formats.
19054
a64012cb49d6 8021108: Clean up doclint warnings and errors in java.text package
peytoia
parents: 18156
diff changeset
   326
     *
a64012cb49d6 8021108: Clean up doclint warnings and errors in java.text package
peytoia
parents: 18156
diff changeset
   327
     * @param limits limits in ascending order
a64012cb49d6 8021108: Clean up doclint warnings and errors in java.text package
peytoia
parents: 18156
diff changeset
   328
     * @param formats corresponding format strings
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 57956
diff changeset
   329
     * @throws    NullPointerException if {@code limits} or {@code formats}
43011
36edf207e84c 8169480: Inconsistencies across Format class hierarchy in their API spec and actual implementation of Exceptions
nishjain
parents: 37521
diff changeset
   330
     *            is {@code null}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
     * @see #setChoices
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
    public ChoiceFormat(double[] limits, String[] formats) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
        setChoices(limits, formats);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
     * Set the choices to be used in formatting.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
     * @param limits contains the top value that you want
19054
a64012cb49d6 8021108: Clean up doclint warnings and errors in java.text package
peytoia
parents: 18156
diff changeset
   340
     * parsed with that format, and should be in ascending sorted order. When
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
     * formatting X, the choice will be the i, where
19054
a64012cb49d6 8021108: Clean up doclint warnings and errors in java.text package
peytoia
parents: 18156
diff changeset
   342
     * limit[i] &le; X {@literal <} limit[i+1].
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
     * If the limit array is not in ascending order, the results of formatting
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
     * will be incorrect.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
     * @param formats are the formats you want to use for each limit.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
     * They can be either Format objects or Strings.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
     * When formatting with object Y,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
     * if the object is a NumberFormat, then ((NumberFormat) Y).format(X)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
     * is called. Otherwise Y.toString() is called.
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 57956
diff changeset
   350
     * @throws    NullPointerException if {@code limits} or
43011
36edf207e84c 8169480: Inconsistencies across Format class hierarchy in their API spec and actual implementation of Exceptions
nishjain
parents: 37521
diff changeset
   351
     *            {@code formats} is {@code null}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
    public void setChoices(double[] limits, String formats[]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
        if (limits.length != formats.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
            throw new IllegalArgumentException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
                "Array and limit arrays must be of the same length.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
        }
14341
d06c0d29521c 8001209: Evaluate findbugs reprot for java.text.ChoiceFormat
peytoia
parents: 12848
diff changeset
   358
        choiceLimits = Arrays.copyOf(limits, limits.length);
d06c0d29521c 8001209: Evaluate findbugs reprot for java.text.ChoiceFormat
peytoia
parents: 12848
diff changeset
   359
        choiceFormats = Arrays.copyOf(formats, formats.length);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
     * Get the limits passed in the constructor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
     * @return the limits.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
    public double[] getLimits() {
14341
d06c0d29521c 8001209: Evaluate findbugs reprot for java.text.ChoiceFormat
peytoia
parents: 12848
diff changeset
   367
        double[] newLimits = Arrays.copyOf(choiceLimits, choiceLimits.length);
d06c0d29521c 8001209: Evaluate findbugs reprot for java.text.ChoiceFormat
peytoia
parents: 12848
diff changeset
   368
        return newLimits;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
     * Get the formats passed in the constructor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
     * @return the formats.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
    public Object[] getFormats() {
14341
d06c0d29521c 8001209: Evaluate findbugs reprot for java.text.ChoiceFormat
peytoia
parents: 12848
diff changeset
   376
        Object[] newFormats = Arrays.copyOf(choiceFormats, choiceFormats.length);
d06c0d29521c 8001209: Evaluate findbugs reprot for java.text.ChoiceFormat
peytoia
parents: 12848
diff changeset
   377
        return newFormats;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
    // Overrides
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
     * Specialization of format. This method really calls
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   384
     * {@code format(double, StringBuffer, FieldPosition)}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
     * thus the range of longs that are supported is only equal to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
     * the range that can be stored by double. This will never be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
     * a practical limitation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
    public StringBuffer format(long number, StringBuffer toAppendTo,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
                               FieldPosition status) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
        return format((double)number, toAppendTo, status);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
     * Returns pattern with formatted double.
18156
edb590d448c5 8016217: More javadoc warnings
alanb
parents: 14342
diff changeset
   396
     * @param number number to be formatted and substituted.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
     * @param toAppendTo where text is appended.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
     * @param status ignore no useful status is returned.
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 57956
diff changeset
   399
     * @throws    NullPointerException if {@code toAppendTo}
43011
36edf207e84c 8169480: Inconsistencies across Format class hierarchy in their API spec and actual implementation of Exceptions
nishjain
parents: 37521
diff changeset
   400
     *            is {@code null}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
   public StringBuffer format(double number, StringBuffer toAppendTo,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
                               FieldPosition status) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
        // find the number
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
        int i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
        for (i = 0; i < choiceLimits.length; ++i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
            if (!(number >= choiceLimits[i])) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
                // same as number < choiceLimits, except catchs NaN
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
        --i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
        if (i < 0) i = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
        // return either a formatted number, or a string
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
        return toAppendTo.append(choiceFormats[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
     * Parses a Number from the input text.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
     * @param text the source text.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
     * @param status an input-output parameter.  On input, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
     * status.index field indicates the first character of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
     * source text that should be parsed.  On exit, if no error
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 19054
diff changeset
   424
     * occurred, status.index is set to the first unparsed character
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
     * in the source text.  On exit, if an error did occur,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
     * status.index is unchanged and status.errorIndex is set to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
     * first index of the character that caused the parse to fail.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
     * @return A Number representing the value of the number parsed.
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 57956
diff changeset
   429
     * @throws    NullPointerException if {@code status} is {@code null}
43011
36edf207e84c 8169480: Inconsistencies across Format class hierarchy in their API spec and actual implementation of Exceptions
nishjain
parents: 37521
diff changeset
   430
     *            or if {@code text} is {@code null} and the list of
36edf207e84c 8169480: Inconsistencies across Format class hierarchy in their API spec and actual implementation of Exceptions
nishjain
parents: 37521
diff changeset
   431
     *            choice strings is not empty.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
    public Number parse(String text, ParsePosition status) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
        // find the best number (defined as the one with the longest parse)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
        int start = status.index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
        int furthest = start;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
        double bestNumber = Double.NaN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
        double tempNumber = 0.0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
        for (int i = 0; i < choiceFormats.length; ++i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
            String tempString = choiceFormats[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
            if (text.regionMatches(start, tempString, 0, tempString.length())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
                status.index = start + tempString.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
                tempNumber = choiceLimits[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
                if (status.index > furthest) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
                    furthest = status.index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
                    bestNumber = tempNumber;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
                    if (furthest == text.length()) break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
        status.index = furthest;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
        if (status.index == start) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
            status.errorIndex = furthest;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
        }
37521
b6e0f285c998 8145468: update java.lang APIs with new deprecations
smarks
parents: 30655
diff changeset
   455
        return Double.valueOf(bestNumber);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
    /**
19054
a64012cb49d6 8021108: Clean up doclint warnings and errors in java.text package
peytoia
parents: 18156
diff changeset
   459
     * Finds the least double greater than {@code d}.
a64012cb49d6 8021108: Clean up doclint warnings and errors in java.text package
peytoia
parents: 18156
diff changeset
   460
     * If {@code NaN}, returns same value.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
     * <p>Used to make half-open intervals.
19054
a64012cb49d6 8021108: Clean up doclint warnings and errors in java.text package
peytoia
parents: 18156
diff changeset
   462
     *
51257
979e349059eb 8021322: [Fmt-Ch] Implementation of ChoiceFormat math methods should delegate to java.lang.Math methods
nishjain
parents: 49524
diff changeset
   463
     * @implNote This is equivalent to calling
979e349059eb 8021322: [Fmt-Ch] Implementation of ChoiceFormat math methods should delegate to java.lang.Math methods
nishjain
parents: 49524
diff changeset
   464
     * {@link Math#nextUp(double) Math.nextUp(d)}
979e349059eb 8021322: [Fmt-Ch] Implementation of ChoiceFormat math methods should delegate to java.lang.Math methods
nishjain
parents: 49524
diff changeset
   465
     *
19054
a64012cb49d6 8021108: Clean up doclint warnings and errors in java.text package
peytoia
parents: 18156
diff changeset
   466
     * @param d the reference value
a64012cb49d6 8021108: Clean up doclint warnings and errors in java.text package
peytoia
parents: 18156
diff changeset
   467
     * @return the least double value greather than {@code d}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
     * @see #previousDouble
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
    public static final double nextDouble (double d) {
51257
979e349059eb 8021322: [Fmt-Ch] Implementation of ChoiceFormat math methods should delegate to java.lang.Math methods
nishjain
parents: 49524
diff changeset
   471
        return Math.nextUp(d);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
    /**
19054
a64012cb49d6 8021108: Clean up doclint warnings and errors in java.text package
peytoia
parents: 18156
diff changeset
   475
     * Finds the greatest double less than {@code d}.
a64012cb49d6 8021108: Clean up doclint warnings and errors in java.text package
peytoia
parents: 18156
diff changeset
   476
     * If {@code NaN}, returns same value.
a64012cb49d6 8021108: Clean up doclint warnings and errors in java.text package
peytoia
parents: 18156
diff changeset
   477
     *
51257
979e349059eb 8021322: [Fmt-Ch] Implementation of ChoiceFormat math methods should delegate to java.lang.Math methods
nishjain
parents: 49524
diff changeset
   478
     * @implNote This is equivalent to calling
979e349059eb 8021322: [Fmt-Ch] Implementation of ChoiceFormat math methods should delegate to java.lang.Math methods
nishjain
parents: 49524
diff changeset
   479
     * {@link Math#nextDown(double) Math.nextDown(d)}
979e349059eb 8021322: [Fmt-Ch] Implementation of ChoiceFormat math methods should delegate to java.lang.Math methods
nishjain
parents: 49524
diff changeset
   480
     *
19054
a64012cb49d6 8021108: Clean up doclint warnings and errors in java.text package
peytoia
parents: 18156
diff changeset
   481
     * @param d the reference value
a64012cb49d6 8021108: Clean up doclint warnings and errors in java.text package
peytoia
parents: 18156
diff changeset
   482
     * @return the greatest double value less than {@code d}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
     * @see #nextDouble
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
    public static final double previousDouble (double d) {
51257
979e349059eb 8021322: [Fmt-Ch] Implementation of ChoiceFormat math methods should delegate to java.lang.Math methods
nishjain
parents: 49524
diff changeset
   486
        return Math.nextDown(d);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
     * Overrides Cloneable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
    public Object clone()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
        ChoiceFormat other = (ChoiceFormat) super.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
        // for primitives or immutables, shallow clone is enough
12848
da701d422d2c 7117230: clean up warnings in java.text
dbhole
parents: 9226
diff changeset
   496
        other.choiceLimits = choiceLimits.clone();
da701d422d2c 7117230: clean up warnings in java.text
dbhole
parents: 9226
diff changeset
   497
        other.choiceFormats = choiceFormats.clone();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
        return other;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
     * Generates a hash code for the message format object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
    public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
        int result = choiceLimits.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
        if (choiceFormats.length > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
            // enough for reasonable distribution
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
            result ^= choiceFormats[choiceFormats.length-1].hashCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
    /**
30655
d83f50188ca9 8080422: some docs cleanup for core libs
avstepan
parents: 27957
diff changeset
   514
     * Equality comparison between two
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
    public boolean equals(Object obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
        if (obj == null) return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
        if (this == obj)                      // quick check
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
        if (getClass() != obj.getClass())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
        ChoiceFormat other = (ChoiceFormat) obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
        return (Arrays.equals(choiceLimits, other.choiceLimits)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
             && Arrays.equals(choiceFormats, other.choiceFormats));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
     * After reading an object from the input stream, do a simple verification
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
     * to maintain class invariants.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
     * @throws InvalidObjectException if the objects read from the stream is invalid.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
     */
57956
e0b8b019d2f5 8229997: Apply java.io.Serial annotations in java.base
darcy
parents: 54206
diff changeset
   532
    @java.io.Serial
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
    private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
        in.defaultReadObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
        if (choiceLimits.length != choiceFormats.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
            throw new InvalidObjectException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
                    "limits and format arrays of different length.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
    // ===============privates===========================
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
     * A list of lower bounds for the choices.  The formatter will return
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   545
     * {@code choiceFormats[i]} if the number being formatted is greater than or equal to
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   546
     * {@code choiceLimits[i]} and less than {@code choiceLimits[i+1]}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
    private double[] choiceLimits;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
     * A list of choice strings.  The formatter will return
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   553
     * {@code choiceFormats[i]} if the number being formatted is greater than or equal to
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   554
     * {@code choiceLimits[i]} and less than {@code choiceLimits[i+1]}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
    private String[] choiceFormats;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
    /**
19054
a64012cb49d6 8021108: Clean up doclint warnings and errors in java.text package
peytoia
parents: 18156
diff changeset
   560
     * Finds the least double greater than {@code d} (if {@code positive} is
a64012cb49d6 8021108: Clean up doclint warnings and errors in java.text package
peytoia
parents: 18156
diff changeset
   561
     * {@code true}), or the greatest double less than {@code d} (if
a64012cb49d6 8021108: Clean up doclint warnings and errors in java.text package
peytoia
parents: 18156
diff changeset
   562
     * {@code positive} is {@code false}).
a64012cb49d6 8021108: Clean up doclint warnings and errors in java.text package
peytoia
parents: 18156
diff changeset
   563
     * If {@code NaN}, returns same value.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
     *
51257
979e349059eb 8021322: [Fmt-Ch] Implementation of ChoiceFormat math methods should delegate to java.lang.Math methods
nishjain
parents: 49524
diff changeset
   565
     * @implNote This is equivalent to calling
979e349059eb 8021322: [Fmt-Ch] Implementation of ChoiceFormat math methods should delegate to java.lang.Math methods
nishjain
parents: 49524
diff changeset
   566
     * {@code positive ? Math.nextUp(d) : Math.nextDown(d)}
19054
a64012cb49d6 8021108: Clean up doclint warnings and errors in java.text package
peytoia
parents: 18156
diff changeset
   567
     *
a64012cb49d6 8021108: Clean up doclint warnings and errors in java.text package
peytoia
parents: 18156
diff changeset
   568
     * @param d        the reference value
a64012cb49d6 8021108: Clean up doclint warnings and errors in java.text package
peytoia
parents: 18156
diff changeset
   569
     * @param positive {@code true} if the least double is desired;
a64012cb49d6 8021108: Clean up doclint warnings and errors in java.text package
peytoia
parents: 18156
diff changeset
   570
     *                 {@code false} otherwise
a64012cb49d6 8021108: Clean up doclint warnings and errors in java.text package
peytoia
parents: 18156
diff changeset
   571
     * @return the least or greater double value
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
    public static double nextDouble (double d, boolean positive) {
51257
979e349059eb 8021322: [Fmt-Ch] Implementation of ChoiceFormat math methods should delegate to java.lang.Math methods
nishjain
parents: 49524
diff changeset
   574
        return positive ? Math.nextUp(d) : Math.nextDown(d);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
    private static double[] doubleArraySize(double[] array) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
        int oldSize = array.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
        double[] newArray = new double[oldSize * 2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
        System.arraycopy(array, 0, newArray, 0, oldSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
        return newArray;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
    private String[] doubleArraySize(String[] array) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
        int oldSize = array.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
        String[] newArray = new String[oldSize * 2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
        System.arraycopy(array, 0, newArray, 0, oldSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
        return newArray;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
}