Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/java.base/share/classes/java/time/format/Parsed.java
41159 views
1
/*
2
* Copyright (c) 2012, 2020, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
*
5
* This code is free software; you can redistribute it and/or modify it
6
* under the terms of the GNU General Public License version 2 only, as
7
* published by the Free Software Foundation. Oracle designates this
8
* particular file as subject to the "Classpath" exception as provided
9
* by Oracle in the LICENSE file that accompanied this code.
10
*
11
* This code is distributed in the hope that it will be useful, but WITHOUT
12
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14
* version 2 for more details (a copy is included in the LICENSE file that
15
* accompanied this code).
16
*
17
* You should have received a copy of the GNU General Public License version
18
* 2 along with this work; if not, write to the Free Software Foundation,
19
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20
*
21
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22
* or visit www.oracle.com if you need additional information or have any
23
* questions.
24
*/
25
26
/*
27
* This file is available under and governed by the GNU General Public
28
* License version 2 only, as published by the Free Software Foundation.
29
* However, the following notice accompanied the original version of this
30
* file:
31
*
32
* Copyright (c) 2008-2013, Stephen Colebourne & Michael Nascimento Santos
33
*
34
* All rights reserved.
35
*
36
* Redistribution and use in source and binary forms, with or without
37
* modification, are permitted provided that the following conditions are met:
38
*
39
* * Redistributions of source code must retain the above copyright notice,
40
* this list of conditions and the following disclaimer.
41
*
42
* * Redistributions in binary form must reproduce the above copyright notice,
43
* this list of conditions and the following disclaimer in the documentation
44
* and/or other materials provided with the distribution.
45
*
46
* * Neither the name of JSR-310 nor the names of its contributors
47
* may be used to endorse or promote products derived from this software
48
* without specific prior written permission.
49
*
50
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
51
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
52
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
53
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
54
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
55
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
56
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
57
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
58
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
59
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
60
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
61
*/
62
package java.time.format;
63
64
import static java.time.format.DateTimeFormatterBuilder.DayPeriod;
65
import static java.time.temporal.ChronoField.AMPM_OF_DAY;
66
import static java.time.temporal.ChronoField.CLOCK_HOUR_OF_AMPM;
67
import static java.time.temporal.ChronoField.CLOCK_HOUR_OF_DAY;
68
import static java.time.temporal.ChronoField.HOUR_OF_AMPM;
69
import static java.time.temporal.ChronoField.HOUR_OF_DAY;
70
import static java.time.temporal.ChronoField.INSTANT_SECONDS;
71
import static java.time.temporal.ChronoField.MICRO_OF_DAY;
72
import static java.time.temporal.ChronoField.MICRO_OF_SECOND;
73
import static java.time.temporal.ChronoField.MILLI_OF_DAY;
74
import static java.time.temporal.ChronoField.MILLI_OF_SECOND;
75
import static java.time.temporal.ChronoField.MINUTE_OF_DAY;
76
import static java.time.temporal.ChronoField.MINUTE_OF_HOUR;
77
import static java.time.temporal.ChronoField.NANO_OF_DAY;
78
import static java.time.temporal.ChronoField.NANO_OF_SECOND;
79
import static java.time.temporal.ChronoField.OFFSET_SECONDS;
80
import static java.time.temporal.ChronoField.SECOND_OF_DAY;
81
import static java.time.temporal.ChronoField.SECOND_OF_MINUTE;
82
83
import java.time.DateTimeException;
84
import java.time.Instant;
85
import java.time.LocalDate;
86
import java.time.LocalTime;
87
import java.time.Period;
88
import java.time.ZoneId;
89
import java.time.ZoneOffset;
90
import java.time.chrono.ChronoLocalDate;
91
import java.time.chrono.ChronoLocalDateTime;
92
import java.time.chrono.ChronoZonedDateTime;
93
import java.time.chrono.Chronology;
94
import java.time.temporal.ChronoField;
95
import java.time.temporal.TemporalAccessor;
96
import java.time.temporal.TemporalField;
97
import java.time.temporal.TemporalQueries;
98
import java.time.temporal.TemporalQuery;
99
import java.time.temporal.UnsupportedTemporalTypeException;
100
import java.util.HashMap;
101
import java.util.Iterator;
102
import java.util.Map;
103
import java.util.Map.Entry;
104
import java.util.Objects;
105
import java.util.Set;
106
107
/**
108
* A store of parsed data.
109
* <p>
110
* This class is used during parsing to collect the data. Part of the parsing process
111
* involves handling optional blocks and multiple copies of the data get created to
112
* support the necessary backtracking.
113
* <p>
114
* Once parsing is completed, this class can be used as the resultant {@code TemporalAccessor}.
115
* In most cases, it is only exposed once the fields have been resolved.
116
*
117
* @implSpec
118
* This class is a mutable context intended for use from a single thread.
119
* Usage of the class is thread-safe within standard parsing as a new instance of this class
120
* is automatically created for each parse and parsing is single-threaded
121
*
122
* @since 1.8
123
*/
124
final class Parsed implements TemporalAccessor {
125
// some fields are accessed using package scope from DateTimeParseContext
126
127
/**
128
* The parsed fields.
129
*/
130
final Map<TemporalField, Long> fieldValues = new HashMap<>();
131
/**
132
* The parsed zone.
133
*/
134
ZoneId zone;
135
/**
136
* The parsed chronology.
137
*/
138
Chronology chrono;
139
/**
140
* Whether a leap-second is parsed.
141
*/
142
boolean leapSecond;
143
/**
144
* The resolver style to use.
145
*/
146
private ResolverStyle resolverStyle;
147
/**
148
* The resolved date.
149
*/
150
private ChronoLocalDate date;
151
/**
152
* The resolved time.
153
*/
154
private LocalTime time;
155
/**
156
* The excess period from time-only parsing.
157
*/
158
Period excessDays = Period.ZERO;
159
/**
160
* The parsed day period.
161
*/
162
DayPeriod dayPeriod;
163
164
/**
165
* Creates an instance.
166
*/
167
Parsed() {
168
}
169
170
/**
171
* Creates a copy.
172
*/
173
Parsed copy() {
174
// only copy fields used in parsing stage
175
Parsed cloned = new Parsed();
176
cloned.fieldValues.putAll(this.fieldValues);
177
cloned.zone = this.zone;
178
cloned.chrono = this.chrono;
179
cloned.leapSecond = this.leapSecond;
180
cloned.dayPeriod = this.dayPeriod;
181
return cloned;
182
}
183
184
//-----------------------------------------------------------------------
185
@Override
186
public boolean isSupported(TemporalField field) {
187
if (fieldValues.containsKey(field) ||
188
(date != null && date.isSupported(field)) ||
189
(time != null && time.isSupported(field))) {
190
return true;
191
}
192
return field != null && (!(field instanceof ChronoField)) && field.isSupportedBy(this);
193
}
194
195
@Override
196
public long getLong(TemporalField field) {
197
Objects.requireNonNull(field, "field");
198
Long value = fieldValues.get(field);
199
if (value != null) {
200
return value;
201
}
202
if (date != null && date.isSupported(field)) {
203
return date.getLong(field);
204
}
205
if (time != null && time.isSupported(field)) {
206
return time.getLong(field);
207
}
208
if (field instanceof ChronoField) {
209
throw new UnsupportedTemporalTypeException("Unsupported field: " + field);
210
}
211
return field.getFrom(this);
212
}
213
214
@SuppressWarnings("unchecked")
215
@Override
216
public <R> R query(TemporalQuery<R> query) {
217
if (query == TemporalQueries.zoneId()) {
218
return (R) zone;
219
} else if (query == TemporalQueries.chronology()) {
220
return (R) chrono;
221
} else if (query == TemporalQueries.localDate()) {
222
return (R) (date != null ? LocalDate.from(date) : null);
223
} else if (query == TemporalQueries.localTime()) {
224
return (R) time;
225
} else if (query == TemporalQueries.offset()) {
226
Long offsetSecs = fieldValues.get(OFFSET_SECONDS);
227
if (offsetSecs != null) {
228
return (R) ZoneOffset.ofTotalSeconds(offsetSecs.intValue());
229
}
230
if (zone instanceof ZoneOffset) {
231
return (R)zone;
232
}
233
return query.queryFrom(this);
234
} else if (query == TemporalQueries.zone()) {
235
return query.queryFrom(this);
236
} else if (query == TemporalQueries.precision()) {
237
return null; // not a complete date/time
238
}
239
// inline TemporalAccessor.super.query(query) as an optimization
240
// non-JDK classes are not permitted to make this optimization
241
return query.queryFrom(this);
242
}
243
244
//-----------------------------------------------------------------------
245
/**
246
* Resolves the fields in this context.
247
*
248
* @param resolverStyle the resolver style, not null
249
* @param resolverFields the fields to use for resolving, null for all fields
250
* @return this, for method chaining
251
* @throws DateTimeException if resolving one field results in a value for
252
* another field that is in conflict
253
*/
254
TemporalAccessor resolve(ResolverStyle resolverStyle, Set<TemporalField> resolverFields) {
255
if (resolverFields != null) {
256
fieldValues.keySet().retainAll(resolverFields);
257
}
258
this.resolverStyle = resolverStyle;
259
resolveFields();
260
resolveTimeLenient();
261
crossCheck();
262
resolvePeriod();
263
resolveFractional();
264
resolveInstant();
265
return this;
266
}
267
268
//-----------------------------------------------------------------------
269
private void resolveFields() {
270
// resolve ChronoField
271
resolveInstantFields();
272
resolveDateFields();
273
resolveTimeFields();
274
275
// if any other fields, handle them
276
// any lenient date resolution should return epoch-day
277
if (fieldValues.size() > 0) {
278
int changedCount = 0;
279
outer:
280
while (changedCount < 50) {
281
for (Map.Entry<TemporalField, Long> entry : fieldValues.entrySet()) {
282
TemporalField targetField = entry.getKey();
283
TemporalAccessor resolvedObject = targetField.resolve(fieldValues, this, resolverStyle);
284
if (resolvedObject != null) {
285
if (resolvedObject instanceof ChronoZonedDateTime<?> czdt) {
286
if (zone == null) {
287
zone = czdt.getZone();
288
} else if (zone.equals(czdt.getZone()) == false) {
289
throw new DateTimeException("ChronoZonedDateTime must use the effective parsed zone: " + zone);
290
}
291
resolvedObject = czdt.toLocalDateTime();
292
}
293
if (resolvedObject instanceof ChronoLocalDateTime<?> cldt) {
294
updateCheckConflict(cldt.toLocalTime(), Period.ZERO);
295
updateCheckConflict(cldt.toLocalDate());
296
changedCount++;
297
continue outer; // have to restart to avoid concurrent modification
298
}
299
if (resolvedObject instanceof ChronoLocalDate) {
300
updateCheckConflict((ChronoLocalDate) resolvedObject);
301
changedCount++;
302
continue outer; // have to restart to avoid concurrent modification
303
}
304
if (resolvedObject instanceof LocalTime) {
305
updateCheckConflict((LocalTime) resolvedObject, Period.ZERO);
306
changedCount++;
307
continue outer; // have to restart to avoid concurrent modification
308
}
309
throw new DateTimeException("Method resolve() can only return ChronoZonedDateTime, " +
310
"ChronoLocalDateTime, ChronoLocalDate or LocalTime");
311
} else if (fieldValues.containsKey(targetField) == false) {
312
changedCount++;
313
continue outer; // have to restart to avoid concurrent modification
314
}
315
}
316
break;
317
}
318
if (changedCount == 50) { // catch infinite loops
319
throw new DateTimeException("One of the parsed fields has an incorrectly implemented resolve method");
320
}
321
// if something changed then have to redo ChronoField resolve
322
if (changedCount > 0) {
323
resolveInstantFields();
324
resolveDateFields();
325
resolveTimeFields();
326
}
327
}
328
}
329
330
private void updateCheckConflict(TemporalField targetField, TemporalField changeField, Long changeValue) {
331
Long old = fieldValues.put(changeField, changeValue);
332
if (old != null && old.longValue() != changeValue.longValue()) {
333
throw new DateTimeException("Conflict found: " + changeField + " " + old +
334
" differs from " + changeField + " " + changeValue +
335
" while resolving " + targetField);
336
}
337
}
338
339
340
//-----------------------------------------------------------------------
341
private void resolveInstantFields() {
342
// resolve parsed instant seconds to date and time if zone available
343
if (fieldValues.containsKey(INSTANT_SECONDS)) {
344
if (zone != null) {
345
resolveInstantFields0(zone);
346
} else {
347
Long offsetSecs = fieldValues.get(OFFSET_SECONDS);
348
if (offsetSecs != null) {
349
ZoneOffset offset = ZoneOffset.ofTotalSeconds(offsetSecs.intValue());
350
resolveInstantFields0(offset);
351
}
352
}
353
}
354
}
355
356
private void resolveInstantFields0(ZoneId selectedZone) {
357
Instant instant = Instant.ofEpochSecond(fieldValues.remove(INSTANT_SECONDS));
358
ChronoZonedDateTime<?> zdt = chrono.zonedDateTime(instant, selectedZone);
359
updateCheckConflict(zdt.toLocalDate());
360
updateCheckConflict(INSTANT_SECONDS, SECOND_OF_DAY, (long) zdt.toLocalTime().toSecondOfDay());
361
}
362
363
//-----------------------------------------------------------------------
364
private void resolveDateFields() {
365
updateCheckConflict(chrono.resolveDate(fieldValues, resolverStyle));
366
}
367
368
private void updateCheckConflict(ChronoLocalDate cld) {
369
if (date != null) {
370
if (cld != null && date.equals(cld) == false) {
371
throw new DateTimeException("Conflict found: Fields resolved to two different dates: " + date + " " + cld);
372
}
373
} else if (cld != null) {
374
if (chrono.equals(cld.getChronology()) == false) {
375
throw new DateTimeException("ChronoLocalDate must use the effective parsed chronology: " + chrono);
376
}
377
date = cld;
378
}
379
}
380
381
//-----------------------------------------------------------------------
382
private void resolveTimeFields() {
383
// simplify fields
384
if (fieldValues.containsKey(CLOCK_HOUR_OF_DAY)) {
385
// lenient allows anything, smart allows 0-24, strict allows 1-24
386
long ch = fieldValues.remove(CLOCK_HOUR_OF_DAY);
387
if (resolverStyle == ResolverStyle.STRICT || (resolverStyle == ResolverStyle.SMART && ch != 0)) {
388
CLOCK_HOUR_OF_DAY.checkValidValue(ch);
389
}
390
updateCheckConflict(CLOCK_HOUR_OF_DAY, HOUR_OF_DAY, ch == 24 ? 0 : ch);
391
}
392
if (fieldValues.containsKey(CLOCK_HOUR_OF_AMPM)) {
393
// lenient allows anything, smart allows 0-12, strict allows 1-12
394
long ch = fieldValues.remove(CLOCK_HOUR_OF_AMPM);
395
if (resolverStyle == ResolverStyle.STRICT || (resolverStyle == ResolverStyle.SMART && ch != 0)) {
396
CLOCK_HOUR_OF_AMPM.checkValidValue(ch);
397
}
398
updateCheckConflict(CLOCK_HOUR_OF_AMPM, HOUR_OF_AMPM, ch == 12 ? 0 : ch);
399
}
400
if (fieldValues.containsKey(AMPM_OF_DAY) && fieldValues.containsKey(HOUR_OF_AMPM)) {
401
long ap = fieldValues.remove(AMPM_OF_DAY);
402
long hap = fieldValues.remove(HOUR_OF_AMPM);
403
if (resolverStyle == ResolverStyle.LENIENT) {
404
updateCheckConflict(AMPM_OF_DAY, HOUR_OF_DAY, Math.addExact(Math.multiplyExact(ap, 12), hap));
405
} else { // STRICT or SMART
406
AMPM_OF_DAY.checkValidValue(ap);
407
HOUR_OF_AMPM.checkValidValue(hap);
408
updateCheckConflict(AMPM_OF_DAY, HOUR_OF_DAY, ap * 12 + hap);
409
}
410
}
411
if (fieldValues.containsKey(NANO_OF_DAY)) {
412
long nod = fieldValues.remove(NANO_OF_DAY);
413
if (resolverStyle != ResolverStyle.LENIENT) {
414
NANO_OF_DAY.checkValidValue(nod);
415
}
416
updateCheckConflict(NANO_OF_DAY, HOUR_OF_DAY, nod / 3600_000_000_000L);
417
updateCheckConflict(NANO_OF_DAY, MINUTE_OF_HOUR, (nod / 60_000_000_000L) % 60);
418
updateCheckConflict(NANO_OF_DAY, SECOND_OF_MINUTE, (nod / 1_000_000_000L) % 60);
419
updateCheckConflict(NANO_OF_DAY, NANO_OF_SECOND, nod % 1_000_000_000L);
420
}
421
if (fieldValues.containsKey(MICRO_OF_DAY)) {
422
long cod = fieldValues.remove(MICRO_OF_DAY);
423
if (resolverStyle != ResolverStyle.LENIENT) {
424
MICRO_OF_DAY.checkValidValue(cod);
425
}
426
updateCheckConflict(MICRO_OF_DAY, SECOND_OF_DAY, cod / 1_000_000L);
427
updateCheckConflict(MICRO_OF_DAY, MICRO_OF_SECOND, cod % 1_000_000L);
428
}
429
if (fieldValues.containsKey(MILLI_OF_DAY)) {
430
long lod = fieldValues.remove(MILLI_OF_DAY);
431
if (resolverStyle != ResolverStyle.LENIENT) {
432
MILLI_OF_DAY.checkValidValue(lod);
433
}
434
updateCheckConflict(MILLI_OF_DAY, SECOND_OF_DAY, lod / 1_000);
435
updateCheckConflict(MILLI_OF_DAY, MILLI_OF_SECOND, lod % 1_000);
436
}
437
if (fieldValues.containsKey(SECOND_OF_DAY)) {
438
long sod = fieldValues.remove(SECOND_OF_DAY);
439
if (resolverStyle != ResolverStyle.LENIENT) {
440
SECOND_OF_DAY.checkValidValue(sod);
441
}
442
updateCheckConflict(SECOND_OF_DAY, HOUR_OF_DAY, sod / 3600);
443
updateCheckConflict(SECOND_OF_DAY, MINUTE_OF_HOUR, (sod / 60) % 60);
444
updateCheckConflict(SECOND_OF_DAY, SECOND_OF_MINUTE, sod % 60);
445
}
446
if (fieldValues.containsKey(MINUTE_OF_DAY)) {
447
long mod = fieldValues.remove(MINUTE_OF_DAY);
448
if (resolverStyle != ResolverStyle.LENIENT) {
449
MINUTE_OF_DAY.checkValidValue(mod);
450
}
451
updateCheckConflict(MINUTE_OF_DAY, HOUR_OF_DAY, mod / 60);
452
updateCheckConflict(MINUTE_OF_DAY, MINUTE_OF_HOUR, mod % 60);
453
}
454
455
// combine partial second fields strictly, leaving lenient expansion to later
456
if (fieldValues.containsKey(NANO_OF_SECOND)) {
457
long nos = fieldValues.get(NANO_OF_SECOND);
458
if (resolverStyle != ResolverStyle.LENIENT) {
459
NANO_OF_SECOND.checkValidValue(nos);
460
}
461
if (fieldValues.containsKey(MICRO_OF_SECOND)) {
462
long cos = fieldValues.remove(MICRO_OF_SECOND);
463
if (resolverStyle != ResolverStyle.LENIENT) {
464
MICRO_OF_SECOND.checkValidValue(cos);
465
}
466
nos = cos * 1000 + (nos % 1000);
467
updateCheckConflict(MICRO_OF_SECOND, NANO_OF_SECOND, nos);
468
}
469
if (fieldValues.containsKey(MILLI_OF_SECOND)) {
470
long los = fieldValues.remove(MILLI_OF_SECOND);
471
if (resolverStyle != ResolverStyle.LENIENT) {
472
MILLI_OF_SECOND.checkValidValue(los);
473
}
474
updateCheckConflict(MILLI_OF_SECOND, NANO_OF_SECOND, los * 1_000_000L + (nos % 1_000_000L));
475
}
476
}
477
478
if (dayPeriod != null && fieldValues.containsKey(HOUR_OF_AMPM)) {
479
long hoap = fieldValues.remove(HOUR_OF_AMPM);
480
if (resolverStyle != ResolverStyle.LENIENT) {
481
HOUR_OF_AMPM.checkValidValue(hoap);
482
}
483
Long mohObj = fieldValues.get(MINUTE_OF_HOUR);
484
long moh = mohObj != null ? Math.floorMod(mohObj, 60) : 0;
485
long excessHours = dayPeriod.includes((Math.floorMod(hoap, 12) + 12) * 60 + moh) ? 12 : 0;
486
long hod = Math.addExact(hoap, excessHours);
487
updateCheckConflict(HOUR_OF_AMPM, HOUR_OF_DAY, hod);
488
dayPeriod = null;
489
}
490
491
// convert to time if all four fields available (optimization)
492
if (fieldValues.containsKey(HOUR_OF_DAY) && fieldValues.containsKey(MINUTE_OF_HOUR) &&
493
fieldValues.containsKey(SECOND_OF_MINUTE) && fieldValues.containsKey(NANO_OF_SECOND)) {
494
long hod = fieldValues.remove(HOUR_OF_DAY);
495
long moh = fieldValues.remove(MINUTE_OF_HOUR);
496
long som = fieldValues.remove(SECOND_OF_MINUTE);
497
long nos = fieldValues.remove(NANO_OF_SECOND);
498
resolveTime(hod, moh, som, nos);
499
}
500
}
501
502
private void resolveTimeLenient() {
503
// leniently create a time from incomplete information
504
// done after everything else as it creates information from nothing
505
// which would break updateCheckConflict(field)
506
507
if (time == null) {
508
// NANO_OF_SECOND merged with MILLI/MICRO above
509
if (fieldValues.containsKey(MILLI_OF_SECOND)) {
510
long los = fieldValues.remove(MILLI_OF_SECOND);
511
if (fieldValues.containsKey(MICRO_OF_SECOND)) {
512
// merge milli-of-second and micro-of-second for better error message
513
long cos = los * 1_000 + (fieldValues.get(MICRO_OF_SECOND) % 1_000);
514
updateCheckConflict(MILLI_OF_SECOND, MICRO_OF_SECOND, cos);
515
fieldValues.remove(MICRO_OF_SECOND);
516
fieldValues.put(NANO_OF_SECOND, cos * 1_000L);
517
} else {
518
// convert milli-of-second to nano-of-second
519
fieldValues.put(NANO_OF_SECOND, los * 1_000_000L);
520
}
521
} else if (fieldValues.containsKey(MICRO_OF_SECOND)) {
522
// convert micro-of-second to nano-of-second
523
long cos = fieldValues.remove(MICRO_OF_SECOND);
524
fieldValues.put(NANO_OF_SECOND, cos * 1_000L);
525
}
526
527
// Set the hour-of-day, if not exist and not in STRICT, to the mid point of the day period or am/pm.
528
if (!fieldValues.containsKey(HOUR_OF_DAY) &&
529
!fieldValues.containsKey(MINUTE_OF_HOUR) &&
530
!fieldValues.containsKey(SECOND_OF_MINUTE) &&
531
!fieldValues.containsKey(NANO_OF_SECOND) &&
532
resolverStyle != ResolverStyle.STRICT) {
533
if (dayPeriod != null) {
534
long midpoint = dayPeriod.mid();
535
resolveTime(midpoint / 60, midpoint % 60, 0, 0);
536
dayPeriod = null;
537
} else if (fieldValues.containsKey(AMPM_OF_DAY)) {
538
long ap = fieldValues.remove(AMPM_OF_DAY);
539
if (resolverStyle == ResolverStyle.LENIENT) {
540
resolveTime(Math.addExact(Math.multiplyExact(ap, 12), 6), 0, 0, 0);
541
} else { // SMART
542
AMPM_OF_DAY.checkValidValue(ap);
543
resolveTime(ap * 12 + 6, 0, 0, 0);
544
}
545
}
546
}
547
548
// merge hour/minute/second/nano leniently
549
Long hod = fieldValues.get(HOUR_OF_DAY);
550
if (hod != null) {
551
Long moh = fieldValues.get(MINUTE_OF_HOUR);
552
Long som = fieldValues.get(SECOND_OF_MINUTE);
553
Long nos = fieldValues.get(NANO_OF_SECOND);
554
555
// check for invalid combinations that cannot be defaulted
556
if ((moh == null && (som != null || nos != null)) ||
557
(moh != null && som == null && nos != null)) {
558
return;
559
}
560
561
// default as necessary and build time
562
long mohVal = (moh != null ? moh : 0);
563
long somVal = (som != null ? som : 0);
564
long nosVal = (nos != null ? nos : 0);
565
566
if (dayPeriod != null && resolverStyle != ResolverStyle.LENIENT) {
567
// Check whether the hod/mohVal is within the day period
568
if (!dayPeriod.includes(hod * 60 + mohVal)) {
569
throw new DateTimeException("Conflict found: Resolved time %02d:%02d".formatted(hod, mohVal) +
570
" conflicts with " + dayPeriod);
571
}
572
}
573
574
resolveTime(hod, mohVal, somVal, nosVal);
575
fieldValues.remove(HOUR_OF_DAY);
576
fieldValues.remove(MINUTE_OF_HOUR);
577
fieldValues.remove(SECOND_OF_MINUTE);
578
fieldValues.remove(NANO_OF_SECOND);
579
}
580
}
581
582
// validate remaining
583
if (resolverStyle != ResolverStyle.LENIENT && fieldValues.size() > 0) {
584
for (Entry<TemporalField, Long> entry : fieldValues.entrySet()) {
585
TemporalField field = entry.getKey();
586
if (field instanceof ChronoField && field.isTimeBased()) {
587
((ChronoField) field).checkValidValue(entry.getValue());
588
}
589
}
590
}
591
}
592
593
private void resolveTime(long hod, long moh, long som, long nos) {
594
if (resolverStyle == ResolverStyle.LENIENT) {
595
long totalNanos = Math.multiplyExact(hod, 3600_000_000_000L);
596
totalNanos = Math.addExact(totalNanos, Math.multiplyExact(moh, 60_000_000_000L));
597
totalNanos = Math.addExact(totalNanos, Math.multiplyExact(som, 1_000_000_000L));
598
totalNanos = Math.addExact(totalNanos, nos);
599
int excessDays = (int) Math.floorDiv(totalNanos, 86400_000_000_000L); // safe int cast
600
long nod = Math.floorMod(totalNanos, 86400_000_000_000L);
601
updateCheckConflict(LocalTime.ofNanoOfDay(nod), Period.ofDays(excessDays));
602
} else { // STRICT or SMART
603
int mohVal = MINUTE_OF_HOUR.checkValidIntValue(moh);
604
int nosVal = NANO_OF_SECOND.checkValidIntValue(nos);
605
// handle 24:00 end of day
606
if (resolverStyle == ResolverStyle.SMART && hod == 24 && mohVal == 0 && som == 0 && nosVal == 0) {
607
updateCheckConflict(LocalTime.MIDNIGHT, Period.ofDays(1));
608
} else {
609
int hodVal = HOUR_OF_DAY.checkValidIntValue(hod);
610
int somVal = SECOND_OF_MINUTE.checkValidIntValue(som);
611
updateCheckConflict(LocalTime.of(hodVal, mohVal, somVal, nosVal), Period.ZERO);
612
}
613
}
614
}
615
616
private void resolvePeriod() {
617
// add whole days if we have both date and time
618
if (date != null && time != null && excessDays.isZero() == false) {
619
date = date.plus(excessDays);
620
excessDays = Period.ZERO;
621
}
622
}
623
624
private void resolveFractional() {
625
// ensure fractional seconds available as ChronoField requires
626
// resolveTimeLenient() will have merged MICRO_OF_SECOND/MILLI_OF_SECOND to NANO_OF_SECOND
627
if (time == null &&
628
(fieldValues.containsKey(INSTANT_SECONDS) ||
629
fieldValues.containsKey(SECOND_OF_DAY) ||
630
fieldValues.containsKey(SECOND_OF_MINUTE))) {
631
if (fieldValues.containsKey(NANO_OF_SECOND)) {
632
long nos = fieldValues.get(NANO_OF_SECOND);
633
fieldValues.put(MICRO_OF_SECOND, nos / 1000);
634
fieldValues.put(MILLI_OF_SECOND, nos / 1000000);
635
} else {
636
fieldValues.put(NANO_OF_SECOND, 0L);
637
fieldValues.put(MICRO_OF_SECOND, 0L);
638
fieldValues.put(MILLI_OF_SECOND, 0L);
639
}
640
}
641
}
642
643
private void resolveInstant() {
644
// add instant seconds if we have date, time and zone
645
// Offset (if present) will be given priority over the zone.
646
if (date != null && time != null) {
647
Long offsetSecs = fieldValues.get(OFFSET_SECONDS);
648
if (offsetSecs != null) {
649
ZoneOffset offset = ZoneOffset.ofTotalSeconds(offsetSecs.intValue());
650
long instant = date.atTime(time).atZone(offset).toEpochSecond();
651
fieldValues.put(INSTANT_SECONDS, instant);
652
} else {
653
if (zone != null) {
654
long instant = date.atTime(time).atZone(zone).toEpochSecond();
655
fieldValues.put(INSTANT_SECONDS, instant);
656
}
657
}
658
}
659
}
660
661
private void updateCheckConflict(LocalTime timeToSet, Period periodToSet) {
662
if (time != null) {
663
if (time.equals(timeToSet) == false) {
664
throw new DateTimeException("Conflict found: Fields resolved to different times: " + time + " " + timeToSet);
665
}
666
if (excessDays.isZero() == false && periodToSet.isZero() == false && excessDays.equals(periodToSet) == false) {
667
throw new DateTimeException("Conflict found: Fields resolved to different excess periods: " + excessDays + " " + periodToSet);
668
} else {
669
excessDays = periodToSet;
670
}
671
} else {
672
time = timeToSet;
673
excessDays = periodToSet;
674
}
675
}
676
677
//-----------------------------------------------------------------------
678
private void crossCheck() {
679
// only cross-check date, time and date-time
680
// avoid object creation if possible
681
if (date != null) {
682
crossCheck(date);
683
}
684
if (time != null) {
685
crossCheck(time);
686
if (date != null && fieldValues.size() > 0) {
687
crossCheck(date.atTime(time));
688
}
689
}
690
}
691
692
private void crossCheck(TemporalAccessor target) {
693
for (Iterator<Entry<TemporalField, Long>> it = fieldValues.entrySet().iterator(); it.hasNext(); ) {
694
Entry<TemporalField, Long> entry = it.next();
695
TemporalField field = entry.getKey();
696
if (target.isSupported(field)) {
697
long val1;
698
try {
699
val1 = target.getLong(field);
700
} catch (RuntimeException ex) {
701
continue;
702
}
703
long val2 = entry.getValue();
704
if (val1 != val2) {
705
throw new DateTimeException("Conflict found: Field " + field + " " + val1 +
706
" differs from " + field + " " + val2 + " derived from " + target);
707
}
708
it.remove();
709
}
710
}
711
}
712
713
//-----------------------------------------------------------------------
714
@Override
715
public String toString() {
716
StringBuilder buf = new StringBuilder(64);
717
buf.append(fieldValues).append(',').append(chrono);
718
if (zone != null) {
719
buf.append(',').append(zone);
720
}
721
if (date != null || time != null) {
722
buf.append(" resolved to ");
723
if (date != null) {
724
buf.append(date);
725
if (time != null) {
726
buf.append('T').append(time);
727
}
728
} else {
729
buf.append(time);
730
}
731
}
732
return buf.toString();
733
}
734
735
}
736
737