jdk/src/share/classes/java/time/PeriodParser.java
author sherman
Tue, 22 Jan 2013 20:59:21 -0800
changeset 15289 3ac550392e43
permissions -rw-r--r--
8003680: JSR 310 Date/Time API Summary: Integration of JSR310 Date/Time API for M6 Reviewed-by: alanb, naoto, dholmes Contributed-by: scolebourne@joda.org, roger.riggs@oracle.com, richard.warburton@gmail.com, misterm@gmail.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
     1
/*
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
     2
 * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
     4
 *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    10
 *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    15
 * accompanied this code).
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    16
 *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    20
 *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    23
 * questions.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    24
 */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    25
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    26
/*
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    27
 * This file is available under and governed by the GNU General Public
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    28
 * License version 2 only, as published by the Free Software Foundation.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    29
 * However, the following notice accompanied the original version of this
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    30
 * file:
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    31
 *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    32
 * Copyright (c) 2009-2012, Stephen Colebourne & Michael Nascimento Santos
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    33
 *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    34
 * All rights reserved.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    35
 *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    36
 * Redistribution and use in source and binary forms, with or without
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    37
 * modification, are permitted provided that the following conditions are met:
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    38
 *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    39
 *  * Redistributions of source code must retain the above copyright notice,
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    40
 *    this list of conditions and the following disclaimer.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    41
 *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    42
 *  * Redistributions in binary form must reproduce the above copyright notice,
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    43
 *    this list of conditions and the following disclaimer in the documentation
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    44
 *    and/or other materials provided with the distribution.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    45
 *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    46
 *  * Neither the name of JSR-310 nor the names of its contributors
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    47
 *    may be used to endorse or promote products derived from this software
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    48
 *    without specific prior written permission.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    49
 *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    50
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    51
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    52
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    53
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    54
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    55
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    56
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    57
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    58
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    59
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    60
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    61
 */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    62
package java.time;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    63
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    64
import java.time.format.DateTimeParseException;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    65
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    66
/**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    67
 * A period parser that creates an instance of {@code Period} from a string.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    68
 * This parses the ISO-8601 period format {@code PnYnMnDTnHnMn.nS}.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    69
 * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    70
 * This class is mutable and intended for use by a single thread.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    71
 *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    72
 * @since 1.8
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    73
 */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    74
