Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Java 8 Date Time support: typeclasses #6

Merged
merged 9 commits into from
Jul 14, 2016
Merged

Conversation

noelmarkham
Copy link
Collaborator

@noelmarkham noelmarkham commented Jul 11, 2016

This PR is a first stab at including support for other datetime libraries as well as Joda. This allows the generators to work with Java 8's java.time library.

You can now call genDateTimeWithinRange with, for instance, a Joda DateTime and Period, or a Java8 ZonedDateTime and Duration, and it should return the appropriate type.

How this works:
genDateTimeWithinRange takes an implicit ScalaCheckDateTimeInfra type, which is parameterized on the date class and the "range". As long as an implicit exists for that date/range pair, then this works. For this PR, Joda DateTime and Period and Java8 ZonedDateTime and Duration are included, but there's no reason not to include more, such as Joda DateTime with Joda Duration, or the other incarnations of Java8's DateTimes that are not Zoned.

To the end-user, the generators such as genDateTimeWithinRange should "just work", without needing to understand ScalaCheckDateTimeInfra - just having the correct imports should be fine.

By example, to work with Java 8, these imports are needed:

import com.fortysevendeg.scalacheck.datetime.GenDateTime._
import com.fortysevendeg.scalacheck.datetime.instances.j8._

and ... instances.joda._ exists too.

Right now, the ScalaCheckDateTimeInfra expects to be able to convert whatever classes are provided to millis. I'm pretty sure I can take another look at this and remove that restriction (especially as Java 8 has nanosecond granularity). Another reason is that this PR uncovered some crazy stuff with Java 8's library, such as getting the milliseconds of a Date that can't be represented by milliseconds throws an arithmetic exception"

Again, I'd love some feedback if possible, especially around the naming (I hate ScalaCheckDateTimeInfra) and the general approach taken.

Resolves #2

@codecov-io
Copy link

Current coverage is 100%

No coverage report found for master at 16b0c23.

Powered by Codecov. Last updated by 16b0c23...3b5a9f8

@franciscodr
Copy link
Member

LGTM!


package object instances {
object joda extends JodaInstances
object j8 extends J8Instances
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

JDK8 may be a better known acronym

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@raulraja
Copy link
Contributor

LGTM!


trait J8Instances {

implicit val j8ForDuration: ScalaCheckDateTimeInfra[ZonedDateTime, Duration] = new ScalaCheckDateTimeInfra[ZonedDateTime, Duration] {
Copy link

@diesalbla diesalbla Jul 14, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be a good idea to replace it for the following ?
implicit object j8ForDuration extends ScalaCheckDateTimeInfre[ZonedDateTime, Duration] {

I have done this before, but I do not know if they are better than a val. It could save an anonymous class.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've had issues in the past with type inference when doing things that way (admittedly with more complex types than this). I seem to recall that implicit val... is the suggested way to do it, though I can't find any evidence to back that up. Doing it my way allows explicit setting of the desired type, which I like, so I'll probably stick with that for now.

@diesalbla
Copy link

LGTM

@noelmarkham
Copy link
Collaborator Author

Thanks for the reviews - I'm going to refactor j8 -> jdk8 in the next pull request as it's only cosmetic.

@noelmarkham noelmarkham merged commit e274989 into master Jul 14, 2016
@noelmarkham noelmarkham deleted the issue-2-typeclasses branch July 14, 2016 15:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants