Skip to content

Commit 4c913bc

Browse files
committed
Add the ability to constrain a Bounds
1 parent f9b101f commit 4c913bc

2 files changed

Lines changed: 71 additions & 3 deletions

File tree

core/src/main/scala/latis/util/Bounds.scala

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,34 @@ final case class Bounds[T] private (val lower: T, val upper: T)(ord: Ordering[T]
2323
// NaN greater than other values but the less than test will be false.
2424
ord.gteq(value, lower) && (ord.lt(value, upper) || (inclusive && ord.equiv(value, upper)))
2525
}
26+
27+
/**
28+
* Constrains the lower bound.
29+
*
30+
* This optionally returns a new Bounds if the given value is greater than
31+
* the current lower bound. Otherwise, it returns itself. If the bounds
32+
* become invalid: lower > upper, this will return None.
33+
*
34+
* @param value the potentially new lower bound
35+
*/
36+
def constrainLower(value: T): Option[Bounds[T]] = {
37+
if (ord.gt(value, lower)) Bounds.of(value, upper)(ord) //TODO: why can't we use ">"?
38+
else Some(this)
39+
}
40+
41+
/**
42+
* Constrains the upper bound.
43+
*
44+
* This optionally returns a new Bounds if the given value is less than the
45+
* current upper bound. Otherwise, it returns itself. If the bounds become
46+
* invalid: lower > upper, this will return None.
47+
*
48+
* @param value the potentially new upper bound
49+
*/
50+
def constrainUpper(value: T): Option[Bounds[T]] = {
51+
if (ord.lt(value, upper)) Bounds.of(lower, value)(ord)
52+
else Some(this)
53+
}
2654
}
2755

2856
object Bounds {

core/src/test/scala/latis/util/BoundsSuite.scala

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,49 @@ class BoundsSuite extends FunSuite {
6868
test("extract bounds") {
6969
bounds match {
7070
case Bounds(l, u) =>
71-
assert(l == -1)
72-
assert(u == 2)
73-
case _ => fail("")
71+
assert(l == -1D)
72+
assert(u == 2D)
7473
}
7574
}
75+
76+
test("constrain lower bound") {
77+
bounds.constrainLower(0).get match {
78+
case Bounds(v, _) => assert(v == 0D)
79+
}
80+
}
81+
82+
test("don't constrain lower bound") {
83+
bounds.constrainLower(-2).get match {
84+
case Bounds(v, _) => assert(v == -1D)
85+
}
86+
}
87+
88+
test("constrain upper bound") {
89+
println(bounds.constrainUpper(1))
90+
bounds.constrainUpper(1).get match {
91+
case Bounds(_, v) => assert(v == 1D)
92+
}
93+
}
94+
95+
test("don't constrain upper bound") {
96+
bounds.constrainUpper(3).get match {
97+
case Bounds(_, v) => assert(v == 2D)
98+
}
99+
}
100+
101+
test("lower constraint greater that upper bound") {
102+
assert(bounds.constrainLower(3).isEmpty)
103+
}
104+
105+
test("upper constraint lower that lower bound") {
106+
assert(bounds.constrainUpper(-2).isEmpty)
107+
}
108+
109+
test("Bound equality") {
110+
assert(bounds == Bounds.of(-1, 2).get)
111+
}
112+
113+
test("to string") {
114+
assert(Bounds.of('a', 'c').get.toString == "Bounds(a,c)")
115+
}
76116
}

0 commit comments

Comments
 (0)