2020-02-13 23:52:33 +01:00
|
|
|
package tk.draganczuk.url;
|
|
|
|
|
|
|
|
import java.util.Random;
|
2020-02-16 16:05:09 +01:00
|
|
|
import java.util.regex.Pattern;
|
2020-02-13 23:52:33 +01:00
|
|
|
|
|
|
|
public class Utils {
|
|
|
|
private static final Random random = new Random(System.currentTimeMillis());
|
|
|
|
|
2020-02-16 16:05:09 +01:00
|
|
|
private static final String SHORT_URL_PATTERN = "[a-z0-9]+";
|
|
|
|
private static final Pattern PATTERN = Pattern.compile(SHORT_URL_PATTERN);
|
|
|
|
|
2020-02-13 23:52:33 +01:00
|
|
|
public static String randomString() {
|
|
|
|
int leftLimit = 48; // numeral '0'
|
|
|
|
int rightLimit = 122; // letter 'z'
|
|
|
|
int targetStringLength = 10;
|
|
|
|
|
2020-02-16 15:58:37 +01:00
|
|
|
return random.ints(leftLimit, rightLimit + 1)
|
|
|
|
.filter(i -> (i <= 57 || i >= 97))
|
2020-02-13 23:52:33 +01:00
|
|
|
.limit(targetStringLength)
|
|
|
|
.collect(StringBuilder::new,
|
|
|
|
StringBuilder::appendCodePoint,
|
|
|
|
StringBuilder::append)
|
|
|
|
.toString();
|
|
|
|
}
|
2020-02-16 16:05:09 +01:00
|
|
|
|
|
|
|
public static boolean validate(String shortUrl) {
|
|
|
|
return PATTERN.matcher(shortUrl)
|
|
|
|
.matches();
|
|
|
|
}
|
2020-02-13 23:52:33 +01:00
|
|
|
}
|