KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > math > random > RandomAdaptor


1 /*
2  * Copyright 2005 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 package org.apache.commons.math.random;
17
18 import java.util.Random JavaDoc;
19
20 /**
21  * Extension of <code>java.util.Random</code> wrapping a
22  * {@link RandomGenerator}.
23  *
24  * @since 1.1
25  * @version $Revision:$ $Date$
26  */

27 public class RandomAdaptor extends Random JavaDoc implements RandomGenerator {
28     
29     /** Wrapped randomGenerator instance */
30     private RandomGenerator randomGenerator = null;
31     
32     /**
33      * Prevent instantiation without a generator argument
34      */

35     private RandomAdaptor() { }
36     
37     /**
38      * Construct a RandomAdaptor wrapping the supplied RandomGenerator.
39      *
40      * @param randomGenerator the wrapped generator
41      */

42     public RandomAdaptor(RandomGenerator randomGenerator) {
43         this.randomGenerator = randomGenerator;
44     }
45     
46     /**
47      * Factory method to create a <code>Random</code> using the supplied
48      * <code>RandomGenerator</code>.
49      *
50      * @param randomGenerator wrapped RandomGenerator instance
51      * @return a Random instance wrapping the RandomGenerator
52      */

53     public static Random JavaDoc createAdaptor(RandomGenerator randomGenerator) {
54         return new RandomAdaptor(randomGenerator);
55     }
56     
57     /* (non-Javadoc)
58      * @see java.util.Random#nextBoolean()
59      */

60     public boolean nextBoolean() {
61         return randomGenerator.nextBoolean();
62     }
63
64     /* (non-Javadoc)
65      * @see java.util.Random#nextBytes(byte[])
66      */

67     public void nextBytes(byte[] bytes) {
68         randomGenerator.nextBytes(bytes);
69     }
70
71     /* (non-Javadoc)
72      * @see java.util.Random#nextDouble()
73      */

74     public double nextDouble() {
75         return randomGenerator.nextDouble();
76     }
77
78     /* (non-Javadoc)
79      * @see java.util.Random#nextFloat()
80      */

81     public float nextFloat() {
82         return randomGenerator.nextFloat();
83     }
84
85     /* (non-Javadoc)
86      * @see java.util.Random#nextGaussian()
87      */

88     public double nextGaussian() {
89         return randomGenerator.nextGaussian();
90     }
91
92     /* (non-Javadoc)
93      * @see java.util.Random#nextInt()
94      */

95     public int nextInt() {
96         return randomGenerator.nextInt();
97     }
98
99     /* (non-Javadoc)
100      * @see java.util.Random#nextInt(int)
101      */

102     public int nextInt(int n) {
103         return randomGenerator.nextInt(n);
104     }
105
106     /* (non-Javadoc)
107      * @see java.util.Random#nextLong()
108      */

109     public long nextLong() {
110         return randomGenerator.nextLong();
111     }
112
113     /* (non-Javadoc)
114      * @see java.util.Random#setSeed(long)
115      */

116     public void setSeed(long seed) {
117         if (randomGenerator != null) { // required to avoid NPE in constructor
118
randomGenerator.setSeed(seed);
119         }
120     }
121 }
122
Popular Tags