jdk/src/share/classes/java/time/format/DateTimeBuilder.java
author sherman
Tue, 22 Jan 2013 20:59:21 -0800
changeset 15289 3ac550392e43
child 15658 55b829ca2334
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) 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.Adjusters.nextOrSame;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    65
import static java.time.temporal.ChronoField.ALIGNED_DAY_OF_WEEK_IN_MONTH;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    66
import static java.time.temporal.ChronoField.ALIGNED_DAY_OF_WEEK_IN_YEAR;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    67
import static java.time.temporal.ChronoField.ALIGNED_WEEK_OF_MONTH;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    68
import static java.time.temporal.ChronoField.ALIGNED_WEEK_OF_YEAR;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    69
import static java.time.temporal.ChronoField.AMPM_OF_DAY;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    70
import static java.time.temporal.ChronoField.CLOCK_HOUR_OF_AMPM;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    71
import static java.time.temporal.ChronoField.CLOCK_HOUR_OF_DAY;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    72
import static java.time.temporal.ChronoField.DAY_OF_MONTH;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    73
import static java.time.temporal.ChronoField.DAY_OF_WEEK;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    74
import static java.time.temporal.ChronoField.DAY_OF_YEAR;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    75
import static java.time.temporal.ChronoField.EPOCH_DAY;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    76
import static java.time.temporal.ChronoField.EPOCH_MONTH;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    77
import static java.time.temporal.ChronoField.HOUR_OF_AMPM;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    78
import static java.time.temporal.ChronoField.HOUR_OF_DAY;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    79
import static java.time.temporal.ChronoField.INSTANT_SECONDS;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    80
import static java.time.temporal.ChronoField.MICRO_OF_DAY;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    81
import static java.time.temporal.ChronoField.MICRO_OF_SECOND;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    82
import static java.time.temporal.ChronoField.MILLI_OF_DAY;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    83
import static java.time.temporal.ChronoField.MILLI_OF_SECOND;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    84
import static java.time.temporal.ChronoField.MINUTE_OF_DAY;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    85
import static java.time.temporal.ChronoField.MINUTE_OF_HOUR;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    86
import static java.time.temporal.ChronoField.MONTH_OF_YEAR;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    87
import static java.time.temporal.ChronoField.NANO_OF_DAY;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    88
import static java.time.temporal.ChronoField.NANO_OF_SECOND;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    89
import static java.time.temporal.ChronoField.OFFSET_SECONDS;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    90
import static java.time.temporal.ChronoField.SECOND_OF_DAY;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    91
import static java.time.temporal.ChronoField.SECOND_OF_MINUTE;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    92
import static java.time.temporal.ChronoField.YEAR;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    93
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    94
import java.time.DateTimeException;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    95
import java.time.DayOfWeek;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    96
import java.time.Instant;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    97
import java.time.LocalDate;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    98
import java.time.LocalTime;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    99
import java.time.ZoneId;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   100
import java.time.ZoneOffset;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   101
import java.time.temporal.Chrono;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   102
import java.time.temporal.ChronoField;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   103
import java.time.temporal.Queries;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   104
import java.time.temporal.TemporalAccessor;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   105
import java.time.temporal.TemporalField;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   106
import java.time.temporal.TemporalQuery;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   107
import java.util.ArrayList;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   108
import java.util.EnumMap;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   109
import java.util.HashMap;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   110
import java.util.HashSet;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   111
import java.util.LinkedHashMap;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   112
import java.util.List;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   113
import java.util.Map;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   114
import java.util.Map.Entry;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   115
import java.util.Objects;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   116
import java.util.Set;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   117
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   118
/**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   119
 * Builder that can holds date and time fields and related date and time objects.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   120
 * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   121
 * <b>This class still needs major revision before JDK1.8 ships.</b>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   122
 * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   123
 * The builder is used to hold onto different elements of date and time.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   124
 * It is designed as two separate maps:
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   125
 * <p><ul>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   126
 * <li>from {@link java.time.temporal.TemporalField} to {@code long} value, where the value may be
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   127
 * outside the valid range for the field
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   128
 * <li>from {@code Class} to {@link java.time.temporal.TemporalAccessor}, holding larger scale objects
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   129
 * like {@code LocalDateTime}.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   130
 * </ul><p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   131
 *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   132
 * <h3>Specification for implementors</h3>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   133
 * This class is mutable and not thread-safe.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   134
 * It should only be used from a single thread.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   135
 *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   136
 * @since 1.8
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   137
 */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   138
