Source Code Cross Referenced for RandomKeyGenerator.java in  » Portal » stringbeans-3.5 » com » nabhinc » util » 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 » stringbeans 3.5 » com.nabhinc.util 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /* 
002:         * (C) Copyright 2003 Nabh Information Systems, Inc. 
003:         * 
004:         * All copyright notices regarding Nabh's products MUST remain
005:         * intact in the scripts and in the outputted HTML.
006:         * This program is free software; you can redistribute it and/or
007:         * modify it under the terms of the GNU Lesser General Public License
008:         * as published by the Free Software Foundation; either version 2.1 
009:         * of the License, or (at your option) any later version.
010:         * 
011:         * This program is distributed in the hope that it will be useful,
012:         * but WITHOUT ANY WARRANTY; without even the implied warranty of 
013:         * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
014:         * GNU Lesser General Public License for more details.
015:         * 
016:         * You should have received a copy of the GNU Lesser General Public License
017:         * along with this program; if not, write to the Free Software 
018:         * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
019:         * 
020:         */
021:
022:        package com.nabhinc.util;
023:
024:        import java.util.Random;
025:
026:        /**
027:         * A simple random key generator utility. The key generated can be an  
028:         * alpha-numeric or alphabetic key with the fixed length or random-range 
029:         * (within the specified lower and upper range) length.
030:         * 
031:         * @author Wirawan Chokry
032:         * (c) 2003 Nabh Information Systems, Inc. All Rights Reserved. 
033:         */
034:        public class RandomKeyGenerator {
035:
036:            private static String[] lowerAlpha = { "a", "b", "c", "d", "e",
037:                    "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q",
038:                    "r", "s", "t", "u", "v", "w", "x", "y", "z" };
039:
040:            private static String[] upperAlpha = { "A", "B", "C", "D", "E",
041:                    "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q",
042:                    "R", "S", "T", "U", "V", "W", "X", "Y", "Z" };
043:
044:            private static String[] numeric = { "0", "1", "2", "3", "4", "5",
045:                    "6", "7", "8", "9" };
046:
047:            private static final RandomKeyGenerator rand = new RandomKeyGenerator();
048:
049:            private static final Random randomNumberStream = new Random(rand
050:                    .hashCode()
051:                    * System.currentTimeMillis());
052:
053:            /**
054:             * 
055:             * @param number
056:             * @return
057:             */
058:            public static int getRandomInteger(double number) {
059:                return getRandomInteger(0, number);
060:            }
061:
062:            /**
063:             * 
064:             * @param lower
065:             * @param upper
066:             * @return
067:             */
068:            public static int getRandomInteger(double lower, double upper) {
069:                double min = Math.min(lower, upper);
070:                double max = Math.max(lower, upper);
071:
072:                return (int) min
073:                        + (int) (randomNumberStream.nextFloat() * (max - min + 1));
074:            }
075:
076:            /**
077:             * 
078:             * @return
079:             */
080:            public static Random getRandom() {
081:                return randomNumberStream;
082:            }
083:
084:            /**
085:             * 
086:             * @param minLength
087:             * @param maxLength
088:             * @return
089:             */
090:            public static String generateAlphaNumericKey(double minLength,
091:                    double maxLength) {
092:                int randomLength = getRandomInteger(minLength, maxLength);
093:                StringBuffer randomKey = new StringBuffer(randomLength - 1);
094:                for (int i = 0; i < randomLength; i++) {
095:                    int randomChar = getRandomInteger(2);
096:                    if (randomChar == 0) {
097:                        int randomPos = getRandomInteger(25);
098:                        randomKey.append(lowerAlpha[randomPos]);
099:                    } else if (randomChar == 1) {
100:                        int randomPos = getRandomInteger(25);
101:                        randomKey.append(upperAlpha[randomPos]);
102:                    } else {
103:                        int randomPos = getRandomInteger(9);
104:                        randomKey.append(numeric[randomPos]);
105:                    }
106:                }
107:                return randomKey.toString();
108:            }
109:
110:            /**
111:             * Creates random alphabet key. 
112:             * @param minLength The minimum length of the key.
113:             * @param maxLength The maximum length of the key.
114:             * @return The random alphabet key.
115:             */
116:            public static String generateAlphaKey(double minLength,
117:                    double maxLength) {
118:                int randomLength = getRandomInteger(minLength, maxLength);
119:                StringBuffer randomKey = new StringBuffer(randomLength - 1);
120:                for (int i = 0; i < randomLength; i++) {
121:                    int randomChar = getRandomInteger(1);
122:                    if (randomChar == 0) {
123:                        int randomPos = getRandomInteger(25);
124:                        randomKey.append(lowerAlpha[randomPos]);
125:                    } else {//if (randomChar == 1) {
126:                        int randomPos = getRandomInteger(25);
127:                        randomKey.append(upperAlpha[randomPos]);
128:                    }
129:                }
130:                return randomKey.toString();
131:            }
132:
133:            /**
134:             * 
135:             * @param length
136:             * @return
137:             */
138:            public static String generateFixedLengthAlphaNumericKey(
139:                    double length) {
140:                int randomLength = (int) length;
141:                StringBuffer randomKey = new StringBuffer(randomLength - 1);
142:                for (int i = 0; i < randomLength; i++) {
143:                    int randomChar = getRandomInteger(2);
144:                    if (randomChar == 0) {
145:                        int randomPos = getRandomInteger(25);
146:                        randomKey.append(lowerAlpha[randomPos]);
147:                    } else if (randomChar == 1) {
148:                        int randomPos = getRandomInteger(25);
149:                        randomKey.append(upperAlpha[randomPos]);
150:                    } else {
151:                        int randomPos = getRandomInteger(9);
152:                        randomKey.append(numeric[randomPos]);
153:                    }
154:                }
155:
156:                return randomKey.toString();
157:            }
158:
159:            /**
160:             * 
161:             * @param length
162:             * @return
163:             */
164:            public static String generateFixedLengthAlphaKey(double length) {
165:                int randomLength = (int) length;
166:                StringBuffer randomKey = new StringBuffer(randomLength - 1);
167:                for (int i = 0; i < randomLength; i++) {
168:                    int randomChar = getRandomInteger(1);
169:                    if (randomChar == 0) {
170:                        int randomPos = getRandomInteger(25);
171:                        randomKey.append(lowerAlpha[randomPos]);
172:                    } else {
173:                        int randomPos = getRandomInteger(25);
174:                        randomKey.append(upperAlpha[randomPos]);
175:                    }
176:                }
177:
178:                return randomKey.toString();
179:            }
180:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.