src/java.base/share/classes/java/time/Period.java
author darcy
Thu, 29 Aug 2019 16:31:34 -0700
changeset 57956 e0b8b019d2f5
parent 52078 4a63197816ce
permissions -rw-r--r--
8229997: Apply java.io.Serial annotations in java.base Reviewed-by: alanb, rriggs
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
     1
/*
57956
e0b8b019d2f5 8229997: Apply java.io.Serial annotations in java.base
darcy
parents: 52078
diff changeset
     2
 * Copyright (c) 2012, 2019, 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) 2008-2012, Stephen Colebourne & Michael Nascimento Santos
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    33
 *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    34
 * All rights reserved.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    35
 *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    36
 * Redistribution and use in source and binary forms, with or without
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    37
 * modification, are permitted provided that the following conditions are met:
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    38
 *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    39
 *  * Redistributions of source code must retain the above copyright notice,
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    40
 *    this list of conditions and the following disclaimer.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    41
 *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    42
 *  * Redistributions in binary form must reproduce the above copyright notice,
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    43
 *    this list of conditions and the following disclaimer in the documentation
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    44
 *    and/or other materials provided with the distribution.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    45
 *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    46
 *  * Neither the name of JSR-310 nor the names of its contributors
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    47
 *    may be used to endorse or promote products derived from this software
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    48
 *    without specific prior written permission.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    49
 *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    50
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    51
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    52
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    53
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    54
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    55
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    56
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    57
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    58
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    59
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    60
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    61
 */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    62
package java.time;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    63
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    64
import static java.time.temporal.ChronoUnit.DAYS;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    65
import static java.time.temporal.ChronoUnit.MONTHS;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    66
import static java.time.temporal.ChronoUnit.YEARS;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    67
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
    68
import java.io.DataInput;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
    69
import java.io.DataOutput;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
    70
import java.io.IOException;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
    71
import java.io.InvalidObjectException;
22081
86eb26ff8f2b 8030002: Enhance deserialization using readObject
rriggs
parents: 21331
diff changeset
    72
import java.io.ObjectInputStream;
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    73
import java.io.Serializable;
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
    74
import java.time.chrono.ChronoLocalDate;
20519
eee7a92074fd 8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents: 19841
diff changeset
    75
import java.time.chrono.ChronoPeriod;
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
    76
import java.time.chrono.Chronology;
20519
eee7a92074fd 8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents: 19841
diff changeset
    77
import java.time.chrono.IsoChronology;
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    78
import java.time.format.DateTimeParseException;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    79
import java.time.temporal.ChronoUnit;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    80
import java.time.temporal.Temporal;
20519
eee7a92074fd 8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents: 19841
diff changeset
    81
import java.time.temporal.TemporalAccessor;
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
    82
import java.time.temporal.TemporalAmount;
20795
8ec9e5b79828 8025722: TemporalAdjusters and TemporalQueries
rriggs
parents: 20519
diff changeset
    83
import java.time.temporal.TemporalQueries;
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    84
import java.time.temporal.TemporalUnit;
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
    85
import java.time.temporal.UnsupportedTemporalTypeException;
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
    86
import java.util.List;
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    87
import java.util.Objects;
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
    88
import java.util.regex.Matcher;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
    89
import java.util.regex.Pattern;
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    90
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    91
/**
20519
eee7a92074fd 8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents: 19841
diff changeset
    92
 * A date-based amount of time in the ISO-8601 calendar system,
eee7a92074fd 8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents: 19841
diff changeset
    93
 * such as '2 years, 3 months and 4 days'.
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
    94
 * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
    95
 * This class models a quantity or amount of time in terms of years, months and days.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
    96
 * See {@link Duration} for the time-based equivalent to this class.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
    97
 * <p>
20519
eee7a92074fd 8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents: 19841
diff changeset
    98
 * Durations and periods differ in their treatment of daylight savings time
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
    99
 * when added to {@link ZonedDateTime}. A {@code Duration} will add an exact
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   100
 * number of seconds, thus a duration of one day is always exactly 24 hours.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   101
 * By contrast, a {@code Period} will add a conceptual day, trying to maintain
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   102
 * the local time.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   103
 * <p>
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   104
 * For example, consider adding a period of one day and a duration of one day to
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   105
 * 18:00 on the evening before a daylight savings gap. The {@code Period} will add
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   106
 * the conceptual day and result in a {@code ZonedDateTime} at 18:00 the following day.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   107
 * By contrast, the {@code Duration} will add exactly 24 hours, resulting in a
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   108
 * {@code ZonedDateTime} at 19:00 the following day (assuming a one hour DST gap).
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   109
 * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   110
 * The supported units of a period are {@link ChronoUnit#YEARS YEARS},
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   111
 * {@link ChronoUnit#MONTHS MONTHS} and {@link ChronoUnit#DAYS DAYS}.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   112
 * All three fields are always present, but may be set to zero.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   113
 * <p>
20519
eee7a92074fd 8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents: 19841
diff changeset
   114
 * The ISO-8601 calendar system is the modern civil calendar system used today
eee7a92074fd 8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents: 19841
diff changeset
   115
 * in most of the world. It is equivalent to the proleptic Gregorian calendar
eee7a92074fd 8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents: 19841
diff changeset
   116
 * system, in which today's rules for leap years are applied for all time.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   117
 * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   118
 * The period is modeled as a directed amount of time, meaning that individual parts of the
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   119
 * period may be negative.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   120
 *
22108
99859c0e9a33 8029551: Add value-type notice to java.time classes
rriggs
parents: 22081
diff changeset
   121
 * <p>
49433
b6671a111395 8199465: {@docRoot} references need to be updated to reflect new module/package structure
jjg
parents: 47216
diff changeset
   122
 * This is a <a href="{@docRoot}/java.base/java/lang/doc-files/ValueBased.html">value-based</a>
22108
99859c0e9a33 8029551: Add value-type notice to java.time classes
rriggs
parents: 22081
diff changeset
   123
 * class; use of identity-sensitive operations (including reference equality
99859c0e9a33 8029551: Add value-type notice to java.time classes
rriggs
parents: 22081
diff changeset
   124
 * ({@code ==}), identity hash code, or synchronization) on instances of
99859c0e9a33 8029551: Add value-type notice to java.time classes
rriggs
parents: 22081
diff changeset
   125
 * {@code Period} may have unpredictable results and should be avoided.
99859c0e9a33 8029551: Add value-type notice to java.time classes
rriggs
parents: 22081
diff changeset
   126
 * The {@code equals} method should be used for comparisons.
99859c0e9a33 8029551: Add value-type notice to java.time classes
rriggs
parents: 22081
diff changeset
   127
 *
17474
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   128
 * @implSpec
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   129
 * This class is immutable and thread-safe.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   130
 *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   131
 * @since 1.8
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   132
 */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   133
