forked from kangjianwei/LearningJDK
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Era.java
378 lines (359 loc) · 16.1 KB
/
Era.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
/*
* Copyright (c) 2012, 2017, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/*
* This file is available under and governed by the GNU General Public
* License version 2 only, as published by the Free Software Foundation.
* However, the following notice accompanied the original version of this
* file:
*
* Copyright (c) 2012, Stephen Colebourne & Michael Nascimento Santos
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* * Neither the name of JSR-310 nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package java.time.chrono;
import java.time.DateTimeException;
import java.time.format.DateTimeFormatterBuilder;
import java.time.format.TextStyle;
import java.time.temporal.ChronoField;
import java.time.temporal.Temporal;
import java.time.temporal.TemporalAccessor;
import java.time.temporal.TemporalAdjuster;
import java.time.temporal.TemporalField;
import java.time.temporal.TemporalQueries;
import java.time.temporal.TemporalQuery;
import java.time.temporal.UnsupportedTemporalTypeException;
import java.time.temporal.ValueRange;
import java.util.Locale;
import static java.time.temporal.ChronoUnit.ERAS;
/**
* An era of the time-line.
* <p>
* Most calendar systems have a single epoch dividing the time-line into two eras.
* However, some calendar systems, have multiple eras, such as one for the reign
* of each leader.
* In all cases, the era is conceptually the largest division of the time-line.
* Each chronology defines the Era's that are known Eras and a
* {@link Chronology#eras Chronology.eras} to get the valid eras.
* <p>
* For example, the Thai Buddhist calendar system divides time into two eras,
* before and after a single date. By contrast, the Japanese calendar system
* has one era for the reign of each Emperor.
* <p>
* Instances of {@code Era} may be compared using the {@code ==} operator.
*
* @implSpec
* This interface must be implemented with care to ensure other classes operate correctly.
* All implementations must be singletons - final, immutable and thread-safe.
* It is recommended to use an enum whenever possible.
*
* @since 1.8
*/
/*
* 纪元
*
* Era支持的纪元包括:
* IsoEra - ISO纪元,基于ISO-8601历法系统
* HijrahEra - 伊斯兰历纪元
* ThaiBuddhistEra - 泰国佛教历纪元
* JapaneseEra - 日本历纪元
* MinguoEra - 中华民国历纪元
*/
public interface Era extends TemporalAccessor, TemporalAdjuster {
/**
* Gets the numeric value associated with the era as defined by the chronology.
* Each chronology defines the predefined Eras and methods to list the Eras
* of the chronology.
* <p>
* All fields, including eras, have an associated numeric value.
* The meaning of the numeric value for era is determined by the chronology
* according to these principles:
* <ul>
* <li>The era in use at the epoch 1970-01-01 (ISO) has the value 1.
* <li>Later eras have sequentially higher values.
* <li>Earlier eras have sequentially lower values, which may be negative.
* </ul>
*
* @return the numeric era value
*/
// 返回纪元标识;在ISO历法系统中,0是公元前,1是公元(后)
int getValue();
/**
* Checks if the specified field is supported.
* <p>
* This checks if this era can be queried for the specified field.
* If false, then calling the {@link #range(TemporalField) range} and
* {@link #get(TemporalField) get} methods will throw an exception.
* <p>
* If the field is a {@link ChronoField} then the query is implemented here.
* The {@code ERA} field returns true.
* All other {@code ChronoField} instances will return false.
* <p>
* If the field is not a {@code ChronoField}, then the result of this method
* is obtained by invoking {@code TemporalField.isSupportedBy(TemporalAccessor)}
* passing {@code this} as the argument.
* Whether the field is supported is determined by the field.
*
* @param field the field to check, null returns false
* @return true if the field is supported on this era, false if not
*/
// 判断当前时间量是否支持指定的时间量字段
@Override
default boolean isSupported(TemporalField field) {
if (field instanceof ChronoField) {
return field == ChronoField.ERA;
}
return field != null && field.isSupportedBy(this);
}
/**
* Gets the range of valid values for the specified field.
* <p>
* The range object expresses the minimum and maximum valid values for a field.
* This era is used to enhance the accuracy of the returned range.
* If it is not possible to return the range, because the field is not supported
* or for some other reason, an exception is thrown.
* <p>
* If the field is a {@link ChronoField} then the query is implemented here.
* The {@code ERA} field returns the range.
* All other {@code ChronoField} instances will throw an {@code UnsupportedTemporalTypeException}.
* <p>
* If the field is not a {@code ChronoField}, then the result of this method
* is obtained by invoking {@code TemporalField.rangeRefinedBy(TemporalAccessor)}
* passing {@code this} as the argument.
* Whether the range can be obtained is determined by the field.
* <p>
* The default implementation must return a range for {@code ERA} from
* zero to one, suitable for two era calendar systems such as ISO.
*
* @param field the field to query the range for, not null
* @return the range of valid values for the field, not null
* @throws DateTimeException if the range for the field cannot be obtained
* @throws UnsupportedTemporalTypeException if the unit is not supported
*/
// 返回时间量字段field的取值区间,通常要求当前时间量支持该时间量字段
@Override
default ValueRange range(TemporalField field) {
return TemporalAccessor.super.range(field);
}
/**
* Gets the value of the specified field from this era as an {@code int}.
* <p>
* This queries this era for the value of the specified field.
* The returned value will always be within the valid range of values for the field.
* If it is not possible to return the value, because the field is not supported
* or for some other reason, an exception is thrown.
* <p>
* If the field is a {@link ChronoField} then the query is implemented here.
* The {@code ERA} field returns the value of the era.
* All other {@code ChronoField} instances will throw an {@code UnsupportedTemporalTypeException}.
* <p>
* If the field is not a {@code ChronoField}, then the result of this method
* is obtained by invoking {@code TemporalField.getFrom(TemporalAccessor)}
* passing {@code this} as the argument. Whether the value can be obtained,
* and what the value represents, is determined by the field.
*
* @param field the field to get, not null
* @return the value for the field
* @throws DateTimeException if a value for the field cannot be obtained or
* the value is outside the range of valid values for the field
* @throws UnsupportedTemporalTypeException if the field is not supported or
* the range of values exceeds an {@code int}
* @throws ArithmeticException if numeric overflow occurs
*/
/*
* 以int形式返回时间量字段field的值
*
* 目前支持的字段包括:
* ChronoField.ERA - [纪元];在ISO历法系统中,0是公元前,1是公元(后)
*/
@Override
default int get(TemporalField field) {
if(field == ChronoField.ERA) {
return getValue();
}
return TemporalAccessor.super.get(field);
}
/**
* Gets the value of the specified field from this era as a {@code long}.
* <p>
* This queries this era for the value of the specified field.
* If it is not possible to return the value, because the field is not supported
* or for some other reason, an exception is thrown.
* <p>
* If the field is a {@link ChronoField} then the query is implemented here.
* The {@code ERA} field returns the value of the era.
* All other {@code ChronoField} instances will throw an {@code UnsupportedTemporalTypeException}.
* <p>
* If the field is not a {@code ChronoField}, then the result of this method
* is obtained by invoking {@code TemporalField.getFrom(TemporalAccessor)}
* passing {@code this} as the argument. Whether the value can be obtained,
* and what the value represents, is determined by the field.
*
* @param field the field to get, not null
* @return the value for the field
* @throws DateTimeException if a value for the field cannot be obtained
* @throws UnsupportedTemporalTypeException if the field is not supported
* @throws ArithmeticException if numeric overflow occurs
*/
/*
* 以long形式返回时间量字段field的值
*
* 目前支持的字段包括:
* ChronoField.ERA - [纪元];在ISO历法系统中,0是公元前,1是公元(后)
*/
@Override
default long getLong(TemporalField field) {
if(field instanceof ChronoField) {
if(field == ChronoField.ERA) {
return getValue();
}
throw new UnsupportedTemporalTypeException("Unsupported field: " + field);
}
return field.getFrom(this);
}
/**
* Queries this era using the specified query.
* <p>
* This queries this era using the specified query strategy object.
* The {@code TemporalQuery} object defines the logic to be used to
* obtain the result. Read the documentation of the query to understand
* what the result of this method will be.
* <p>
* The result of this method is obtained by invoking the
* {@link TemporalQuery#queryFrom(TemporalAccessor)} method on the
* specified query passing {@code this} as the argument.
*
* @param <R> the type of the result
* @param query the query to invoke, not null
* @return the query result, null may be returned (defined by the query)
* @throws DateTimeException if unable to query (defined by the query)
* @throws ArithmeticException if numeric overflow occurs (defined by the query)
*/
// 使用指定的时间量查询器,从当前时间量中查询目标信息
@SuppressWarnings("unchecked")
@Override
default <R> R query(TemporalQuery<R> query) {
if (query == TemporalQueries.precision()) {
return (R) ERAS;
}
return TemporalAccessor.super.query(query);
}
/**
* Adjusts the specified temporal object to have the same era as this object.
* <p>
* This returns a temporal object of the same observable type as the input
* with the era changed to be the same as this.
* <p>
* The adjustment is equivalent to using {@link Temporal#with(TemporalField, long)}
* passing {@link ChronoField#ERA} as the field.
* <p>
* In most cases, it is clearer to reverse the calling pattern by using
* {@link Temporal#with(TemporalAdjuster)}:
* <pre>
* // these two lines are equivalent, but the second approach is recommended
* temporal = thisEra.adjustInto(temporal);
* temporal = temporal.with(thisEra);
* </pre>
* <p>
* This instance is immutable and unaffected by this method call.
*
* @param temporal the target object to be adjusted, not null
* @return the adjusted object, not null
* @throws DateTimeException if unable to make the adjustment
* @throws ArithmeticException if numeric overflow occurs
*/
/*
* 拿当前时间量中的特定字段与时间量temporal中的其他字段进行整合。
*
* 如果整合后的值与temporal中原有的值相等,则可以直接使用temporal本身;否则,会返回新构造的时间量对象。
*
* 注:通常,这会用到当前时间量的所有部件信息
*
*
* 当前时间量参与整合字段包括:
* ChronoField.ERA - 当前时间量的纪元标识;在ISO历法系统中,0是公元前,1是公元(后)
*
* 目标时间量temporal的取值可以是:
* Year
* YearMonth
* LocalDate
* LocalDateTime
* OffsetDateTime
* ZonedDateTime
* ChronoLocalDateTimeImpl的子类
* ChronoZonedDateTimeImpl的子类
*/
@Override
default Temporal adjustInto(Temporal temporal) {
// 获取当前时间量的纪元标识;在ISO历法系统中,0是公元前,1是公元(后)
int value = getValue();
return temporal.with(ChronoField.ERA, value);
}
/**
* Gets the textual representation of this era.
* <p>
* This returns the textual name used to identify the era,
* suitable for presentation to the user.
* The parameters control the style of the returned text and the locale.
* <p>
* If no textual mapping is found then the {@link #getValue() numeric value} is returned.
*
* @apiNote This default implementation is suitable for most implementations.
*
* @param style the style of the text required, not null
* @param locale the locale to use, not null
* @return the text value of the era, not null
*/
// 返回当前纪元的文本显示
default String getDisplayName(TextStyle style, Locale locale) {
return new DateTimeFormatterBuilder().appendText(ChronoField.ERA, style).toFormatter(locale).format(this);
}
/* NOTE: methods to convert year-of-era/proleptic-year cannot be here as they may depend on month/day (Japanese) */
}