-
-
Notifications
You must be signed in to change notification settings - Fork 42
/
RandomService.kt
259 lines (215 loc) · 9.22 KB
/
RandomService.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
package io.github.serpro69.kfaker
import com.ibm.icu.text.UnicodeSet
import com.ibm.icu.util.LocaleData
import com.ibm.icu.util.ULocale
import java.time.Duration
import java.time.Instant
import java.time.OffsetDateTime
import java.time.ZoneOffset
import java.util.*
import kotlin.experimental.and
import kotlin.experimental.or
import kotlin.random.asKotlinRandom
/**
* Wrapper around [Random] that also contains some additional functions not covered by [Random].
*
* If two instances of this [RandomService] are created with the same seed,
* and the same sequence of method calls is made for each,
* then they will generate and return identical sequences of values.
*
* Instances of [RandomService] are not cryptographically secure by default.
* Consider passing [java.security.SecureRandom] to the constructor of this [RandomService]
* to get a cryptographically secure pseudo-random generator.
*/
class RandomService internal constructor(override val config: FakerConfig) : IRandom {
private val random = config.random
private val alphabeticLowerCharset = ('a'..'z')
private val alphabeticUpperCharset = ('A'..'Z')
private val numericCharset = ('0'..'9')
override fun nextInt() = random.nextInt()
override fun nextInt(bound: Int) = random.nextInt(bound)
override fun nextInt(intRange: IntRange): Int {
val lowerBound = requireNotNull(intRange.minOrNull())
val upperBound = requireNotNull(intRange.maxOrNull())
return nextInt(lowerBound, upperBound)
}
override fun nextInt(min: Int, max: Int) = random.nextInt(max - min + 1) + min
override fun <T> randomValue(list: List<T>) = list[nextInt(list.size)]
override fun <T> randomValue(array: Array<T>) = array[nextInt(array.size)]
override fun nextLetter(upper: Boolean): Char {
val source = if (upper) alphabeticUpperCharset else alphabeticLowerCharset
return source.random(config.random.asKotlinRandom())
}
override fun randomString(length: Int, numericalChars: Boolean): String {
if (length < 1) return ""
val charset = if (numericalChars) {
alphabeticLowerCharset + alphabeticUpperCharset + numericCharset
} else alphabeticLowerCharset + alphabeticUpperCharset
return (1..length)
.map { charset.random(this.random.asKotlinRandom()) }
.joinToString("")
}
override fun nextBoolean() = random.nextBoolean()
override fun nextLong() = random.nextLong()
override fun nextLong(bound: Long): Long {
return if (bound > 0) {
var value: Long
do {
val bits = (nextLong().shl(1)).shr(1)
value = bits % bound
} while (bits - value + (bound - 1) < 0L)
value
} else throw IllegalArgumentException("Bound bound must be greater than 0")
}
override fun nextLong(longRange: LongRange): Long {
val lowerBound = requireNotNull(longRange.minOrNull())
val upperBound = requireNotNull(longRange.maxOrNull())
return nextLong(lowerBound, upperBound)
}
override fun nextLong(min: Long, max: Long) = nextLong(max - min + 1) + min
override fun nextFloat() = random.nextFloat()
override fun nextDouble() = random.nextDouble()
override fun nextChar() = nextInt().toChar()
override fun randomString(
length: Int,
locale: Locale,
indexChars: Boolean,
auxiliaryChars: Boolean,
punctuationChars: Boolean,
numericalChars: Boolean,
): String {
if (length < 1) return "" // base case
val localeData = LocaleData.getInstance(ULocale.forLocale(locale))
val mainChars = localeData.getExemplarSet(UnicodeSet.MIN_VALUE, LocaleData.ES_STANDARD)
.ranges()
.flatMap { (it.codepoint..it.codepointEnd).map { code -> Char(code) } }
val auxChars = if (auxiliaryChars) {
localeData.getExemplarSet(UnicodeSet.MIN_VALUE, LocaleData.ES_AUXILIARY)
.ranges()
.flatMap { (it.codepoint..it.codepointEnd).map { code -> Char(code) } }
} else emptyList()
val idxChars = if (indexChars) {
localeData.getExemplarSet(UnicodeSet.MIN_VALUE, LocaleData.ES_INDEX)
.ranges()
.flatMap { (it.codepoint..it.codepointEnd).map { code -> Char(code) } }
} else emptyList()
val punctChars = if (punctuationChars) {
localeData.getExemplarSet(UnicodeSet.MIN_VALUE, LocaleData.ES_PUNCTUATION)
.ranges()
.flatMap { (it.codepoint..it.codepointEnd).map { code -> Char(code) } }
} else emptyList()
val numChars = if (numericalChars) numericCharset else emptyList()
val chars = (mainChars + auxChars + idxChars + punctChars + numChars)
return List(length) { chars.random(random.asKotlinRandom()) }.joinToString("")
}
override fun randomString(
min: Int,
max: Int,
locale: Locale,
indexChars: Boolean,
auxiliaryChars: Boolean,
punctuationChars: Boolean,
numericalChars: Boolean,
): String {
val len = nextInt(min, max)
return randomString(
len,
locale,
indexChars = indexChars,
auxiliaryChars = auxiliaryChars,
punctuationChars = punctuationChars,
numericalChars = numericalChars,
)
}
/**
* Returns a randomly selected enum entry of type [E].
*/
inline fun <reified E : Enum<E>> nextEnum(): E {
val x: Int = nextInt(enumValues<E>().size)
return enumValues<E>()[x]
}
override fun <E : Enum<E>> nextEnum(enum: Class<E>): E {
return randomValue(enum.enumConstants)
}
override fun <E : Enum<E>> nextEnum(values: Array<E>): E {
return randomValue(values)
}
override tailrec fun <E : Enum<E>> nextEnum(enum: Class<E>, predicate: (E) -> Boolean): E {
val enumClass = nextEnum(enum)
return if (predicate(enumClass)) enumClass else nextEnum(enum, predicate)
}
/**
* Returns a randomly selected enum entry of type [E] excluding a particular enum class by its name.
*/
inline fun <reified E : Enum<E>> nextEnum(excludeName: String): E {
do {
val e: E = nextEnum()
if (e.name.lowercase() != excludeName.lowercase()) {
return e
}
} while (true)
}
override fun nextUUID(): String {
val randomBytes = ByteArray(16)
random.nextBytes(randomBytes)
randomBytes[6] = randomBytes[6] and 0x0f // clear version
randomBytes[6] = randomBytes[6] or 0x40 // set to version
randomBytes[8] = randomBytes[8] and 0x3f // clear variant
randomBytes[8] = randomBytes[8] or 0x80.toByte() // set to IETF variant
return UUID.nameUUIDFromBytes(randomBytes).toString()
}
override fun <T> randomSublist(list: List<T>, size: Int, shuffled: Boolean): List<T> {
val (from, to) = list.randomFromToIndices(size)
return list.ifEmpty { emptyList() }
.let { if (shuffled) it.shuffled(random) else it }
.subList(from, to)
}
override fun <T> randomSublist(list: List<T>, sizeRange: IntRange, shuffled: Boolean): List<T> {
return randomSublist(list, nextInt(sizeRange), shuffled)
}
override fun <T> randomSubset(set: Set<T>, size: Int, shuffled: Boolean): Set<T> {
val (from, to) = set.randomFromToIndices(size)
return set.ifEmpty { emptyList() }
.let { if (shuffled) it.shuffled(random) else it }
.mapIndexedNotNull { i, v -> if (i in from until to) v else null }
.toSet()
}
override fun <T> randomSubset(set: Set<T>, sizeRange: IntRange, shuffled: Boolean): Set<T> {
return randomSubset(set, nextInt(sizeRange), shuffled)
}
override fun randomPastDate(): OffsetDateTime {
return randomPastDate(Instant.ofEpochSecond(0))
}
override fun randomPastDate(min: Instant): OffsetDateTime {
val now = Instant.now()
require(min.isBefore(now))
return randomDate(min, now.minusMillis(1), ZoneOffset.UTC)
}
override fun randomFutureDate(): OffsetDateTime {
val maxInstant = Instant.now().plus(Duration.ofDays(50 * 365))
return randomFutureDate(maxInstant)
}
override fun randomFutureDate(max: Instant): OffsetDateTime {
val now = Instant.now()
require(max.isAfter(now))
return randomDate(now.plusMillis(1), max, ZoneOffset.UTC)
}
override fun randomDate(min: Instant, max: Instant, zoneOffset: ZoneOffset): OffsetDateTime {
val randomSeconds = nextLong(min.epochSecond, max.epochSecond)
val randomInstant = Instant.ofEpochSecond(randomSeconds)
return OffsetDateTime.ofInstant(randomInstant, zoneOffset)
}
private fun <T> Collection<T>.randomFromToIndices(s: Int): Pair<Int, Int> {
val fromIndex = if (s > 0) {
(0..size - s).random(random.asKotlinRandom())
} else {
(indices).random(random.asKotlinRandom())
}
val toIndex = if (s > 0) {
fromIndex + s
} else {
(fromIndex..size).random(random.asKotlinRandom())
}
return fromIndex to toIndex
}
}