-
Notifications
You must be signed in to change notification settings - Fork 123
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #136 from CruncherBigData/develop
jodatime re-sending to reflect latest changes.
- Loading branch information
Showing
5 changed files
with
91 additions
and
1 deletion.
There are no files selected for viewing
Empty file.
21 changes: 21 additions & 0 deletions
21
bijection-jodatime/src/main/scala/com/twitter/bijection/jodatime/DateBijections.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package com.twitter.bijection.jodatime | ||
|
||
import com.twitter.bijection.{ Bijection, AbstractBijection} | ||
import java.util.Date | ||
import org.joda.time.DateTime | ||
|
||
trait DateBijections { | ||
|
||
implicit val date2joda: Bijection[java.util.Date, DateTime] = | ||
new AbstractBijection[java.util.Date, DateTime] { | ||
def apply(d: java.util.Date) = new DateTime(d) | ||
override def invert(joda: DateTime) = joda.toDate() | ||
} | ||
|
||
implicit val joda2Long: Bijection[DateTime, Long] = | ||
new AbstractBijection[DateTime, Long] { | ||
def apply(d: DateTime) = d.getMillis() | ||
override def invert(l: Long) = new DateTime(l) | ||
} | ||
|
||
} |
29 changes: 29 additions & 0 deletions
29
bijection-jodatime/src/main/scala/com/twitter/bijection/jodatime/DateInjections.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package com.twitter.bijection.jodatime | ||
|
||
import java.util.Date | ||
import org.joda.time.DateTime | ||
import com.twitter.bijection.Inversion.attempt | ||
import com.twitter.bijection.{ Injection, InversionFailure, AbstractInjection } | ||
|
||
trait DateInjections { | ||
|
||
implicit val date2String: Injection[Date, String] = | ||
new AbstractInjection[Date, String] { | ||
def apply(d: Date) = d.toString | ||
override def invert(s: String) = attempt(s)(new DateTime(_).toDate()) | ||
|
||
} | ||
|
||
implicit val joda2String: Injection[DateTime, String] = | ||
new AbstractInjection[DateTime, String] { | ||
def apply(d: DateTime) = d.toString | ||
override def invert(s: String) = attempt(s)(new DateTime(_)) | ||
} | ||
|
||
|
||
implicit val joda2Date: Injection[DateTime, Date] = | ||
new AbstractInjection[DateTime, Date] { | ||
def apply(d: DateTime) = d.toDate() | ||
override def invert(d: Date) = attempt(d)(new DateTime(_)) | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
bijection-jodatime/src/test/scala/com/twitter/bijection/jodatime/DateBijectionLaws.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package com.twitter.bijection.jodatime | ||
|
||
import org.scalacheck.Properties | ||
import org.scalacheck.Gen._ | ||
import org.scalacheck.Arbitrary | ||
import org.scalacheck.Prop._ | ||
import com.twitter.bijection.{ Bijection, BaseProperties, ImplicitBijection } | ||
import java.util.Date | ||
import org.joda.time.DateTime | ||
import com.twitter.bijection._ | ||
|
||
object DateBijectionsLaws extends Properties("DateBijections") with BaseProperties with DateBijections with DateInjections { | ||
|
||
import Rep._ | ||
|
||
implicit val strByte = arbitraryViaBijection[Date, String @@ Rep[Date]] | ||
|
||
implicit val date = arbitraryViaFn { (dtime: Long) => new DateTime(dtime) } | ||
|
||
property("Long <=> Joda") = isBijection[Long, DateTime] | ||
|
||
property("Date <=> Joda") = isBijection[Date, DateTime] | ||
|
||
property("round trips Date -> String") = isLooseInjection[DateTime,String] | ||
|
||
property("round trips Joda -> Date") = isLooseInjection[DateTime, Date] | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters