-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Moving a couple of classes from ODB (#915)
- Loading branch information
Showing
4 changed files
with
106 additions
and
0 deletions.
There are no files selected for viewing
12 changes: 12 additions & 0 deletions
12
modules/core/shared/src/main/scala/lucuma/core/enums/ScienceBand.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,12 @@ | ||
// Copyright (c) 2016-2023 Association of Universities for Research in Astronomy, Inc. (AURA) | ||
// For license information see LICENSE or https://opensource.org/licenses/BSD-3-Clause | ||
|
||
package lucuma.core.enums | ||
|
||
import lucuma.core.util.Enumerated | ||
|
||
enum ScienceBand(val tag: String) derives Enumerated: | ||
case Band1 extends ScienceBand("band1") | ||
case Band2 extends ScienceBand("band2") | ||
case Band3 extends ScienceBand("band3") | ||
case Band4 extends ScienceBand("band4") |
44 changes: 44 additions & 0 deletions
44
modules/core/shared/src/main/scala/lucuma/core/util/DateInterval.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,44 @@ | ||
// Copyright (c) 2016-2023 Association of Universities for Research in Astronomy, Inc. (AURA) | ||
// For license information see LICENSE or https://opensource.org/licenses/BSD-3-Clause | ||
|
||
package lucuma.core.util | ||
|
||
import cats.Eq | ||
import cats.syntax.order.* | ||
import org.typelevel.cats.time.* | ||
|
||
import java.time.LocalDate | ||
import java.time.Period | ||
import java.time.temporal.ChronoUnit | ||
|
||
/** | ||
* Interval between a start date (inclusive) and an end date (exclusive). | ||
*/ | ||
sealed class DateInterval private (val start: LocalDate, val end: LocalDate): | ||
assert(start <= end, s"start date ($start) must be <= end date ($end)") | ||
|
||
def isEmpty: Boolean = | ||
start === end | ||
|
||
def isNonEmpty: Boolean = | ||
!isEmpty | ||
|
||
def period: Period = | ||
start.until(end) | ||
|
||
def days: Long = | ||
ChronoUnit.DAYS.between(start, end) | ||
|
||
object DateInterval: | ||
|
||
given Eq[DateInterval] = | ||
Eq.by(a => (a.start, a.end)) | ||
|
||
/** | ||
* Creates an interval between the two dates, swapping the order if | ||
* necessary. If d0 comes before d1, the interval is [d0, d1). Otherwise | ||
* it is [d1, d0). The start date (the lesser of the two inputs) is | ||
* inclusive while the end date is exclusive. | ||
*/ | ||
def between(d0: LocalDate, d1: LocalDate): DateInterval = | ||
if (d0 <= d1) new DateInterval(d0, d1) else new DateInterval(d1, d0) |
27 changes: 27 additions & 0 deletions
27
modules/testkit/src/main/scala/lucuma/core/util/arb/ArbDateInterval.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,27 @@ | ||
// Copyright (c) 2016-2023 Association of Universities for Research in Astronomy, Inc. (AURA) | ||
// For license information see LICENSE or https://opensource.org/licenses/BSD-3-Clause | ||
|
||
package lucuma.core.util | ||
package arb | ||
|
||
import org.scalacheck.* | ||
import org.scalacheck.Arbitrary.arbitrary | ||
|
||
import java.time.LocalDate | ||
|
||
trait ArbDateInterval { | ||
|
||
given Arbitrary[DateInterval] = | ||
Arbitrary { | ||
for { | ||
d0 <- arbitrary[LocalDate] | ||
d1 <- arbitrary[LocalDate] | ||
} yield DateInterval.between(d0, d1) | ||
} | ||
|
||
given Cogen[DateInterval] = | ||
Cogen[(LocalDate, LocalDate)].contramap { a => (a.start, a.end) } | ||
|
||
} | ||
|
||
object ArbDateInterval extends ArbDateInterval |
23 changes: 23 additions & 0 deletions
23
modules/tests/shared/src/test/scala/lucuma/core/util/DateIntervalSuite.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,23 @@ | ||
// Copyright (c) 2016-2023 Association of Universities for Research in Astronomy, Inc. (AURA) | ||
// For license information see LICENSE or https://opensource.org/licenses/BSD-3-Clause | ||
|
||
package lucuma.core.util | ||
|
||
import cats.kernel.laws.discipline.* | ||
import lucuma.core.util.arb.ArbDateInterval | ||
import munit.DisciplineSuite | ||
import org.scalacheck.Prop.forAll | ||
|
||
class DateIntervalSuite extends DisciplineSuite { | ||
|
||
import ArbDateInterval.given | ||
|
||
checkAll("Eq", EqTests[DateInterval].eqv) | ||
|
||
test("period in days") { | ||
forAll { (interval: DateInterval) => | ||
assertEquals(interval.start.plusDays(interval.days), interval.end) | ||
} | ||
} | ||
|
||
} |