55
66package kotlinx.datetime
77
8- import kotlinx.datetime.internal.clampToInt
9- import kotlinx.datetime.internal.safeAdd
10- import kotlinx.datetime.internal.safeMultiplyOrClamp
8+ import kotlinx.datetime.internal.*
119import kotlin.random.Random
12- import kotlin.random.nextLong
1310
1411private class YearMonthProgressionIterator (private val iterator : LongIterator ) : Iterator<YearMonth> {
1512 override fun hasNext (): Boolean = iterator.hasNext()
@@ -67,7 +64,7 @@ internal constructor(internal val longProgression: LongProgression) : Collection
6764 * Returns [Int.MAX_VALUE] if the number of months overflows [Int]
6865 */
6966 override val size: Int
70- get() = longProgression.size
67+ get() = longProgression.sizeUnsafe
7168
7269 /* *
7370 * Returns true iff every element in [elements] is a member of the progression.
@@ -82,7 +79,7 @@ internal constructor(internal val longProgression: LongProgression) : Collection
8279 @Suppress(" USELESS_CAST" )
8380 if ((value as Any? ) !is YearMonth ) return false
8481
85- return longProgression.contains (value.prolepticMonth)
82+ return longProgression.containsUnsafe (value.prolepticMonth)
8683 }
8784
8885 override fun equals (other : Any? ): Boolean =
@@ -267,7 +264,7 @@ public infix fun YearMonth.downTo(that: YearMonth): YearMonthProgression =
267264 */
268265public fun YearMonthProgression.random (random : Random = Random ): YearMonth =
269266 if (isEmpty()) throw NoSuchElementException (" Cannot get random in empty range: $this " )
270- else longProgression.random (random).let (YearMonth .Companion ::fromProlepticMonth)
267+ else longProgression.randomUnsafe (random).let (YearMonth .Companion ::fromProlepticMonth)
271268
272269/* *
273270 * Returns a random [YearMonth] within the bounds of the [YearMonthProgression] or null if the progression is empty.
@@ -277,29 +274,5 @@ public fun YearMonthProgression.random(random: Random = Random): YearMonth =
277274 *
278275 * @sample kotlinx.datetime.test.samples.YearMonthRangeSamples.random
279276 */
280- public fun YearMonthProgression.randomOrNull (random : Random = Random ): YearMonth ? = longProgression.randomOrNull(random)
281- ?.let (YearMonth .Companion ::fromProlepticMonth)
282-
283- // this implementation is incorrect in general
284- // (for example, `(Long.MIN_VALUE..Long.MAX_VALUE).random()` throws an exception),
285- // but for the range of epoch days in YearMonth it's good enough
286- private fun LongProgression.random (random : Random = Random ): Long =
287- random.nextLong(0L .. (last - first) / step) * step + first
288-
289- // incorrect in general; see `random` just above
290- private fun LongProgression.randomOrNull (random : Random = Random ): Long? = if (isEmpty()) null else random(random)
291-
292- // this implementation is incorrect in general (for example, `(Long.MIN_VALUE..Long.MAX_VALUE).step(5).contains(2)`
293- // returns `false` incorrectly https://www.wolframalpha.com/input?i=-2%5E63+%2B+1844674407370955162+*+5),
294- // but for the range of epoch days in YearMonth it's good enough
295- private fun LongProgression.contains (value : Long ): Boolean =
296- value in (if (step > 0 ) first.. last else last.. first) && (value - first) % step == 0L
297-
298- // this implementation is incorrect in general (for example, `Long.MIN_VALUE..Long.MAX_VALUE` has size == 0),
299- // but for the range of epoch days in YearMonth it's good enough
300- private val LongProgression .size: Int
301- get() = if (isEmpty()) 0 else try {
302- (safeAdd(last, - first) / step + 1 ).clampToInt()
303- } catch (e: ArithmeticException ) {
304- Int .MAX_VALUE
305- }
277+ public fun YearMonthProgression.randomOrNull (random : Random = Random ): YearMonth ? = longProgression.randomUnsafeOrNull(random)
278+ ?.let (YearMonth .Companion ::fromProlepticMonth)
0 commit comments