1
|
|
|
/* |
2
|
|
|
* This file is part of Araknemu. |
3
|
|
|
* |
4
|
|
|
* Araknemu is free software: you can redistribute it and/or modify |
5
|
|
|
* it under the terms of the GNU Lesser General Public License as published by |
6
|
|
|
* the Free Software Foundation, either version 3 of the License, or |
7
|
|
|
* (at your option) any later version. |
8
|
|
|
* |
9
|
|
|
* Araknemu is distributed in the hope that it will be useful, |
10
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
11
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12
|
|
|
* GNU Lesser General Public License for more details. |
13
|
|
|
* |
14
|
|
|
* You should have received a copy of the GNU Lesser General Public License |
15
|
|
|
* along with Araknemu. If not, see <https://www.gnu.org/licenses/>. |
16
|
|
|
* |
17
|
|
|
* Copyright (c) 2017-2019 Vincent Quatrevieux |
18
|
|
|
*/ |
19
|
|
|
|
20
|
|
|
package fr.quatrevieux.araknemu.util; |
21
|
|
|
|
22
|
|
|
import org.checkerframework.checker.index.qual.NonNegative; |
23
|
|
|
import org.checkerframework.common.value.qual.MinLen; |
24
|
|
|
|
25
|
|
|
import java.util.Random; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Random generation for strings |
29
|
|
|
*/ |
30
|
|
|
public final class RandomStringUtil { |
31
|
|
|
private final Random random; |
32
|
|
|
private final @MinLen(1) String charset; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Create instance |
36
|
|
|
* @param random The random generator instance |
37
|
|
|
* @param charset The charset to use |
38
|
|
|
*/ |
39
|
1 |
|
public RandomStringUtil(Random random, @MinLen(1) String charset) { |
40
|
1 |
|
this.random = random; |
41
|
1 |
|
this.charset = charset; |
42
|
1 |
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Generate the random string |
46
|
|
|
* |
47
|
|
|
* @param length The required string length |
48
|
|
|
*/ |
49
|
|
|
public String generate(@NonNegative int length) { |
50
|
1 |
|
final char[] buffer = new char[length]; |
51
|
|
|
|
52
|
1 |
|
for (int i = 0; i < length; ++i) { |
53
|
1 |
|
buffer[i] = charset.charAt(random.nextInt(charset.length())); |
54
|
|
|
} |
55
|
|
|
|
56
|
1 |
|
return new String(buffer); |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
|