public final class Period
20519
eee7a92074fd 8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents: 19841
diff changeset
   134
        implements ChronoPeriod, Serializable {
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   135
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   136
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   137
     * A constant for a period of zero.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   138
     */
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   139
    public static final Period ZERO = new Period(0, 0, 0);
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   140
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   141
     * Serialization version.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   142
     */
57956
e0b8b019d2f5 8229997: Apply java.io.Serial annotations in java.base
darcy
parents: 52078
diff changeset
   143
    @java.io.Serial
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   144
    private static final long serialVersionUID = -3587258372562876L;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   145
    /**
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   146
     * The pattern for parsing.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   147
     */
21331
8ee181e7e48b 8024686: Cleanup of java.time serialization source
rriggs
parents: 20795
diff changeset
   148
    private static final Pattern PATTERN =
19030
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   149
            Pattern.compile("([-+]?)P(?:([-+]?[0-9]+)Y)?(?:([-+]?[0-9]+)M)?(?:([-+]?[0-9]+)W)?(?:([-+]?[0-9]+)D)?", Pattern.CASE_INSENSITIVE);
20519
eee7a92074fd 8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents: 19841
diff changeset
   150
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   151
    /**
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   152
     * The set of supported units.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   153
     */
41482
05e75e5f3afb 8134373: use collections convenience factories in the JDK
smarks
parents: 26462
diff changeset
   154
    private static final List<TemporalUnit> SUPPORTED_UNITS = List.of(YEARS, MONTHS, DAYS);
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   155
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   156
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   157
     * The number of years.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   158
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   159
    private final int years;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   160
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   161
     * The number of months.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   162
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   163
    private final int months;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   164
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   165
     * The number of days.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   166
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   167
    private final int days;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   168
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   169
    //-----------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   170
    /**
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   171
     * Obtains a {@code Period} representing a number of years.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   172
     * <p>
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   173
     * The resulting period will have the specified years.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   174
     * The months and days units will be zero.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   175
     *
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   176
     * @param years  the number of years, positive or negative
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   177
     * @return the period of years, not null
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   178
     */
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   179
    public static Period ofYears(int years) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   180
        return create(years, 0, 0);
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   181
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   182
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   183
    /**
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   184
     * Obtains a {@code Period} representing a number of months.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   185
     * <p>
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   186
     * The resulting period will have the specified months.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   187
     * The years and days units will be zero.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   188
     *
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   189
     * @param months  the number of months, positive or negative
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   190
     * @return the period of months, not null
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   191
     */
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   192
    public static Period ofMonths(int months) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   193
        return create(0, months, 0);
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   194
    }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   195
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   196
    /**
19030
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   197
     * Obtains a {@code Period} representing a number of weeks.
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   198
     * <p>
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   199
     * The resulting period will be day-based, with the amount of days
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   200
     * equal to the number of weeks multiplied by 7.
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   201
     * The years and months units will be zero.
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   202
     *
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   203
     * @param weeks  the number of weeks, positive or negative
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   204
     * @return the period, with the input weeks converted to days, not null
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   205
     */
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   206
    public static Period ofWeeks(int weeks) {
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   207
        return create(0, 0, Math.multiplyExact(weeks, 7));
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   208
    }
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   209
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   210
    /**
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   211
     * Obtains a {@code Period} representing a number of days.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   212
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   213
     * The resulting period will have the specified days.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   214
     * The years and months units will be zero.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   215
     *
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   216
     * @param days  the number of days, positive or negative
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   217
     * @return the period of days, not null
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   218
     */
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   219
    public static Period ofDays(int days) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   220
        return create(0, 0, days);
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   221
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   222
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   223
    //-----------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   224
    /**
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   225
     * Obtains a {@code Period} representing a number of years, months and days.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   226
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   227
     * This creates an instance based on years, months and days.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   228
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   229
     * @param years  the amount of years, may be negative
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   230
     * @param months  the amount of months, may be negative
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   231
     * @param days  the amount of days, may be negative
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   232
     * @return the period of years, months and days, not null
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   233
     */
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   234
    public static Period of(int years, int months, int days) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   235
        return create(years, months, days);
