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.pluto.internal.impl;
018:
019: import java.util.Random;
020:
021: /**
022: * <strong>Copied from commons-lang. No need for dependency.</strong>
023: *
024: * <p>Operations for random <code>String</code>s.</p>
025: *
026: * @since 1.0
027: * @version $Id: RandomStringUtils.java 516149 2007-03-08 19:10:26Z cziegeler $
028: */
029: public class RandomStringUtils {
030:
031: /**
032: * <p>Random object used by random method. This has to be not local
033: * to the random method so as to not return the same value in the
034: * same millisecond.</p>
035: */
036: private static final Random RANDOM = new Random();
037:
038: /**
039: * <p><code>RandomStringUtils</code> instances should NOT be constructed in
040: * standard programming. Instead, the class should be used as
041: * <code>RandomStringUtils.random(5);</code>.</p>
042: *
043: * <p>This constructor is public to permit tools that require a JavaBean instance
044: * to operate.</p>
045: */
046: public RandomStringUtils() {
047: }
048:
049: // Random
050: //-----------------------------------------------------------------------
051: /**
052: * <p>Creates a random string whose length is the number of characters
053: * specified.</p>
054: *
055: * <p>Characters will be chosen from the set of all characters.</p>
056: *
057: * @param count the length of random string to create
058: * @return the random string
059: */
060: public static String random(int count) {
061: return random(count, false, false);
062: }
063:
064: /**
065: * <p>Creates a random string whose length is the number of characters
066: * specified.</p>
067: *
068: * <p>Characters will be chosen from the set of characters whose
069: * ASCII value is between <code>32</code> and <code>126</code> (inclusive).</p>
070: *
071: * @param count the length of random string to create
072: * @return the random string
073: */
074: public static String randomAscii(int count) {
075: return random(count, 32, 127, false, false);
076: }
077:
078: /**
079: * <p>Creates a random string whose length is the number of characters
080: * specified.</p>
081: *
082: * <p>Characters will be chosen from the set of alphabetic
083: * characters.</p>
084: *
085: * @param count the length of random string to create
086: * @return the random string
087: */
088: public static String randomAlphabetic(int count) {
089: return random(count, true, false);
090: }
091:
092: /**
093: * <p>Creates a random string whose length is the number of characters
094: * specified.</p>
095: *
096: * <p>Characters will be chosen from the set of alpha-numeric
097: * characters.</p>
098: *
099: * @param count the length of random string to create
100: * @return the random string
101: */
102: public static String randomAlphanumeric(int count) {
103: return random(count, true, true);
104: }
105:
106: /**
107: * <p>Creates a random string whose length is the number of characters
108: * specified.</p>
109: *
110: * <p>Characters will be chosen from the set of numeric
111: * characters.</p>
112: *
113: * @param count the length of random string to create
114: * @return the random string
115: */
116: public static String randomNumeric(int count) {
117: return random(count, false, true);
118: }
119:
120: /**
121: * <p>Creates a random string whose length is the number of characters
122: * specified.</p>
123: *
124: * <p>Characters will be chosen from the set of alpha-numeric
125: * characters as indicated by the arguments.</p>
126: *
127: * @param count the length of random string to create
128: * @param letters if <code>true</code>, generated string will include
129: * alphabetic characters
130: * @param numbers if <code>true</code>, generated string will include
131: * numeric characters
132: * @return the random string
133: */
134: public static String random(int count, boolean letters,
135: boolean numbers) {
136: return random(count, 0, 0, letters, numbers);
137: }
138:
139: /**
140: * <p>Creates a random string whose length is the number of characters
141: * specified.</p>
142: *
143: * <p>Characters will be chosen from the set of alpha-numeric
144: * characters as indicated by the arguments.</p>
145: *
146: * @param count the length of random string to create
147: * @param start the position in set of chars to start at
148: * @param end the position in set of chars to end before
149: * @param letters if <code>true</code>, generated string will include
150: * alphabetic characters
151: * @param numbers if <code>true</code>, generated string will include
152: * numeric characters
153: * @return the random string
154: */
155: public static String random(int count, int start, int end,
156: boolean letters, boolean numbers) {
157: return random(count, start, end, letters, numbers, null, RANDOM);
158: }
159:
160: /**
161: * <p>Creates a random string based on a variety of options, using
162: * default source of randomness.</p>
163: *
164: * <p>This method has exactly the same semantics as
165: * {@link #random(int,int,int,boolean,boolean,char[],Random)}, but
166: * instead of using an externally supplied source of randomness, it uses
167: * the internal static {@link Random} instance.</p>
168: *
169: * @param count the length of random string to create
170: * @param start the position in set of chars to start at
171: * @param end the position in set of chars to end before
172: * @param letters only allow letters?
173: * @param numbers only allow numbers?
174: * @param chars the set of chars to choose randoms from.
175: * If <code>null</code>, then it will use the set of all chars.
176: * @return the random string
177: * @throws ArrayIndexOutOfBoundsException if there are not
178: * <code>(end - start) + 1</code> characters in the set array.
179: */
180: public static String random(int count, int start, int end,
181: boolean letters, boolean numbers, char[] chars) {
182: return random(count, start, end, letters, numbers, chars,
183: RANDOM);
184: }
185:
186: /**
187: * <p>Creates a random string based on a variety of options, using
188: * supplied source of randomness.</p>
189: *
190: * <p>If start and end are both <code>0</code>, start and end are set
191: * to <code>' '</code> and <code>'z'</code>, the ASCII printable
192: * characters, will be used, unless letters and numbers are both
193: * <code>false</code>, in which case, start and end are set to
194: * <code>0</code> and <code>Integer.MAX_VALUE</code>.
195: *
196: * <p>If set is not <code>null</code>, characters between start and
197: * end are chosen.</p>
198: *
199: * <p>This method accepts a user-supplied {@link Random}
200: * instance to use as a source of randomness. By seeding a single
201: * {@link Random} instance with a fixed seed and using it for each call,
202: * the same random sequence of strings can be generated repeatedly
203: * and predictably.</p>
204: *
205: * @param count the length of random string to create
206: * @param start the position in set of chars to start at
207: * @param end the position in set of chars to end before
208: * @param letters only allow letters?
209: * @param numbers only allow numbers?
210: * @param chars the set of chars to choose randoms from.
211: * If <code>null</code>, then it will use the set of all chars.
212: * @param random a source of randomness.
213: * @return the random string
214: * @throws ArrayIndexOutOfBoundsException if there are not
215: * <code>(end - start) + 1</code> characters in the set array.
216: * @throws IllegalArgumentException if <code>count</code> < 0.
217: * @since 2.0
218: */
219: public static String random(int count, int start, int end,
220: boolean letters, boolean numbers, char[] chars,
221: Random random) {
222: if (count == 0) {
223: return "";
224: } else if (count < 0) {
225: throw new IllegalArgumentException(
226: "Requested random string length " + count
227: + " is less than 0.");
228: }
229: if ((start == 0) && (end == 0)) {
230: end = 'z' + 1;
231: start = ' ';
232: if (!letters && !numbers) {
233: start = 0;
234: end = Integer.MAX_VALUE;
235: }
236: }
237:
238: StringBuffer buffer = new StringBuffer();
239: int gap = end - start;
240:
241: while (count-- != 0) {
242: char ch;
243: if (chars == null) {
244: ch = (char) (random.nextInt(gap) + start);
245: } else {
246: ch = chars[random.nextInt(gap) + start];
247: }
248: if ((letters && numbers && Character.isLetterOrDigit(ch))
249: || (letters && Character.isLetter(ch))
250: || (numbers && Character.isDigit(ch))
251: || (!letters && !numbers)) {
252: buffer.append(ch);
253: } else {
254: count++;
255: }
256: }
257: return buffer.toString();
258: }
259:
260: /**
261: * <p>Creates a random string whose length is the number of characters
262: * specified.</p>
263: *
264: * <p>Characters will be chosen from the set of characters
265: * specified.</p>
266: *
267: * @param count the length of random string to create
268: * @param chars the String containing the set of characters to use,
269: * may be null
270: * @return the random string
271: * @throws IllegalArgumentException if <code>count</code> < 0.
272: */
273: public static String random(int count, String chars) {
274: if (chars == null) {
275: return random(count, 0, 0, false, false, null, RANDOM);
276: }
277: return random(count, chars.toCharArray());
278: }
279:
280: /**
281: * <p>Creates a random string whose length is the number of characters
282: * specified.</p>
283: *
284: * <p>Characters will be chosen from the set of characters specified.</p>
285: *
286: * @param count the length of random string to create
287: * @param chars the character array containing the set of characters to use,
288: * may be null
289: * @return the random string
290: * @throws IllegalArgumentException if <code>count</code> < 0.
291: */
292: public static String random(int count, char[] chars) {
293: if (chars == null) {
294: return random(count, 0, 0, false, false, null, RANDOM);
295: }
296: return random(count, 0, chars.length, false, false, chars,
297: RANDOM);
298: }
299:
300: }
|