use of java.time.temporal.TemporalAccessor in project jdk8u_jdk by JetBrains.
the class DateTimePrintContext method adjust.
private static TemporalAccessor adjust(final TemporalAccessor temporal, DateTimeFormatter formatter) {
// normal case first (early return is an optimization)
Chronology overrideChrono = formatter.getChronology();
ZoneId overrideZone = formatter.getZone();
if (overrideChrono == null && overrideZone == null) {
return temporal;
}
// ensure minimal change (early return is an optimization)
Chronology temporalChrono = temporal.query(TemporalQueries.chronology());
ZoneId temporalZone = temporal.query(TemporalQueries.zoneId());
if (Objects.equals(overrideChrono, temporalChrono)) {
overrideChrono = null;
}
if (Objects.equals(overrideZone, temporalZone)) {
overrideZone = null;
}
if (overrideChrono == null && overrideZone == null) {
return temporal;
}
// make adjustment
final Chronology effectiveChrono = (overrideChrono != null ? overrideChrono : temporalChrono);
if (overrideZone != null) {
// if have zone and instant, calculation is simple, defaulting chrono if necessary
if (temporal.isSupported(INSTANT_SECONDS)) {
Chronology chrono = (effectiveChrono != null ? effectiveChrono : IsoChronology.INSTANCE);
return chrono.zonedDateTime(Instant.from(temporal), overrideZone);
}
// block changing zone on OffsetTime, and similar problem cases
if (overrideZone.normalized() instanceof ZoneOffset && temporal.isSupported(OFFSET_SECONDS) && temporal.get(OFFSET_SECONDS) != overrideZone.getRules().getOffset(Instant.EPOCH).getTotalSeconds()) {
throw new DateTimeException("Unable to apply override zone '" + overrideZone + "' because the temporal object being formatted has a different offset but" + " does not represent an instant: " + temporal);
}
}
final ZoneId effectiveZone = (overrideZone != null ? overrideZone : temporalZone);
final ChronoLocalDate effectiveDate;
if (overrideChrono != null) {
if (temporal.isSupported(EPOCH_DAY)) {
effectiveDate = effectiveChrono.date(temporal);
} else {
// check for date fields other than epoch-day, ignoring case of converting null to ISO
if (!(overrideChrono == IsoChronology.INSTANCE && temporalChrono == null)) {
for (ChronoField f : ChronoField.values()) {
if (f.isDateBased() && temporal.isSupported(f)) {
throw new DateTimeException("Unable to apply override chronology '" + overrideChrono + "' because the temporal object being formatted contains date fields but" + " does not represent a whole date: " + temporal);
}
}
}
effectiveDate = null;
}
} else {
effectiveDate = null;
}
// this better handles map-like underlying temporal instances
return new TemporalAccessor() {
@Override
public boolean isSupported(TemporalField field) {
if (effectiveDate != null && field.isDateBased()) {
return effectiveDate.isSupported(field);
}
return temporal.isSupported(field);
}
@Override
public ValueRange range(TemporalField field) {
if (effectiveDate != null && field.isDateBased()) {
return effectiveDate.range(field);
}
return temporal.range(field);
}
@Override
public long getLong(TemporalField field) {
if (effectiveDate != null && field.isDateBased()) {
return effectiveDate.getLong(field);
}
return temporal.getLong(field);
}
@SuppressWarnings("unchecked")
@Override
public <R> R query(TemporalQuery<R> query) {
if (query == TemporalQueries.chronology()) {
return (R) effectiveChrono;
}
if (query == TemporalQueries.zoneId()) {
return (R) effectiveZone;
}
if (query == TemporalQueries.precision()) {
return temporal.query(query);
}
return query.queryFrom(this);
}
};
}
use of java.time.temporal.TemporalAccessor in project jdk8u_jdk by JetBrains.
the class Parsed method resolveFields.
//-----------------------------------------------------------------------
private void resolveFields() {
// resolve ChronoField
resolveInstantFields();
resolveDateFields();
resolveTimeFields();
// any lenient date resolution should return epoch-day
if (fieldValues.size() > 0) {
int changedCount = 0;
outer: while (changedCount < 50) {
for (Map.Entry<TemporalField, Long> entry : fieldValues.entrySet()) {
TemporalField targetField = entry.getKey();
TemporalAccessor resolvedObject = targetField.resolve(fieldValues, this, resolverStyle);
if (resolvedObject != null) {
if (resolvedObject instanceof ChronoZonedDateTime) {
ChronoZonedDateTime<?> czdt = (ChronoZonedDateTime<?>) resolvedObject;
if (zone == null) {
zone = czdt.getZone();
} else if (zone.equals(czdt.getZone()) == false) {
throw new DateTimeException("ChronoZonedDateTime must use the effective parsed zone: " + zone);
}
resolvedObject = czdt.toLocalDateTime();
}
if (resolvedObject instanceof ChronoLocalDateTime) {
ChronoLocalDateTime<?> cldt = (ChronoLocalDateTime<?>) resolvedObject;
updateCheckConflict(cldt.toLocalTime(), Period.ZERO);
updateCheckConflict(cldt.toLocalDate());
changedCount++;
// have to restart to avoid concurrent modification
continue outer;
}
if (resolvedObject instanceof ChronoLocalDate) {
updateCheckConflict((ChronoLocalDate) resolvedObject);
changedCount++;
// have to restart to avoid concurrent modification
continue outer;
}
if (resolvedObject instanceof LocalTime) {
updateCheckConflict((LocalTime) resolvedObject, Period.ZERO);
changedCount++;
// have to restart to avoid concurrent modification
continue outer;
}
throw new DateTimeException("Method resolve() can only return ChronoZonedDateTime, " + "ChronoLocalDateTime, ChronoLocalDate or LocalTime");
} else if (fieldValues.containsKey(targetField) == false) {
changedCount++;
// have to restart to avoid concurrent modification
continue outer;
}
}
break;
}
if (changedCount == 50) {
// catch infinite loops
throw new DateTimeException("One of the parsed fields has an incorrectly implemented resolve method");
}
// if something changed then have to redo ChronoField resolve
if (changedCount > 0) {
resolveInstantFields();
resolveDateFields();
resolveTimeFields();
}
}
}
use of java.time.temporal.TemporalAccessor in project jdk8u_jdk by JetBrains.
the class AbstractDateTimeTest method basicTest_get_TemporalField_unsupported.
@Test()
public void basicTest_get_TemporalField_unsupported() {
for (TemporalAccessor sample : samples()) {
for (TemporalField field : invalidFields()) {
try {
sample.get(field);
fail("Failed on " + sample + " " + field);
} catch (DateTimeException ex) {
// expected
}
}
}
}
use of java.time.temporal.TemporalAccessor in project jdk8u_jdk by JetBrains.
the class AbstractDateTimeTest method basicTest_range_TemporalField_unsupported.
@Test()
public void basicTest_range_TemporalField_unsupported() {
for (TemporalAccessor sample : samples()) {
for (TemporalField field : invalidFields()) {
try {
sample.range(field);
fail("Failed on " + sample + " " + field);
} catch (DateTimeException ex) {
// expected
}
}
}
}
use of java.time.temporal.TemporalAccessor in project jdk8u_jdk by JetBrains.
the class AbstractDateTimeTest method basicTest_get_TemporalField_supported.
//-----------------------------------------------------------------------
// get(TemporalField)
//-----------------------------------------------------------------------
@Test()
public void basicTest_get_TemporalField_supported() {
for (TemporalAccessor sample : samples()) {
for (TemporalField field : validFields()) {
if (sample.range(field).isIntValue()) {
// no exception
sample.get(field);
} else {
try {
sample.get(field);
fail("Failed on " + sample + " " + field);
} catch (DateTimeException ex) {
// expected
}
}
}
}
}
Aggregations