Skip to content

Commit 30ab30e

Browse files
committed
Implement == for ValueBag
1 parent 766674a commit 30ab30e

File tree

4 files changed

+32
-1
lines changed

4 files changed

+32
-1
lines changed

core/common/src/format/LocalDateFormat.kt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,13 @@ internal class IncompleteLocalDate(
135135

136136
override fun copy(): IncompleteLocalDate = IncompleteLocalDate(year, monthNumber, dayOfMonth, isoDayOfWeek)
137137

138+
override fun equals(other: Any?): Boolean =
139+
other is IncompleteLocalDate && year == other.year && monthNumber == other.monthNumber &&
140+
dayOfMonth == other.dayOfMonth && isoDayOfWeek == other.isoDayOfWeek
141+
142+
override fun hashCode(): Int =
143+
year.hashCode() * 31 + monthNumber.hashCode() * 31 + dayOfMonth.hashCode() * 31 + isoDayOfWeek.hashCode() * 31
144+
138145
override fun toString(): String =
139146
"${year ?: "??"}-${monthNumber ?: "??"}-${dayOfMonth ?: "??"} (day of week is ${isoDayOfWeek ?: "??"})"
140147
}

core/common/src/format/LocalTimeFormat.kt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,13 @@ internal class IncompleteLocalTime(
234234

235235
override fun copy(): IncompleteLocalTime = IncompleteLocalTime(hour, isPm, minute, second, nanosecond)
236236

237+
override fun equals(other: Any?): Boolean =
238+
other is IncompleteLocalTime && hourField == other.hourField && minute == other.minute &&
239+
second == other.second && nanosecond == other.nanosecond
240+
241+
override fun hashCode(): Int =
242+
hourField.hashCode() * 31 + minute.hashCode() * 31 + second.hashCode() * 31 + nanosecond.hashCode()
243+
237244
override fun toString(): String =
238245
"${hour ?: "??"}:${minute ?: "??"}:${second ?: "??"}.${
239246
nanosecond?.let {

core/common/src/format/UtcOffsetFormat.kt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,13 @@ internal class IncompleteUtcOffset(
114114
) : UtcOffsetFieldContainer, Copyable<IncompleteUtcOffset> {
115115
fun toUtcOffset(): UtcOffset = UtcOffset(totalHours, minutesOfHour, secondsOfMinute)
116116

117+
override fun equals(other: Any?): Boolean =
118+
other is IncompleteUtcOffset && totalHours == other.totalHours &&
119+
minutesOfHour == other.minutesOfHour && secondsOfMinute == other.secondsOfMinute
120+
121+
override fun hashCode(): Int =
122+
totalHours.hashCode() * 31 + minutesOfHour.hashCode() * 31 + secondsOfMinute.hashCode()
123+
117124
override fun copy(): IncompleteUtcOffset = IncompleteUtcOffset(totalHours, minutesOfHour, secondsOfMinute)
118125
}
119126

core/common/src/format/ValueBagFormat.kt

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,10 @@ public class ValueBag internal constructor(internal val contents: ValueBagConten
222222
throw DateTimeFormatException("The parsed date is outside the range representable by Instant")
223223
return Instant.fromEpochSeconds(totalSeconds, nanosecond ?: 0)
224224
}
225+
226+
override fun equals(other: Any?): Boolean = other is ValueBag && contents == other.contents
227+
228+
override fun hashCode(): Int = contents.hashCode()
225229
}
226230

227231
/**
@@ -403,7 +407,13 @@ internal class ValueBagContents internal constructor(
403407
var timeZoneId: String? = null,
404408
) : DateFieldContainer by date, TimeFieldContainer by time, UtcOffsetFieldContainer by offset,
405409
Copyable<ValueBagContents> {
406-
override fun copy(): ValueBagContents = ValueBagContents(date.copy(), time.copy(), offset.copy(), timeZoneId)
410+
override fun copy(): ValueBagContents = ValueBagContents(date.copy(), time.copy(), offset.copy(), timeZoneId)
411+
412+
override fun equals(other: Any?): Boolean =
413+
other is ValueBagContents && other.date == date && other.time == time &&
414+
other.offset == offset && other.timeZoneId == timeZoneId
415+
override fun hashCode(): Int =
416+
date.hashCode() xor time.hashCode() xor offset.hashCode() xor (timeZoneId?.hashCode() ?: 0)
407417
}
408418

409419
internal val timeZoneField = GenericFieldSpec(ValueBagContents::timeZoneId)

0 commit comments

Comments
 (0)