author | darcy |
Wed, 09 Oct 2019 10:17:50 -0700 | |
changeset 58520 | e036ee8bae56 |
parent 57956 | e0b8b019d2f5 |
child 58679 | 9c3209ff7550 |
permissions | -rw-r--r-- |
20519
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
1 |
/* |
57956
e0b8b019d2f5
8229997: Apply java.io.Serial annotations in java.base
darcy
parents:
52078
diff
changeset
|
2 |
* Copyright (c) 2013, 2019, Oracle and/or its affiliates. All rights reserved. |
20519
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
4 |
* |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
5 |
* This code is free software; you can redistribute it and/or modify it |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
7 |
* published by the Free Software Foundation. Oracle designates this |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
8 |
* particular file as subject to the "Classpath" exception as provided |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
9 |
* by Oracle in the LICENSE file that accompanied this code. |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
10 |
* |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
15 |
* accompanied this code). |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
16 |
* |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
17 |
* You should have received a copy of the GNU General Public License version |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
20 |
* |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
22 |
* or visit www.oracle.com if you need additional information or have any |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
23 |
* questions. |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
24 |
*/ |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
25 |
|
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
26 |
/* |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
27 |
* Copyright (c) 2013, Stephen Colebourne & Michael Nascimento Santos |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
28 |
* |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
29 |
* All rights reserved. |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
30 |
* |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
31 |
* Redistribution and use in source and binary forms, with or without |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
32 |
* modification, are permitted provided that the following conditions are met: |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
33 |
* |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
34 |
* * Redistributions of source code must retain the above copyright notice, |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
35 |
* this list of conditions and the following disclaimer. |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
36 |
* |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
37 |
* * Redistributions in binary form must reproduce the above copyright notice, |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
38 |
* this list of conditions and the following disclaimer in the documentation |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
39 |
* and/or other materials provided with the distribution. |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
40 |
* |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
41 |
* * Neither the name of JSR-310 nor the names of its contributors |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
42 |
* may be used to endorse or promote products derived from this software |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
43 |
* without specific prior written permission. |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
44 |
* |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
45 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
46 |
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
47 |
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
48 |
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
49 |
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
50 |
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
51 |
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
52 |
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
53 |
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
54 |
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
55 |
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
56 |
*/ |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
57 |
package java.time.chrono; |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
58 |
|
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
59 |
import static java.time.temporal.ChronoField.MONTH_OF_YEAR; |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
60 |
import static java.time.temporal.ChronoUnit.DAYS; |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
61 |
import static java.time.temporal.ChronoUnit.MONTHS; |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
62 |
import static java.time.temporal.ChronoUnit.YEARS; |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
63 |
|
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
64 |
import java.io.DataInput; |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
65 |
import java.io.DataOutput; |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
66 |
import java.io.IOException; |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
67 |
import java.io.InvalidObjectException; |
22081 | 68 |
import java.io.ObjectInputStream; |
20519
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
69 |
import java.io.ObjectStreamException; |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
70 |
import java.io.Serializable; |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
71 |
import java.time.DateTimeException; |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
72 |
import java.time.temporal.ChronoUnit; |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
73 |
import java.time.temporal.Temporal; |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
74 |
import java.time.temporal.TemporalAccessor; |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
75 |
import java.time.temporal.TemporalAmount; |
20795 | 76 |
import java.time.temporal.TemporalQueries; |
20519
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
77 |
import java.time.temporal.TemporalUnit; |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
78 |
import java.time.temporal.UnsupportedTemporalTypeException; |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
79 |
import java.time.temporal.ValueRange; |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
80 |
import java.util.List; |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
81 |
import java.util.Objects; |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
82 |
|
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
83 |
/** |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
84 |
* A period expressed in terms of a standard year-month-day calendar system. |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
85 |
* <p> |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
86 |
* This class is used by applications seeking to handle dates in non-ISO calendar systems. |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
87 |
* For example, the Japanese, Minguo, Thai Buddhist and others. |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
88 |
* |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
89 |
* @implSpec |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
90 |
* This class is immutable nad thread-safe. |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
91 |
* |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
92 |
* @since 1.8 |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
93 |
*/ |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
94 |
final class ChronoPeriodImpl |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
95 |
implements ChronoPeriod, Serializable { |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
96 |
// this class is only used by JDK chronology implementations and makes assumptions based on that fact |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
97 |
|
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
98 |
/** |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
99 |
* Serialization version. |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
100 |
*/ |
57956
e0b8b019d2f5
8229997: Apply java.io.Serial annotations in java.base
darcy
parents:
52078
diff
changeset
|
101 |
@java.io.Serial |
20519
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
102 |
private static final long serialVersionUID = 57387258289L; |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
103 |
|
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
104 |
/** |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
105 |
* The set of supported units. |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
106 |
*/ |
41482
05e75e5f3afb
8134373: use collections convenience factories in the JDK
smarks
parents:
25859
diff
changeset
|
107 |
private static final List<TemporalUnit> SUPPORTED_UNITS = List.of(YEARS, MONTHS, DAYS); |
20519
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
108 |
|
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
109 |
/** |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
110 |
* The chronology. |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
111 |
*/ |
58520
e036ee8bae56
8231202: Suppress warnings on non-serializable non-transient instance fields in serializable classes
darcy
parents:
57956
diff
changeset
|
112 |
@SuppressWarnings("serial") // Not statically typed as Serializable |
20519
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
113 |
private final Chronology chrono; |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
114 |
/** |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
115 |
* The number of years. |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
116 |
*/ |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
117 |
final int years; |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
118 |
/** |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
119 |
* The number of months. |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
120 |
*/ |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
121 |
final int months; |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
122 |
/** |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
123 |
* The number of days. |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
124 |
*/ |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
125 |
final int days; |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
126 |
|
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
127 |
/** |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
128 |
* Creates an instance. |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
129 |
*/ |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
130 |
ChronoPeriodImpl(Chronology chrono, int years, int months, int days) { |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
131 |
Objects.requireNonNull(chrono, "chrono"); |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
132 |
this.chrono = chrono; |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
133 |
this.years = years; |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
134 |
this.months = months; |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
135 |
this.days = days; |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
136 |
} |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
137 |
|
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
138 |
//----------------------------------------------------------------------- |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
139 |
@Override |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
140 |
public long get(TemporalUnit unit) { |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
141 |
if (unit == ChronoUnit.YEARS) { |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
142 |
return years; |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
143 |
} else if (unit == ChronoUnit.MONTHS) { |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
144 |
return months; |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
145 |
} else if (unit == ChronoUnit.DAYS) { |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
146 |
return days; |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
147 |
} else { |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
148 |
throw new UnsupportedTemporalTypeException("Unsupported unit: " + unit); |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
149 |
} |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
150 |
} |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
151 |
|
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
152 |
@Override |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
153 |
public List<TemporalUnit> getUnits() { |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
154 |
return ChronoPeriodImpl.SUPPORTED_UNITS; |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
155 |
} |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
156 |
|
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
157 |
@Override |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
158 |
public Chronology getChronology() { |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
159 |
return chrono; |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
160 |
} |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
161 |
|
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
162 |
//----------------------------------------------------------------------- |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
163 |
@Override |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
164 |
public boolean isZero() { |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
165 |
return years == 0 && months == 0 && days == 0; |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
166 |
} |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
167 |
|
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
168 |
@Override |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
169 |
public boolean isNegative() { |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
170 |
return years < 0 || months < 0 || days < 0; |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
171 |
} |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
172 |
|
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
173 |
//----------------------------------------------------------------------- |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
174 |
@Override |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
175 |
public ChronoPeriod plus(TemporalAmount amountToAdd) { |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
176 |
ChronoPeriodImpl amount = validateAmount(amountToAdd); |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
177 |
return new ChronoPeriodImpl( |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
178 |
chrono, |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
179 |
Math.addExact(years, amount.years), |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
180 |
Math.addExact(months, amount.months), |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
181 |
Math.addExact(days, amount.days)); |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
182 |
} |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
183 |
|
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
184 |
@Override |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
185 |
public ChronoPeriod minus(TemporalAmount amountToSubtract) { |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
186 |
ChronoPeriodImpl amount = validateAmount(amountToSubtract); |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
187 |
return new ChronoPeriodImpl( |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
188 |
chrono, |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
189 |
Math.subtractExact(years, amount.years), |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
190 |
Math.subtractExact(months, amount.months), |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
191 |
Math.subtractExact(days, amount.days)); |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
192 |
} |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
193 |
|
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
194 |
/** |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
195 |
* Obtains an instance of {@code ChronoPeriodImpl} from a temporal amount. |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
196 |
* |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
197 |
* @param amount the temporal amount to convert, not null |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
198 |
* @return the period, not null |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
199 |
*/ |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
200 |
private ChronoPeriodImpl validateAmount(TemporalAmount amount) { |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
201 |
Objects.requireNonNull(amount, "amount"); |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
202 |
if (amount instanceof ChronoPeriodImpl == false) { |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
203 |
throw new DateTimeException("Unable to obtain ChronoPeriod from TemporalAmount: " + amount.getClass()); |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
204 |
} |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
205 |
ChronoPeriodImpl period = (ChronoPeriodImpl) amount; |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
206 |
if (chrono.equals(period.getChronology()) == false) { |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
207 |
throw new ClassCastException("Chronology mismatch, expected: " + chrono.getId() + ", actual: " + period.getChronology().getId()); |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
208 |
} |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
209 |
return period; |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
210 |
} |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
211 |
|
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
212 |
//----------------------------------------------------------------------- |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
213 |
@Override |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
214 |
public ChronoPeriod multipliedBy(int scalar) { |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
215 |
if (this.isZero() || scalar == 1) { |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
216 |
return this; |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
217 |
} |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
218 |
return new ChronoPeriodImpl( |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
219 |
chrono, |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
220 |
Math.multiplyExact(years, scalar), |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
221 |
Math.multiplyExact(months, scalar), |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
222 |
Math.multiplyExact(days, scalar)); |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
223 |
} |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
224 |
|
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
225 |
//----------------------------------------------------------------------- |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
226 |
@Override |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
227 |
public ChronoPeriod normalized() { |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
228 |
long monthRange = monthRange(); |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
229 |
if (monthRange > 0) { |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
230 |
long totalMonths = years * monthRange + months; |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
231 |
long splitYears = totalMonths / monthRange; |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
232 |
int splitMonths = (int) (totalMonths % monthRange); // no overflow |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
233 |
if (splitYears == years && splitMonths == months) { |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
234 |
return this; |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
235 |
} |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
236 |
return new ChronoPeriodImpl(chrono, Math.toIntExact(splitYears), splitMonths, days); |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
237 |
|
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
238 |
} |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
239 |
return this; |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
240 |
} |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
241 |
|
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
242 |
/** |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
243 |
* Calculates the range of months. |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
244 |
* |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
245 |
* @return the month range, -1 if not fixed range |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
246 |
*/ |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
247 |
private long monthRange() { |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
248 |
ValueRange startRange = chrono.range(MONTH_OF_YEAR); |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
249 |
if (startRange.isFixed() && startRange.isIntValue()) { |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
250 |
return startRange.getMaximum() - startRange.getMinimum() + 1; |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
251 |
} |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
252 |
return -1; |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
253 |
} |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
254 |
|
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
255 |
//------------------------------------------------------------------------- |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
256 |
@Override |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
257 |
public Temporal addTo(Temporal temporal) { |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
258 |
validateChrono(temporal); |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
259 |
if (months == 0) { |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
260 |
if (years != 0) { |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
261 |
temporal = temporal.plus(years, YEARS); |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
262 |
} |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
263 |
} else { |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
264 |
long monthRange = monthRange(); |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
265 |
if (monthRange > 0) { |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
266 |
temporal = temporal.plus(years * monthRange + months, MONTHS); |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
267 |
} else { |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
268 |
if (years != 0) { |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
269 |
temporal = temporal.plus(years, YEARS); |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
270 |
} |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
271 |
temporal = temporal.plus(months, MONTHS); |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
272 |
} |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
273 |
} |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
274 |
if (days != 0) { |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
275 |
temporal = temporal.plus(days, DAYS); |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
276 |
} |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
277 |
return temporal; |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
278 |
} |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
279 |
|
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
280 |
|
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
281 |
|
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
282 |
@Override |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
283 |
public Temporal subtractFrom(Temporal temporal) { |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
284 |
validateChrono(temporal); |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
285 |
if (months == 0) { |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
286 |
if (years != 0) { |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
287 |
temporal = temporal.minus(years, YEARS); |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
288 |
} |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
289 |
} else { |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
290 |
long monthRange = monthRange(); |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
291 |
if (monthRange > 0) { |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
292 |
temporal = temporal.minus(years * monthRange + months, MONTHS); |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
293 |
} else { |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
294 |
if (years != 0) { |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
295 |
temporal = temporal.minus(years, YEARS); |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
296 |
} |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
297 |
temporal = temporal.minus(months, MONTHS); |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
298 |
} |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
299 |
} |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
300 |
if (days != 0) { |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
301 |
temporal = temporal.minus(days, DAYS); |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
302 |
} |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
303 |
return temporal; |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
304 |
} |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
305 |
|
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
306 |
/** |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
307 |
* Validates that the temporal has the correct chronology. |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
308 |
*/ |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
309 |
private void validateChrono(TemporalAccessor temporal) { |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
310 |
Objects.requireNonNull(temporal, "temporal"); |
20795 | 311 |
Chronology temporalChrono = temporal.query(TemporalQueries.chronology()); |
20519
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
312 |
if (temporalChrono != null && chrono.equals(temporalChrono) == false) { |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
313 |
throw new DateTimeException("Chronology mismatch, expected: " + chrono.getId() + ", actual: " + temporalChrono.getId()); |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
314 |
} |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
315 |
} |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
316 |
|
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
317 |
//----------------------------------------------------------------------- |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
318 |
@Override |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
319 |
public boolean equals(Object obj) { |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
320 |
if (this == obj) { |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
321 |
return true; |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
322 |
} |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
323 |
if (obj instanceof ChronoPeriodImpl) { |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
324 |
ChronoPeriodImpl other = (ChronoPeriodImpl) obj; |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
325 |
return years == other.years && months == other.months && |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
326 |
days == other.days && chrono.equals(other.chrono); |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
327 |
} |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
328 |
return false; |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
329 |
} |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
330 |
|
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
331 |
@Override |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
332 |
public int hashCode() { |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
333 |
return (years + Integer.rotateLeft(months, 8) + Integer.rotateLeft(days, 16)) ^ chrono.hashCode(); |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
334 |
} |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
335 |
|
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
336 |
//----------------------------------------------------------------------- |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
337 |
@Override |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
338 |
public String toString() { |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
339 |
if (isZero()) { |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
340 |
return getChronology().toString() + " P0D"; |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
341 |
} else { |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
342 |
StringBuilder buf = new StringBuilder(); |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
343 |
buf.append(getChronology().toString()).append(' ').append('P'); |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
344 |
if (years != 0) { |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
345 |
buf.append(years).append('Y'); |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
346 |
} |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
347 |
if (months != 0) { |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
348 |
buf.append(months).append('M'); |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
349 |
} |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
350 |
if (days != 0) { |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
351 |
buf.append(days).append('D'); |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
352 |
} |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
353 |
return buf.toString(); |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
354 |
} |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
355 |
} |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
356 |
|
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
357 |
//----------------------------------------------------------------------- |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
358 |
/** |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
359 |
* Writes the Chronology using a |
52078 | 360 |
* <a href="{@docRoot}/serialized-form.html#java.time.chrono.Ser">dedicated serialized form</a>. |
20519
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
361 |
* <pre> |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
362 |
* out.writeByte(12); // identifies this as a ChronoPeriodImpl |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
363 |
* out.writeUTF(getId()); // the chronology |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
364 |
* out.writeInt(years); |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
365 |
* out.writeInt(months); |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
366 |
* out.writeInt(days); |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
367 |
* </pre> |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
368 |
* |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
369 |
* @return the instance of {@code Ser}, not null |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
370 |
*/ |
57956
e0b8b019d2f5
8229997: Apply java.io.Serial annotations in java.base
darcy
parents:
52078
diff
changeset
|
371 |
@java.io.Serial |
20519
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
372 |
protected Object writeReplace() { |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
373 |
return new Ser(Ser.CHRONO_PERIOD_TYPE, this); |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
374 |
} |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
375 |
|
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
376 |
/** |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
377 |
* Defend against malicious streams. |
22081 | 378 |
* |
22566 | 379 |
* @param s the stream to read |
20519
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
380 |
* @throws InvalidObjectException always |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
381 |
*/ |
57956
e0b8b019d2f5
8229997: Apply java.io.Serial annotations in java.base
darcy
parents:
52078
diff
changeset
|
382 |
@java.io.Serial |
22081 | 383 |
private void readObject(ObjectInputStream s) throws ObjectStreamException { |
20519
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
384 |
throw new InvalidObjectException("Deserialization via serialization delegate"); |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
385 |
} |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
386 |
|
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
387 |
void writeExternal(DataOutput out) throws IOException { |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
388 |
out.writeUTF(chrono.getId()); |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
389 |
out.writeInt(years); |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
390 |
out.writeInt(months); |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
391 |
out.writeInt(days); |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
392 |
} |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
393 |
|
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
394 |
static ChronoPeriodImpl readExternal(DataInput in) throws IOException { |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
395 |
Chronology chrono = Chronology.of(in.readUTF()); |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
396 |
int years = in.readInt(); |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
397 |
int months = in.readInt(); |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
398 |
int days = in.readInt(); |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
399 |
return new ChronoPeriodImpl(chrono, years, months, days); |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
400 |
} |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
401 |
|
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
diff
changeset
|
402 |
} |