public final class DateTimeBuilder
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   139
        implements TemporalAccessor, Cloneable {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   140
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   141
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   142
     * The map of other fields.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   143
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   144
    private Map<TemporalField, Long> otherFields;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   145
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   146
     * The map of date-time fields.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   147
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   148
    private final EnumMap<ChronoField, Long> standardFields = new EnumMap<ChronoField, Long>(ChronoField.class);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   149
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   150
     * The list of complete date-time objects.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   151
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   152
    private final List<Object> objects = new ArrayList<>(2);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   153
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   154
    //-----------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   155
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   156
     * Creates an empty instance of the builder.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   157
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   158
    public DateTimeBuilder() {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   159
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   160
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   161
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   162
     * Creates a new instance of the builder with a single field-value.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   163
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   164
     * This is equivalent to using {@link #addFieldValue(TemporalField, long)} on an empty builder.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   165
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   166
     * @param field  the field to add, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   167
     * @param value  the value to add, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   168
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   169
    public DateTimeBuilder(TemporalField field, long value) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   170
        addFieldValue(field, value);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   171
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   172
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   173
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   174
     * Creates a new instance of the builder.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   175
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   176
     * @param zone  the zone, may be null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   177
     * @param chrono  the chronology, may be null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   178
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   179
    public DateTimeBuilder(ZoneId zone, Chrono<?> chrono) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   180
        if (zone != null) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   181
            objects.add(zone);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   182
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   183
        if (chrono != null) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   184
            objects.add(chrono);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   185
        }
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
     * Gets the map of field-value pairs in the builder.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   191
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   192
     * @return a modifiable copy of the field-value map, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   193
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   194
    public Map<TemporalField, Long> getFieldValueMap() {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   195
        Map<TemporalField, Long> map = new HashMap<TemporalField, Long>(standardFields);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   196
        if (otherFields != null) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   197
            map.putAll(otherFields);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   198
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   199
        return map;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   200
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   201
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   202
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   203
     * Checks whether the specified field is present in the builder.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   204
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   205
     * @param field  the field to find in the field-value map, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   206
     * @return true if the field is present
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   207
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   208
    public boolean containsFieldValue(TemporalField field) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   209
        Objects.requireNonNull(field, "field");
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   210
        return standardFields.containsKey(field) || (otherFields != null && otherFields.containsKey(field));
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   211
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   212
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   213
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   214
     * Gets the value of the specified field from the builder.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   215
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   216
     * @param field  the field to query in the field-value map, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   217
     * @return the value of the field, may be out of range
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   218
     * @throws DateTimeException if the field is not present
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   219
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   220
    public long getFieldValue(TemporalField field) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   221
        Objects.requireNonNull(field, "field");
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   222
        Long value = getFieldValue0(field);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   223
        if (value == null) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   224
            throw new DateTimeException("Field not found: " + field);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   225
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   226
        return value;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   227
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   228
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   229
    private Long getFieldValue0(TemporalField field) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   230
        if (field instanceof ChronoField) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   231
            return standardFields.get(field);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   232
        } else if (otherFields != null) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   233
            return otherFields.get(field);
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 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
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   238
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   239
     * Gets the value of the specified field from the builder ensuring it is valid.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   240
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   241
     * @param field  the field to query in the field-value map, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   242
     * @return the value of the field, may be out of range
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   243
     * @throws DateTimeException if the field is not present
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   244
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   245
    public long getValidFieldValue(TemporalField field) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   246
        long value = getFieldValue(field);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   247
        return field.range().checkValidValue(value, field);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   248
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   249
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   250
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   251
     * Adds a field-value pair to the builder.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   252
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   253
     * This adds a field to the builder.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   254
     * If the field is not already present, then the field-value pair is added to the map.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   255
     * If the field is already present and it has the same value as that specified, no action occurs.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   256
     * If the field is already present and it has a different value to that specified, then
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   257
     * an exception is thrown.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   258
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   259
     * @param field  the field to add, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   260
     * @param value  the value to add, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   261
     * @return {@code this}, for method chaining
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   262
     * @throws DateTimeException if the field is already present with a different value
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   263
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   264
    public DateTimeBuilder addFieldValue(TemporalField field, long value) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   265
        Objects.requireNonNull(field, "field");
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   266
        Long old = getFieldValue0(field);  // check first for better error message
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   267
        if (old != null && old.longValue() != value) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   268
            throw new DateTimeException("Conflict found: " + field + " " + old + " differs from " + field + " " + value + ": " + this);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   269
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   270
        return putFieldValue0(field, value);
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
    private DateTimeBuilder putFieldValue0(TemporalField field, long value) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   274
        if (field instanceof ChronoField) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   275
            standardFields.put((ChronoField) field, value);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   276
        } else {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   277
            if (otherFields == null) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   278
                otherFields = new LinkedHashMap<TemporalField, Long>();
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   279
            }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   280
            otherFields.put(field, value);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   281
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   282
        return this;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   283
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   284
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   285
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   286
     * Removes a field-value pair from the builder.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   287
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   288
     * This removes a field, which must exist, from the builder.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   289
     * See {@link #removeFieldValues(TemporalField...)} for a version which does not throw an exception
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   290
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   291
     * @param field  the field to remove, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   292
     * @return the previous value of the field
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   293
     * @throws DateTimeException if the field is not found
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   294
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   295
    public long removeFieldValue(TemporalField field) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   296
        Objects.requireNonNull(field, "field");
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   297
        Long value = null;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   298
        if (field instanceof ChronoField) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   299
            value = standardFields.remove(field);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   300
        } else if (otherFields != null) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   301
            value = otherFields.remove(field);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   302
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   303
        if (value == null) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   304
            throw new DateTimeException("Field not found: " + field);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   305
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   306
        return value;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   307
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   308
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
     * Removes a list of fields from the builder.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   312
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   313
     * This removes the specified fields from the builder.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   314
     * No exception is thrown if the fields are not present.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   315
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   316
     * @param fields  the fields to remove, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   317
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   318
    public void removeFieldValues(TemporalField... fields) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   319
        for (TemporalField field : fields) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   320
            if (field instanceof ChronoField) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   321
                standardFields.remove(field);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   322
            } else if (otherFields != null) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   323
                otherFields.remove(field);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   324
            }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   325
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   326
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   327
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   328
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   329
     * Queries a list of fields from the builder.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   330
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   331
     * This gets the value of the specified fields from the builder into
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   332
     * an array where the positions match the order of the fields.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   333
     * If a field is not present, the array will contain null in that position.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   334
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   335
     * @param fields  the fields to query, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   336
     * @return the array of field values, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   337
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   338
    public Long[] queryFieldValues(TemporalField... fields) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   339
        Long[] values = new Long[fields.length];
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   340
        int i = 0;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   341
        for (TemporalField field : fields) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   342
            values[i++] = getFieldValue0(field);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   343
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   344
        return values;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   345
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   346
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   347
    //-----------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   348
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   349
     * Gets the list of date-time objects in the builder.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   350
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   351
     * This map is intended for use with {@link ZoneOffset} and {@link ZoneId}.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   352
     * The returned map is live and may be edited.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   353
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   354
     * @return the editable list of date-time objects, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   355
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   356
    public List<Object> getCalendricalList() {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   357
        return objects;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   358
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   359
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   360
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   361
     * Adds a date-time object to the builder.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   362
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   363
     * This adds a date-time object to the builder.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   364
     * If the object is a {@code DateTimeBuilder}, each field is added using {@link #addFieldValue}.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   365
     * If the object is not already present, then the object is added.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   366
     * If the object is already present and it is equal to that specified, no action occurs.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   367
     * If the object is already present and it is not equal to that specified, then an exception is thrown.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   368
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   369
     * @param object  the object to add, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   370
     * @return {@code this}, for method chaining
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   371
     * @throws DateTimeException if the field is already present with a different value
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   372
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   373
    public DateTimeBuilder addCalendrical(Object object) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   374
        Objects.requireNonNull(object, "object");
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   375
        // special case
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   376
        if (object instanceof DateTimeBuilder) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   377
            DateTimeBuilder dtb = (DateTimeBuilder) object;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   378
            for (TemporalField field : dtb.getFieldValueMap().keySet()) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   379
                addFieldValue(field, dtb.getFieldValue(field));
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   380
            }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   381
            return this;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   382
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   383
        if (object instanceof Instant) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   384
            addFieldValue(INSTANT_SECONDS, ((Instant) object).getEpochSecond());
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   385
            addFieldValue(NANO_OF_SECOND, ((Instant) object).getNano());
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   386
        } else {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   387
            objects.add(object);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   388
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   389
//      TODO
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   390
//        // preserve state of builder until validated
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   391
//        Class<?> cls = dateTime.extract(Class.class);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   392
//        if (cls == null) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   393
//            throw new DateTimeException("Invalid dateTime, unable to extract Class");
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   394
//        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   395
//        Object obj = objects.get(cls);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   396
//        if (obj != null) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   397
//            if (obj.equals(dateTime) == false) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   398
//                throw new DateTimeException("Conflict found: " + dateTime.getClass().getSimpleName() + " " + obj + " differs from " + dateTime + ": " + this);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   399
//            }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   400
//        } else {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   401
//            objects.put(cls, dateTime);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   402
//        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   403
        return this;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   404
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   405
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   406
    //-----------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   407
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   408
     * Resolves the builder, evaluating the date and time.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   409
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   410
     * This examines the contents of the builder and resolves it to produce the best
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   411
     * available date and time, throwing an exception if a problem occurs.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   412
     * Calling this method changes the state of the builder.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   413
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   414
     * @return {@code this}, for method chaining
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   415
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   416
    public DateTimeBuilder resolve() {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   417
        splitObjects();
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   418
        // handle unusual fields
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   419
        if (otherFields != null) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   420
            outer:
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   421
            while (true) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   422
                Set<Entry<TemporalField, Long>> entrySet = new HashSet<>(otherFields.entrySet());
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   423
                for (Entry<TemporalField, Long> entry : entrySet) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   424
                    if (entry.getKey().resolve(this, entry.getValue())) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   425
                        continue outer;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   426
                    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   427
                }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   428
                break;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   429
            }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   430
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   431
        // handle standard fields
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   432
        mergeDate();
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   433
        mergeTime();
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   434
        // TODO: cross validate remaining fields?
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   435
        return this;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   436
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   437
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   438
    private void mergeDate() {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   439
        if (standardFields.containsKey(EPOCH_DAY)) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   440
            checkDate(LocalDate.ofEpochDay(standardFields.remove(EPOCH_DAY)));
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   441
            return;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   442
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   443
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   444
        // normalize fields
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   445
        if (standardFields.containsKey(EPOCH_MONTH)) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   446
            long em = standardFields.remove(EPOCH_MONTH);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   447
            addFieldValue(MONTH_OF_YEAR, (em % 12) + 1);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   448
            addFieldValue(YEAR, (em / 12) + 1970);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   449
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   450
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   451
        // build date
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   452
        if (standardFields.containsKey(YEAR)) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   453
            if (standardFields.containsKey(MONTH_OF_YEAR)) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   454
                if (standardFields.containsKey(DAY_OF_MONTH)) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   455
                    int y = Math.toIntExact(standardFields.remove(YEAR));
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   456
                    int moy = Math.toIntExact(standardFields.remove(MONTH_OF_YEAR));
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   457
                    int dom = Math.toIntExact(standardFields.remove(DAY_OF_MONTH));
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   458
                    checkDate(LocalDate.of(y, moy, dom));
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   459
                    return;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   460
                }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   461
                if (standardFields.containsKey(ALIGNED_WEEK_OF_MONTH)) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   462
                    if (standardFields.containsKey(ALIGNED_DAY_OF_WEEK_IN_MONTH)) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   463
                        int y = Math.toIntExact(standardFields.remove(YEAR));
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   464
                        int moy = Math.toIntExact(standardFields.remove(MONTH_OF_YEAR));
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   465
                        int aw = Math.toIntExact(standardFields.remove(ALIGNED_WEEK_OF_MONTH));
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   466
                        int ad = Math.toIntExact(standardFields.remove(ALIGNED_DAY_OF_WEEK_IN_MONTH));
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   467
                        checkDate(LocalDate.of(y, moy, 1).plusDays((aw - 1) * 7 + (ad - 1)));
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   468
                        return;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   469
                    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   470
                    if (standardFields.containsKey(DAY_OF_WEEK)) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   471
                        int y = Math.toIntExact(standardFields.remove(YEAR));
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   472
                        int moy = Math.toIntExact(standardFields.remove(MONTH_OF_YEAR));
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   473
                        int aw = Math.toIntExact(standardFields.remove(ALIGNED_WEEK_OF_MONTH));
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   474
                        int dow = Math.toIntExact(standardFields.remove(DAY_OF_WEEK));
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   475
                        checkDate(LocalDate.of(y, moy, 1).plusDays((aw - 1) * 7).with(nextOrSame(DayOfWeek.of(dow))));
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   476
                        return;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   477
                    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   478
                }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   479
            }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   480
            if (standardFields.containsKey(DAY_OF_YEAR)) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   481
                int y = Math.toIntExact(standardFields.remove(YEAR));
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   482
                int doy = Math.toIntExact(standardFields.remove(DAY_OF_YEAR));
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   483
                checkDate(LocalDate.ofYearDay(y, doy));
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   484
                return;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   485
            }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   486
            if (standardFields.containsKey(ALIGNED_WEEK_OF_YEAR)) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   487
                if (standardFields.containsKey(ALIGNED_DAY_OF_WEEK_IN_YEAR)) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   488
                    int y = Math.toIntExact(standardFields.remove(YEAR));
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   489
                    int aw = Math.toIntExact(standardFields.remove(ALIGNED_WEEK_OF_YEAR));
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   490
                    int ad = Math.toIntExact(standardFields.remove(ALIGNED_DAY_OF_WEEK_IN_YEAR));
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   491
                    checkDate(LocalDate.of(y, 1, 1).plusDays((aw - 1) * 7 + (ad - 1)));
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   492
                    return;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   493
                }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   494
                if (standardFields.containsKey(DAY_OF_WEEK)) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   495
                    int y = Math.toIntExact(standardFields.remove(YEAR));
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   496
                    int aw = Math.toIntExact(standardFields.remove(ALIGNED_WEEK_OF_YEAR));
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   497
                    int dow = Math.toIntExact(standardFields.remove(DAY_OF_WEEK));
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   498
                    checkDate(LocalDate.of(y, 1, 1).plusDays((aw - 1) * 7).with(nextOrSame(DayOfWeek.of(dow))));
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   499
                    return;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   500
                }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   501
            }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   502
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   503
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   504
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   505
    private void checkDate(LocalDate date) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   506
        // TODO: this doesn't handle aligned weeks over into next month which would otherwise be valid
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   507
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   508
        addCalendrical(date);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   509
        for (ChronoField field : standardFields.keySet()) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   510
            long val1;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   511
            try {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   512
                val1 = date.getLong(field);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   513
            } catch (DateTimeException ex) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   514
                continue;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   515
            }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   516
            Long val2 = standardFields.get(field);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   517
            if (val1 != val2) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   518
                throw new DateTimeException("Conflict found: Field " + field + " " + val1 + " differs from " + field + " " + val2 + " derived from " + date);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   519
            }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   520
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   521
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   522
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   523
    private void mergeTime() {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   524
        if (standardFields.containsKey(CLOCK_HOUR_OF_DAY)) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   525
            long ch = standardFields.remove(CLOCK_HOUR_OF_DAY);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   526
            addFieldValue(HOUR_OF_DAY, ch == 24 ? 0 : ch);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   527
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   528
        if (standardFields.containsKey(CLOCK_HOUR_OF_AMPM)) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   529
            long ch = standardFields.remove(CLOCK_HOUR_OF_AMPM);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   530
            addFieldValue(HOUR_OF_AMPM, ch == 12 ? 0 : ch);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   531
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   532
        if (standardFields.containsKey(AMPM_OF_DAY) && standardFields.containsKey(HOUR_OF_AMPM)) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   533
            long ap = standardFields.remove(AMPM_OF_DAY);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   534
            long hap = standardFields.remove(HOUR_OF_AMPM);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   535
            addFieldValue(HOUR_OF_DAY, ap * 12 + hap);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   536
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   537
//        if (timeFields.containsKey(HOUR_OF_DAY) && timeFields.containsKey(MINUTE_OF_HOUR)) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   538
//            long hod = timeFields.remove(HOUR_OF_DAY);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   539
//            long moh = timeFields.remove(MINUTE_OF_HOUR);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   540
//            addFieldValue(MINUTE_OF_DAY, hod * 60 + moh);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   541
//        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   542
//        if (timeFields.containsKey(MINUTE_OF_DAY) && timeFields.containsKey(SECOND_OF_MINUTE)) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   543
//            long mod = timeFields.remove(MINUTE_OF_DAY);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   544
//            long som = timeFields.remove(SECOND_OF_MINUTE);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   545
//            addFieldValue(SECOND_OF_DAY, mod * 60 + som);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   546
//        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   547
        if (standardFields.containsKey(NANO_OF_DAY)) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   548
            long nod = standardFields.remove(NANO_OF_DAY);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   549
            addFieldValue(SECOND_OF_DAY, nod / 1000_000_000L);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   550
            addFieldValue(NANO_OF_SECOND, nod % 1000_000_000L);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   551
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   552
        if (standardFields.containsKey(MICRO_OF_DAY)) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   553
            long cod = standardFields.remove(MICRO_OF_DAY);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   554
            addFieldValue(SECOND_OF_DAY, cod / 1000_000L);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   555
            addFieldValue(MICRO_OF_SECOND, cod % 1000_000L);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   556
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   557
        if (standardFields.containsKey(MILLI_OF_DAY)) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   558
            long lod = standardFields.remove(MILLI_OF_DAY);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   559
            addFieldValue(SECOND_OF_DAY, lod / 1000);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   560
            addFieldValue(MILLI_OF_SECOND, lod % 1000);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   561
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   562
        if (standardFields.containsKey(SECOND_OF_DAY)) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   563
            long sod = standardFields.remove(SECOND_OF_DAY);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   564
            addFieldValue(HOUR_OF_DAY, sod / 3600);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   565
            addFieldValue(MINUTE_OF_HOUR, (sod / 60) % 60);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   566
            addFieldValue(SECOND_OF_MINUTE, sod % 60);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   567
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   568
        if (standardFields.containsKey(MINUTE_OF_DAY)) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   569
            long mod = standardFields.remove(MINUTE_OF_DAY);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   570
            addFieldValue(HOUR_OF_DAY, mod / 60);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   571
            addFieldValue(MINUTE_OF_HOUR, mod % 60);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   572
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   573
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   574
//            long sod = nod / 1000_000_000L;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   575
//            addFieldValue(HOUR_OF_DAY, sod / 3600);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   576
//            addFieldValue(MINUTE_OF_HOUR, (sod / 60) % 60);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   577
//            addFieldValue(SECOND_OF_MINUTE, sod % 60);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   578
//            addFieldValue(NANO_OF_SECOND, nod % 1000_000_000L);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   579
        if (standardFields.containsKey(MILLI_OF_SECOND) && standardFields.containsKey(MICRO_OF_SECOND)) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   580
            long los = standardFields.remove(MILLI_OF_SECOND);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   581
            long cos = standardFields.get(MICRO_OF_SECOND);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   582
            addFieldValue(MICRO_OF_SECOND, los * 1000 + (cos % 1000));
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   583
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   584
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   585
        Long hod = standardFields.get(HOUR_OF_DAY);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   586
        Long moh = standardFields.get(MINUTE_OF_HOUR);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   587
        Long som = standardFields.get(SECOND_OF_MINUTE);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   588
        Long nos = standardFields.get(NANO_OF_SECOND);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   589
        if (hod != null) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   590
            int hodVal = Math.toIntExact(hod);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   591
            if (moh != null) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   592
                int mohVal = Math.toIntExact(moh);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   593
                if (som != null) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   594
                    int somVal = Math.toIntExact(som);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   595
                    if (nos != null) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   596
                        int nosVal = Math.toIntExact(nos);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   597
                        addCalendrical(LocalTime.of(hodVal, mohVal, somVal, nosVal));
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   598
                    } else {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   599
                        addCalendrical(LocalTime.of(hodVal, mohVal, somVal));
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   600
                    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   601
                } else {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   602
                    addCalendrical(LocalTime.of(hodVal, mohVal));
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   603
                }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   604
            } else {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   605
                addCalendrical(LocalTime.of(hodVal, 0));
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   606
            }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   607
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   608
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   609
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   610
    private void splitObjects() {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   611
        List<Object> objectsToAdd = new ArrayList<>();
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   612
        for (Object object : objects) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   613
            if (object instanceof LocalDate || object instanceof LocalTime ||
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   614
                            object instanceof ZoneId || object instanceof Chrono) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   615
                continue;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   616
            }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   617
            if (object instanceof ZoneOffset || object instanceof Instant) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   618
                objectsToAdd.add(object);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   619
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   620
            } else if (object instanceof TemporalAccessor) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   621
                // TODO
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   622
//                TemporalAccessor dt = (TemporalAccessor) object;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   623
//                objectsToAdd.add(dt.extract(LocalDate.class));
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   624
//                objectsToAdd.add(dt.extract(LocalTime.class));
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   625
//                objectsToAdd.add(dt.extract(ZoneId.class));
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   626
//                objectsToAdd.add(dt.extract(Chrono.class));
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   627
            }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   628
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   629
        for (Object object : objectsToAdd) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   630
            if (object != null) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   631
                addCalendrical(object);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   632
            }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   633
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   634
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   635
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   636
    //-----------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   637
    @Override
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   638
    public <R> R query(TemporalQuery<R> query) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   639
        if (query == Queries.zoneId()) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   640
            return (R) extract(ZoneId.class);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   641
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   642
        if (query == Queries.offset()) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   643
            ZoneOffset offset = extract(ZoneOffset.class);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   644
            if (offset == null && standardFields.containsKey(OFFSET_SECONDS)) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   645
                offset = ZoneOffset.ofTotalSeconds(Math.toIntExact(standardFields.get(OFFSET_SECONDS)));
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   646
            }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   647
            return (R) offset;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   648
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   649
        if (query == Queries.chrono()) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   650
            return extract(Chrono.class);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   651
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   652
        // incomplete, so no need to handle PRECISION
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   653
        return TemporalAccessor.super.query(query);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   654
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   655
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   656
    @SuppressWarnings("unchecked")
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   657
    public <R> R extract(Class<?> type) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   658
        R result = null;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   659
        for (Object obj : objects) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   660
            if (type.isInstance(obj)) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   661
                if (result != null && result.equals(obj) == false) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   662
                    throw new DateTimeException("Conflict found: " + type.getSimpleName() + " differs " + result + " vs " + obj + ": " + this);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   663
                }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   664
                result = (R) obj;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   665
            }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   666
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   667
        return result;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   668
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   669
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   670
    //-----------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   671
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   672
     * Clones this builder, creating a new independent copy referring to the
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   673
     * same map of fields and objects.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   674
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   675
     * @return the cloned builder, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   676
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   677
    @Override
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   678
    public DateTimeBuilder clone() {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   679
        DateTimeBuilder dtb = new DateTimeBuilder();
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   680
        dtb.objects.addAll(this.objects);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   681
        dtb.standardFields.putAll(this.standardFields);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   682
        dtb.standardFields.putAll(this.standardFields);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   683
        if (this.otherFields != null) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   684
            dtb.otherFields.putAll(this.otherFields);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   685
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   686
        return dtb;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   687
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   688
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   689
    //-----------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   690
    @Override
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   691
    public String toString() {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   692
        StringBuilder buf = new StringBuilder(128);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   693
        buf.append("DateTimeBuilder[");
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   694
        Map<TemporalField, Long> fields = getFieldValueMap();
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   695
        if (fields.size() > 0) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   696
            buf.append("fields=").append(fields);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   697
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   698
        if (objects.size() > 0) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   699
            if (fields.size() > 0) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   700
                buf.append(", ");
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   701
            }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   702
            buf.append("objects=").append(objects);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   703
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   704
        buf.append(']');
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   705
        return buf.toString();
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   706
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   707
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   708
    //-----------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   709
    @Override
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   710
    public boolean isSupported(TemporalField field) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   711
        return field != null && containsFieldValue(field);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   712
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   713
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   714
    @Override
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   715
    public long getLong(TemporalField field) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   716
        return getFieldValue(field);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   717
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   718
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   719
}