PageRenderTime 49ms CodeModel.GetById 32ms RepoModel.GetById 0ms app.codeStats 0ms

/src/mpv5/utils/text/RandomStringUtils.java

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