15289
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
    /**
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   240
     * Obtains an instance of {@code Period} from a temporal amount.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   241
     * <p>
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   242
     * This obtains a period based on the specified amount.
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   243
     * A {@code TemporalAmount} represents an  amount of time, which may be
20519
eee7a92074fd 8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents: 19841
diff changeset
   244
     * date-based or time-based, which this factory extracts to a {@code Period}.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   245
     * <p>
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   246
     * The conversion loops around the set of units from the amount and uses
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   247
     * the {@link ChronoUnit#YEARS YEARS}, {@link ChronoUnit#MONTHS MONTHS}
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   248
     * and {@link ChronoUnit#DAYS DAYS} units to create a period.
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   249
     * If any other units are found then an exception is thrown.
20519
eee7a92074fd 8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents: 19841
diff changeset
   250
     * <p>
eee7a92074fd 8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents: 19841
diff changeset
   251
     * If the amount is a {@code ChronoPeriod} then it must use the ISO chronology.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   252
     *
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   253
     * @param amount  the temporal amount to convert, not null
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   254
     * @return the equivalent period, not null
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   255
     * @throws DateTimeException if unable to convert to a {@code Period}
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   256
     * @throws ArithmeticException if the amount of years, months or days exceeds an int
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   257
     */
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   258
    public static Period from(TemporalAmount amount) {
20519
eee7a92074fd 8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents: 19841
diff changeset
   259
        if (amount instanceof Period) {
eee7a92074fd 8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents: 19841
diff changeset
   260
            return (Period) amount;
eee7a92074fd 8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents: 19841
diff changeset
   261
        }
eee7a92074fd 8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents: 19841
diff changeset
   262
        if (amount instanceof ChronoPeriod) {
eee7a92074fd 8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents: 19841
diff changeset
   263
            if (IsoChronology.INSTANCE.equals(((ChronoPeriod) amount).getChronology()) == false) {
eee7a92074fd 8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents: 19841
diff changeset
   264
                throw new DateTimeException("Period requires ISO chronology: " + amount);
eee7a92074fd 8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents: 19841
diff changeset
   265
            }
eee7a92074fd 8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents: 19841
diff changeset
   266
        }
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   267
        Objects.requireNonNull(amount, "amount");
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   268
        int years = 0;
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   269
        int months = 0;
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   270
        int days = 0;
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   271
        for (TemporalUnit unit : amount.getUnits()) {
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   272
            long unitAmount = amount.get(unit);
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   273
            if (unit == ChronoUnit.YEARS) {
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   274
                years = Math.toIntExact(unitAmount);
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   275
            } else if (unit == ChronoUnit.MONTHS) {
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   276
                months = Math.toIntExact(unitAmount);
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   277
            } else if (unit == ChronoUnit.DAYS) {
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   278
                days = Math.toIntExact(unitAmount);
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   279
            } else {
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   280
                throw new DateTimeException("Unit must be Years, Months or Days, but was " + unit);
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   281
            }
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   282
        }
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   283
        return create(years, months, days);
15289
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
    //-----------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   287
    /**
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   288
     * Obtains a {@code Period} from a text string such as {@code PnYnMnD}.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   289
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   290
     * This will parse the string produced by {@code toString()} which is
19030
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   291
     * based on the ISO-8601 period formats {@code PnYnMnD} and {@code PnW}.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   292
     * <p>
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   293
     * The string starts with an optional sign, denoted by the ASCII negative
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   294
     * or positive symbol. If negative, the whole period is negated.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   295
     * The ASCII letter "P" is next in upper or lower case.
19030
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   296
     * There are then four sections, each consisting of a number and a suffix.
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   297
     * At least one of the four sections must be present.
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   298
     * The sections have suffixes in ASCII of "Y", "M", "W" and "D" for
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   299
     * years, months, weeks and days, accepted in upper or lower case.
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   300
     * The suffixes must occur in order.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   301
     * The number part of each section must consist of ASCII digits.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   302
     * The number may be prefixed by the ASCII negative or positive symbol.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   303
     * The number must parse to an {@code int}.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   304
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   305
     * The leading plus/minus sign, and negative values for other units are
19030
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   306
     * not part of the ISO-8601 standard. In addition, ISO-8601 does not
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   307
     * permit mixing between the {@code PnYnMnD} and {@code PnW} formats.
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   308
     * Any week-based input is multiplied by 7 and treated as a number of days.
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   309
     * <p>
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   310
     * For example, the following are valid inputs:
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   311
     * <pre>
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   312
     *   "P2Y"             -- Period.ofYears(2)
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   313
     *   "P3M"             -- Period.ofMonths(3)
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   314
     *   "P4W"             -- Period.ofWeeks(4)
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   315
     *   "P5D"             -- Period.ofDays(5)
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   316
     *   "P1Y2M3D"         -- Period.of(1, 2, 3)
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   317
     *   "P1Y2M3W4D"       -- Period.of(1, 2, 25)
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   318
     *   "P-1Y2M"          -- Period.of(-1, 2, 0)
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   319
     *   "-P1Y2M"          -- Period.of(-1, -2, 0)
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   320
     * </pre>
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   321
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   322
     * @param text  the text to parse, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   323
     * @return the parsed period, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   324
     * @throws DateTimeParseException if the text cannot be parsed to a period
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   325
     */
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   326
    public static Period parse(CharSequence text) {
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   327
        Objects.requireNonNull(text, "text");
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   328
        Matcher matcher = PATTERN.matcher(text);
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   329
        if (matcher.matches()) {
25986
12bcf9a5994f 8055004: Reduce allocation overhead in java.time.Period/Duration parse methods
sherman
parents: 24256
diff changeset
   330
            int negate = (charMatch(text, matcher.start(1), matcher.end(1), '-') ? -1 : 1);
12bcf9a5994f 8055004: Reduce allocation overhead in java.time.Period/Duration parse methods
sherman
parents: 24256
diff changeset
   331
            int yearStart = matcher.start(2), yearEnd = matcher.end(2);
12bcf9a5994f 8055004: Reduce allocation overhead in java.time.Period/Duration parse methods
sherman
parents: 24256
diff changeset
   332
            int monthStart = matcher.start(3), monthEnd = matcher.end(3);
12bcf9a5994f 8055004: Reduce allocation overhead in java.time.Period/Duration parse methods
sherman
parents: 24256
diff changeset
   333
            int weekStart = matcher.start(4), weekEnd = matcher.end(4);
12bcf9a5994f 8055004: Reduce allocation overhead in java.time.Period/Duration parse methods
sherman
parents: 24256
diff changeset
   334
            int dayStart = matcher.start(5), dayEnd = matcher.end(5);
12bcf9a5994f 8055004: Reduce allocation overhead in java.time.Period/Duration parse methods
sherman
parents: 24256
diff changeset
   335
            if (yearStart >= 0 || monthStart >= 0 || weekStart >= 0 || dayStart >= 0) {
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   336
                try {
25986
12bcf9a5994f 8055004: Reduce allocation overhead in java.time.Period/Duration parse methods
sherman
parents: 24256
diff changeset
   337
                    int years = parseNumber(text, yearStart, yearEnd, negate);
12bcf9a5994f 8055004: Reduce allocation overhead in java.time.Period/Duration parse methods
sherman
parents: 24256
diff changeset
   338
                    int months = parseNumber(text, monthStart, monthEnd, negate);
12bcf9a5994f 8055004: Reduce allocation overhead in java.time.Period/Duration parse methods
sherman
parents: 24256
diff changeset
   339
                    int weeks = parseNumber(text, weekStart, weekEnd, negate);
12bcf9a5994f 8055004: Reduce allocation overhead in java.time.Period/Duration parse methods
sherman
parents: 24256
diff changeset
   340
                    int days = parseNumber(text, dayStart, dayEnd, negate);
19030
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   341
                    days = Math.addExact(days, Math.multiplyExact(weeks, 7));
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   342
                    return create(years, months, days);
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   343
                } catch (NumberFormatException ex) {
19030
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   344
                    throw new DateTimeParseException("Text cannot be parsed to a Period", text, 0, ex);
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   345
                }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   346
            }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   347
        }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   348
        throw new DateTimeParseException("Text cannot be parsed to a Period", text, 0);
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   349
    }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   350
25986
12bcf9a5994f 8055004: Reduce allocation overhead in java.time.Period/Duration parse methods
sherman
parents: 24256
diff changeset
   351
    private static boolean charMatch(CharSequence text, int start, int end, char c) {
12bcf9a5994f 8055004: Reduce allocation overhead in java.time.Period/Duration parse methods
sherman
parents: 24256
diff changeset
   352
        return (start >= 0 && end == start + 1 && text.charAt(start) == c);
12bcf9a5994f 8055004: Reduce allocation overhead in java.time.Period/Duration parse methods
sherman
parents: 24256
diff changeset
   353
    }
12bcf9a5994f 8055004: Reduce allocation overhead in java.time.Period/Duration parse methods
sherman
parents: 24256
diff changeset
   354
12bcf9a5994f 8055004: Reduce allocation overhead in java.time.Period/Duration parse methods
sherman
parents: 24256
diff changeset
   355
    private static int parseNumber(CharSequence text, int start, int end, int negate) {
12bcf9a5994f 8055004: Reduce allocation overhead in java.time.Period/Duration parse methods
sherman
parents: 24256
diff changeset
   356
        if (start < 0 || end < 0) {
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   357
            return 0;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   358
        }
26462
d6d34934be12 8055251: Re-examine Integer.parseInt and Long.parseLong methods
alanb
parents: 25991
diff changeset
   359
        int val = Integer.parseInt(text, start, end, 10);
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   360
        try {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   361
            return Math.multiplyExact(val, negate);
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   362
        } catch (ArithmeticException ex) {
19030
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   363
            throw new DateTimeParseException("Text cannot be parsed to a Period", text, 0, ex);
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   364
        }
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   365
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   366
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   367
    //-----------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   368
    /**
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   369
     * Obtains a {@code Period} consisting of the number of years, months,
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   370
     * and days between two dates.
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   371
     * <p>
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   372
     * The start date is included, but the end date is not.
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   373
     * The period is calculated by removing complete months, then calculating
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   374
     * the remaining number of days, adjusting to ensure that both have the same sign.
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   375
     * The number of months is then split into years and months based on a 12 month year.
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   376
     * A month is considered if the end day-of-month is greater than or equal to the start day-of-month.
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   377
     * For example, from {@code 2010-01-15} to {@code 2011-03-18} is one year, two months and three days.
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   378
     * <p>
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   379
     * The result of this method can be a negative period if the end is before the start.
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   380
     * The negative sign will be the same in each of year, month and day.
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   381
     *
20519
eee7a92074fd 8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents: 19841
diff changeset
   382
     * @param startDateInclusive  the start date, inclusive, not null
eee7a92074fd 8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents: 19841
diff changeset
   383
     * @param endDateExclusive  the end date, exclusive, not null
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   384
     * @return the period between this date and the end date, not null
19030
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   385
     * @see ChronoLocalDate#until(ChronoLocalDate)
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   386
     */
20519
eee7a92074fd 8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents: 19841
diff changeset
   387
    public static Period between(LocalDate startDateInclusive, LocalDate endDateExclusive) {
eee7a92074fd 8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents: 19841
diff changeset
   388
        return startDateInclusive.until(endDateExclusive);
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   389
    }
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   390
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   391
    //-----------------------------------------------------------------------
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   392
    /**
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   393
     * Creates an instance.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   394
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   395
     * @param years  the amount
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   396
     * @param months  the amount
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   397
     * @param days  the amount
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   398
     */
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   399
    private static Period create(int years, int months, int days) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   400
        if ((years | months | days) == 0) {
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   401
            return ZERO;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   402
        }
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   403
        return new Period(years, months, days);
15289
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
     * Constructor.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   408
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   409
     * @param years  the amount
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   410
     * @param months  the amount
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   411
     * @param days  the amount
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   412
     */
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   413
    private Period(int years, int months, int days) {
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   414
        this.years = years;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   415
        this.months = months;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   416
        this.days = days;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   417
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   418
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   419
    //-----------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   420
    /**
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   421
     * Gets the value of the requested unit.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   422
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   423
     * This returns a value for each of the three supported units,
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   424
     * {@link ChronoUnit#YEARS YEARS}, {@link ChronoUnit#MONTHS MONTHS} and
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   425
     * {@link ChronoUnit#DAYS DAYS}.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   426
     * All other units throw an exception.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   427
     *
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   428
     * @param unit the {@code TemporalUnit} for which to return the value
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   429
     * @return the long value of the unit
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   430
     * @throws DateTimeException if the unit is not supported
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   431
     * @throws UnsupportedTemporalTypeException if the unit is not supported
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   432
     */
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   433
    @Override
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   434
    public long get(TemporalUnit unit) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   435
        if (unit == ChronoUnit.YEARS) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   436
            return getYears();
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   437
        } else if (unit == ChronoUnit.MONTHS) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   438
            return getMonths();
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   439
        } else if (unit == ChronoUnit.DAYS) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   440
            return getDays();
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   441
        } else {
19030
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   442
            throw new UnsupportedTemporalTypeException("Unsupported unit: " + unit);
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   443
        }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   444
    }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   445
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   446
    /**
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   447
     * Gets the set of units supported by this period.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   448
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   449
     * The supported units are {@link ChronoUnit#YEARS YEARS},
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   450
     * {@link ChronoUnit#MONTHS MONTHS} and {@link ChronoUnit#DAYS DAYS}.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   451
     * They are returned in the order years, months, days.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   452
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   453
     * This set can be used in conjunction with {@link #get(TemporalUnit)}
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   454
     * to access the entire state of the period.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   455
     *
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   456
     * @return a list containing the years, months and days units, not null
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   457
     */
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   458
    @Override
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   459
    public List<TemporalUnit> getUnits() {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   460
        return SUPPORTED_UNITS;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   461
    }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   462
