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 LocalDateProgressionIterator (private val iterator : LongIterator ) : Iterator<LocalDate> {
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 dates 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 LocalDate ) return false
8481
85- return longProgression.contains (value.toEpochDays())
82+ return longProgression.containsUnsafe (value.toEpochDays())
8683 }
8784
8885 override fun equals (other : Any? ): Boolean =
@@ -261,13 +258,13 @@ public infix fun LocalDate.downTo(that: LocalDate): LocalDateProgression =
261258 * Takes the step into account;
262259 * will not return any value within the range that would be skipped over by the progression.
263260 *
264- * @throws IllegalArgumentException if the progression is empty.
261+ * @throws NoSuchElementException if the progression is empty.
265262 *
266263 * @sample kotlinx.datetime.test.samples.LocalDateRangeSamples.random
267264 */
268265public fun LocalDateProgression.random (random : Random = Random ): LocalDate =
269266 if (isEmpty()) throw NoSuchElementException (" Cannot get random in empty range: $this " )
270- else longProgression.random (random).let (LocalDate .Companion ::fromEpochDays)
267+ else longProgression.randomUnsafe (random).let (LocalDate .Companion ::fromEpochDays)
271268
272269/* *
273270 * Returns a random [LocalDate] within the bounds of the [LocalDateProgression] or null if the progression is empty.
@@ -277,29 +274,5 @@ public fun LocalDateProgression.random(random: Random = Random): LocalDate =
277274 *
278275 * @sample kotlinx.datetime.test.samples.LocalDateRangeSamples.random
279276 */
280- public fun LocalDateProgression.randomOrNull (random : Random = Random ): LocalDate ? = longProgression.randomOrNull(random)
281- ?.let (LocalDate .Companion ::fromEpochDays)
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 LocalDate 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 LocalDate 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 LocalDate 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 LocalDateProgression.randomOrNull (random : Random = Random ): LocalDate ? = longProgression.randomUnsafeOrNull(random)
278+ ?.let (LocalDate .Companion ::fromEpochDays)
0 commit comments