KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > tutorial > RandomGeneratorProvider


1 /*
2  * Copyright 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 tutorial;
18
19 import java.util.Random JavaDoc;
20
21 import org.apache.avalon.framework.configuration.Configuration;
22 import org.apache.avalon.framework.configuration.ConfigurationException;
23 import org.apache.avalon.framework.configuration.Configurable;
24 import org.apache.avalon.framework.logger.AbstractLogEnabled;
25
26 /**
27  * An implementation of a random number generator.
28  *
29  * @avalon.component name="randomizer" lifestyle="singleton"
30  * @avalon.service type="tutorial.RandomGenerator"
31  */

32 public class RandomGeneratorProvider extends AbstractLogEnabled
33   implements Configurable, RandomGenerator
34 {
35
36     private Random JavaDoc m_random = null;
37
38    /**
39     * Configuration of the component by the container. The
40     * implementation get a child element named 'source' and
41     * assigns the value of the element to a local variable.
42     *
43     * @param config the component configuration
44     * @exception ConfigurationException if a configuration error occurs
45     */

46     public void configure( Configuration config ) throws ConfigurationException
47     {
48         getLogger().info( "configuration stage" );
49         long seed = config.getChild( "seed" ).getValueAsLong( 0 );
50         getLogger().info( "seed: " + seed );
51         m_random = new Random JavaDoc( System.currentTimeMillis() * seed );
52     }
53
54    /**
55     * Return a random integer
56     * @return the random number
57     */

58     public int getRandom()
59     {
60         return m_random.nextInt();
61     }
62 }
63
Popular Tags