KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > continuent > sequoia > controller > sql > macros > RandomMacro


1 /**
2  * Sequoia: Database clustering technology.
3  * Copyright (C) 2006 Continuent, Inc.
4  * Contact: sequoia@continuent.org
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  * Initial developer(s): Emmanuel Cecchet.
19  * Contributor(s): ______________________.
20  */

21
22 package org.continuent.sequoia.controller.sql.macros;
23
24 import java.util.Random JavaDoc;
25
26 import org.continuent.sequoia.common.xml.DatabasesXmlTags;
27
28 /**
29  * This class defines a RandomMacro to handle macros such as rand()
30  *
31  * @author <a HREF="mailto:emmanuel.cecchet@continuent.com">Emmanuel Cecchet</a>
32  * @version 1.0
33  */

34 public class RandomMacro extends AbstractMacro
35 {
36   private int numberFormat;
37
38   /** Value if rand() macro should be replaced by an integer value */
39   public static final int RAND_INT = 1;
40   /** Value if rand() macro should be replaced by an long value */
41   public static final int RAND_LONG = 2;
42   /** Value if rand() macro should be replaced by an float value (default) */
43   public static final int RAND_FLOAT = 3;
44   /** Value if rand() macro should be replaced by an double value */
45   public static final int RAND_DOUBLE = 4;
46
47   private final Random JavaDoc randGenerator = new Random JavaDoc(System.currentTimeMillis());
48
49   /**
50    * Creates a new <code>RandomMacro</code> object
51    *
52    * @param name macro name
53    * @param format number format (see constants in this class)
54    */

55   public RandomMacro(String JavaDoc name, int format)
56   {
57     super(name);
58     numberFormat = format;
59   }
60
61   /**
62    * @see org.continuent.sequoia.controller.sql.macros.AbstractMacro#generateMacroValue(long)
63    */

64   public String JavaDoc generateMacroValue(long currentTimeInMs)
65   {
66     switch (numberFormat)
67     {
68       case RAND_INT :
69         return Integer.toString(randGenerator.nextInt());
70       case RAND_LONG :
71         return Long.toString(randGenerator.nextLong());
72       case RAND_FLOAT :
73         return Float.toString(randGenerator.nextFloat());
74       case RAND_DOUBLE :
75         return Double.toString(randGenerator.nextDouble());
76       default :
77         throw new RuntimeException JavaDoc(
78             "Unexpected replacement strategy for random macro (" + numberFormat
79                 + ")");
80     }
81   }
82
83   /**
84    * Convert the rand number format from string (xml value) to integer
85    *
86    * @param numberFormat the number format XML tag
87    * @return an int corresponding to the string description
88    * @throws IllegalArgumentException if the tag is not supported
89    */

90   public static final int getRandNumberFormat(String JavaDoc numberFormat)
91       throws IllegalArgumentException JavaDoc
92   {
93     if (numberFormat.equalsIgnoreCase(DatabasesXmlTags.VAL_double))
94       return RAND_DOUBLE;
95     else if (numberFormat.equalsIgnoreCase(DatabasesXmlTags.VAL_float))
96       return RAND_FLOAT;
97     else if (numberFormat.equalsIgnoreCase(DatabasesXmlTags.VAL_int))
98       return RAND_INT;
99     else if (numberFormat.equalsIgnoreCase(DatabasesXmlTags.VAL_long))
100       return RAND_LONG;
101     else
102       throw new IllegalArgumentException JavaDoc("Invalid random number format tag: "
103           + numberFormat);
104   }
105
106   /**
107    * Convert the rand level from int (java code) to string (xml value)
108    *
109    * @param numberFormat the number format constant
110    * @return a string description corresponding to that level
111    * @throws IllegalArgumentException if the number format is not supported
112    */

113   public static final String JavaDoc getStringRandLevel(int numberFormat)
114       throws IllegalArgumentException JavaDoc
115   {
116     switch (numberFormat)
117     {
118       case RAND_DOUBLE :
119         return DatabasesXmlTags.VAL_double;
120       case RAND_FLOAT :
121         return DatabasesXmlTags.VAL_float;
122       case RAND_INT :
123         return DatabasesXmlTags.VAL_int;
124       case RAND_LONG :
125         return DatabasesXmlTags.VAL_long;
126       default :
127         throw new IllegalArgumentException JavaDoc("Invalid random number format: "
128             + numberFormat);
129     }
130   }
131
132   /**
133    * @see org.continuent.sequoia.common.xml.XmlComponent#getXml()
134    */

135   public String JavaDoc getXml()
136   {
137     return "<" + DatabasesXmlTags.ELT_RandomMacro + " "
138         + DatabasesXmlTags.ATT_macroName + "=\"" + macroName + "\" "
139         + DatabasesXmlTags.ATT_numberFormat + "=\""
140         + getStringRandLevel(numberFormat) + "\" />";
141   }
142 }
143
Popular Tags