Source Code Cross Referenced for RandomStringUtils.java in  » Portal » Pluto » org » apache » pluto » internal » impl » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Portal » Pluto » org.apache.pluto.internal.impl 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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> &lt; 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> &lt; 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> &lt; 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:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.