final class PeriodParser {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    75
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    76
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    77
     * Used to validate the correct sequence of tokens.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    78
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    79
    private static final String TOKEN_SEQUENCE = "PYMDTHMS";
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    80
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    81
     * The standard string representing a zero period.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    82
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    83
    private static final String ZERO = "PT0S";
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    84
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    85
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    86
     * The number of years.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    87
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    88
    private int years;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    89
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    90
     * The number of months.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    91
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    92
    private int months;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    93
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    94
     * The number of days.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    95
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    96
    private int days;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    97
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    98
     * The number of hours.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    99
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   100
    private int hours;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   101
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   102
     * The number of minutes.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   103
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   104
    private int minutes;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   105
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   106
     * The number of seconds.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   107
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   108
    private int seconds;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   109
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   110
     * The number of nanoseconds.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   111
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   112
    private long nanos;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   113
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   114
     * Whether the seconds were negative.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   115
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   116
    private boolean negativeSecs;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   117
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   118
     * Parser position index.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   119
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   120
    private int index;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   121
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   122
     * Original text.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   123
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   124
    private CharSequence text;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   125
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   126
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   127
     * Constructor.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   128
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   129
     * @param text  the text to parse, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   130
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   131
    PeriodParser(CharSequence text) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   132
        this.text = text;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   133
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   134
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   135
    //-----------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   136
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   137
     * Performs the parse.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   138
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   139
     * This parses the text set in the constructor in the format PnYnMnDTnHnMn.nS.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   140
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   141
     * @return the created Period, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   142
     * @throws DateTimeParseException if the text cannot be parsed to a Period
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   143
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   144
    Period parse() {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   145
        // force to upper case and coerce the comma to dot
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   146
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   147
        String s = text.toString().toUpperCase().replace(',', '.');
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   148
        // check for zero and skip parse
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   149
        if (ZERO.equals(s)) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   150
            return Period.ZERO;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   151
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   152
        if (s.length() < 3 || s.charAt(0) != 'P') {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   153
            throw new DateTimeParseException("Period could not be parsed: " + text, text, 0);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   154
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   155
        validateCharactersAndOrdering(s, text);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   156
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   157
        // strip off the leading P
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   158
        String[] datetime = s.substring(1).split("T");
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   159
        switch (datetime.length) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   160
            case 2:
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   161
                parseDate(datetime[0], 1);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   162
                parseTime(datetime[1], datetime[0].length() + 2);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   163
                break;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   164
            case 1:
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   165
                parseDate(datetime[0], 1);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   166
                break;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   167
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   168
        return toPeriod();
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   169
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   170
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   171
    private void parseDate(String s, int baseIndex) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   172
        index = 0;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   173
        while (index < s.length()) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   174
            String value = parseNumber(s);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   175
            if (index < s.length()) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   176
                char c = s.charAt(index);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   177
                switch(c) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   178
                    case 'Y': years = parseInt(value, baseIndex) ; break;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   179
                    case 'M': months = parseInt(value, baseIndex) ; break;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   180
                    case 'D': days = parseInt(value, baseIndex) ; break;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   181
                    default:
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   182
                        throw new DateTimeParseException("Period could not be parsed, unrecognized letter '" +
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   183
                                c + ": " + text, text, baseIndex + index);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   184
                }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   185
                index++;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   186
            }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   187
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   188
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   189
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   190
    private void parseTime(String s, int baseIndex) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   191
        index = 0;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   192
        s = prepareTime(s, baseIndex);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   193
        while (index < s.length()) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   194
            String value = parseNumber(s);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   195
            if (index < s.length()) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   196
                char c = s.charAt(index);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   197
                switch(c) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   198
                    case 'H': hours = parseInt(value, baseIndex) ; break;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   199
                    case 'M': minutes = parseInt(value, baseIndex) ; break;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   200
                    case 'S': seconds = parseInt(value, baseIndex) ; break;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   201
                    case 'N': nanos = parseNanos(value, baseIndex); break;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   202
                    default:
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   203
                        throw new DateTimeParseException("Period could not be parsed, unrecognized letter '" +
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   204
                                c + "': " + text, text, baseIndex + index);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   205
                }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   206
                index++;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   207
            }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   208
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   209
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   210
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   211
    private long parseNanos(String s, int baseIndex) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   212
        if (s.length() > 9) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   213
            throw new DateTimeParseException("Period could not be parsed, nanosecond range exceeded: " +
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   214
                    text, text, baseIndex + index - s.length());
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   215
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   216
        // pad to the right to create 10**9, then trim
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   217
        return Long.parseLong((s + "000000000").substring(0, 9));
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   218
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   219
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   220
    private String prepareTime(String s, int baseIndex) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   221
        if (s.contains(".")) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   222
            int i = s.indexOf(".") + 1;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   223
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   224
            // verify that the first character after the dot is a digit
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   225
            if (Character.isDigit(s.charAt(i))) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   226
                i++;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   227
            } else {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   228
                throw new DateTimeParseException("Period could not be parsed, invalid decimal number: " +
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   229
                        text, text, baseIndex + index);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   230
            }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   231
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   232
            // verify that only digits follow the decimal point followed by an S
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   233
            while (i < s.length()) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   234
                // || !Character.isDigit(s.charAt(i))
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   235
                char c = s.charAt(i);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   236
                if (Character.isDigit(c) || c == 'S') {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   237
                    i++;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   238
                } else {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   239
                    throw new DateTimeParseException("Period could not be parsed, invalid decimal number: " +
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   240
                            text, text, baseIndex + index);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   241
                }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   242
            }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   243
            s = s.replace('S', 'N').replace('.', 'S');
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   244
            if (s.contains("-0S")) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   245
                negativeSecs = true;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   246
                s = s.replace("-0S", "0S");
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   247
            }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   248
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   249
        return s;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   250
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   251
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   252
    private int parseInt(String s, int baseIndex) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   253
        try {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   254
            int value = Integer.parseInt(s);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   255
            if (s.charAt(0) == '-' && value == 0) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   256
                throw new DateTimeParseException("Period could not be parsed, invalid number '" +
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   257
                        s + "': " + text, text, baseIndex + index - s.length());
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   258
            }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   259
            return value;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   260
        } catch (NumberFormatException ex) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   261
            throw new DateTimeParseException("Period could not be parsed, invalid number '" +
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   262
                    s + "': " + text, text, baseIndex + index - s.length());
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   263
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   264
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   265
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   266
    private String parseNumber(String s) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   267
        int start = index;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   268
        while (index < s.length()) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   269
            char c = s.charAt(index);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   270
            if ((c < '0' || c > '9') && c != '-') {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   271
                break;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   272
            }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   273
            index++;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   274
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   275
        return s.substring(start, index);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   276
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   277
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   278
    private void validateCharactersAndOrdering(String s, CharSequence text) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   279
        char[] chars = s.toCharArray();
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   280
        int tokenPos = 0;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   281
        boolean lastLetter = false;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   282
        for (int i = 0; i < chars.length; i++) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   283
            if (tokenPos >= TOKEN_SEQUENCE.length()) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   284
                throw new DateTimeParseException("Period could not be parsed, characters after last 'S': " + text, text, i);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   285
            }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   286
            char c = chars[i];
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   287
            if ((c < '0' || c > '9') && c != '-' && c != '.') {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   288
                tokenPos = TOKEN_SEQUENCE.indexOf(c, tokenPos);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   289
                if (tokenPos < 0) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   290
                    throw new DateTimeParseException("Period could not be parsed, invalid character '" + c + "': " + text, text, i);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   291
                }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   292
                tokenPos++;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   293
                lastLetter = true;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   294
            } else {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   295
                lastLetter = false;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   296
            }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   297
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   298
        if (lastLetter == false) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   299
            throw new DateTimeParseException("Period could not be parsed, invalid last character: " + text, text, s.length() - 1);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   300
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   301
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   302
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   303
    private Period toPeriod() {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   304
        return Period.of(years, months, days, hours, minutes, seconds, negativeSecs || seconds < 0 ? -nanos : nanos);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   305
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   306
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   307
}