Search in sources :

Example 1 with TemporalQuery

use of java.time.temporal.TemporalQuery in project jdk8u_jdk by JetBrains.

the class TCKZoneId method factory_from_TemporalAccessor_zoneId.

//-----------------------------------------------------------------------
// from(TemporalAccessor)
//-----------------------------------------------------------------------
@Test
public void factory_from_TemporalAccessor_zoneId() {
    TemporalAccessor mock = new TemporalAccessor() {

        @Override
        public boolean isSupported(TemporalField field) {
            return false;
        }

        @Override
        public long getLong(TemporalField field) {
            throw new DateTimeException("Mock");
        }

        @SuppressWarnings("unchecked")
        @Override
        public <R> R query(TemporalQuery<R> query) {
            if (query == TemporalQueries.zoneId()) {
                return (R) ZoneId.of("Europe/Paris");
            }
            return TemporalAccessor.super.query(query);
        }
    };
    assertEquals(ZoneId.from(mock), ZoneId.of("Europe/Paris"));
}
Also used : TemporalAccessor(java.time.temporal.TemporalAccessor) TemporalField(java.time.temporal.TemporalField) DateTimeException(java.time.DateTimeException) TemporalQuery(java.time.temporal.TemporalQuery) Test(org.testng.annotations.Test)

Example 2 with TemporalQuery

use of java.time.temporal.TemporalQuery 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);
        }
    };
}
Also used : ChronoLocalDate(java.time.chrono.ChronoLocalDate) DateTimeException(java.time.DateTimeException) TemporalAccessor(java.time.temporal.TemporalAccessor) TemporalField(java.time.temporal.TemporalField) ZoneId(java.time.ZoneId) ChronoField(java.time.temporal.ChronoField) TemporalQuery(java.time.temporal.TemporalQuery) Chronology(java.time.chrono.Chronology) IsoChronology(java.time.chrono.IsoChronology) ZoneOffset(java.time.ZoneOffset)

Example 3 with TemporalQuery

use of java.time.temporal.TemporalQuery in project jdk8u_jdk by JetBrains.

the class TCKDateTimeFormatter method test_parseBest_String_nullRules.

@Test(expectedExceptions = NullPointerException.class)
public void test_parseBest_String_nullRules() throws Exception {
    DateTimeFormatter test = fmt.withLocale(Locale.ENGLISH).withDecimalStyle(DecimalStyle.STANDARD);
    test.parseBest("30", (TemporalQuery<?>[]) null);
}
Also used : TemporalQuery(java.time.temporal.TemporalQuery) DateTimeFormatter(java.time.format.DateTimeFormatter) Test(org.testng.annotations.Test)

Aggregations

TemporalQuery (java.time.temporal.TemporalQuery)3 DateTimeException (java.time.DateTimeException)2 TemporalAccessor (java.time.temporal.TemporalAccessor)2 TemporalField (java.time.temporal.TemporalField)2 Test (org.testng.annotations.Test)2 ZoneId (java.time.ZoneId)1 ZoneOffset (java.time.ZoneOffset)1 ChronoLocalDate (java.time.chrono.ChronoLocalDate)1 Chronology (java.time.chrono.Chronology)1 IsoChronology (java.time.chrono.IsoChronology)1 DateTimeFormatter (java.time.format.DateTimeFormatter)1 ChronoField (java.time.temporal.ChronoField)1