ThreeTen / threeten

This project was the home of code used to develop a modern date and time library for JDK8. Development has moved to OpenJDK and a separate backport project, threetenbp.
http://threeten.github.io/
191 stars 37 forks source link

Consider an easier date adjuster #286

Closed jodastephen closed 11 years ago

jodastephen commented 11 years ago

TemporalAdjuster is based on the low level Temporal, whereas most adjustments want to be written for LocalDate. Consider the following which may help:

    //-----------------------------------------------------------------------
    /**
     * Obtains a {@code TemporalAdjuster} that wraps a date adjuster.
     * <p>
     * The {@code TemporalAdjuster} is based on the low level {@code Temporal} interface.
     * This method allows an adjustment from {@code LocalDate} to {@code LocalDate}
     * to be wrapped to match the temporal-based interface.
     *
     * @return the temporal adjuster wrapping on the date adjuster, not null
     */
    public static TemporalAdjuster ofDateAdjuster(UnaryOperator<LocalDate> adjuster) {
        return (temporal) -> {
            LocalDate input = LocalDate.from(temporal);
            LocalDate output = adjuster.apply(input);
            return temporal.with(output);
        };
    }

Note, its not certain that this necessarily helps, or whether it should be hidden internally as part of a streams API.

RogerRiggs commented 11 years ago

What exceptions can ofDateAdjuster throw?

jodastephen commented 11 years ago

None. The returned adjuster might throw exceptions.

Still not sure that this is worth it yet though.

jodastephen commented 11 years ago

I think this is worth doing, as pretty much all implementations I can foresee in my day job are date-based, and this method makes writing them significantly easier.

RogerRiggs commented 11 years ago

How would it be used? It might lead to adjusters that make incorrect assumptions that are ISO biased, like the days in months, etc. I don't it as that valuable.

jodastephen commented 11 years ago

I've pointed out many times before that running algorithms or manipulations on dates in an arbitrary/unknown calendar system is risky and likely to go wrong, due to the wide variation in calendar systems. Most business applications (what Java is principally used for) use ISO for algorithms, so making it easier to write correct ISO adjusters is a good thing, not a bad one.

The method would be used to help users write adjusters.

public static TemporalAdjuster TWEAKER = TemporalAdjuster.ofDateAdjuster(date -> { ... });
RogerRiggs commented 11 years ago

ok

jodastephen commented 11 years ago

Fixed in http://hg.openjdk.java.net/threeten/threeten/jdk/rev/3dbe3cd949dd