If it were me, I would create character arrays ( char[] ... ) that represent the different character sets that you allow, and then in your generator method you select the appropriate character array and generate a password from that. Then the hard part creates arrays of characters ...
public String generate(char[] validchars, int len) { char[] password = new char[len]; Random rand = new Random(System.nanoTime()); for (int i = 0; i < len; i++) { password[i] = validchars[rand.nextInt(validchars.length)]; } return new String(password); }
Then your problem just becomes generating char [] arrays that represent the different rules that you have, and how to pass this set to the generation method.
one way to do this is to create a list of regular expression rules that match the rules that you allow, and then send each character through the rules .... and if they match the rules, add them .....
Consider a function that looks like this:
public static final char[] getValid(final String regex, final int lastchar) { char[] potential = new char[lastchar];
Then you can get an array of arabitic characters (lowercase only) with:
getValid("[az]", Character.MAX_VALUE);
Or a list of all "word" characters with:
getValid("\\w", Character.MAX_VALUE);
Then it becomes an example of choosing a regular expression according to your requirements and "storing" an array of valid characters that will be reused every time. (Do not generate characters every time you create a password ....)
rolfl
source share