-
-
Notifications
You must be signed in to change notification settings - Fork 202
/
Bounded.scala
49 lines (36 loc) · 1.14 KB
/
Bounded.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package monocle.internal
import monocle.Prism
import scalaz.syntax.order._
import scalaz.{Maybe, Order}
private[monocle] trait Bounded[T] {
def MinValue: T
def MaxValue: T
}
private[monocle] object Bounded extends BoundedInstances {
def apply[T](implicit ev: Bounded[T]): Bounded[T] = ev
def orderingBoundedSafeCast[S: Order, A: Bounded](unsafeCast: S => A)(reverseCast: A => S): Prism[S, A] =
Prism[S, A]( from =>
if (from > reverseCast(Bounded[A].MaxValue) || from < reverseCast(Bounded[A].MinValue))
None
else
Some(unsafeCast(from))
)(reverseCast)
}
private[monocle] trait BoundedInstances {
implicit val booleanBounded = new Bounded[Boolean]{
val MaxValue: Boolean = true
val MinValue: Boolean = false
}
implicit val byteBounded= new Bounded[Byte] {
val MaxValue: Byte = Byte.MaxValue
val MinValue: Byte = Byte.MinValue
}
implicit val charBounded = new Bounded[Char] {
val MaxValue: Char = Char.MaxValue
val MinValue: Char = Char.MinValue
}
implicit val intBounded = new Bounded[Int] {
val MaxValue: Int = Int.MaxValue
val MinValue: Int = Int.MinValue
}
}