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

Add SupportedUnits() to Temporal Interface #333

Closed JeremyBetts closed 10 years ago

JeremyBetts commented 10 years ago

Probably a long shot to get this at this point but it would be extremely handy to have a supported Units method in the temporal interface an not just an isSupported() method. to discover all supported units on a temporal this currently requires looping through the ChronoUnits(which further limits support to only the provided ChronoUnits)

jodastephen commented 10 years ago

It would be possible to add a getSupportedFields() method, but it would only work for ChronoField. Same for units. That seems like a difficult restriction to me, as it ties the high level Temporal interface to the lower level Chrono* enum.

Instead, I think you could write a TemporalQuery implementation that has explicit knowledge for the supplied classes (testing by instanceof) and is checked for directly by any classes you create.

JeremyBetts commented 10 years ago

i was thinking somthing along the lines of adding the following to the Temporal interface: public TemporalField[] getSupportedFields(); public TemporalUnit[] getSupportedUnit();

That way there is not direct tie to the Chrono* enums in the interface. in addition this would allow objects to return any custom Fields or Units. I was just thinking that this would provide a compatible method for getting the units alongside PeriodUnit(Temporal, TemporalUnit), plus(long, TemporalUnit) and minus(long, TemporalUnit)

Although the TemporalQuery Method would be a possible solution.

jodastephen commented 10 years ago

I think the overall answer is that it is possible, but doesn't carry its weight in the API.

(For example, it has to be a selected subset of all possible units, as the entire set of supported units is unbounded).

JeremyBetts commented 10 years ago

I'm confused? The list is bounded for every class that implements an the interface. for example, Instant does not support a year field.

Anyway I think it would be highly useful, but cannot force it to be there.

jodastephen commented 10 years ago

LocalDate supports a bounded list of ChronoUnit, but anyone can implement TemporalUnit, so the list of those is actually unbounded.

I do think there are some use cases for this, but it seems like they are edge cases. A concrete one might help, although I think the chances are slim.

jodastephen commented 10 years ago

We discussed this today. While there are some use cases where this might be useful, it is tricky to come up with a definition as there are multiple possible options each of which has some potential value. The concept could be added in JDK9 using a TemporalQuery or a default method so we are not limited by not adding it now.

As such, we decided to make no change.