Skip to content

Commit

Permalink
Moving a couple of classes from ODB (#915)
Browse files Browse the repository at this point in the history
  • Loading branch information
swalker2m authored Jul 4, 2024
1 parent f3df592 commit e1d61a1
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 0 deletions.
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")
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)
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
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)
}
}

}

0 comments on commit e1d61a1

Please sign in to comment.