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

UUID instances #1399

Merged
merged 4 commits into from
Oct 24, 2016
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions core/src/main/scala/cats/instances/all.scala
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@ trait AllInstances
with FutureInstances
with TryInstances
with TupleInstances
with UUIDInstances
9 changes: 9 additions & 0 deletions core/src/main/scala/cats/instances/uuid.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package cats
package instances

import java.util.UUID

trait UUIDInstances extends cats.kernel.instances.UUIDInstances {
implicit val catsStdShowForUUID: Show[UUID] =
Show.fromToString[UUID]
}
9 changes: 9 additions & 0 deletions kernel-laws/src/test/scala/cats/kernel/laws/LawTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import org.scalatest.FunSuite
import scala.util.Random
import scala.collection.immutable.BitSet

import java.util.UUID

class LawTests extends FunSuite with Discipline {

// The scalacheck defaults (100,100) are too high for scala-js.
Expand All @@ -33,6 +35,9 @@ class LawTests extends FunSuite with Discipline {
implicit val arbitrarySymbol: Arbitrary[Symbol] =
Arbitrary(arbitrary[String].map(s => Symbol(s)))

implicit val arbitraryUUID: Arbitrary[UUID] =
Arbitrary(Gen.uuid)

// this instance is not available in scalacheck 1.13.2.
// remove this once a newer version is available.
implicit val cogenBigInt: Cogen[BigInt] =
Expand All @@ -46,6 +51,9 @@ class LawTests extends FunSuite with Discipline {
implicit val cogenSymbol: Cogen[Symbol] =
Cogen[String].contramap(_.name)

implicit val cogenUUID: Cogen[UUID] =
Cogen[(Long, Long)].contramap(u => (u.getMostSignificantBits, u.getLeastSignificantBits))

{
// needed for Cogen[Map[...]]
implicit val ohe: Ordering[HasEq[Int]] = Ordering[Int].on(_.a)
Expand Down Expand Up @@ -78,6 +86,7 @@ class LawTests extends FunSuite with Discipline {
laws[OrderLaws, Long].check(_.order)
laws[OrderLaws, BitSet].check(_.partialOrder)
laws[OrderLaws, BigInt].check(_.order)
laws[OrderLaws, UUID].check(_.order)
laws[OrderLaws, List[Int]].check(_.order)
laws[OrderLaws, Option[String]].check(_.order)
laws[OrderLaws, List[String]].check(_.order)
Expand Down
7 changes: 7 additions & 0 deletions kernel/src/main/scala/cats/kernel/Order.scala
Original file line number Diff line number Diff line change
Expand Up @@ -203,4 +203,11 @@ object Order extends OrderFunctions[Order] {

override def toOrdering: Ordering[A] = ev
}

def fromComparable[A <: Comparable[A]]: Order[A] = {
new Order[A] {
override def compare(x: A, y: A): Int =
x compareTo y
}
}
}
1 change: 1 addition & 0 deletions kernel/src/main/scala/cats/kernel/instances/all.scala
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,5 @@ trait AllInstances
with SymbolInstances
with TupleInstances
with UnitInstances
with UUIDInstances
with VectorInstances
11 changes: 11 additions & 0 deletions kernel/src/main/scala/cats/kernel/instances/uuid.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package cats.kernel
package instances

import java.util.UUID

package object uuid extends UUIDInstances

trait UUIDInstances {
implicit val catsKernelStdOrderForUUID: Order[UUID] =
Order.fromComparable[UUID]
}