jdk/src/share/classes/java/time/Period.java
author sherman
Tue, 12 Feb 2013 09:25:43 -0800
changeset 15658 55b829ca2334
parent 15289 3ac550392e43
child 16852 60207b2b4b42
permissions -rw-r--r--
8007392: JSR 310: DateTime API Updates 8007520: Update date/time classes in j.util and j.sql packages 8007572: Replace existing jdk timezone data at <java.home>/lib/zi with JSR310's tzdb Summary: Integration of JSR310 Date/Time API for M7 Reviewed-by: darcy, alanb, naoto Contributed-by: scolebourne@joda.org, roger.riggs@oracle.com, masayoshi.okutsu@oracle.com, patrick.zhang@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
     1
/*
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
     2
 * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
     4
 *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    10
 *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    15
 * accompanied this code).
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    16
 *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    20
 *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    23
 * questions.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    24
 */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    25
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    26
/*
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    27
 * This file is available under and governed by the GNU General Public
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    28
 * License version 2 only, as published by the Free Software Foundation.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    29
 * However, the following notice accompanied the original version of this
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    30
 * file:
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    31
 *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    32
 * Copyright (c) 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.ChronoField.MONTH_OF_YEAR;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    65
import static java.time.temporal.ChronoUnit.DAYS;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    66
import static java.time.temporal.ChronoUnit.MONTHS;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    67
import static java.time.temporal.ChronoUnit.YEARS;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    68
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
    69
import java.io.DataInput;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
    70
import java.io.DataOutput;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
    71
import java.io.IOException;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
    72
import java.io.InvalidObjectException;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
    73
import java.io.ObjectStreamException;
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    74
import java.io.Serializable;
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
    75
import java.time.chrono.ChronoLocalDate;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
    76
import java.time.chrono.Chronology;
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    77
import java.time.format.DateTimeParseException;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    78
import java.time.temporal.ChronoUnit;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    79
import java.time.temporal.Temporal;
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
    80
import java.time.temporal.TemporalAmount;
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    81
import java.time.temporal.TemporalUnit;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    82
import java.time.temporal.ValueRange;
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
    83
import java.util.Arrays;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
    84
import java.util.Collections;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
    85
import java.util.List;
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    86
import java.util.Objects;
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
    87
import java.util.regex.Matcher;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
    88
import java.util.regex.Pattern;
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    89
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    90
/**
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
    91
 * A date-based amount of time, such as '2 years, 3 months and 4 days'.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
    92
 * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
    93
 * 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
    94
 * See {@link Duration} for the time-based equivalent to this class.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
    95
 * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
    96
 * Durations and period differ in their treatment of daylight savings time
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
    97
 * when added to {@link ZonedDateTime}. A {@code Duration} will add an exact
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
    98
 * 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
    99
 * 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
   100
 * the local time.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   101
 * <p>
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   102
 * 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
   103
 * 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
   104
 * 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
   105
 * 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
   106
 * {@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
   107
 * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   108
 * The supported units of a period are {@link ChronoUnit#YEARS YEARS},
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   109
 * {@link ChronoUnit#MONTHS MONTHS} and {@link ChronoUnit#DAYS DAYS}.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   110
 * All three fields are always present, but may be set to zero.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   111
 * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   112
 * The period may be used with any calendar system.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   113
 * The meaning of a "year" or "month" is only applied when the object is added to a date.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   114
 * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   115
 * 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
   116
 * period may be negative.
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   117
 * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   118
 * The months and years fields may be {@linkplain #normalized() normalized}.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   119
 * The normalization assumes a 12 month year, so is not appropriate for all calendar systems.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   120
 *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   121
 * <h3>Specification for implementors</h3>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   122
 * This class is immutable and thread-safe.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   123
 *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   124
 * @since 1.8
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   125
 */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   126
public final class Period
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   127
        implements TemporalAmount, Serializable {
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   128
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   129
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   130
     * A constant for a period of zero.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   131
     */
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   132
    public static final Period ZERO = new Period(0, 0, 0);
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   133
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   134
     * Serialization version.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   135
     */
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   136
    private static final long serialVersionUID = -3587258372562876L;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   137
    /**
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   138
     * The pattern for parsing.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   139
     */
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   140
    private final static Pattern PATTERN =
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   141
            Pattern.compile("([-+]?)P(?:([-+]?[0-9]+)Y)?(?:([-+]?[0-9]+)M)?(?:([-+]?[0-9]+)D)?", Pattern.CASE_INSENSITIVE);
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   142
    /**
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   143
     * The set of supported units.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   144
     */
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   145
    private final static List<TemporalUnit> SUPPORTED_UNITS =
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   146
            Collections.unmodifiableList(Arrays.<TemporalUnit>asList(YEARS, MONTHS, DAYS));
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   147
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   148
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   149
     * The number of years.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   150
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   151
    private final int years;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   152
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   153
     * The number of months.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   154
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   155
    private final int months;
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 days.
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 days;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   160
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   161
    //-----------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   162
    /**
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   163
     * Obtains a {@code Period} representing a number of years.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   164
     * <p>
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   165
     * The resulting period will have the specified years.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   166
     * The months and days units will be zero.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   167
     *
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   168
     * @param years  the number of years, positive or negative
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   169
     * @return the period of years, not null
15289
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
    public static Period ofYears(int years) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   172
        return create(years, 0, 0);
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   173
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   174
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   175
    /**
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   176
     * Obtains a {@code Period} representing a number of months.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   177
     * <p>
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   178
     * The resulting period will have the specified months.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   179
     * The years and days units will be zero.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   180
     *
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   181
     * @param months  the number of months, positive or negative
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   182
     * @return the period of months, not null
15289
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
    public static Period ofMonths(int months) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   185
        return create(0, months, 0);
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   186
    }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   187
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   188
    /**
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   189
     * Obtains a {@code Period} representing a number of days.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   190
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   191
     * The resulting period will have the specified days.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   192
     * The years and months units will be zero.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   193
     *
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   194
     * @param days  the number of days, positive or negative
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   195
     * @return the period of days, not null
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   196
     */
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   197
    public static Period ofDays(int days) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   198
        return create(0, 0, days);
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   199
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   200
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   201
    //-----------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   202
    /**
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   203
     * Obtains a {@code Period} representing a number of years, months and days.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   204
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   205
     * This creates an instance based on years, months and days.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   206
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   207
     * @param years  the amount of years, may be negative
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   208
     * @param months  the amount of months, may be negative
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   209
     * @param days  the amount of days, may be negative
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   210
     * @return the period of years, months and days, not null
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   211
     */
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   212
    public static Period of(int years, int months, int days) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   213
        return create(years, months, days);
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   214
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   215
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   216
    //-----------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   217
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   218
     * Obtains a {@code Period} consisting of the number of years, months,
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   219
     * and days between two dates.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   220
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   221
     * The start date is included, but the end date is not.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   222
     * The period is calculated by removing complete months, then calculating
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   223
     * the remaining number of days, adjusting to ensure that both have the same sign.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   224
     * The number of months is then split into years and months based on a 12 month year.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   225
     * A month is considered if the end day-of-month is greater than or equal to the start day-of-month.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   226
     * For example, from {@code 2010-01-15} to {@code 2011-03-18} is one year, two months and three days.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   227
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   228
     * The result of this method can be a negative period if the end is before the start.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   229
     * The negative sign will be the same in each of year, month and day.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   230
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   231
     * @param startDate  the start date, inclusive, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   232
     * @param endDate  the end date, exclusive, not null
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   233
     * @return the period between this date and the end date, not null
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   234
     * @see ChronoLocalDate#periodUntil(ChronoLocalDate)
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   235
     */
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   236
    public static Period between(LocalDate startDate, LocalDate endDate) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   237
        return startDate.periodUntil(endDate);
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   238
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   239
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   240
    //-----------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   241
    /**
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   242
     * Obtains a {@code Period} from a text string such as {@code PnYnMnD}.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   243
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   244
     * This will parse the string produced by {@code toString()} which is
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   245
     * based on the ISO-8601 period format {@code PnYnMnD}.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   246
     * <p>
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   247
     * The string starts with an optional sign, denoted by the ASCII negative
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   248
     * or positive symbol. If negative, the whole period is negated.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   249
     * The ASCII letter "P" is next in upper or lower case.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   250
     * There are then three sections, each consisting of a number and a suffix.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   251
     * At least one of the three sections must be present.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   252
     * The sections have suffixes in ASCII of "Y", "M" and "D" for
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   253
     * years, months and days, accepted in upper or lower case.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   254
     * The suffixes must occur in order.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   255
     * The number part of each section must consist of ASCII digits.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   256
     * The number may be prefixed by the ASCII negative or positive symbol.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   257
     * The number must parse to an {@code int}.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   258
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   259
     * The leading plus/minus sign, and negative values for other units are
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   260
     * not part of the ISO-8601 standard.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   261
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   262
     * @param text  the text to parse, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   263
     * @return the parsed period, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   264
     * @throws DateTimeParseException if the text cannot be parsed to a period
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   265
     */
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   266
    public static Period parse(CharSequence text) {
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   267
        Objects.requireNonNull(text, "text");
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   268
        Matcher matcher = PATTERN.matcher(text);
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   269
        if (matcher.matches()) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   270
            int negate = ("-".equals(matcher.group(1)) ? -1 : 1);
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   271
            String yearMatch = matcher.group(2);
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   272
            String monthMatch = matcher.group(3);
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   273
            String dayMatch = matcher.group(4);
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   274
            if (yearMatch != null || monthMatch != null || dayMatch != null) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   275
                try {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   276
                    return create(parseNumber(text, yearMatch, negate),
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   277
                            parseNumber(text, monthMatch, negate),
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   278
                            parseNumber(text, dayMatch, negate));
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   279
                } catch (NumberFormatException ex) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   280
                    throw (DateTimeParseException) new DateTimeParseException("Text cannot be parsed to a Period", text, 0).initCause(ex);
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   281
                }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   282
            }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   283
        }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   284
        throw new DateTimeParseException("Text cannot be parsed to a Period", text, 0);
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   285
    }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   286
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   287
    private static int parseNumber(CharSequence text, String str, int negate) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   288
        if (str == null) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   289
            return 0;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   290
        }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   291
        int val = Integer.parseInt(str);
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   292
        try {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   293
            return Math.multiplyExact(val, negate);
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   294
        } catch (ArithmeticException ex) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   295
            throw (DateTimeParseException) new DateTimeParseException("Text cannot be parsed to a Period", text, 0).initCause(ex);
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   296
        }
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   297
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   298
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   299
    //-----------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   300
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   301
     * Creates an instance.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   302
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   303
     * @param years  the amount
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   304
     * @param months  the amount
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   305
     * @param days  the amount
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   306
     */
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   307
    private static Period create(int years, int months, int days) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   308
        if ((years | months | days) == 0) {
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   309
            return ZERO;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   310
        }
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   311
        return new Period(years, months, days);
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   312
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   313
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   314
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   315
     * Constructor.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   316
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   317
     * @param years  the amount
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   318
     * @param months  the amount
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   319
     * @param days  the amount
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   320
     */
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   321
    private Period(int years, int months, int days) {
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   322
        this.years = years;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   323
        this.months = months;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   324
        this.days = days;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   325
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   326
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   327
    //-----------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   328
    /**
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   329
     * Gets the value of the requested unit.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   330
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   331
     * This returns a value for each of the three supported units,
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   332
     * {@link ChronoUnit#YEARS YEARS}, {@link ChronoUnit#MONTHS MONTHS} and
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   333
     * {@link ChronoUnit#DAYS DAYS}.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   334
     * All other units throw an exception.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   335
     *
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   336
     * @param unit the {@code TemporalUnit} for which to return the value
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   337
     * @return the long value of the unit
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   338
     * @throws DateTimeException if the unit is not supported
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   339
     */
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   340
    @Override
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   341
    public long get(TemporalUnit unit) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   342
        if (unit == ChronoUnit.YEARS) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   343
            return getYears();
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   344
        } else if (unit == ChronoUnit.MONTHS) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   345
            return getMonths();
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   346
        } else if (unit == ChronoUnit.DAYS) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   347
            return getDays();
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   348
        } else {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   349
            throw new DateTimeException("Unsupported unit: " + unit.getName());
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   350
        }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   351
    }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   352
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   353
    /**
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   354
     * Gets the set of units supported by this period.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   355
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   356
     * The supported units are {@link ChronoUnit#YEARS YEARS},
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   357
     * {@link ChronoUnit#MONTHS MONTHS} and {@link ChronoUnit#DAYS DAYS}.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   358
     * They are returned in the order years, months, days.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   359
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   360
     * This set can be used in conjunction with {@link #get(TemporalUnit)}
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   361
     * to access the entire state of the period.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   362
     *
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   363
     * @return a list containing the years, months and days units, not null
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   364
     */
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   365
    @Override
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   366
    public List<TemporalUnit> getUnits() {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   367
        return SUPPORTED_UNITS;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   368
    }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   369
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   370
    //-----------------------------------------------------------------------
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   371
    /**
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   372
     * Checks if all three units of this period are zero.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   373
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   374
     * 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
   375
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   376
     * @return true if this period is zero-length
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   377
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   378
    public boolean isZero() {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   379
        return (this == ZERO);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   380
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   381
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   382
    /**
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   383
     * Checks if any of the three units of this period are negative.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   384
     * <p>
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   385
     * 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
   386
     *
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   387
     * @return true if any unit of this period is negative
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   388
     */
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   389
    public boolean isNegative() {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   390
        return years < 0 || months < 0 || days < 0;
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   391
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   392
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   393
    //-----------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   394
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   395
     * Gets the amount of years of this period.
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   396
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   397
     * This returns the years unit.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   398
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   399
     * The months unit is not normalized with the years unit.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   400
     * 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
   401
     * of "1 year and 3 months".
15289
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 the amount of years of this period, may be negative
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
    public int getYears() {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   406
        return years;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   407
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   408
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   409
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   410
     * Gets the amount of months of this period.
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   411
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   412
     * This returns the months unit.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   413
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   414
     * The months unit is not normalized with the years unit.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   415
     * 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
   416
     * of "1 year and 3 months".
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   417
     *
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   418
     * @return the amount of months of this period, may be negative
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   419
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   420
    public int getMonths() {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   421
        return months;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   422
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   423
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   424
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   425
     * Gets the amount of days of this period.
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   426
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   427
     * This returns the days unit.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   428
     *
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   429
     * @return the amount of days of this period, may be negative
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   430
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   431
    public int getDays() {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   432
        return days;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   433
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   434
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   435
    //-----------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   436
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   437
     * Returns a copy of this period with the specified amount of years.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   438
     * <p>
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   439
     * 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
   440
     * The months and days units are unaffected.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   441
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   442
     * The months unit is not normalized with the years unit.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   443
     * 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
   444
     * of "1 year and 3 months".
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   445
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   446
     * This instance is immutable and unaffected by this method call.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   447
     *
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   448
     * @param years  the years to represent, may be negative
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   449
     * @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
   450
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   451
    public Period withYears(int years) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   452
        if (years == this.years) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   453
            return this;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   454
        }
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   455
        return create(years, months, days);
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   456
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   457
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   458
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   459
     * Returns a copy of this period with the specified amount of months.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   460
     * <p>
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   461
     * 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
   462
     * The years and days units are unaffected.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   463
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   464
     * The months unit is not normalized with the years unit.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   465
     * 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
   466
     * of "1 year and 3 months".
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   467
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   468
     * This instance is immutable and unaffected by this method call.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   469
     *
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   470
     * @param months  the months to represent, may be negative
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   471
     * @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
   472
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   473
    public Period withMonths(int months) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   474
        if (months == this.months) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   475
            return this;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   476
        }
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   477
        return create(years, months, days);
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   478
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   479
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   480
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   481
     * Returns a copy of this period with the specified amount of days.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   482
     * <p>
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   483
     * 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
   484
     * The years and months units are unaffected.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   485
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   486
     * This instance is immutable and unaffected by this method call.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   487
     *
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   488
     * @param days  the days to represent, may be negative
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   489
     * @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
   490
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   491
    public Period withDays(int days) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   492
        if (days == this.days) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   493
            return this;
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 create(years, months, days);
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   496
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   497
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   498
    //-----------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   499
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   500
     * Returns a copy of this period with the specified period added.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   501
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   502
     * This operates separately on the years, months, days and the normalized time.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   503
     * There is no further normalization beyond the normalized time.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   504
     * <p>
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   505
     * 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
   506
     * returns "3 years, 8 months and 5 days".
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   507
     * <p>
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   508
     * This instance is immutable and unaffected by this method call.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   509
     *
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   510
     * @param amountToAdd  the period to add, not null
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   511
     * @return a {@code Period} based on this period with the requested period added, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   512
     * @throws ArithmeticException if numeric overflow occurs
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   513
     */
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   514
    public Period plus(Period amountToAdd) {
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   515
        return create(
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   516
                Math.addExact(years, amountToAdd.years),
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   517
                Math.addExact(months, amountToAdd.months),
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   518
                Math.addExact(days, amountToAdd.days));
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   519
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   520
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   521
    /**
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   522
     * Returns a copy of this period with the specified years added.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   523
     * <p>
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   524
     * 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
   525
     * The months and days units are unaffected.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   526
     * 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
   527
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   528
     * This instance is immutable and unaffected by this method call.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   529
     *
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   530
     * @param yearsToAdd  the years to add, positive or negative
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   531
     * @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
   532
     * @throws ArithmeticException if numeric overflow occurs
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   533
     */
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   534
    public Period plusYears(long yearsToAdd) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   535
        if (yearsToAdd == 0) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   536
            return this;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   537
        }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   538
        return create(Math.toIntExact(Math.addExact(years, yearsToAdd)), months, days);
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   539
    }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   540
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   541
    /**
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   542
     * Returns a copy of this period with the specified months added.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   543
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   544
     * 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
   545
     * The years and days units are unaffected.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   546
     * 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
   547
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   548
     * This instance is immutable and unaffected by this method call.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   549
     *
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   550
     * @param monthsToAdd  the months to add, positive or negative
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   551
     * @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
   552
     * @throws ArithmeticException if numeric overflow occurs
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   553
     */
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   554
    public Period plusMonths(long monthsToAdd) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   555
        if (monthsToAdd == 0) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   556
            return this;
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   557
        }
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   558
        return create(years, Math.toIntExact(Math.addExact(months, monthsToAdd)), days);
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   559
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   560
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   561
    /**
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   562
     * Returns a copy of this period with the specified days added.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   563
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   564
     * 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
   565
     * The years and months units are unaffected.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   566
     * 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
   567
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   568
     * This instance is immutable and unaffected by this method call.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   569
     *
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   570
     * @param daysToAdd  the days to add, positive or negative
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   571
     * @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
   572
     * @throws ArithmeticException if numeric overflow occurs
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   573
     */
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   574
    public Period plusDays(long daysToAdd) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   575
        if (daysToAdd == 0) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   576
            return this;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   577
        }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   578
        return create(years, months, Math.toIntExact(Math.addExact(days, daysToAdd)));
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   579
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   580
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   581
    //-----------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   582
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   583
     * Returns a copy of this period with the specified period subtracted.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   584
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   585
     * This operates separately on the years, months, days and the normalized time.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   586
     * There is no further normalization beyond the normalized time.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   587
     * <p>
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   588
     * 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
   589
     * returns "-1 years, 4 months and 1 day".
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   590
     * <p>
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   591
     * This instance is immutable and unaffected by this method call.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   592
     *
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   593
     * @param amountToSubtract  the period to subtract, not null
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   594
     * @return a {@code Period} based on this period with the requested period subtracted, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   595
     * @throws ArithmeticException if numeric overflow occurs
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   596
     */
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   597
    public Period minus(Period amountToSubtract) {
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   598
        return create(
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   599
                Math.subtractExact(years, amountToSubtract.years),
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   600
                Math.subtractExact(months, amountToSubtract.months),
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   601
                Math.subtractExact(days, amountToSubtract.days));
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   602
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   603
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   604
    /**
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   605
     * Returns a copy of this period with the specified years subtracted.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   606
     * <p>
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   607
     * 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
   608
     * The months and days units are unaffected.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   609
     * 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
   610
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   611
     * This instance is immutable and unaffected by this method call.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   612
     *
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   613
     * @param yearsToSubtract  the years to subtract, positive or negative
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   614
     * @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
   615
     * @throws ArithmeticException if numeric overflow occurs
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   616
     */
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   617
    public Period minusYears(long yearsToSubtract) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   618
        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
   619
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   620
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   621
    /**
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   622
     * Returns a copy of this period with the specified months subtracted.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   623
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   624
     * 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
   625
     * The years and days units are unaffected.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   626
     * 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
   627
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   628
     * This instance is immutable and unaffected by this method call.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   629
     *
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   630
     * @param monthsToSubtract  the years to subtract, positive or negative
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   631
     * @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
   632
     * @throws ArithmeticException if numeric overflow occurs
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   633
     */
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   634
    public Period minusMonths(long monthsToSubtract) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   635
        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
   636
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   637
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   638
    /**
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   639
     * Returns a copy of this period with the specified days subtracted.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   640
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   641
     * 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
   642
     * The years and months units are unaffected.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   643
     * 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
   644
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   645
     * This instance is immutable and unaffected by this method call.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   646
     *
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   647
     * @param daysToSubtract  the months to subtract, positive or negative
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   648
     * @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
   649
     * @throws ArithmeticException if numeric overflow occurs
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   650
     */
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   651
    public Period minusDays(long daysToSubtract) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   652
        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
   653
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   654
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   655
    //-----------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   656
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   657
     * Returns a new instance with each element in this period multiplied
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   658
     * by the specified scalar.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   659
     * <p>
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   660
     * 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
   661
     * individually multiplied.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   662
     * 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
   663
     * 3 will return "6 years, -9 months and 12 days".
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   664
     * No normalization is performed.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   665
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   666
     * @param scalar  the scalar to multiply by, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   667
     * @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
   668
     * @throws ArithmeticException if numeric overflow occurs
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   669
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   670
    public Period multipliedBy(int scalar) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   671
        if (this == ZERO || scalar == 1) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   672
            return this;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   673
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   674
        return create(
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   675
                Math.multiplyExact(years, scalar),
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   676
                Math.multiplyExact(months, scalar),
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   677
                Math.multiplyExact(days, scalar));
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   678
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   679
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   680
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   681
     * Returns a new instance with each amount in this period negated.
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   682
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   683
     * 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
   684
     * individually negated.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   685
     * 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
   686
     * negated to "-2 years, 3 months and -4 days".
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   687
     * No normalization is performed.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   688
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   689
     * @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
   690
     * @throws ArithmeticException if numeric overflow occurs, which only happens if
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   691
     *  one of the units has the value {@code Long.MIN_VALUE}
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   692
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   693
    public Period negated() {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   694
        return multipliedBy(-1);
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
    //-----------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   698
    /**
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   699
     * Returns a copy of this period with the years and months normalized
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   700
     * using a 12 month year.
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
     * This normalizes the years and months units, leaving the days unit unchanged.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   703
     * The months unit is adjusted to have an absolute value less than 11,
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   704
     * 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
   705
     * "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
   706
     * <p>
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   707
     * 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
   708
     * 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
   709
     * "-1 year and -1 month".
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   710
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   711
     * This normalization uses a 12 month year which is not valid for all calendar systems.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   712
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   713
     * This instance is immutable and unaffected by this method call.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   714
     *
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   715
     * @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
   716
     * @throws ArithmeticException if numeric overflow occurs
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   717
     */
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   718
    public Period normalized() {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   719
        long totalMonths = toTotalMonths();
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   720
        long splitYears = totalMonths / 12;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   721
        int splitMonths = (int) (totalMonths % 12);  // no overflow
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   722
        if (splitYears == years && splitMonths == months) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   723
            return this;
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
        return create(Math.toIntExact(splitYears), splitMonths, days);
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   726
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   727
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   728
    /**
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   729
     * Gets the total number of months in this period using a 12 month year.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   730
     * <p>
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   731
     * 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
   732
     * number of years by 12 and adding the number of months.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   733
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   734
     * This uses a 12 month year which is not valid for all calendar systems.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   735
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   736
     * This instance is immutable and unaffected by this method call.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   737
     *
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   738
     * @return the total number of months in the period, may be negative
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   739
     */
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   740
    public long toTotalMonths() {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   741
        return years * 12L + months;  // no overflow
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   742
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   743
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   744
    //-------------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   745
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   746
     * Adds this period to the specified temporal object.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   747
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   748
     * This returns a temporal object of the same observable type as the input
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   749
     * with this period added.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   750
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   751
     * 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
   752
     * {@link Temporal#plus(TemporalAmount)}.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   753
     * <pre>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   754
     *   // these two lines are equivalent, but the second approach is recommended
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   755
     *   dateTime = thisPeriod.addTo(dateTime);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   756
     *   dateTime = dateTime.plus(thisPeriod);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   757
     * </pre>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   758
     * <p>
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   759
     * The calculation will add the years, then months, then days.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   760
     * Only non-zero amounts will be added.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   761
     * If the date-time has a calendar system with a fixed number of months in a
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   762
     * year, then the years and months will be combined before being added.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   763
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   764
     * This instance is immutable and unaffected by this method call.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   765
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   766
     * @param temporal  the temporal object to adjust, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   767
     * @return an object of the same type with the adjustment made, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   768
     * @throws DateTimeException if unable to add
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   769
     * @throws ArithmeticException if numeric overflow occurs
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   770
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   771
    @Override
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   772
    public Temporal addTo(Temporal temporal) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   773
        Objects.requireNonNull(temporal, "temporal");
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   774
        if ((years | months) != 0) {
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   775
            long monthRange = monthRange(temporal);
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   776
            if (monthRange >= 0) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   777
                temporal = temporal.plus(years * monthRange + months, MONTHS);
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   778
            } else {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   779
                if (years != 0) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   780
                    temporal = temporal.plus(years, YEARS);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   781
                }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   782
                if (months != 0) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   783
                    temporal = temporal.plus(months, MONTHS);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   784
                }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   785
            }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   786
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   787
        if (days != 0) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   788
            temporal = temporal.plus(days, DAYS);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   789
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   790
        return temporal;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   791
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   792
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   793
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   794
     * Subtracts this period from the specified temporal object.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   795
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   796
     * This returns a temporal object of the same observable type as the input
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   797
     * with this period subtracted.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   798
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   799
     * 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
   800
     * {@link Temporal#minus(TemporalAmount)}.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   801
     * <pre>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   802
     *   // these two lines are equivalent, but the second approach is recommended
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   803
     *   dateTime = thisPeriod.subtractFrom(dateTime);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   804
     *   dateTime = dateTime.minus(thisPeriod);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   805
     * </pre>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   806
     * <p>
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   807
     * The calculation will subtract the years, then months, then days.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   808
     * Only non-zero amounts will be subtracted.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   809
     * If the date-time has a calendar system with a fixed number of months in a
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   810
     * year, then the years and months will be combined before being subtracted.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   811
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   812
     * This instance is immutable and unaffected by this method call.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   813
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   814
     * @param temporal  the temporal object to adjust, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   815
     * @return an object of the same type with the adjustment made, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   816
     * @throws DateTimeException if unable to subtract
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   817
     * @throws ArithmeticException if numeric overflow occurs
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   818
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   819
    @Override
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   820
    public Temporal subtractFrom(Temporal temporal) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   821
        Objects.requireNonNull(temporal, "temporal");
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   822
        if ((years | months) != 0) {
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   823
            long monthRange = monthRange(temporal);
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   824
            if (monthRange >= 0) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   825
                temporal = temporal.minus(years * monthRange + months, MONTHS);
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   826
            } else {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   827
                if (years != 0) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   828
                    temporal = temporal.minus(years, YEARS);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   829
                }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   830
                if (months != 0) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   831
                    temporal = temporal.minus(months, MONTHS);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   832
                }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   833
            }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   834
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   835
        if (days != 0) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   836
            temporal = temporal.minus(days, DAYS);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   837
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   838
        return temporal;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   839
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   840
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
     * Calculates the range of months based on the temporal.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   843
     *
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   844
     * @param temporal  the temporal, not null
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   845
     * @return the month range, negative if not fixed range
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   846
     */
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   847
    private long monthRange(Temporal temporal) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   848
        ValueRange startRange = Chronology.from(temporal).range(MONTH_OF_YEAR);
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   849
        if (startRange.isFixed() && startRange.isIntValue()) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   850
            return startRange.getMaximum() - startRange.getMinimum() + 1;
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   851
        }
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   852
        return -1;
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   853
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   854
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   855
    //-----------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   856
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   857
     * Checks if this period is equal to another period.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   858
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   859
     * The comparison is based on the amounts held in the period.
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   860
     * 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
   861
     * 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
   862
     * of "1 Year and 3 Months".
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   863
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   864
     * @param obj  the object to check, null returns false
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   865
     * @return true if this is equal to the other period
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   866
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   867
    @Override
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   868
    public boolean equals(Object obj) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   869
        if (this == obj) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   870
            return true;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   871
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   872
        if (obj instanceof Period) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   873
            Period other = (Period) obj;
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   874
            return years == other.years &&
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   875
                    months == other.months &&
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   876
                    days == other.days;
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   877
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   878
        return false;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   879
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   880
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   881
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   882
     * A hash code for this period.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   883
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   884
     * @return a suitable hash code
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   885
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   886
    @Override
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   887
    public int hashCode() {
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   888
        return years + Integer.rotateLeft(months, 8) + Integer.rotateLeft(days, 16);
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   889
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   890
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   891
    //-----------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   892
    /**
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   893
     * Outputs this period as a {@code String}, such as {@code P6Y3M1D}.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   894
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   895
     * The output will be in the ISO-8601 period format.
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   896
     * A zero period will be represented as zero days, 'P0D'.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   897
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   898
     * @return a string representation of this period, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   899
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   900
    @Override
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   901
    public String toString() {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   902
        if (this == ZERO) {
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   903
            return "P0D";
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   904
        } else {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   905
            StringBuilder buf = new StringBuilder();
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   906
            buf.append('P');
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   907
            if (years != 0) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   908
                buf.append(years).append('Y');
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   909
            }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   910
            if (months != 0) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   911
                buf.append(months).append('M');
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   912
            }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   913
            if (days != 0) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   914
                buf.append(days).append('D');
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   915
            }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   916
            return buf.toString();
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   917
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   918
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   919
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   920
    //-----------------------------------------------------------------------
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   921
    /**
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   922
     * Writes the object using a
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   923
     * <a href="../../serialized-form.html#java.time.Ser">dedicated serialized form</a>.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   924
     * <pre>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   925
     *  out.writeByte(14);  // identifies this as a Period
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   926
     *  out.writeInt(years);
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   927
     *  out.writeInt(months);
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   928
     *  out.writeInt(seconds);
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   929
     * </pre>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   930
     *
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   931
     * @return the instance of {@code Ser}, not null
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   932
     */
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   933
    private Object writeReplace() {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   934
        return new Ser(Ser.PERIOD_TYPE, this);
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   935
    }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   936
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   937
    /**
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   938
     * Defend against malicious streams.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   939
     * @return never
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   940
     * @throws java.io.InvalidObjectException always
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   941
     */
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   942
    private Object readResolve() throws ObjectStreamException {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   943
        throw new InvalidObjectException("Deserialization via serialization delegate");
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   944
    }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   945
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   946
    void writeExternal(DataOutput out) throws IOException {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   947
        out.writeInt(years);
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   948
        out.writeInt(months);
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   949
        out.writeInt(days);
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   950
    }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   951
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   952
    static Period readExternal(DataInput in) throws IOException {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   953
        int years = in.readInt();
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   954
        int months = in.readInt();
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   955
        int days = in.readInt();
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   956
        return Period.of(years, months, days);
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   957
    }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   958
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   959
}