5
5
6
6
package kotlinx.datetime
7
7
8
- import kotlinx.datetime.internal.clampToInt
9
- import kotlinx.datetime.internal.safeAdd
10
- import kotlinx.datetime.internal.safeMultiplyOrClamp
8
+ import kotlinx.datetime.internal.*
11
9
import kotlin.random.Random
12
- import kotlin.random.nextLong
13
10
14
11
private class YearMonthProgressionIterator (private val iterator : LongIterator ) : Iterator<YearMonth> {
15
12
override fun hasNext (): Boolean = iterator.hasNext()
@@ -67,7 +64,7 @@ internal constructor(internal val longProgression: LongProgression) : Collection
67
64
* Returns [Int.MAX_VALUE] if the number of months overflows [Int]
68
65
*/
69
66
override val size: Int
70
- get() = longProgression.size
67
+ get() = longProgression.sizeUnsafe
71
68
72
69
/* *
73
70
* Returns true iff every element in [elements] is a member of the progression.
@@ -82,7 +79,7 @@ internal constructor(internal val longProgression: LongProgression) : Collection
82
79
@Suppress(" USELESS_CAST" )
83
80
if ((value as Any? ) !is YearMonth ) return false
84
81
85
- return longProgression.contains (value.prolepticMonth)
82
+ return longProgression.containsUnsafe (value.prolepticMonth)
86
83
}
87
84
88
85
override fun equals (other : Any? ): Boolean =
@@ -267,7 +264,7 @@ public infix fun YearMonth.downTo(that: YearMonth): YearMonthProgression =
267
264
*/
268
265
public fun YearMonthProgression.random (random : Random = Random ): YearMonth =
269
266
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)
271
268
272
269
/* *
273
270
* 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 =
277
274
*
278
275
* @sample kotlinx.datetime.test.samples.YearMonthRangeSamples.random
279
276
*/
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