KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > components > modules > input > RandomNumberModule


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.cocoon.components.modules.input;
18
19 import org.apache.avalon.framework.configuration.Configuration;
20 import org.apache.avalon.framework.configuration.ConfigurationException;
21 import org.apache.avalon.framework.thread.ThreadSafe;
22
23 import java.util.Iterator JavaDoc;
24 import java.util.LinkedList JavaDoc;
25 import java.util.List JavaDoc;
26 import java.util.Map JavaDoc;
27 import java.util.Vector JavaDoc;
28
29 /**
30  * RandomNumberModule returns a random number as a string.
31  * Configuration through child elements: "min", "max" setting
32  * range of random number. Defaults to "0" and "9999999999"
33  * respectively.
34  *
35  * @author <a HREF="mailto:haul@apache.org">Christian Haul</a>
36  * @version CVS $Id: RandomNumberModule.java 30932 2004-07-29 17:35:38Z vgritsenko $
37  */

38 public class RandomNumberModule extends AbstractInputModule implements ThreadSafe {
39
40     final static Vector JavaDoc returnNames;
41     static {
42         Vector JavaDoc tmp = new Vector JavaDoc();
43         tmp.add("randomNumber");
44         returnNames = tmp;
45     }
46
47     public Object JavaDoc getAttribute( String JavaDoc name, Configuration modeConf, Map JavaDoc objectModel ) throws ConfigurationException {
48         
49         long min = 0;
50         long max = java.lang.Long.MAX_VALUE;
51         if (modeConf != null) {
52             min = Long.parseLong(modeConf.getAttribute("min","0"));
53             max = Long.parseLong(modeConf.getAttribute("max",String.valueOf(max)));
54             
55             //preferred
56
min = Long.parseLong(modeConf.getChild("min").getValue("0"));
57             max = Long.parseLong(modeConf.getChild("max").getValue(String.valueOf(max)));
58         }
59         return Long.toString(java.lang.Math.round(java.lang.Math.random()*(max-min)));
60
61     }
62
63
64     public Iterator JavaDoc getAttributeNames( Configuration modeConf, Map JavaDoc objectModel ) throws ConfigurationException {
65
66         return RandomNumberModule.returnNames.iterator();
67     }
68
69
70     public Object JavaDoc[] getAttributeValues( String JavaDoc name, Configuration modeConf, Map JavaDoc objectModel )
71         throws ConfigurationException {
72
73             List JavaDoc values = new LinkedList JavaDoc();
74             values.add( this.getAttribute(name, modeConf, objectModel ) );
75
76             return values.toArray();
77             
78     }
79
80 }
81
Popular Tags