20519
eee7a92074fd 8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents: 19841
diff changeset
   463
    /**
eee7a92074fd 8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents: 19841
diff changeset
   464
     * Gets the chronology of this period, which is the ISO calendar system.
eee7a92074fd 8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents: 19841
diff changeset
   465
     * <p>
eee7a92074fd 8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents: 19841
diff changeset
   466
     * The {@code Chronology} represents the calendar system in use.
eee7a92074fd 8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents: 19841
diff changeset
   467
     * The ISO-8601 calendar system is the modern civil calendar system used today
eee7a92074fd 8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents: 19841
diff changeset
   468
     * in most of the world. It is equivalent to the proleptic Gregorian calendar
eee7a92074fd 8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents: 19841
diff changeset
   469
     * system, in which today's rules for leap years are applied for all time.
eee7a92074fd 8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents: 19841
diff changeset
   470
     *
eee7a92074fd 8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents: 19841
diff changeset
   471
     * @return the ISO chronology, not null
eee7a92074fd 8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents: 19841
diff changeset
   472
     */
eee7a92074fd 8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents: 19841
diff changeset
   473
    @Override
eee7a92074fd 8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents: 19841
diff changeset
   474
    public IsoChronology getChronology() {
eee7a92074fd 8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents: 19841
diff changeset
   475
        return IsoChronology.INSTANCE;
eee7a92074fd 8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents: 19841
diff changeset
   476
    }
eee7a92074fd 8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents: 19841
diff changeset
   477
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   478
    //-----------------------------------------------------------------------
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   479
    /**
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   480
     * Checks if all three units of this period are zero.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   481
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   482
     * A zero period has the value zero for the years, months and days units.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   483
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   484
     * @return true if this period is zero-length
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   485
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   486
    public boolean isZero() {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   487
        return (this == ZERO);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   488
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   489
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   490
    /**
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   491
     * Checks if any of the three units of this period are negative.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   492
     * <p>
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   493
     * This checks whether the years, months or days units are less than zero.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   494
     *
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   495
     * @return true if any unit of this period is negative
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   496
     */
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   497
    public boolean isNegative() {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   498
        return years < 0 || months < 0 || days < 0;
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   499
    }
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
     * Gets the amount of years of this period.
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   504
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   505
     * This returns the years unit.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   506
     * <p>
