jdk/src/java.base/share/classes/java/time/format/DateTimePrintContext.java
author ameena
Thu, 17 Nov 2016 11:55:59 +0000
changeset 42160 c229da92b1a9
parent 36638 bc1438c48f1b
permissions -rw-r--r--
8167618: DateTimeFormatter.format() uses exceptions for flow control Summary: Removed flow control in exception catch Reviewed-by: rriggs, scolebourne
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
     1
/*
42160
c229da92b1a9 8167618: DateTimeFormatter.format() uses exceptions for flow control
ameena
parents: 36638
diff changeset
     2
 * Copyright (c) 2012, 2016, Oracle and/or its affiliates. All rights reserved.
15289
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) 2011-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.format;
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 static java.time.temporal.ChronoField.EPOCH_DAY;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    65
import static java.time.temporal.ChronoField.INSTANT_SECONDS;
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
    66
import static java.time.temporal.ChronoField.OFFSET_SECONDS;
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    67
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    68
import java.time.DateTimeException;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    69
import java.time.Instant;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    70
import java.time.ZoneId;
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
    71
import java.time.ZoneOffset;
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
    72
import java.time.chrono.ChronoLocalDate;
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
    73
import java.time.chrono.Chronology;
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
    74
import java.time.chrono.IsoChronology;
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    75
import java.time.temporal.ChronoField;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    76
import java.time.temporal.TemporalAccessor;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    77
import java.time.temporal.TemporalField;
20795
8ec9e5b79828 8025722: TemporalAdjusters and TemporalQueries
rriggs
parents: 19030
diff changeset
    78
import java.time.temporal.TemporalQueries;
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    79
import java.time.temporal.TemporalQuery;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    80
import java.time.temporal.ValueRange;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    81
import java.util.Locale;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    82
import java.util.Objects;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    83
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    84
/**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    85
 * Context object used during date and time printing.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    86
 * <p>
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
    87
 * This class provides a single wrapper to items used in the format.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    88
 *
17474
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
    89
 * @implSpec
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    90
 * This class is a mutable context intended for use from a single thread.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    91
 * Usage of the class is thread-safe within standard printing as the framework creates
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
    92
 * a new instance of the class for each format and printing is single-threaded.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    93
 *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    94
 * @since 1.8
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    95
 */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    96
