/src/mpv5/utils/text/RandomStringUtils.java
Java | 304 lines | 75 code | 28 blank | 201 comment | 27 complexity | a4a7137d87b08cd0570ee46ade2bba3c MD5 | raw file
1package mpv5.utils.text; 2 3//~--- JDK imports ------------------------------------------------------------ 4 5/* 6* Copyright 2002-2004 The Apache Software Foundation. 7* 8* Licensed under the Apache License, Version 2.0 (the "License"); 9* you may not use this file except in compliance with the License. 10* You may obtain a copy of the License at 11* 12* http://www.apache.org/licenses/LICENSE-2.0 13* 14* Unless required by applicable law or agreed to in writing, software 15* distributed under the License is distributed on an "AS IS" BASIS, 16* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 17* See the License for the specific language governing permissions and 18* limitations under the License. 19 */ 20import java.util.Random; 21 22/** 23 * <p>Operations for random <code>String</code>s.</p> 24 * 25 * @author GenerationJava Core library 26 * @author <a href="mailto:bayard@generationjava.com">Henri Yandell</a> 27 * @author <a href="mailto:steven@caswell.name">Steven Caswell</a> 28 * @author Stephen Colebourne 29 * @author Gary Gregory 30 * @author Phil Steitz 31 * @since 1.0 32 * @version $Id: RandomStringUtils.java,v 1.27 2004/02/18 22:59:49 ggregory Exp $ 33 */ 34public class RandomStringUtils { 35 36 /** 37 * <p>Random object used by random method. This has to be not local 38 * to the random method so as to not return the same value in the 39 * same millisecond.</p> 40 */ 41 private static final Random RANDOM = new Random(); 42 43 /** 44 * <p><code>RandomStringUtils</code> instances should NOT be constructed in 45 * standard programming. Instead, the class should be used as 46 * <code>RandomStringUtils.random(5);</code>.</p> 47 * 48 * <p>This constructor is public to permit tools that require a JavaBean instance 49 * to operate.</p> 50 */ 51 public RandomStringUtils() {} 52 53 // Random 54 // ----------------------------------------------------------------------- 55 56 /** 57 * <p>Creates a random string whose length is the number of characters 58 * specified.</p> 59 * 60 * <p>Characters will be chosen from the set of all characters.</p> 61 * 62 * @param count the length of random string to create 63 * @return the random string 64 */ 65 public static String random(int count) { 66 return random(count, false, false); 67 } 68 69 /** 70 * <p>Creates a random string whose length is the number of characters 71 * specified.</p> 72 * 73 * <p>Characters will be chosen from the set of characters whose 74 * ASCII value is between <code>32</code> and <code>126</code> (inclusive).</p> 75 * 76 * @param count the length of random string to create 77 * @return the random string 78 */ 79 public static String randomAscii(int count) { 80 return random(count, 32, 127, false, false); 81 } 82 83 /** 84 * <p>Creates a random string whose length is the number of characters 85 * specified.</p> 86 * 87 * <p>Characters will be chosen from the set of alphabetic 88 * characters.</p> 89 * 90 * @param count the length of random string to create 91 * @return the random string 92 */ 93 public static String randomAlphabetic(int count) { 94 return random(count, true, false); 95 } 96 97 /** 98 * <p>Creates a random string whose length is the number of characters 99 * specified.</p> 100 * 101 * <p>Characters will be chosen from the set of alpha-numeric 102 * characters.</p> 103 * 104 * @param count the length of random string to create 105 * @return the random string 106 */ 107 public static String randomAlphanumeric(int count) { 108 return random(count, true, true); 109 } 110 111 /** 112 * <p>Creates a random string whose length is the number of characters 113 * specified.</p> 114 * 115 * <p>Characters will be chosen from the set of numeric 116 * characters.</p> 117 * 118 * @param count the length of random string to create 119 * @return the random string 120 */ 121 public static String randomNumeric(int count) { 122 return random(count, false, true); 123 } 124 125 /** 126 * <p>Creates a random string whose length is the number of characters 127 * specified.</p> 128 * 129 * <p>Characters will be chosen from the set of alpha-numeric 130 * characters as indicated by the arguments.</p> 131 * 132 * @param count the length of random string to create 133 * @param letters if <code>true</code>, generated string will include 134 * alphabetic characters 135 * @param numbers if <code>true</code>, generated string will include 136 * numeric characters 137 * @return the random string 138 */ 139 public static String random(int count, boolean letters, boolean numbers) { 140 return random(count, 0, 0, letters, numbers); 141 } 142 143 /** 144 * <p>Creates a random string whose length is the number of characters 145 * specified.</p> 146 * 147 * <p>Characters will be chosen from the set of alpha-numeric 148 * characters as indicated by the arguments.</p> 149 * 150 * @param count the length of random string to create 151 * @param start the position in set of chars to start at 152 * @param end the position in set of chars to end before 153 * @param letters if <code>true</code>, generated string will include 154 * alphabetic characters 155 * @param numbers if <code>true</code>, generated string will include 156 * numeric characters 157 * @return the random string 158 */ 159 public static String random(int count, int start, int end, boolean letters, boolean numbers) { 160 return random(count, start, end, letters, numbers, null, RANDOM); 161 } 162 163 /** 164 * <p>Creates a random string based on a variety of options, using 165 * default source of randomness.</p> 166 * 167 * <p>This method has exactly the same semantics as 168 * {@link #random(int,int,int,boolean,boolean,char[],Random)}, but 169 * instead of using an externally supplied source of randomness, it uses 170 * the internal static {@link Random} instance.</p> 171 * 172 * @param count the length of random string to create 173 * @param start the position in set of chars to start at 174 * @param end the position in set of chars to end before 175 * @param letters only allow letters? 176 * @param numbers only allow numbers? 177 * @param chars the set of chars to choose randoms from. 178 * If <code>null</code>, then it will use the set of all chars. 179 * @return the random string 180 * @throws ArrayIndexOutOfBoundsException if there are not 181 * <code>(end - start) + 1</code> characters in the set array. 182 */ 183 public static String random(int count, int start, int end, boolean letters, boolean numbers, char[] chars) { 184 return random(count, start, end, letters, numbers, chars, RANDOM); 185 } 186 187 /** 188 * <p>Creates a random string based on a variety of options, using 189 * supplied source of randomness.</p> 190 * 191 * <p>If start and end are both <code>0</code>, start and end are set 192 * to <code>' '</code> and <code>'z'</code>, the ASCII printable 193 * characters, will be used, unless letters and numbers are both 194 * <code>false</code>, in which case, start and end are set to 195 * <code>0</code> and <code>Integer.MAX_VALUE</code>. 196 * 197 * <p>If set is not <code>null</code>, characters between start and 198 * end are chosen.</p> 199 * 200 * <p>This method accepts a user-supplied {@link Random} 201 * instance to use as a source of randomness. By seeding a single 202 * {@link Random} instance with a fixed seed and using it for each call, 203 * the same random sequence of strings can be generated repeatedly 204 * and predictably.</p> 205 * 206 * @param count the length of random string to create 207 * @param start the position in set of chars to start at 208 * @param end the position in set of chars to end before 209 * @param letters only allow letters? 210 * @param numbers only allow numbers? 211 * @param chars the set of chars to choose randoms from. 212 * If <code>null</code>, then it will use the set of all chars. 213 * @param random a source of randomness. 214 * @return the random string 215 * @throws ArrayIndexOutOfBoundsException if there are not 216 * <code>(end - start) + 1</code> characters in the set array. 217 * @throws IllegalArgumentException if <code>count</code> < 0. 218 * @since 2.0 219 */ 220 public static String random(int count, int start, int end, boolean letters, boolean numbers, char[] chars, 221 Random random) { 222 if (count == 0) { 223 return ""; 224 } else if (count < 0) { 225 throw new IllegalArgumentException("Requested random string length " + count + " is less than 0."); 226 } 227 228 if ((start == 0) && (end == 0)) { 229 end = 'z' + 1; 230 start = ' '; 231 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 244 if (chars == null) { 245 ch = (char) (random.nextInt(gap) + start); 246 } else { 247 ch = chars[random.nextInt(gap) + start]; 248 } 249 250 if ((letters && numbers && Character.isLetterOrDigit(ch)) || (letters && Character.isLetter(ch)) 251 || (numbers && Character.isDigit(ch)) || (!letters &&!numbers)) { 252 buffer.append(ch); 253 } else { 254 count++; 255 } 256 } 257 258 return buffer.toString(); 259 } 260 261 /** 262 * <p>Creates a random string whose length is the number of characters 263 * specified.</p> 264 * 265 * <p>Characters will be chosen from the set of characters 266 * specified.</p> 267 * 268 * @param count the length of random string to create 269 * @param chars the String containing the set of characters to use, 270 * may be null 271 * @return the random string 272 * @throws IllegalArgumentException if <code>count</code> < 0. 273 */ 274 public static String random(int count, String chars) { 275 if (chars == null) { 276 return random(count, 0, 0, false, false, null, RANDOM); 277 } 278 279 return random(count, chars.toCharArray()); 280 } 281 282 /** 283 * <p>Creates a random string whose length is the number of characters 284 * specified.</p> 285 * 286 * <p>Characters will be chosen from the set of characters specified.</p> 287 * 288 * @param count the length of random string to create 289 * @param chars the character array containing the set of characters to use, 290 * may be null 291 * @return the random string 292 * @throws IllegalArgumentException if <code>count</code> < 0. 293 */ 294 public static String random(int count, char[] chars) { 295 if (chars == null) { 296 return random(count, 0, 0, false, false, null, RANDOM); 297 } 298 299 return random(count, 0, chars.length, false, false, chars, RANDOM); 300 } 301} 302 303 304//~ Formatted by Jindent --- http://www.jindent.com