From 21554ea3ecff07f0f0bdd6f11934347c55aef70b Mon Sep 17 00:00:00 2001 From: Erik Christensen <40830816+erikc5000@users.noreply.github.com> Date: Thu, 3 Jun 2021 14:32:30 -0400 Subject: [PATCH] Rename Comparators to better align with Kotlin conventions (#189) --- .../kotlin/io/islandtime/OffsetDateTime.kt | 18 ++++++++++-- .../kotlin/io/islandtime/OffsetTime.kt | 28 ++++++++++++++----- .../kotlin/io/islandtime/ZonedDateTime.kt | 18 ++++++++++-- .../kotlin/io/islandtime/base/TimePoint.kt | 9 +++++- .../io/islandtime/OffsetDateTimeTest.kt | 10 +++---- .../kotlin/io/islandtime/OffsetTimeTest.kt | 22 +++++++-------- .../kotlin/io/islandtime/ZonedDateTimeTest.kt | 12 ++++---- 7 files changed, 83 insertions(+), 34 deletions(-) diff --git a/core/src/commonMain/kotlin/io/islandtime/OffsetDateTime.kt b/core/src/commonMain/kotlin/io/islandtime/OffsetDateTime.kt index 0f6b85f17..92260b049 100644 --- a/core/src/commonMain/kotlin/io/islandtime/OffsetDateTime.kt +++ b/core/src/commonMain/kotlin/io/islandtime/OffsetDateTime.kt @@ -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 = compareBy { it.secondOfUnixEpoch } + val DefaultSortOrder: Comparator = compareBy { it.secondOfUnixEpoch } .thenBy { it.nanosecond } .thenBy { it.dateTime } /** * A [Comparator] that compares by timeline order only, ignoring any offset differences. */ - val TIMELINE_ORDER: Comparator> get() = TimePoint.TIMELINE_ORDER + val TimelineOrder: Comparator> get() = TimePoint.TimelineOrder + + @Deprecated( + message = "Replace with DefaultSortOrder", + replaceWith = ReplaceWith("this.DefaultSortOrder"), + level = DeprecationLevel.WARNING + ) + val DEFAULT_SORT_ORDER: Comparator get() = DefaultSortOrder + + @Deprecated( + message = "Replace with TimelineOrder", + replaceWith = ReplaceWith("this.TimelineOrder"), + level = DeprecationLevel.WARNING + ) + val TIMELINE_ORDER: Comparator> get() = TimelineOrder /** * Creates an [OffsetDateTime] from a duration of milliseconds relative to the Unix epoch at [offset]. diff --git a/core/src/commonMain/kotlin/io/islandtime/OffsetTime.kt b/core/src/commonMain/kotlin/io/islandtime/OffsetTime.kt index c18f5971b..c6e8659a2 100644 --- a/core/src/commonMain/kotlin/io/islandtime/OffsetTime.kt +++ b/core/src/commonMain/kotlin/io/islandtime/OffsetTime.kt @@ -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) @@ -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 = + val DefaultSortOrder: Comparator = compareBy { 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 = compareBy { it.nanosecondsSinceStartOfUtcDay } + val TimelineOrder: Comparator = compareBy { it.nanosecondsSinceStartOfUtcDay } + + @Deprecated( + message = "Replace with DefaultSortOrder", + replaceWith = ReplaceWith("this.DefaultSortOrder"), + level = DeprecationLevel.WARNING + ) + val DEFAULT_SORT_ORDER: Comparator get() = DefaultSortOrder + + @Deprecated( + message = "Replace with TimelineOrder", + replaceWith = ReplaceWith("this.TimelineOrder"), + level = DeprecationLevel.WARNING + ) + val TIMELINE_ORDER: Comparator get() = TimelineOrder } } diff --git a/core/src/commonMain/kotlin/io/islandtime/ZonedDateTime.kt b/core/src/commonMain/kotlin/io/islandtime/ZonedDateTime.kt index a80e1267d..7da3ae3fa 100644 --- a/core/src/commonMain/kotlin/io/islandtime/ZonedDateTime.kt +++ b/core/src/commonMain/kotlin/io/islandtime/ZonedDateTime.kt @@ -485,7 +485,7 @@ 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 = compareBy { it.secondOfUnixEpoch } + val DefaultSortOrder: Comparator = compareBy { it.secondOfUnixEpoch } .thenBy { it.nanosecond } .thenBy { it.dateTime } .thenBy { it.zone } @@ -493,7 +493,21 @@ class ZonedDateTime private constructor( /** * A [Comparator] that compares by timeline order only, ignoring any offset or time zone differences. */ - val TIMELINE_ORDER: Comparator> get() = TimePoint.TIMELINE_ORDER + val TimelineOrder: Comparator> get() = TimePoint.TimelineOrder + + @Deprecated( + message = "Replace with DefaultSortOrder", + replaceWith = ReplaceWith("this.DefaultSortOrder"), + level = DeprecationLevel.WARNING + ) + val DEFAULT_SORT_ORDER: Comparator get() = DefaultSortOrder + + @Deprecated( + message = "Replace with TimelineOrder", + replaceWith = ReplaceWith("this.TimelineOrder"), + level = DeprecationLevel.WARNING + ) + val TIMELINE_ORDER: Comparator> get() = TimelineOrder /** * Creates a [ZonedDateTime] from a local date and time, optionally using a preferred offset. If the local date diff --git a/core/src/commonMain/kotlin/io/islandtime/base/TimePoint.kt b/core/src/commonMain/kotlin/io/islandtime/base/TimePoint.kt index 3da54c852..4278da66f 100644 --- a/core/src/commonMain/kotlin/io/islandtime/base/TimePoint.kt +++ b/core/src/commonMain/kotlin/io/islandtime/base/TimePoint.kt @@ -107,7 +107,14 @@ interface TimePoint { /** * A [Comparator] that compares by timeline order. */ - val TIMELINE_ORDER: Comparator> = + val TimelineOrder: Comparator> = compareBy> { it.secondOfUnixEpoch }.thenBy { it.nanosecond } + + @Deprecated( + message = "Replace with TimelineOrder", + replaceWith = ReplaceWith("this.TimelineOrder"), + level = DeprecationLevel.WARNING + ) + val TIMELINE_ORDER: Comparator> get() = TimelineOrder } } diff --git a/core/src/commonTest/kotlin/io/islandtime/OffsetDateTimeTest.kt b/core/src/commonTest/kotlin/io/islandtime/OffsetDateTimeTest.kt index 9bcb30198..8f41955ed 100644 --- a/core/src/commonTest/kotlin/io/islandtime/OffsetDateTimeTest.kt +++ b/core/src/commonTest/kotlin/io/islandtime/OffsetDateTimeTest.kt @@ -133,16 +133,16 @@ 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 @@ -150,9 +150,9 @@ class OffsetDateTimeTest : AbstractIslandTimeTest() { } @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 diff --git a/core/src/commonTest/kotlin/io/islandtime/OffsetTimeTest.kt b/core/src/commonTest/kotlin/io/islandtime/OffsetTimeTest.kt index db55c0b03..429c6dac4 100644 --- a/core/src/commonTest/kotlin/io/islandtime/OffsetTimeTest.kt +++ b/core/src/commonTest/kotlin/io/islandtime/OffsetTimeTest.kt @@ -28,39 +28,39 @@ 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 @@ -68,23 +68,23 @@ class OffsetTimeTest : AbstractIslandTimeTest() { } @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 diff --git a/core/src/commonTest/kotlin/io/islandtime/ZonedDateTimeTest.kt b/core/src/commonTest/kotlin/io/islandtime/ZonedDateTimeTest.kt index c161f9236..619f692df 100644 --- a/core/src/commonTest/kotlin/io/islandtime/ZonedDateTimeTest.kt +++ b/core/src/commonTest/kotlin/io/islandtime/ZonedDateTimeTest.kt @@ -211,23 +211,23 @@ 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 @@ -235,9 +235,9 @@ class ZonedDateTimeTest : AbstractIslandTimeTest() { } @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