final class DateTimePrintContext {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    97
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    98
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    99
     * The temporal being output.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   100
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   101
    private TemporalAccessor temporal;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   102
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   103
     * The formatter, not null.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   104
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   105
    private DateTimeFormatter formatter;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   106
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   107
     * Whether the current formatter is optional.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   108
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   109
    private int optional;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   110
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   111
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   112
     * Creates a new instance of the context.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   113
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   114
     * @param temporal  the temporal object being output, not null
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   115
     * @param formatter  the formatter controlling the format, not null
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   116
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   117
    DateTimePrintContext(TemporalAccessor temporal, DateTimeFormatter formatter) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   118
        super();
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   119
        this.temporal = adjust(temporal, formatter);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   120
        this.formatter = formatter;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   121
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   122
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   123
    private static TemporalAccessor adjust(final TemporalAccessor temporal, DateTimeFormatter formatter) {
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   124
        // normal case first (early return is an optimization)
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   125
        Chronology overrideChrono = formatter.getChronology();
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   126
        ZoneId overrideZone = formatter.getZone();
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   127
        if (overrideChrono == null && overrideZone == null) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   128
            return temporal;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   129
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   130
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   131
        // ensure minimal change (early return is an optimization)
20795
8ec9e5b79828 8025722: TemporalAdjusters and TemporalQueries
rriggs
parents: 19030
diff changeset
   132
        Chronology temporalChrono = temporal.query(TemporalQueries.chronology());
8ec9e5b79828 8025722: TemporalAdjusters and TemporalQueries
rriggs
parents: 19030
diff changeset
   133
        ZoneId temporalZone = temporal.query(TemporalQueries.zoneId());
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   134
        if (Objects.equals(overrideChrono, temporalChrono)) {
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   135
            overrideChrono = null;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   136
        }
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   137
        if (Objects.equals(overrideZone, temporalZone)) {
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   138
            overrideZone = null;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   139
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   140
        if (overrideChrono == null && overrideZone == null) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   141
            return temporal;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   142
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   143
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   144
        // make adjustment
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   145
        final Chronology effectiveChrono = (overrideChrono != null ? overrideChrono : temporalChrono);
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   146
        if (overrideZone != null) {
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   147
            // if have zone and instant, calculation is simple, defaulting chrono if necessary
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   148
            if (temporal.isSupported(INSTANT_SECONDS)) {
33675
7d9d372a41df 8141652: Rename methods Objects.nonNullElse* to requireNonNullElse*
rriggs
parents: 25859
diff changeset
   149
                Chronology chrono = Objects.requireNonNullElse(effectiveChrono, IsoChronology.INSTANCE);
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   150
                return chrono.zonedDateTime(Instant.from(temporal), overrideZone);
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   151
            }
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   152
            // block changing zone on OffsetTime, and similar problem cases
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   153
            if (overrideZone.normalized() instanceof ZoneOffset && temporal.isSupported(OFFSET_SECONDS) &&
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   154
                    temporal.get(OFFSET_SECONDS) != overrideZone.getRules().getOffset(Instant.EPOCH).getTotalSeconds()) {
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   155
                throw new DateTimeException("Unable to apply override zone '" + overrideZone +
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   156
                        "' because the temporal object being formatted has a different offset but" +
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   157
                        " does not represent an instant: " + temporal);
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   158
            }
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   159
        }
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   160
        final ZoneId effectiveZone = (overrideZone != null ? overrideZone : temporalZone);
19030
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   161
        final ChronoLocalDate effectiveDate;
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   162
        if (overrideChrono != null) {
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   163
            if (temporal.isSupported(EPOCH_DAY)) {
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   164
                effectiveDate = effectiveChrono.date(temporal);
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   165
            } else {
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   166
                // check for date fields other than epoch-day, ignoring case of converting null to ISO
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   167
                if (!(overrideChrono == IsoChronology.INSTANCE && temporalChrono == null)) {
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   168
                    for (ChronoField f : ChronoField.values()) {
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   169
                        if (f.isDateBased() && temporal.isSupported(f)) {
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   170
                            throw new DateTimeException("Unable to apply override chronology '" + overrideChrono +
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   171
                                    "' because the temporal object being formatted contains date fields but" +
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   172
                                    " does not represent a whole date: " + temporal);
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   173
                        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   174
                    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   175
                }
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   176
                effectiveDate = null;
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   177
            }
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   178
        } else {
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   179
            effectiveDate = null;
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   180
        }
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   181
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   182
        // combine available data
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   183
        // this is a non-standard temporal that is almost a pure delegate
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   184
        // this better handles map-like underlying temporal instances
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   185
        return new TemporalAccessor() {
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   186
            @Override
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   187
            public boolean isSupported(TemporalField field) {
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   188
                if (effectiveDate != null && field.isDateBased()) {
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   189
                    return effectiveDate.isSupported(field);
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   190
                }
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   191
                return temporal.isSupported(field);
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   192
            }
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   193
            @Override
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   194
            public ValueRange range(TemporalField field) {
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   195
                if (effectiveDate != null && field.isDateBased()) {
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   196
                    return effectiveDate.range(field);
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   197
                }
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   198
                return temporal.range(field);
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   199
            }
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   200
            @Override
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   201
            public long getLong(TemporalField field) {
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   202
                if (effectiveDate != null && field.isDateBased()) {
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   203
                    return effectiveDate.getLong(field);
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   204
                }
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   205
                return temporal.getLong(field);
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   206
            }
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   207
            @SuppressWarnings("unchecked")
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   208
            @Override
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   209
            public <R> R query(TemporalQuery<R> query) {
20795
8ec9e5b79828 8025722: TemporalAdjusters and TemporalQueries
rriggs
parents: 19030
diff changeset
   210
                if (query == TemporalQueries.chronology()) {
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   211
                    return (R) effectiveChrono;
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   212
                }
20795
8ec9e5b79828 8025722: TemporalAdjusters and TemporalQueries
rriggs
parents: 19030
diff changeset
   213
                if (query == TemporalQueries.zoneId()) {
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   214
                    return (R) effectiveZone;
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   215
                }
20795
8ec9e5b79828 8025722: TemporalAdjusters and TemporalQueries
rriggs
parents: 19030
diff changeset
   216
                if (query == TemporalQueries.precision()) {
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   217
                    return temporal.query(query);
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   218
                }
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   219
                return query.queryFrom(this);
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   220
            }
36638
bc1438c48f1b 8085887: java.time.format.FormatStyle.LONG or FULL causes unchecked exception
rriggs
parents: 33675
diff changeset
   221
bc1438c48f1b 8085887: java.time.format.FormatStyle.LONG or FULL causes unchecked exception
rriggs
parents: 33675
diff changeset
   222
            @Override
bc1438c48f1b 8085887: java.time.format.FormatStyle.LONG or FULL causes unchecked exception
rriggs
parents: 33675
diff changeset
   223
            public String toString() {
bc1438c48f1b 8085887: java.time.format.FormatStyle.LONG or FULL causes unchecked exception
rriggs
parents: 33675
diff changeset
   224
                return temporal +
bc1438c48f1b 8085887: java.time.format.FormatStyle.LONG or FULL causes unchecked exception
rriggs
parents: 33675
diff changeset
   225
                        (effectiveChrono != null ? " with chronology " + effectiveChrono : "") +
bc1438c48f1b 8085887: java.time.format.FormatStyle.LONG or FULL causes unchecked exception
rriggs
parents: 33675
diff changeset
   226
                        (effectiveZone != null ? " with zone " + effectiveZone : "");
bc1438c48f1b 8085887: java.time.format.FormatStyle.LONG or FULL causes unchecked exception
rriggs
parents: 33675
diff changeset
   227
            }
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   228
        };
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   229
    }
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
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   233
     * Gets the temporal object being output.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   234
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   235
     * @return the temporal object, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   236
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   237
    TemporalAccessor getTemporal() {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   238
        return temporal;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   239
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   240
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   241
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   242
     * Gets the locale.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   243
     * <p>
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   244
     * This locale is used to control localization in the format output except
17474
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   245
     * where localization is controlled by the DecimalStyle.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   246
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   247
     * @return the locale, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   248
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   249
    Locale getLocale() {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   250
        return formatter.getLocale();
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   251
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   252
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   253
    /**
17474
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   254
     * Gets the DecimalStyle.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   255
     * <p>
17474
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   256
     * The DecimalStyle controls the localization of numeric output.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   257
     *
17474
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   258
     * @return the DecimalStyle, not null
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   259
     */
17474
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   260
    DecimalStyle getDecimalStyle() {
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   261
        return formatter.getDecimalStyle();
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   262
    }
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
     * Starts the printing of an optional segment of the input.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   267
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   268
    void startOptional() {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   269
        this.optional++;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   270
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   271
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   272
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   273
     * Ends the printing of an optional segment of the input.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   274
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   275
    void endOptional() {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   276
        this.optional--;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   277
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   278
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   279
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   280
     * Gets a value using a query.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   281
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   282
     * @param query  the query to use, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   283
     * @return the result, null if not found and optional is true
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   284
     * @throws DateTimeException if the type is not available and the section is not optional
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   285
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   286
    <R> R getValue(TemporalQuery<R> query) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   287
        R result = temporal.query(query);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   288
        if (result == null && optional == 0) {
36638
bc1438c48f1b 8085887: java.time.format.FormatStyle.LONG or FULL causes unchecked exception
rriggs
parents: 33675
diff changeset
   289
            throw new DateTimeException("Unable to extract " +
bc1438c48f1b 8085887: java.time.format.FormatStyle.LONG or FULL causes unchecked exception
rriggs
parents: 33675
diff changeset
   290
                    query + " from temporal " + temporal);
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   291
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   292
        return result;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   293
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   294
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   295
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   296
     * Gets the value of the specified field.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   297
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   298
     * This will return the value for the specified field.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   299
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   300
     * @param field  the field to find, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   301
     * @return the value, null if not found and optional is true
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   302
     * @throws DateTimeException if the field is not available and the section is not optional
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   303
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   304
    Long getValue(TemporalField field) {
42160
c229da92b1a9 8167618: DateTimeFormatter.format() uses exceptions for flow control
ameena
parents: 36638
diff changeset
   305
        if (optional > 0 && !temporal.isSupported(field)) {
c229da92b1a9 8167618: DateTimeFormatter.format() uses exceptions for flow control
ameena
parents: 36638
diff changeset
   306
            return null;
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   307
        }
42160
c229da92b1a9 8167618: DateTimeFormatter.format() uses exceptions for flow control
ameena
parents: 36638
diff changeset
   308
        return temporal.getLong(field);
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   309
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   310
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   311
    //-----------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   312
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   313
     * Returns a string version of the context for debugging.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   314
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   315
     * @return a string representation of the context, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   316
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   317
    @Override
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   318
    public String toString() {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   319
        return temporal.toString();
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   320
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   321
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   322
}