Skip to content

Commit

Permalink
Merge pull request EventFahrplan#605 from EventFahrplan/moment-class-…
Browse files Browse the repository at this point in the history
…adoption

Refactor ConferenceTimeFrame to use closed range internally.
  • Loading branch information
johnjohndoe authored Jan 13, 2024
2 parents 5903404 + a51b7e5 commit ad6de6b
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -1,35 +1,49 @@
package nerd.tuxmobil.fahrplan.congress.utils

import info.metadude.android.eventfahrplan.commons.temporal.Moment

import nerd.tuxmobil.fahrplan.congress.alarms.AlarmUpdater
import nerd.tuxmobil.fahrplan.congress.schedule.Conference

/**
* Represents the time frame of a conference which is taken into account by the [AlarmUpdater]
* to calculate if and when alarms should fire.
*
* This class should not be used for other purposes.
* It is not a replacement for the [Conference] class.
*/
// TODO Merge with Conference class?
class ConferenceTimeFrame(

val firstDayStartTime: Moment,
private val lastDayEndTime: Moment
firstDayStartTime: Moment,
lastDayEndTime: Moment

) {

private val timeFrame = firstDayStartTime..lastDayEndTime

init {
check(isValid) { "Invalid conference time frame: $this" }
}

val firstDayStartTime: Moment
get() = timeFrame.start

val isValid: Boolean
get() = firstDayStartTime.isBefore(lastDayEndTime)
get() = timeFrame.start.isBefore(timeFrame.endInclusive)

operator fun contains(moment: Moment) =
startsAtOrBefore(moment) && lastDayEndTime.isAfter(moment)
startsAtOrBefore(moment) && timeFrame.endInclusive.isAfter(moment)

fun endsAtOrBefore(moment: Moment) =
lastDayEndTime.isSimultaneousWith(moment) || lastDayEndTime.isBefore(moment)
timeFrame.endInclusive.isSimultaneousWith(moment) || timeFrame.endInclusive.isBefore(moment)

fun startsAfter(moment: Moment) =
firstDayStartTime.isAfter(moment)
timeFrame.start.isAfter(moment)

fun startsAtOrBefore(moment: Moment) =
firstDayStartTime.isSimultaneousWith(moment) || firstDayStartTime.isBefore(moment)
timeFrame.start.isSimultaneousWith(moment) || timeFrame.start.isBefore(moment)

override fun toString() =
"firstDayStartTime = $firstDayStartTime, lastDayEndTime = $lastDayEndTime"
timeFrame.toString()

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import org.threeten.bp.temporal.ChronoUnit
*
* > Moment.now().toZonedDateTime(ZoneOffset.of("GMT+1"))
*/
class Moment private constructor(private val time: Instant) {
class Moment private constructor(private val time: Instant): Comparable<Moment> {

val year: Int
get() = time.atZone(ZoneOffset.UTC).year
Expand Down Expand Up @@ -137,6 +137,10 @@ class Moment private constructor(private val time: Instant) {
return Duration.between(time, moment.time).toMinutes()
}

override fun compareTo(other: Moment): Int {
return time.compareTo(other.time)
}

override fun equals(other: Any?): Boolean {
return time == (other as? Moment)?.time
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,16 @@ class MomentTest {
assertThat(momentOne.hashCode()).isNotEqualTo(momentTwo.hashCode())
}

@Test
fun `compareTo returns the correct integer value when comparing moments`() {
val momentOne = Moment.ofEpochMilli(DEC_30_22_47_2019)
val momentTwo = Moment.ofEpochMilli(DEC_30_22_47_2019).plusSeconds(1)

assertThat(momentOne.compareTo(momentTwo)).isEqualTo(-1)
assertThat(momentOne.compareTo(momentTwo.minusSeconds(1))).isEqualTo(0)
assertThat(momentTwo.compareTo(momentOne)).isEqualTo(1)
}

@Test
fun `toString returns toString representation of internal instant`() {
val moment = Moment.ofEpochMilli(DEC_30_22_47_2019)
Expand Down

0 comments on commit ad6de6b

Please sign in to comment.