View Javadoc

1   /*
2    * Copyright 2011 Király Attila
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *   http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package com.github.cage.token;
17  
18  import java.util.Random;
19  
20  import com.github.cage.IGenerator;
21  import com.github.cage.IGeneratorFactory;
22  
23  /**
24   * A simple random String generator that can be used to generate tokens for the
25   * captcha images.
26   * 
27   * In its default mode instances of this class generate words from English lower
28   * cased letters where vowels and consonants are alternating.
29   * 
30   * @author akiraly
31   */
32  public class RandomTokenGenerator implements IGenerator<String> {
33  
34  	/**
35  	 * Default minimum length of token.
36  	 */
37  	protected static final int DEFAULT_TOKEN_LEN_MIN = 8;
38  
39  	/**
40  	 * Default maximum length of token is {@link #DEFAULT_TOKEN_LEN_MIN} +
41  	 * {@value #DEFAULT_TOKEN_LEN_DELTA}.
42  	 */
43  	protected static final int DEFAULT_TOKEN_LEN_DELTA = 2;
44  
45  	private final IGeneratorFactory<Character> characterGeneratorFactory;
46  	private final int minLength;
47  	private final int delta;
48  	private final Random rnd;
49  
50  	/**
51  	 * Constructor.
52  	 */
53  	public RandomTokenGenerator() {
54  		this(null);
55  	}
56  
57  	/**
58  	 * Constructor.
59  	 * 
60  	 * @param rnd
61  	 *            random generator to be used, can be null
62  	 */
63  	public RandomTokenGenerator(Random rnd) {
64  		this(rnd, DEFAULT_TOKEN_LEN_MIN, DEFAULT_TOKEN_LEN_DELTA);
65  	}
66  
67  	/**
68  	 * Constructor.
69  	 * 
70  	 * @param length
71  	 *            the length of the generated words, must be > 0
72  	 * @param rnd
73  	 *            random generator to be used, can be null
74  	 */
75  	public RandomTokenGenerator(Random rnd, int length) {
76  		this(rnd, length, 0);
77  	}
78  
79  	/**
80  	 * Constructor.
81  	 * 
82  	 * @param rnd
83  	 *            random generator to be used, can be null
84  	 * @param minLength
85  	 *            the minimum length of the generated words, must be > 0
86  	 * @param delta
87  	 *            minLength + delta is the maximum length of the generated
88  	 *            words, delta must be >= 0
89  	 */
90  	public RandomTokenGenerator(Random rnd, int minLength, int delta) {
91  		this(rnd, new RandomCharacterGeneratorFactory(rnd != null ? rnd
92  				: new Random()), minLength, delta);
93  	}
94  
95  	/**
96  	 * Constructor.
97  	 * 
98  	 * @param rnd
99  	 *            random generator to be used, can be null
100 	 * @param characterGeneratorFactory
101 	 *            character generator factory to be used for the actual
102 	 *            character creation, can be null
103 	 * @param minLength
104 	 *            the minimum length of the generated words, must be > 0
105 	 * @param delta
106 	 *            minLength + delta is the maximum length of the generated
107 	 *            words, delta must be >= 0
108 	 */
109 	public RandomTokenGenerator(Random rnd,
110 			IGeneratorFactory<Character> characterGeneratorFactory,
111 			int minLength, int delta) {
112 		this.characterGeneratorFactory = characterGeneratorFactory != null ? characterGeneratorFactory
113 				: new RandomCharacterGeneratorFactory();
114 		this.minLength = Math.abs(minLength);
115 		this.delta = Math.abs(delta);
116 		this.rnd = rnd != null ? rnd : new Random();
117 	}
118 
119 	public String next() {
120 		int length = (delta <= 1 ? 0 : rnd.nextInt(delta) + 1) + minLength;
121 		char[] word = new char[length];
122 		IGenerator<Character> generator = characterGeneratorFactory.next();
123 
124 		for (int fi = 0; fi < word.length; fi++) {
125 			word[fi] = generator.next();
126 		}
127 
128 		return new String(word);
129 	}
130 
131 	/**
132 	 * @return character generator factory used by this class, not null
133 	 */
134 	public IGeneratorFactory<Character> getCharacterGeneratorFactory() {
135 		return characterGeneratorFactory;
136 	}
137 
138 	/**
139 	 * @return minimum length of generated tokens
140 	 */
141 	public int getMinLength() {
142 		return minLength;
143 	}
144 
145 	/**
146 	 * @return maximum length difference to add to the minimum length
147 	 */
148 	public int getDelta() {
149 		return delta;
150 	}
151 }