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: }
|