-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Perusopetuksen päällekkäisten opiskeluoikeuksien validointi
- Loading branch information
1 parent
06571e4
commit 98d589b
Showing
4 changed files
with
134 additions
and
0 deletions.
There are no files selected for viewing
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
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,18 @@ | ||
package fi.oph.koski.util | ||
|
||
import java.time.LocalDate | ||
import fi.oph.koski.util.ChainingSyntax._ | ||
|
||
case class DateRange( | ||
start: Option[LocalDate], | ||
end: Option[LocalDate], | ||
) { | ||
def contains(date: LocalDate): Boolean = | ||
date.isEqualOrAfter(start.getOrElse(LocalDate.MIN)) && date.isEqualOrBefore(end.getOrElse(LocalDate.MAX)) | ||
|
||
def intersects(other: DateRange): Boolean = | ||
other.start.exists(contains) || | ||
other.end.exists(contains) || | ||
start.exists(other.contains) || | ||
end.exists(other.contains) | ||
} |
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,80 @@ | ||
package fi.oph.koski.api.misc | ||
|
||
import fi.oph.koski.util.DateRange | ||
import org.scalatest.freespec.AnyFreeSpec | ||
import org.scalatest.matchers.should.Matchers._ | ||
|
||
import java.time.LocalDate | ||
|
||
class DateRangeSpec extends AnyFreeSpec { | ||
"DateRange" - { | ||
def closedRange(a: LocalDate, b: LocalDate) = DateRange(Some(a), Some(b)) | ||
def openEndedRange(a: LocalDate) = DateRange(Some(a), None) | ||
|
||
"Suljettu aikaväli vs suljettu aikaväli" - { | ||
"a ennen b -> eivät leikkaa" in { | ||
val a = closedRange(LocalDate.of(2000, 1, 1), LocalDate.of(2000, 12, 30)) | ||
val b = closedRange(LocalDate.of(2001, 1, 1), LocalDate.of(2001, 12, 30)) | ||
a.intersects(b) should equal (false) | ||
} | ||
|
||
"a jälkeen b -> eivät leikkaa" in { | ||
val a = closedRange(LocalDate.of(2001, 1, 1), LocalDate.of(2001, 12, 30)) | ||
val b = closedRange(LocalDate.of(2000, 1, 1), LocalDate.of(2000, 12, 30)) | ||
a.intersects(b) should equal(false) | ||
} | ||
|
||
"a ympäröi b -> leikkaavat" in { | ||
val a = closedRange(LocalDate.of(2000, 1, 1), LocalDate.of(2000, 12, 30)) | ||
val b = closedRange(LocalDate.of(2000, 2, 1), LocalDate.of(2000, 3, 30)) | ||
a.intersects(b) should equal(true) | ||
} | ||
|
||
"a ja b lomittain, a alkaa ennen b -> leikkaavat" in { | ||
val a = closedRange(LocalDate.of(2000, 1, 1), LocalDate.of(2000, 12, 30)) | ||
val b = closedRange(LocalDate.of(2000, 2, 1), LocalDate.of(2001, 3, 30)) | ||
a.intersects(b) should equal(true) | ||
} | ||
|
||
"a ja b lomittain, b alkaa ennen a -> leikkaavat" in { | ||
val a = closedRange(LocalDate.of(2000, 2, 1), LocalDate.of(2001, 3, 30)) | ||
val b = closedRange(LocalDate.of(2000, 1, 1), LocalDate.of(2000, 12, 30)) | ||
a.intersects(b) should equal(true) | ||
} | ||
} | ||
|
||
"Suljettu aikaväli vs puoliavoin aikaväli" - { | ||
"a ennen b -> eivät leikkaa" in { | ||
val a = closedRange(LocalDate.of(2000, 1, 1), LocalDate.of(2000, 12, 30)) | ||
val b = openEndedRange(LocalDate.of(2001, 1, 1)) | ||
a.intersects(b) should equal(false) | ||
} | ||
|
||
"a jälkeen b -> leikkaavat" in { | ||
val a = closedRange(LocalDate.of(2001, 1, 1), LocalDate.of(2001, 12, 30)) | ||
val b = openEndedRange(LocalDate.of(2000, 1, 1)) | ||
a.intersects(b) should equal(true) | ||
} | ||
|
||
"a ympäröi b -> leikkaavat" in { | ||
val a = closedRange(LocalDate.of(2000, 1, 1), LocalDate.of(2000, 12, 30)) | ||
val b = openEndedRange(LocalDate.of(2000, 2, 1)) | ||
a.intersects(b) should equal(true) | ||
} | ||
|
||
"a ja b lomittain, a alkaa ennen b -> leikkaavat" in { | ||
val a = closedRange(LocalDate.of(2000, 1, 1), LocalDate.of(2000, 12, 30)) | ||
val b = openEndedRange(LocalDate.of(2000, 2, 1)) | ||
a.intersects(b) should equal(true) | ||
} | ||
|
||
"a ja b lomittain, b alkaa ennen a -> leikkaavat" in { | ||
val a = closedRange(LocalDate.of(2000, 2, 1), LocalDate.of(2001, 3, 30)) | ||
val b = openEndedRange(LocalDate.of(2000, 1, 1)) | ||
a.intersects(b) should equal(true) | ||
} | ||
} | ||
|
||
} | ||
|
||
} |
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