20519
eee7a92074fd 8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents: 19841
diff changeset
   507
     * The months unit is not automatically normalized with the years unit.
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   508
     * This means that a period of "15 months" is different to a period
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   509
     * of "1 year and 3 months".
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   510
     *
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   511
     * @return the amount of years of this period, may be negative
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   512
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   513
    public int getYears() {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   514
        return years;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   515
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   516
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   517
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   518
     * Gets the amount of months of this period.
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   519
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   520
     * This returns the months unit.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   521
     * <p>
20519
eee7a92074fd 8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents: 19841
diff changeset
   522
     * The months unit is not automatically normalized with the years unit.
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   523
     * This means that a period of "15 months" is different to a period
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   524
     * of "1 year and 3 months".
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   525
     *
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   526
     * @return the amount of months of this period, may be negative
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   527
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   528
    public int getMonths() {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   529
        return months;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   530
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   531
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   532
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   533
     * Gets the amount of days of this period.
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   534
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   535
     * This returns the days unit.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   536
     *
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   537
     * @return the amount of days of this period, may be negative
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   538
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   539
    public int getDays() {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   540
        return days;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   541
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   542
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   543
    //-----------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   544
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   545
     * Returns a copy of this period with the specified amount of years.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   546
     * <p>
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   547
     * This sets the amount of the years unit in a copy of this period.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   548
     * The months and days units are unaffected.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   549
     * <p>
20519
eee7a92074fd 8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents: 19841
diff changeset
   550
     * The months unit is not automatically normalized with the years unit.
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   551
     * This means that a period of "15 months" is different to a period
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   552
     * of "1 year and 3 months".
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   553
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   554
     * This instance is immutable and unaffected by this method call.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   555
     *
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   556
     * @param years  the years to represent, may be negative
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   557
     * @return a {@code Period} based on this period with the requested years, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   558
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   559
    public Period withYears(int years) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   560
        if (years == this.years) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   561
            return this;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   562
        }
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   563
        return create(years, months, days);
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   564
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   565
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   566
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   567
     * Returns a copy of this period with the specified amount of months.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   568
     * <p>
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   569
     * This sets the amount of the months unit in a copy of this period.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   570
     * The years and days units are unaffected.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   571
     * <p>
20519
eee7a92074fd 8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents: 19841
diff changeset
   572
     * The months unit is not automatically normalized with the years unit.
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   573
     * This means that a period of "15 months" is different to a period
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   574
     * of "1 year and 3 months".
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   575
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   576
     * This instance is immutable and unaffected by this method call.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   577
     *
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   578
     * @param months  the months to represent, may be negative
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   579
     * @return a {@code Period} based on this period with the requested months, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   580
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   581
    public Period withMonths(int months) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   582
        if (months == this.months) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   583
            return this;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   584
        }
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   585
        return create(years, months, days);
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   586
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   587
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   588
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   589
     * Returns a copy of this period with the specified amount of days.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   590
     * <p>
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   591
     * This sets the amount of the days unit in a copy of this period.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   592
     * The years and months units are unaffected.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   593
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   594
     * This instance is immutable and unaffected by this method call.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   595
     *
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   596
     * @param days  the days to represent, may be negative
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   597
     * @return a {@code Period} based on this period with the requested days, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   598
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   599
    public Period withDays(int days) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   600
        if (days == this.days) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   601
            return this;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   602
        }
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   603
        return create(years, months, days);
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   604
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   605
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
     * Returns a copy of this period with the specified period added.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   609
     * <p>
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   610
     * This operates separately on the years, months and days.
20519
eee7a92074fd 8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents: 19841
diff changeset
   611
     * No normalization is performed.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   612
     * <p>
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   613
     * For example, "1 year, 6 months and 3 days" plus "2 years, 2 months and 2 days"
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   614
     * returns "3 years, 8 months and 5 days".
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   615
     * <p>
20519
eee7a92074fd 8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents: 19841
diff changeset
   616
     * The specified amount is typically an instance of {@code Period}.
eee7a92074fd 8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents: 19841
diff changeset
   617
     * Other types are interpreted using {@link Period#from(TemporalAmount)}.
eee7a92074fd 8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents: 19841
diff changeset
   618
     * <p>
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   619
     * This instance is immutable and unaffected by this method call.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   620
     *
24256
da9a41004459 8034906: Fix typos, errors and Javadoc differences in java.time
scolebourne
parents: 22566
diff changeset
   621
     * @param amountToAdd  the amount to add, not null
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   622
     * @return a {@code Period} based on this period with the requested period added, not null
20519
eee7a92074fd 8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents: 19841
diff changeset
   623
     * @throws DateTimeException if the specified amount has a non-ISO chronology or
eee7a92074fd 8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents: 19841
diff changeset
   624
     *  contains an invalid unit
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   625
     * @throws ArithmeticException if numeric overflow occurs
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   626
     */
20519
eee7a92074fd 8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents: 19841
diff changeset
   627
    public Period plus(TemporalAmount amountToAdd) {
eee7a92074fd 8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents: 19841
diff changeset
   628
        Period isoAmount = Period.from(amountToAdd);
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   629
        return create(
20519
eee7a92074fd 8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents: 19841
diff changeset
   630
                Math.addExact(years, isoAmount.years),
eee7a92074fd 8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents: 19841
diff changeset
   631
                Math.addExact(months, isoAmount.months),
eee7a92074fd 8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents: 19841
diff changeset
   632
                Math.addExact(days, isoAmount.days));
15289
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
    /**
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   636
     * Returns a copy of this period with the specified years added.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   637
     * <p>
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   638
     * This adds the amount to the years unit in a copy of this period.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   639
     * The months and days units are unaffected.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   640
     * For example, "1 year, 6 months and 3 days" plus 2 years returns "3 years, 6 months and 3 days".
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   641
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   642
     * This instance is immutable and unaffected by this method call.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   643
     *
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   644
     * @param yearsToAdd  the years to add, positive or negative
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   645
     * @return a {@code Period} based on this period with the specified years added, not null
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   646
     * @throws ArithmeticException if numeric overflow occurs
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   647
     */
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   648
    public Period plusYears(long yearsToAdd) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   649
        if (yearsToAdd == 0) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   650
            return this;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   651
        }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   652
        return create(Math.toIntExact(Math.addExact(years, yearsToAdd)), months, days);
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   653
    }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   654
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   655
    /**
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   656
     * Returns a copy of this period with the specified months added.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   657
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   658
     * This adds the amount to the months unit in a copy of this period.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   659
     * The years and days units are unaffected.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   660
     * For example, "1 year, 6 months and 3 days" plus 2 months returns "1 year, 8 months and 3 days".
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   661
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   662
     * This instance is immutable and unaffected by this method call.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   663
     *
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   664
     * @param monthsToAdd  the months to add, positive or negative
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   665
     * @return a {@code Period} based on this period with the specified months added, not null
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   666
     * @throws ArithmeticException if numeric overflow occurs
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   667
     */
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   668
    public Period plusMonths(long monthsToAdd) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   669
        if (monthsToAdd == 0) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   670
            return this;
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   671
        }
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   672
        return create(years, Math.toIntExact(Math.addExact(months, monthsToAdd)), days);
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   673
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   674
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   675
    /**
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   676
     * Returns a copy of this period with the specified days added.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   677
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   678
     * This adds the amount to the days unit in a copy of this period.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   679
     * The years and months units are unaffected.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   680
     * For example, "1 year, 6 months and 3 days" plus 2 days returns "1 year, 6 months and 5 days".
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   681
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   682
     * This instance is immutable and unaffected by this method call.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   683
     *
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   684
     * @param daysToAdd  the days to add, positive or negative
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   685
     * @return a {@code Period} based on this period with the specified days added, not null
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   686
     * @throws ArithmeticException if numeric overflow occurs
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   687
     */
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   688
    public Period plusDays(long daysToAdd) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   689
        if (daysToAdd == 0) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   690
            return this;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   691
        }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   692
        return create(years, months, Math.toIntExact(Math.addExact(days, daysToAdd)));
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   693
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   694
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   695
    //-----------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   696
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   697
     * Returns a copy of this period with the specified period subtracted.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   698
     * <p>
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   699
     * This operates separately on the years, months and days.
