Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rename Comparators to better align with Kotlin conventions #189

Merged
merged 1 commit into from
Jun 3, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions core/src/commonMain/kotlin/io/islandtime/OffsetDateTime.kt
Original file line number Diff line number Diff line change
Expand Up @@ -419,14 +419,28 @@ class OffsetDateTime(
* A [Comparator] that compares by instant, then date-time. Using this `Comparator` guarantees a deterministic
* order when sorting.
*/
val DEFAULT_SORT_ORDER: Comparator<OffsetDateTime> = compareBy<OffsetDateTime> { it.secondOfUnixEpoch }
val DefaultSortOrder: Comparator<OffsetDateTime> = compareBy<OffsetDateTime> { it.secondOfUnixEpoch }
.thenBy { it.nanosecond }
.thenBy { it.dateTime }

/**
* A [Comparator] that compares by timeline order only, ignoring any offset differences.
*/
val TIMELINE_ORDER: Comparator<TimePoint<*>> get() = TimePoint.TIMELINE_ORDER
val TimelineOrder: Comparator<TimePoint<*>> get() = TimePoint.TimelineOrder

@Deprecated(
message = "Replace with DefaultSortOrder",
replaceWith = ReplaceWith("this.DefaultSortOrder"),
level = DeprecationLevel.WARNING
)
val DEFAULT_SORT_ORDER: Comparator<OffsetDateTime> get() = DefaultSortOrder

@Deprecated(
message = "Replace with TimelineOrder",
replaceWith = ReplaceWith("this.TimelineOrder"),
level = DeprecationLevel.WARNING
)
val TIMELINE_ORDER: Comparator<TimePoint<*>> get() = TimelineOrder

/**
* Creates an [OffsetDateTime] from a duration of milliseconds relative to the Unix epoch at [offset].
Expand Down
28 changes: 21 additions & 7 deletions core/src/commonMain/kotlin/io/islandtime/OffsetTime.kt
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,8 @@ class OffsetTime(

/**
* Compares to another [OffsetTime] based on timeline order, ignoring offset differences.
* @see DEFAULT_SORT_ORDER
* @see TIMELINE_ORDER
* @see DefaultSortOrder
* @see TimelineOrder
*/
operator fun compareTo(other: OffsetTime): Int {
return nanosecondsSinceStartOfUtcDay.compareTo(other.nanosecondsSinceStartOfUtcDay)
Expand Down Expand Up @@ -202,16 +202,30 @@ class OffsetTime(
val MAX: OffsetTime = Time.MAX at UtcOffset.MIN

/**
* Compares by UTC equivalent instant, then time. Using this `Comparator` guarantees a deterministic order when
* sorting.
* A [Comparator] that compares by UTC equivalent instant, then time. Using this `Comparator` guarantees a
* deterministic order when sorting.
*/
val DEFAULT_SORT_ORDER: Comparator<OffsetTime> =
val DefaultSortOrder: Comparator<OffsetTime> =
compareBy<OffsetTime> { it.nanosecondsSinceStartOfUtcDay }.thenBy { it.time }

/**
* Compares by timeline order only, ignoring any offset differences.
* A [Comparator] that compares by timeline order only, ignoring any offset differences.
*/
val TIMELINE_ORDER: Comparator<OffsetTime> = compareBy { it.nanosecondsSinceStartOfUtcDay }
val TimelineOrder: Comparator<OffsetTime> = compareBy { it.nanosecondsSinceStartOfUtcDay }

@Deprecated(
message = "Replace with DefaultSortOrder",
replaceWith = ReplaceWith("this.DefaultSortOrder"),
level = DeprecationLevel.WARNING
)
val DEFAULT_SORT_ORDER: Comparator<OffsetTime> get() = DefaultSortOrder

@Deprecated(
message = "Replace with TimelineOrder",
replaceWith = ReplaceWith("this.TimelineOrder"),
level = DeprecationLevel.WARNING
)
val TIMELINE_ORDER: Comparator<OffsetTime> get() = TimelineOrder
}
}

Expand Down
18 changes: 16 additions & 2 deletions core/src/commonMain/kotlin/io/islandtime/ZonedDateTime.kt
Original file line number Diff line number Diff line change
Expand Up @@ -485,15 +485,29 @@ class ZonedDateTime private constructor(
* A [Comparator] that compares by instant, then date-time, then time zone. Using this `Comparator` guarantees a
* deterministic order when sorting.
*/
val DEFAULT_SORT_ORDER: Comparator<ZonedDateTime> = compareBy<ZonedDateTime> { it.secondOfUnixEpoch }
val DefaultSortOrder: Comparator<ZonedDateTime> = compareBy<ZonedDateTime> { it.secondOfUnixEpoch }
.thenBy { it.nanosecond }
.thenBy { it.dateTime }
.thenBy { it.zone }

/**
* A [Comparator] that compares by timeline order only, ignoring any offset or time zone differences.
*/
val TIMELINE_ORDER: Comparator<TimePoint<*>> get() = TimePoint.TIMELINE_ORDER
val TimelineOrder: Comparator<TimePoint<*>> get() = TimePoint.TimelineOrder

@Deprecated(
message = "Replace with DefaultSortOrder",
replaceWith = ReplaceWith("this.DefaultSortOrder"),
level = DeprecationLevel.WARNING
)
val DEFAULT_SORT_ORDER: Comparator<ZonedDateTime> get() = DefaultSortOrder

@Deprecated(
message = "Replace with TimelineOrder",
replaceWith = ReplaceWith("this.TimelineOrder"),
level = DeprecationLevel.WARNING
)
val TIMELINE_ORDER: Comparator<TimePoint<*>> get() = TimelineOrder

/**
* Creates a [ZonedDateTime] from a local date and time, optionally using a preferred offset. If the local date
Expand Down
9 changes: 8 additions & 1 deletion core/src/commonMain/kotlin/io/islandtime/base/TimePoint.kt
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,14 @@ interface TimePoint<T> {
/**
* A [Comparator] that compares by timeline order.
*/
val TIMELINE_ORDER: Comparator<TimePoint<*>> =
val TimelineOrder: Comparator<TimePoint<*>> =
compareBy<TimePoint<*>> { it.secondOfUnixEpoch }.thenBy { it.nanosecond }

@Deprecated(
message = "Replace with TimelineOrder",
replaceWith = ReplaceWith("this.TimelineOrder"),
level = DeprecationLevel.WARNING
)
val TIMELINE_ORDER: Comparator<TimePoint<*>> get() = TimelineOrder
}
}
10 changes: 5 additions & 5 deletions core/src/commonTest/kotlin/io/islandtime/OffsetDateTimeTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -133,26 +133,26 @@ class OffsetDateTimeTest : AbstractIslandTimeTest() {
}

