Event Dispatcher  v0.1.6-beta.1
Project that offers a small library that allows to subscribe and fire events.
RandomString.java
Go to the documentation of this file.
1 package com.fermod.util;
2 
3 import java.security.SecureRandom;
4 import java.util.Locale;
5 import java.util.Objects;
6 import java.util.Random;
7 
11 public class RandomString {
12 
13  public static final String UPPER = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
14  public static final String LOWER = UPPER.toLowerCase(Locale.ROOT);
15  public static final String DIGITS = "0123456789";
16  public static final String ALPHANUM = UPPER + LOWER + DIGITS;
17 
18  private final Random random;
19  private final char[] symbols;
20  private final char[] buffer;
21 
29  public RandomString(int length, Random random, String symbols) {
30  if (length < 1) {
31  throw new IllegalArgumentException();
32  }
33  if (symbols.length() < 2) {
34  throw new IllegalArgumentException();
35  }
36  this.random = Objects.requireNonNull(random);
37  this.symbols = symbols.toCharArray();
38  this.buffer = new char[length];
39  }
40 
41 
49  public RandomString(int length, Random random) {
50  this(length, random, ALPHANUM);
51  }
52 
59  public RandomString(int length, String symbols) {
60  this(length, new SecureRandom(), symbols);
61  }
62 
71  public RandomString(int length) {
72  this(length, new SecureRandom());
73  }
74 
92  public String nextString() {
93  for (int i = 0; i < buffer.length; ++i) {
94  buffer[i] = symbols[random.nextInt(symbols.length)];
95  }
96  return new String(buffer);
97  }
98 
99 }
RandomString(int length, Random random)
Constructs a new random string generator instance.
static final String LOWER
static final String UPPER
RandomString(int length)
Constructs a new random string generator instance used to create pseudorandom strings from a secure g...
RandomString(int length, String symbols)
Constructs a new random string generator instance used to create pseudorandom strings from a secure g...
String nextString()
Returns a pseudorandom, uniformly distributed.
static final String ALPHANUM
RandomString(int length, Random random, String symbols)
Constructs a new random string generator instance used to create pseudorandom strings.
A class to generate pseudorandom sequences of strings.
static final String DIGITS