cn.atomtool.captcha.word.RandomWordFactory Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of atomtool-captcha Show documentation
Show all versions of atomtool-captcha Show documentation
atomTool核心工具包,包括集合、字符串、Bean等工具类
The newest version!
package cn.atomtool.captcha.word;
import java.util.Random;
public class RandomWordFactory implements WordFactory {
protected String characters;
protected int minLength;
protected int maxLength;
public void setCharacters(String characters) {
this.characters = characters;
}
public void setMinLength(int minLength) {
this.minLength = minLength;
}
public void setMaxLength(int maxLength) {
this.maxLength = maxLength;
}
public RandomWordFactory() {
characters = "absdegkmnopwx23456789";
minLength = 6;
maxLength = 6;
}
@Override
public String getNextWord() {
Random rnd = new Random();
StringBuffer sb = new StringBuffer();
int l = minLength + (maxLength > minLength ? rnd.nextInt(maxLength - minLength) : 0);
for (int i = 0; i < l; i++) {
int j = rnd.nextInt(characters.length());
sb.append(characters.charAt(j));
}
return sb.toString();
}
}