KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > taglibs > random > RandomNum


1 /*
2  * Copyright 1999,2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.taglibs.random;
18
19 import java.security.NoSuchAlgorithmException JavaDoc;
20 import java.security.NoSuchProviderException JavaDoc;
21 import java.security.SecureRandom JavaDoc;
22 import java.util.Random JavaDoc;
23
24 import javax.servlet.jsp.JspException JavaDoc;
25
26 /**
27  * The RandomNum will produce a variable set of random numbers.
28  *
29  * @author Rich Catlett
30  *
31  * @version 1.0
32  *
33  */

34
35 public class RandomNum {
36
37     /**
38      * the random number is stroed here for retrieval
39      */

40     private Long JavaDoc randomnum = null;
41     /**
42      * the random number is stroed here for retrieval
43      */

44     private Float JavaDoc randomfloat = null;
45     /**
46      * flag tells if a float value is expected or not
47      */

48     private boolean floatvalue = false;
49     /**
50      * upper bound in which to search for a number defaults to 9
51      */

52     private long upper = 100;
53     /**
54      * lower bound in which to search for a number defaults to 0
55      */

56     private long lower = 0;
57     /**
58      * the algorithm to use for a SecureRandom object
59      */

60     private String JavaDoc algorithm = null;
61     /**
62      * the provider package to check for the algorithm
63      */

64     private String JavaDoc provider = null;
65     /**
66      * boolean value that marks if a Random or SecureRandom object is to be used,
67      * default value of false says that a Random object will be used
68      */

69     private boolean secure = false;
70     /**
71      * random object that could be used
72      */

73     private Random JavaDoc random = null;
74     /**
75      * SecureRandom object that could be used
76      */

77     private SecureRandom JavaDoc secrandom = null;
78
79     /**
80      * nethod determines if a Random or SecureRandom object is to be used to
81      * generate the random number
82      *
83      */

84     private final float getFloat() {
85     if (random == null)
86         return secrandom.nextFloat();
87     else
88         return random.nextFloat();
89     }
90
91     /**
92      * generate the Random object that will be used for this random number
93      * generator
94      *
95      */

96     public final void generateRandomObject() throws JspException JavaDoc {
97
98     // check to see if the object is a SecureRandom object
99
if (secure) {
100         try {
101         // get an instance of a SecureRandom object
102
if (provider != null)
103             // search for algorithm in package provider
104
secrandom = SecureRandom.getInstance(algorithm, provider);
105         else
106             secrandom = SecureRandom.getInstance(algorithm);
107         } catch (NoSuchAlgorithmException JavaDoc ne) {
108         throw new JspException JavaDoc(ne.getMessage());
109         } catch (NoSuchProviderException JavaDoc pe) {
110         throw new JspException JavaDoc(pe.getMessage());
111         }
112         } else
113         random = new Random JavaDoc();
114     }
115
116     /**
117      * generate the random number
118      *
119      */

120     private final void generaterandom() {
121
122     int tmprandom = 0; // temp storage for random generated number
123
Integer JavaDoc rand;
124
125     // check to see if float value is expected
126
if (floatvalue)
127         randomfloat = new Float JavaDoc(getFloat());
128         else
129         randomnum = new Long JavaDoc(lower + (long) ((getFloat() * (upper - lower))));
130     }
131
132     /**
133      * get the random number, for use with the
134      * &lt;jsp:getProperty name=<i>"id"</i> property="floatval"/&gt;
135      *
136      * @return - randomly created float value
137      *
138      * @throws - JspException if the range entered for the number generator is not
139      * 0-1
140      */

141     public final Number JavaDoc getRandom() {
142
143     generaterandom(); // generate the first random number
144

145     if (floatvalue)
146         return randomfloat;
147     else
148         return randomnum;
149     }
150
151     /**
152      * set the Range
153      *
154      * @param low lower bound of the range
155      * @param up upper bound of the range
156      *
157      */

158     public final void setRange(long low, long up) {
159
160     // set the upper and lower bound of the range
161
lower = low;
162     upper = up;
163
164     // check to see if a float value is expected
165
if ((lower == 0) && (upper == 1))
166         floatvalue = true;
167     }
168
169     /**
170      * set the algorithm name
171      *
172      * @param value name of the algorithm to use for a SecureRandom object
173      *
174      */

175     public final void setAlgorithm(String JavaDoc value) {
176     algorithm = value;
177     secure = true; // a SecureRandom object is to be used
178
}
179
180     /**
181      * set the provider name
182      *
183      * @param value name of the package to check for the algorithm
184      *
185      */

186     public final void setProvider(String JavaDoc value) {
187     provider = value;
188     }
189 }
190
Popular Tags