@Test
fun `DEFAULT_SORT_ORDER compares based on instant, then date and time when there are differing offsets`() {
fun `DefaultSortOrder compares based on instant, then date and time when there are differing offsets`() {
assertTrue {
OffsetDateTime.DEFAULT_SORT_ORDER.compare(
OffsetDateTime.DefaultSortOrder.compare(
Date(1969, 365) at Time(23, 0) at UtcOffset((-1).hours),
Date(1970, 1) at Time(0, 0) at UtcOffset.ZERO
) < 0
}

assertTrue {
OffsetDateTime.DEFAULT_SORT_ORDER.compare(
OffsetDateTime.DefaultSortOrder.compare(
Date(1969, 365) at Time(23, 0) at UtcOffset((-1).hours),
Date(1969, 365) at Time(23, 0) at UtcOffset((-1).hours)
) == 0
}
}

@Test
fun `TIMELINE_ORDER compares based on instant only`() {
fun `TimelineOrder compares based on instant only`() {
assertTrue {
OffsetDateTime.TIMELINE_ORDER.compare(
OffsetDateTime.TimelineOrder.compare(
Date(1969, 365) at Time(23, 0) at UtcOffset((-1).hours),
Date(1970, 1) at Time(0, 0) at UtcOffset.ZERO
) == 0
Expand Down
22 changes: 11 additions & 11 deletions core/src/commonTest/kotlin/io/islandtime/OffsetTimeTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -28,63 +28,63 @@ class OffsetTimeTest : AbstractIslandTimeTest() {
}

@Test
fun `TIMELINE_ORDER compares based on instant only`() {
fun `TimelineOrder compares based on instant only`() {
assertTrue {
OffsetTime.TIMELINE_ORDER.compare(
OffsetTime.TimelineOrder.compare(
Time.MIN at UtcOffset.ZERO,
Time(0, 0, 0, 1) at UtcOffset.ZERO
) < 0
}
assertTrue {
OffsetTime.TIMELINE_ORDER.compare(
OffsetTime.TimelineOrder.compare(
Time.MAX at UtcOffset.MIN,
Time(23, 59, 59) at UtcOffset.MIN
) > 0
}
assertTrue {
OffsetTime.TIMELINE_ORDER.compare(
OffsetTime.TimelineOrder.compare(
Time(1, 0) at UtcOffset((-1).hours),
Time(2, 0) at UtcOffset.ZERO
) == 0
}
assertTrue {
OffsetTime.TIMELINE_ORDER.compare(
OffsetTime.TimelineOrder.compare(
Time(1, 0, 0, 1) at UtcOffset((-1).hours),
Time(2, 0) at UtcOffset.ZERO
) > 0
}
assertTrue {
OffsetTime.TIMELINE_ORDER.compare(
OffsetTime.TimelineOrder.compare(
Time(2, 0) at UtcOffset(1.hours),
Time(3, 0) at UtcOffset.ZERO
) < 0
}
assertTrue {
OffsetTime.TIMELINE_ORDER.compare(
OffsetTime.TimelineOrder.compare(
Time(1, 0) at UtcOffset((-1).hours),
Time(0, 0) at UtcOffset.ZERO
) > 0
}
}

@Test
fun `DEFAULT_SORT_ORDER compares based on instant, then time when there are differing offsets`() {
fun `DefaultSortOrder compares based on instant, then time when there are differing offsets`() {
assertTrue {
OffsetTime.DEFAULT_SORT_ORDER.compare(
OffsetTime.DefaultSortOrder.compare(
Time(1, 0, 0, 1) at UtcOffset((-1).hours),
Time(2, 0) at UtcOffset.ZERO
) > 0
}

assertTrue {
OffsetTime.DEFAULT_SORT_ORDER.compare(
OffsetTime.DefaultSortOrder.compare(
Time(2, 0) at UtcOffset(1.hours),
Time(3, 0) at UtcOffset.ZERO
) < 0
}

assertTrue {
OffsetTime.DEFAULT_SORT_ORDER.compare(
OffsetTime.DefaultSortOrder.compare(
Time(1, 0) at UtcOffset((-1).hours),
Time(0, 0) at UtcOffset.ZERO
) > 0
Expand Down
12 changes: 6 additions & 6 deletions core/src/commonTest/kotlin/io/islandtime/ZonedDateTimeTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -211,33 +211,33 @@ class ZonedDateTimeTest : AbstractIslandTimeTest() {
}

@Test
fun `DEFAULT_SORT_ORDER compares based on instant, then date and time, and then zone`() {
fun `DefaultSortOrder compares based on instant, then date and time, and then zone`() {
assertTrue {
ZonedDateTime.DEFAULT_SORT_ORDER.compare(
ZonedDateTime.DefaultSortOrder.compare(
Date(1969, 365) at Time(23, 0) at TimeZone("America/Chicago"),
Date(1970, 1) at Time(0, 0) at nyZone
) < 0
}

assertTrue {
ZonedDateTime.DEFAULT_SORT_ORDER.compare(
ZonedDateTime.DefaultSortOrder.compare(
Date(1970, 1) at Time(0, 0) at TimeZone("Etc/GMT+5"),
Date(1970, 1) at Time(0, 0) at nyZone
) > 0
}

assertTrue {
ZonedDateTime.DEFAULT_SORT_ORDER.compare(
ZonedDateTime.DefaultSortOrder.compare(
Date(1969, 365) at Time(23, 0) at TimeZone("Etc/GMT+5"),
Date(1969, 365) at Time(23, 0) at TimeZone("Etc/GMT+5")
) == 0
}
}

@Test
fun `TIMELINE_ORDER compare based on instant only`() {
fun `TimelineOrder compares based on instant only`() {
assertTrue {
ZonedDateTime.TIMELINE_ORDER.compare(
ZonedDateTime.TimelineOrder.compare(
Date(1969, 365) at Time(23, 0) at UtcOffset((-1).hours),
Date(1970, 1) at Time(0, 0) at UtcOffset.ZERO
) == 0
Expand Down