001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.commons.lang;
018:
019: import java.util.Random;
020:
021: /**
022: * <p>Operations for random <code>String</code>s.</p>
023: * <p>Currently <em>private high surrogate</em> characters are ignored.
024: * These are unicode characters that fall between the values 56192 (db80)
025: * and 56319 (dbff) as we don't know how to handle them.
026: * High and low surrogates are correctly dealt with - that is if a
027: * high surrogate is randomly chosen, 55296 (d800) to 56191 (db7f)
028: * then it is followed by a low surrogate. If a low surrogate is chosen,
029: * 56320 (dc00) to 57343 (dfff) then it is placed after a randomly
030: * chosen high surrogate. </p>
031: *
032: * @author <a href="mailto:steven@caswell.name">Steven Caswell</a>
033: * @author Stephen Colebourne
034: * @author Gary Gregory
035: * @author Phil Steitz
036: * @since 1.0
037: * @version $Id: RandomStringUtils.java 471626 2006-11-06 04:02:09Z bayard $
038: */
039: public class RandomStringUtils {
040:
041: /**
042: * <p>Random object used by random method. This has to be not local
043: * to the random method so as to not return the same value in the
044: * same millisecond.</p>
045: */
046: private static final Random RANDOM = new Random();
047:
048: /**
049: * <p><code>RandomStringUtils</code> instances should NOT be constructed in
050: * standard programming. Instead, the class should be used as
051: * <code>RandomStringUtils.random(5);</code>.</p>
052: *
053: * <p>This constructor is public to permit tools that require a JavaBean instance
054: * to operate.</p>
055: */
056: public RandomStringUtils() {
057: super ();
058: }
059:
060: // Random
061: //-----------------------------------------------------------------------
062: /**
063: * <p>Creates a random string whose length is the number of characters
064: * specified.</p>
065: *
066: * <p>Characters will be chosen from the set of all characters.</p>
067: *
068: * @param count the length of random string to create
069: * @return the random string
070: */
071: public static String random(int count) {
072: return random(count, false, false);
073: }
074:
075: /**
076: * <p>Creates a random string whose length is the number of characters
077: * specified.</p>
078: *
079: * <p>Characters will be chosen from the set of characters whose
080: * ASCII value is between <code>32</code> and <code>126</code> (inclusive).</p>
081: *
082: * @param count the length of random string to create
083: * @return the random string
084: */
085: public static String randomAscii(int count) {
086: return random(count, 32, 127, false, false);
087: }
088:
089: /**
090: * <p>Creates a random string whose length is the number of characters
091: * specified.</p>
092: *
093: * <p>Characters will be chosen from the set of alphabetic
094: * characters.</p>
095: *
096: * @param count the length of random string to create
097: * @return the random string
098: */
099: public static String randomAlphabetic(int count) {
100: return random(count, true, false);
101: }
102:
103: /**
104: * <p>Creates a random string whose length is the number of characters
105: * specified.</p>
106: *
107: * <p>Characters will be chosen from the set of alpha-numeric
108: * characters.</p>
109: *
110: * @param count the length of random string to create
111: * @return the random string
112: */
113: public static String randomAlphanumeric(int count) {
114: return random(count, true, true);
115: }
116:
117: /**
118: * <p>Creates a random string whose length is the number of characters
119: * specified.</p>
120: *
121: * <p>Characters will be chosen from the set of numeric
122: * characters.</p>
123: *
124: * @param count the length of random string to create
125: * @return the random string
126: */
127: public static String randomNumeric(int count) {
128: return random(count, false, true);
129: }
130:
131: /**
132: * <p>Creates a random string whose length is the number of characters
133: * specified.</p>
134: *
135: * <p>Characters will be chosen from the set of alpha-numeric
136: * characters as indicated by the arguments.</p>
137: *
138: * @param count the length of random string to create
139: * @param letters if <code>true</code>, generated string will include
140: * alphabetic characters
141: * @param numbers if <code>true</code>, generated string will include
142: * numeric characters
143: * @return the random string
144: */
145: public static String random(int count, boolean letters,
146: boolean numbers) {
147: return random(count, 0, 0, letters, numbers);
148: }
149:
150: /**
151: * <p>Creates a random string whose length is the number of characters
152: * specified.</p>
153: *
154: * <p>Characters will be chosen from the set of alpha-numeric
155: * characters as indicated by the arguments.</p>
156: *
157: * @param count the length of random string to create
158: * @param start the position in set of chars to start at
159: * @param end the position in set of chars to end before
160: * @param letters if <code>true</code>, generated string will include
161: * alphabetic characters
162: * @param numbers if <code>true</code>, generated string will include
163: * numeric characters
164: * @return the random string
165: */
166: public static String random(int count, int start, int end,
167: boolean letters, boolean numbers) {
168: return random(count, start, end, letters, numbers, null, RANDOM);
169: }
170:
171: /**
172: * <p>Creates a random string based on a variety of options, using
173: * default source of randomness.</p>
174: *
175: * <p>This method has exactly the same semantics as
176: * {@link #random(int,int,int,boolean,boolean,char[],Random)}, but
177: * instead of using an externally supplied source of randomness, it uses
178: * the internal static {@link Random} instance.</p>
179: *
180: * @param count the length of random string to create
181: * @param start the position in set of chars to start at
182: * @param end the position in set of chars to end before
183: * @param letters only allow letters?
184: * @param numbers only allow numbers?
185: * @param chars the set of chars to choose randoms from.
186: * If <code>null</code>, then it will use the set of all chars.
187: * @return the random string
188: * @throws ArrayIndexOutOfBoundsException if there are not
189: * <code>(end - start) + 1</code> characters in the set array.
190: */
191: public static String random(int count, int start, int end,
192: boolean letters, boolean numbers, char[] chars) {
193: return random(count, start, end, letters, numbers, chars,
194: RANDOM);
195: }
196:
197: /**
198: * <p>Creates a random string based on a variety of options, using
199: * supplied source of randomness.</p>
200: *
201: * <p>If start and end are both <code>0</code>, start and end are set
202: * to <code>' '</code> and <code>'z'</code>, the ASCII printable
203: * characters, will be used, unless letters and numbers are both
204: * <code>false</code>, in which case, start and end are set to
205: * <code>0</code> and <code>Integer.MAX_VALUE</code>.
206: *
207: * <p>If set is not <code>null</code>, characters between start and
208: * end are chosen.</p>
209: *
210: * <p>This method accepts a user-supplied {@link Random}
211: * instance to use as a source of randomness. By seeding a single
212: * {@link Random} instance with a fixed seed and using it for each call,
213: * the same random sequence of strings can be generated repeatedly
214: * and predictably.</p>
215: *
216: * @param count the length of random string to create
217: * @param start the position in set of chars to start at
218: * @param end the position in set of chars to end before
219: * @param letters only allow letters?
220: * @param numbers only allow numbers?
221: * @param chars the set of chars to choose randoms from.
222: * If <code>null</code>, then it will use the set of all chars.
223: * @param random a source of randomness.
224: * @return the random string
225: * @throws ArrayIndexOutOfBoundsException if there are not
226: * <code>(end - start) + 1</code> characters in the set array.
227: * @throws IllegalArgumentException if <code>count</code> < 0.
228: * @since 2.0
229: */
230: public static String random(int count, int start, int end,
231: boolean letters, boolean numbers, char[] chars,
232: Random random) {
233: if (count == 0) {
234: return "";
235: } else if (count < 0) {
236: throw new IllegalArgumentException(
237: "Requested random string length " + count
238: + " is less than 0.");
239: }
240: if ((start == 0) && (end == 0)) {
241: end = 'z' + 1;
242: start = ' ';
243: if (!letters && !numbers) {
244: start = 0;
245: end = Integer.MAX_VALUE;
246: }
247: }
248:
249: char[] buffer = new char[count];
250: int gap = end - start;
251:
252: while (count-- != 0) {
253: char ch;
254: if (chars == null) {
255: ch = (char) (random.nextInt(gap) + start);
256: } else {
257: ch = chars[random.nextInt(gap) + start];
258: }
259: if ((letters && Character.isLetter(ch))
260: || (numbers && Character.isDigit(ch))
261: || (!letters && !numbers)) {
262: if (ch >= 56320 && ch <= 57343) {
263: if (count == 0) {
264: count++;
265: } else {
266: // low surrogate, insert high surrogate after putting it in
267: buffer[count] = ch;
268: count--;
269: buffer[count] = (char) (55296 + random
270: .nextInt(128));
271: }
272: } else if (ch >= 55296 && ch <= 56191) {
273: if (count == 0) {
274: count++;
275: } else {
276: // high surrogate, insert low surrogate before putting it in
277: buffer[count] = (char) (56320 + random
278: .nextInt(128));
279: count--;
280: buffer[count] = ch;
281: }
282: } else if (ch >= 56192 && ch <= 56319) {
283: // private high surrogate, no effing clue, so skip it
284: count++;
285: } else {
286: buffer[count] = ch;
287: }
288: } else {
289: count++;
290: }
291: }
292: return new String(buffer);
293: }
294:
295: /**
296: * <p>Creates a random string whose length is the number of characters
297: * specified.</p>
298: *
299: * <p>Characters will be chosen from the set of characters
300: * specified.</p>
301: *
302: * @param count the length of random string to create
303: * @param chars the String containing the set of characters to use,
304: * may be null
305: * @return the random string
306: * @throws IllegalArgumentException if <code>count</code> < 0.
307: */
308: public static String random(int count, String chars) {
309: if (chars == null) {
310: return random(count, 0, 0, false, false, null, RANDOM);
311: }
312: return random(count, chars.toCharArray());
313: }
314:
315: /**
316: * <p>Creates a random string whose length is the number of characters
317: * specified.</p>
318: *
319: * <p>Characters will be chosen from the set of characters specified.</p>
320: *
321: * @param count the length of random string to create
322: * @param chars the character array containing the set of characters to use,
323: * may be null
324: * @return the random string
325: * @throws IllegalArgumentException if <code>count</code> < 0.
326: */
327: public static String random(int count, char[] chars) {
328: if (chars == null) {
329: return random(count, 0, 0, false, false, null, RANDOM);
330: }
331: return random(count, 0, chars.length, false, false, chars,
332: RANDOM);
333: }
334:
335: }
|