20519
eee7a92074fd 8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents: 19841
diff changeset
   700
     * No normalization is performed.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   701
     * <p>
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   702
     * For example, "1 year, 6 months and 3 days" minus "2 years, 2 months and 2 days"
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   703
     * returns "-1 years, 4 months and 1 day".
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   704
     * <p>
20519
eee7a92074fd 8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents: 19841
diff changeset
   705
     * The specified amount is typically an instance of {@code Period}.
eee7a92074fd 8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents: 19841
diff changeset
   706
     * Other types are interpreted using {@link Period#from(TemporalAmount)}.
eee7a92074fd 8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents: 19841
diff changeset
   707
     * <p>
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   708
     * This instance is immutable and unaffected by this method call.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   709
     *
24256
da9a41004459 8034906: Fix typos, errors and Javadoc differences in java.time
scolebourne
parents: 22566
diff changeset
   710
     * @param amountToSubtract  the amount to subtract, not null
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   711
     * @return a {@code Period} based on this period with the requested period subtracted, not null
20519
eee7a92074fd 8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents: 19841
diff changeset
   712
     * @throws DateTimeException if the specified amount has a non-ISO chronology or
eee7a92074fd 8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents: 19841
diff changeset
   713
     *  contains an invalid unit
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   714
     * @throws ArithmeticException if numeric overflow occurs
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   715
     */
20519
eee7a92074fd 8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents: 19841
diff changeset
   716
    public Period minus(TemporalAmount amountToSubtract) {
eee7a92074fd 8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents: 19841
diff changeset
   717
        Period isoAmount = Period.from(amountToSubtract);
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   718
        return create(
20519
eee7a92074fd 8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents: 19841
diff changeset
   719
                Math.subtractExact(years, isoAmount.years),
eee7a92074fd 8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents: 19841
diff changeset
   720
                Math.subtractExact(months, isoAmount.months),
eee7a92074fd 8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents: 19841
diff changeset
   721
                Math.subtractExact(days, isoAmount.days));
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   722
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   723
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   724
    /**
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   725
     * Returns a copy of this period with the specified years subtracted.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   726
     * <p>
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   727
     * This subtracts the amount from the years unit in a copy of this period.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   728
     * The months and days units are unaffected.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   729
     * For example, "1 year, 6 months and 3 days" minus 2 years returns "-1 years, 6 months and 3 days".
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   730
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   731
     * This instance is immutable and unaffected by this method call.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   732
     *
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   733
     * @param yearsToSubtract  the years to subtract, positive or negative
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   734
     * @return a {@code Period} based on this period with the specified years subtracted, not null
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   735
     * @throws ArithmeticException if numeric overflow occurs
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   736
     */
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   737
    public Period minusYears(long yearsToSubtract) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   738
        return (yearsToSubtract == Long.MIN_VALUE ? plusYears(Long.MAX_VALUE).plusYears(1) : plusYears(-yearsToSubtract));
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   739
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   740
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   741
    /**
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   742
     * Returns a copy of this period with the specified months subtracted.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   743
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   744
     * This subtracts the amount from the months unit in a copy of this period.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   745
     * The years and days units are unaffected.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   746
     * For example, "1 year, 6 months and 3 days" minus 2 months returns "1 year, 4 months and 3 days".
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   747
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   748
     * This instance is immutable and unaffected by this method call.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   749
     *
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   750
     * @param monthsToSubtract  the years to subtract, positive or negative
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   751
     * @return a {@code Period} based on this period with the specified months subtracted, not null
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   752
     * @throws ArithmeticException if numeric overflow occurs
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   753
     */
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   754
    public Period minusMonths(long monthsToSubtract) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   755
        return (monthsToSubtract == Long.MIN_VALUE ? plusMonths(Long.MAX_VALUE).plusMonths(1) : plusMonths(-monthsToSubtract));
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   756
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   757
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   758
    /**
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   759
     * Returns a copy of this period with the specified days subtracted.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   760
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   761
     * This subtracts the amount from the days unit in a copy of this period.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   762
     * The years and months units are unaffected.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   763
     * For example, "1 year, 6 months and 3 days" minus 2 days returns "1 year, 6 months and 1 day".
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   764
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   765
     * This instance is immutable and unaffected by this method call.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   766
     *
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   767
     * @param daysToSubtract  the months to subtract, positive or negative
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   768
     * @return a {@code Period} based on this period with the specified days subtracted, not null
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   769
     * @throws ArithmeticException if numeric overflow occurs
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   770
     */
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   771
    public Period minusDays(long daysToSubtract) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   772
        return (daysToSubtract == Long.MIN_VALUE ? plusDays(Long.MAX_VALUE).plusDays(1) : plusDays(-daysToSubtract));
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   773
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   774
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   775
    //-----------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   776
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   777
     * Returns a new instance with each element in this period multiplied
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   778
     * by the specified scalar.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   779
     * <p>
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   780
     * This returns a period with each of the years, months and days units
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   781
     * individually multiplied.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   782
     * For example, a period of "2 years, -3 months and 4 days" multiplied by
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   783
     * 3 will return "6 years, -9 months and 12 days".
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   784
     * No normalization is performed.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   785
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   786
     * @param scalar  the scalar to multiply by, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   787
     * @return a {@code Period} based on this period with the amounts multiplied by the scalar, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   788
     * @throws ArithmeticException if numeric overflow occurs
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   789
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   790
    public Period multipliedBy(int scalar) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   791
        if (this == ZERO || scalar == 1) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   792
            return this;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   793
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   794
        return create(
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   795
                Math.multiplyExact(years, scalar),
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   796
                Math.multiplyExact(months, scalar),
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   797
                Math.multiplyExact(days, scalar));
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   798
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   799
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   800
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   801
     * Returns a new instance with each amount in this period negated.
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   802
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   803
     * This returns a period with each of the years, months and days units
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   804
     * individually negated.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   805
     * For example, a period of "2 years, -3 months and 4 days" will be
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   806
     * negated to "-2 years, 3 months and -4 days".
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   807
     * No normalization is performed.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   808
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   809
     * @return a {@code Period} based on this period with the amounts negated, not null
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   810
     * @throws ArithmeticException if numeric overflow occurs, which only happens if
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   811
     *  one of the units has the value {@code Long.MIN_VALUE}
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   812
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   813
    public Period negated() {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   814
        return multipliedBy(-1);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   815
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   816
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   817
    //-----------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   818
    /**
20519
eee7a92074fd 8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents: 19841
diff changeset
   819
     * Returns a copy of this period with the years and months normalized.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   820
     * <p>
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   821
     * This normalizes the years and months units, leaving the days unit unchanged.
42946
955f6bc7abfd 8171940: Incorrect statement about an absolute value of months unit after period's normalization
rriggs
parents: 41482
diff changeset
   822
     * The months unit is adjusted to have an absolute value less than 12,
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   823
     * with the years unit being adjusted to compensate. For example, a period of
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   824
     * "1 Year and 15 months" will be normalized to "2 years and 3 months".
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   825
     * <p>
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   826
     * The sign of the years and months units will be the same after normalization.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   827
     * For example, a period of "1 year and -25 months" will be normalized to
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   828
     * "-1 year and -1 month".
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   829
     * <p>
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   830
     * This instance is immutable and unaffected by this method call.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   831
     *
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   832
     * @return a {@code Period} based on this period with excess months normalized to years, not null
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   833
     * @throws ArithmeticException if numeric overflow occurs
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   834
     */
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   835
    public Period normalized() {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   836
        long totalMonths = toTotalMonths();
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   837
        long splitYears = totalMonths / 12;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   838
        int splitMonths = (int) (totalMonths % 12);  // no overflow
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   839
        if (splitYears == years && splitMonths == months) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   840
            return this;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   841
        }
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   842
        return create(Math.toIntExact(splitYears), splitMonths, days);
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   843
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   844
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   845
    /**
20519
eee7a92074fd 8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents: 19841
diff changeset
   846
     * Gets the total number of months in this period.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   847
     * <p>
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   848
     * This returns the total number of months in the period by multiplying the
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   849
     * number of years by 12 and adding the number of months.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   850
     * <p>
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   851
     * This instance is immutable and unaffected by this method call.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   852
     *
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   853
     * @return the total number of months in the period, may be negative
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   854
     */
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   855
    public long toTotalMonths() {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   856
        return years * 12L + months;  // no overflow
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   857
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   858
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   859
    //-------------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   860
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   861
     * Adds this period to the specified temporal object.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   862
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   863
     * This returns a temporal object of the same observable type as the input
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   864
     * with this period added.
20519
eee7a92074fd 8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents: 19841
diff changeset
   865
     * If the temporal has a chronology, it must be the ISO chronology.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   866
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   867
     * In most cases, it is clearer to reverse the calling pattern by using
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   868
     * {@link Temporal#plus(TemporalAmount)}.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   869
     * <pre>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   870
     *   // these two lines are equivalent, but the second approach is recommended
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   871
     *   dateTime = thisPeriod.addTo(dateTime);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   872
     *   dateTime = dateTime.plus(thisPeriod);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   873
     * </pre>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   874
     * <p>
20519
eee7a92074fd 8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents: 19841
diff changeset
   875
     * The calculation operates as follows.
eee7a92074fd 8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents: 19841
diff changeset
   876
     * First, the chronology of the temporal is checked to ensure it is ISO chronology or null.
eee7a92074fd 8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents: 19841
diff changeset
   877
     * Second, if the months are zero, the years are added if non-zero, otherwise
eee7a92074fd 8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents: 19841
diff changeset
   878
     * the combination of years and months is added if non-zero.
eee7a92074fd 8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents: 19841
diff changeset
   879
     * Finally, any days are added.
eee7a92074fd 8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents: 19841
diff changeset
   880
     * <p>
eee7a92074fd 8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents: 19841
diff changeset
   881
     * This approach ensures that a partial period can be added to a partial date.
eee7a92074fd 8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents: 19841
diff changeset
   882
     * For example, a period of years and/or months can be added to a {@code YearMonth},
eee7a92074fd 8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents: 19841
diff changeset
   883
     * but a period including days cannot.
eee7a92074fd 8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents: 19841
diff changeset
   884
     * The approach also adds years and months together when necessary, which ensures
eee7a92074fd 8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents: 19841
diff changeset
   885
     * correct behaviour at the end of the month.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   886
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   887
     * This instance is immutable and unaffected by this method call.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   888
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   889
     * @param temporal  the temporal object to adjust, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   890
     * @return an object of the same type with the adjustment made, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   891
     * @throws DateTimeException if unable to add
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   892
     * @throws ArithmeticException if numeric overflow occurs
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   893
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   894
    @Override
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   895
    public Temporal addTo(Temporal temporal) {
20519
eee7a92074fd 8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents: 19841
diff changeset
   896
        validateChrono(temporal);
eee7a92074fd 8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents: 19841
diff changeset
   897
        if (months == 0) {
eee7a92074fd 8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents: 19841
diff changeset
   898
            if (years != 0) {
eee7a92074fd 8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents: 19841
diff changeset
   899
                temporal = temporal.plus(years, YEARS);
eee7a92074fd 8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents: 19841
diff changeset
   900
            }
eee7a92074fd 8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents: 19841
diff changeset
   901
        } else {
eee7a92074fd 8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents: 19841
diff changeset
   902
            long totalMonths = toTotalMonths();
eee7a92074fd 8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents: 19841
diff changeset
   903
            if (totalMonths != 0) {
eee7a92074fd 8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents: 19841
diff changeset
   904
                temporal = temporal.plus(totalMonths, MONTHS);
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   905
            }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   906
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   907
        if (days != 0) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   908
            temporal = temporal.plus(days, DAYS);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   909
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   910
        return temporal;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   911
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   912
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   913
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   914
     * Subtracts this period from the specified temporal object.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   915
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   916
     * This returns a temporal object of the same observable type as the input
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   917
     * with this period subtracted.
20519
eee7a92074fd 8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents: 19841
diff changeset
   918
     * If the temporal has a chronology, it must be the ISO chronology.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   919
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   920
     * In most cases, it is clearer to reverse the calling pattern by using
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   921
     * {@link Temporal#minus(TemporalAmount)}.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   922
     * <pre>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   923
     *   // these two lines are equivalent, but the second approach is recommended
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   924
     *   dateTime = thisPeriod.subtractFrom(dateTime);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   925
     *   dateTime = dateTime.minus(thisPeriod);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   926
     * </pre>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   927
     * <p>
20519
eee7a92074fd 8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents: 19841
diff changeset
   928
     * The calculation operates as follows.
eee7a92074fd 8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents: 19841
diff changeset
   929
     * First, the chronology of the temporal is checked to ensure it is ISO chronology or null.
eee7a92074fd 8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents: 19841
diff changeset
   930
     * Second, if the months are zero, the years are subtracted if non-zero, otherwise
eee7a92074fd 8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents: 19841
diff changeset
   931
     * the combination of years and months is subtracted if non-zero.
eee7a92074fd 8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents: 19841
diff changeset
   932
     * Finally, any days are subtracted.
eee7a92074fd 8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents: 19841
diff changeset
   933
     * <p>
eee7a92074fd 8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents: 19841
diff changeset
   934
     * This approach ensures that a partial period can be subtracted from a partial date.
eee7a92074fd 8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents: 19841
diff changeset
   935
     * For example, a period of years and/or months can be subtracted from a {@code YearMonth},
eee7a92074fd 8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents: 19841
diff changeset
   936
     * but a period including days cannot.
eee7a92074fd 8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents: 19841
diff changeset
   937
     * The approach also subtracts years and months together when necessary, which ensures
eee7a92074fd 8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents: 19841
diff changeset
   938
     * correct behaviour at the end of the month.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   939
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   940
     * This instance is immutable and unaffected by this method call.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   941
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   942
     * @param temporal  the temporal object to adjust, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   943
     * @return an object of the same type with the adjustment made, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   944
     * @throws DateTimeException if unable to subtract
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   945
     * @throws ArithmeticException if numeric overflow occurs
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   946
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   947
    @Override
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   948
    public Temporal subtractFrom(Temporal temporal) {
20519
eee7a92074fd 8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents: 19841
diff changeset
   949
        validateChrono(temporal);
eee7a92074fd 8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents: 19841
diff changeset
   950
        if (months == 0) {
eee7a92074fd 8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents: 19841
diff changeset
   951
            if (years != 0) {
eee7a92074fd 8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents: 19841
diff changeset
   952
                temporal = temporal.minus(years, YEARS);
eee7a92074fd 8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents: 19841
diff changeset
   953
            }
eee7a92074fd 8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents: 19841
diff changeset
   954
        } else {
eee7a92074fd 8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents: 19841
diff changeset
   955
            long totalMonths = toTotalMonths();
eee7a92074fd 8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents: 19841
diff changeset
   956
            if (totalMonths != 0) {
eee7a92074fd 8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents: 19841
diff changeset
   957
                temporal = temporal.minus(totalMonths, MONTHS);
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   958
            }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   959
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   960
        if (days != 0) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   961
            temporal = temporal.minus(days, DAYS);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   962
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   963
        return temporal;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   964
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   965
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   966
    /**
20519
eee7a92074fd 8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents: 19841
diff changeset
   967
     * Validates that the temporal has the correct chronology.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   968
     */
20519
eee7a92074fd 8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents: 19841
diff changeset
   969
    private void validateChrono(TemporalAccessor temporal) {
eee7a92074fd 8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents: 19841
diff changeset
   970
        Objects.requireNonNull(temporal, "temporal");
20795
8ec9e5b79828 8025722: TemporalAdjusters and TemporalQueries
rriggs
parents: 20519
diff changeset
   971
        Chronology temporalChrono = temporal.query(TemporalQueries.chronology());
20519
eee7a92074fd 8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents: 19841
diff changeset
   972
        if (temporalChrono != null && IsoChronology.INSTANCE.equals(temporalChrono) == false) {
eee7a92074fd 8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents: 19841
diff changeset
   973
            throw new DateTimeException("Chronology mismatch, expected: ISO, actual: " + temporalChrono.getId());
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   974
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   975
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   976
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   977
    //-----------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   978
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   979
     * Checks if this period is equal to another period.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   980
     * <p>
20519
eee7a92074fd 8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents: 19841
diff changeset
   981
     * The comparison is based on the type {@code Period} and each of the three amounts.
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   982
     * To be equal, the years, months and days units must be individually equal.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   983
     * Note that this means that a period of "15 Months" is not equal to a period
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   984
     * of "1 Year and 3 Months".
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   985
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   986
     * @param obj  the object to check, null returns false
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   987
     * @return true if this is equal to the other period
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   988
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   989
    @Override
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   990
    public boolean equals(Object obj) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   991
        if (this == obj) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   992
            return true;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   993
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   994
        if (obj instanceof Period) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   995
            Period other = (Period) obj;
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   996
            return years == other.years &&
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   997
                    months == other.months &&
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   998
                    days == other.days;
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   999
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1000
        return false;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1001
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1002
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1003
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1004
     * A hash code for this period.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1005
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1006
     * @return a suitable hash code
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1007
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1008
    @Override
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1009
    public int hashCode() {
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1010
        return years + Integer.rotateLeft(months, 8) + Integer.rotateLeft(days, 16);
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1011
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1012
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1013
    //-----------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1014
    /**
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1015
     * Outputs this period as a {@code String}, such as {@code P6Y3M1D}.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1016
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1017
     * The output will be in the ISO-8601 period format.
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1018
     * A zero period will be represented as zero days, 'P0D'.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1019
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1020
     * @return a string representation of this period, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1021
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1022
    @Override
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1023
    public String toString() {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1024
        if (this == ZERO) {
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1025
            return "P0D";
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1026
        } else {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1027
            StringBuilder buf = new StringBuilder();
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1028
            buf.append('P');
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1029
            if (years != 0) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1030
                buf.append(years).append('Y');
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1031
            }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1032
            if (months != 0) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1033
                buf.append(months).append('M');
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1034
            }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1035
            if (days != 0) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1036
                buf.append(days).append('D');
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1037
            }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1038
            return buf.toString();
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1039
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1040
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1041
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1042
    //-----------------------------------------------------------------------
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1043
    /**
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1044
     * Writes the object using a
52078
4a63197816ce 8211952: Broken links in java.time API
jjg
parents: 49433
diff changeset
  1045
     * <a href="{@docRoot}/serialized-form.html#java.time.Ser">dedicated serialized form</a>.
19841
15c8e97d6a14 8024164: JSR310 serialization should be described in details
rriggs
parents: 19030
diff changeset
  1046
     * @serialData
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1047
     * <pre>
19841
15c8e97d6a14 8024164: JSR310 serialization should be described in details
rriggs
parents: 19030
diff changeset
  1048
     *  out.writeByte(14);  // identifies a Period
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1049
     *  out.writeInt(years);
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1050
     *  out.writeInt(months);
19841
15c8e97d6a14 8024164: JSR310 serialization should be described in details
rriggs
parents: 19030
diff changeset
  1051
     *  out.writeInt(days);
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1052
     * </pre>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1053
     *
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1054
     * @return the instance of {@code Ser}, not null
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1055
     */
57956
e0b8b019d2f5 8229997: Apply java.io.Serial annotations in java.base
darcy
parents: 52078
diff changeset
  1056
    @java.io.Serial
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1057
    private Object writeReplace() {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1058
        return new Ser(Ser.PERIOD_TYPE, this);
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1059
    }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1060
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1061
    /**
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1062
     * Defend against malicious streams.
22081
86eb26ff8f2b 8030002: Enhance deserialization using readObject
rriggs
parents: 21331
diff changeset
  1063
     *
22566
4ebe53dd7814 8032502: java.time add @param tags to readObject
rriggs
parents: 22108
diff changeset
  1064
     * @param s the stream to read
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1065
     * @throws java.io.InvalidObjectException always
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1066
     */
57956
e0b8b019d2f5 8229997: Apply java.io.Serial annotations in java.base
darcy
parents: 52078
diff changeset
  1067
    @java.io.Serial
22081
86eb26ff8f2b 8030002: Enhance deserialization using readObject
rriggs
parents: 21331
diff changeset
  1068
    private void readObject(ObjectInputStream s) throws InvalidObjectException {
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1069
        throw new InvalidObjectException("Deserialization via serialization delegate");
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1070
    }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1071
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1072
    void writeExternal(DataOutput out) throws IOException {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1073
        out.writeInt(years);
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1074
        out.writeInt(months);
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1075
        out.writeInt(days);
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1076
    }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1077
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1078
    static Period readExternal(DataInput in) throws IOException {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1079
        int years = in.readInt();
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1080
        int months = in.readInt();
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1081
        int days = in.readInt();
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1082
        return Period.of(years, months, days);
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1083
    }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1084
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1085
}