eh3rrera / ocpj8-book

Study guide for the Oracle Certified Professional, Java SE 8 Programmer Exam (1Z0-809)
Other
131 stars 91 forks source link

ch21: Duration and ChronoUnit table #65

Closed txpokey closed 6 years ago

txpokey commented 6 years ago

I'm seeing only:

being accepted without a throw...

txpokey commented 6 years ago

... as per

        List<TemporalUnit> u = dif.getUnits(); // Seconds and Nanos
eh3rrera commented 6 years ago

Without a throw? Sorry, I don't understand Duration only works in terms of seconds and nanoseconds

txpokey commented 6 years ago

sure the book has the following table in the section for Duration:

Valid values of ChronoUnit are:

NANOS
MICROS
MILLIS
SECONDS
MINUTES
HOURS
HALF_DAYS
DAYS

but that list really seems applicable to Duration.of() not Duration.get()

To test it out, I wrote the following:


    public void exploreDuration() {
        Duration dif = Duration.between(Instant.ofEpochSecond(123456789), Instant.ofEpochSecond(99999));
        List<TemporalUnit> willPass = dif.getUnits(); // Seconds and Nanos
        final Predicate<ChronoUnit> chronoUnitPredicate = v -> !willPass.contains(v);
        List<TemporalUnit> getWillFailOnThese = Arrays.stream(ChronoUnit.values()).filter(chronoUnitPredicate).collect
                (Collectors.toList());
        log.debug("temporalUnitBiFunction4Get");
        findWhichUnitsAreSupported(dif, getWillFailOnThese, temporalUnitBiFunction4Get);
        findWhichUnitsAreSupported(dif, willPass, temporalUnitBiFunction4Get);
        log.debug("temporalUnitBiFunction4Of");
        findWhichUnitsAreSupported(dif, getWillFailOnThese, temporalUnitBiFunction4Of);
        findWhichUnitsAreSupported(dif, willPass, temporalUnitBiFunction4Of);
        log.debug("");
    }
    final private BiFunction<Duration, TemporalUnit, Object> temporalUnitBiFunction4Get = 
        (duration, couldThrow ) -> duration.get(couldThrow);
    final private BiFunction<Duration, TemporalUnit, Object> temporalUnitBiFunction4Of = 
        (dummy, couldThrow ) ->  Duration.of(1, couldThrow);

    private void findWhichUnitsAreSupported(Duration duration, List<TemporalUnit> mayFailOnThese,
                                            BiFunction<Duration, TemporalUnit, Object> biFunction) {
        for( TemporalUnit couldThrow : mayFailOnThese )
        {
            try {
                Object candidate = biFunction.apply(duration,couldThrow);
                log.debug("works:> " + couldThrow );
            } catch (UnsupportedTemporalTypeException e) {
                log.debug(e.getMessage());
            }
        }
    }

and I got the following results:

2018-05-01|23:20:20 ExploreLocalTimeTest.exploreDuration.66 [DEBUG] temporalUnitBiFunction4Get
2018-05-01|23:20:20 ExploreLocalTimeTest.findWhichUnitsAreSupported.89 [DEBUG] Unsupported unit: Micros
2018-05-01|23:20:20 ExploreLocalTimeTest.findWhichUnitsAreSupported.89 [DEBUG] Unsupported unit: Millis
2018-05-01|23:20:20 ExploreLocalTimeTest.findWhichUnitsAreSupported.89 [DEBUG] Unsupported unit: Minutes
2018-05-01|23:20:20 ExploreLocalTimeTest.findWhichUnitsAreSupported.89 [DEBUG] Unsupported unit: Hours
2018-05-01|23:20:20 ExploreLocalTimeTest.findWhichUnitsAreSupported.89 [DEBUG] Unsupported unit: HalfDays
2018-05-01|23:20:20 ExploreLocalTimeTest.findWhichUnitsAreSupported.89 [DEBUG] Unsupported unit: Days
2018-05-01|23:20:20 ExploreLocalTimeTest.findWhichUnitsAreSupported.89 [DEBUG] Unsupported unit: Weeks
2018-05-01|23:20:20 ExploreLocalTimeTest.findWhichUnitsAreSupported.89 [DEBUG] Unsupported unit: Months
2018-05-01|23:20:20 ExploreLocalTimeTest.findWhichUnitsAreSupported.89 [DEBUG] Unsupported unit: Years
2018-05-01|23:20:20 ExploreLocalTimeTest.findWhichUnitsAreSupported.89 [DEBUG] Unsupported unit: Decades
2018-05-01|23:20:20 ExploreLocalTimeTest.findWhichUnitsAreSupported.89 [DEBUG] Unsupported unit: Centuries
2018-05-01|23:20:20 ExploreLocalTimeTest.findWhichUnitsAreSupported.89 [DEBUG] Unsupported unit: Millennia
2018-05-01|23:20:20 ExploreLocalTimeTest.findWhichUnitsAreSupported.89 [DEBUG] Unsupported unit: Eras
2018-05-01|23:20:20 ExploreLocalTimeTest.findWhichUnitsAreSupported.89 [DEBUG] Unsupported unit: Forever
2018-05-01|23:20:20 ExploreLocalTimeTest.findWhichUnitsAreSupported.87 [DEBUG] works:> Seconds
2018-05-01|23:20:20 ExploreLocalTimeTest.findWhichUnitsAreSupported.87 [DEBUG] works:> Nanos
2018-05-01|23:20:20 ExploreLocalTimeTest.exploreDuration.69 [DEBUG] temporalUnitBiFunction4Of
2018-05-01|23:20:20 ExploreLocalTimeTest.findWhichUnitsAreSupported.87 [DEBUG] works:> Micros
2018-05-01|23:20:20 ExploreLocalTimeTest.findWhichUnitsAreSupported.87 [DEBUG] works:> Millis
2018-05-01|23:20:20 ExploreLocalTimeTest.findWhichUnitsAreSupported.87 [DEBUG] works:> Minutes
2018-05-01|23:20:20 ExploreLocalTimeTest.findWhichUnitsAreSupported.87 [DEBUG] works:> Hours
2018-05-01|23:20:20 ExploreLocalTimeTest.findWhichUnitsAreSupported.87 [DEBUG] works:> HalfDays
2018-05-01|23:20:20 ExploreLocalTimeTest.findWhichUnitsAreSupported.87 [DEBUG] works:> Days
2018-05-01|23:20:20 ExploreLocalTimeTest.findWhichUnitsAreSupported.89 [DEBUG] Unit must not have an estimated duration
2018-05-01|23:20:20 ExploreLocalTimeTest.findWhichUnitsAreSupported.89 [DEBUG] Unit must not have an estimated duration
2018-05-01|23:20:20 ExploreLocalTimeTest.findWhichUnitsAreSupported.89 [DEBUG] Unit must not have an estimated duration
2018-05-01|23:20:20 ExploreLocalTimeTest.findWhichUnitsAreSupported.89 [DEBUG] Unit must not have an estimated duration
2018-05-01|23:20:20 ExploreLocalTimeTest.findWhichUnitsAreSupported.89 [DEBUG] Unit must not have an estimated duration
2018-05-01|23:20:20 ExploreLocalTimeTest.findWhichUnitsAreSupported.89 [DEBUG] Unit must not have an estimated duration
2018-05-01|23:20:20 ExploreLocalTimeTest.findWhichUnitsAreSupported.89 [DEBUG] Unit must not have an estimated duration
2018-05-01|23:20:20 ExploreLocalTimeTest.findWhichUnitsAreSupported.89 [DEBUG] Unit must not have an estimated duration
2018-05-01|23:20:20 ExploreLocalTimeTest.findWhichUnitsAreSupported.87 [DEBUG] works:> Seconds
2018-05-01|23:20:20 ExploreLocalTimeTest.findWhichUnitsAreSupported.87 [DEBUG] works:> Nanos
eh3rrera commented 6 years ago

Oh, well, yes it makes sense because get() doesn't create a Duration instance like of(). It just gets the value for each of the two units supported by Duration, SECONDS and NANOS.

The table of values of ChronoUnit belongs to the of() example.

A couple of paragraphs later, when the method get() is presented in an example, there's a comment that mentions "Supports SECONDS and NANOS.Other units throw an exception".

But if you prefer, to be clearer, I can write instead:

Valid values of `ChronoUnit` for the method `Duration.of(long amount, TemporalUnit unit)` are:
 - NANOS
- MICROS
- ...

What do you think? This is the issue?

Thanks!

txpokey commented 6 years ago

1st I think you meant to say:

Valid values of `ChronoUnit` for the method `Duration.get(TemporalUnit unit)` are:
- NANOS
- SECONDS

And TemporalUnit is most correct, of course.

But that is just part of the issue because there's a separate sub set of ChronoUnit enums for the Duration.get() use case and the Duration.of() use case. And they are proper subsets, because (e.g.) CENTURIES is not used in either "Duration" use case scenario.

So the existing table for Duration.of() needs to be labeled or otherwise qualified to say valid values for the Duration.of() method.

Valid values of `ChronoUnit` for the method `Duration.of(long amount, TemporalUnit unit)`
- NANOS
- MICROS
- MILLIS
- SECONDS
- MINUTES
- HOURS
- HALF_DAYS
- DAYS

And I do think that adding a second table:

Valid values of `ChronoUnit` for the method `Duration.get(TemporalUnit unit)` are:
- NANOS
- SECONDS

is the right idea for the instant situation.

eh3rrera commented 6 years ago

Done, but due to space restrictions, get(TemporalUnit) didn't get a table. Thanks!

txpokey commented 6 years ago

... I should have mentioned this before, but the change you dropped -- plus the Key Points discussion (e.g.) on Duration -- IMHO mistakenly refer the Duration.of() as a constructor. Since there actually is a Constructor concept in Java and java reflection, I think the static *.of() methods are factory methods and not constructors.

but relative to any table for get(TemporalUnit), I think you handled that fine in the code I just read.

So next steps on "constructor"? ....

eh3rrera commented 6 years ago

I agree. I don't know why used that word. There was just one reference to constructor left to remove.

txpokey commented 6 years ago

Oh BTW: besides just updating my profile here on github, I just rebased on your latest version of the Duration and Key Points on Duration. MUCH BETTER. Thanks for working together on this.

eh3rrera commented 6 years ago

Thank you 😄