svn-gvsig-desktop / trunk / libraries / libGDBMS / src / com / hardcode / gdbms / engine / values / ValueFactory.java @ 538
History | View | Annotate | Download (5.37 KB)
1 |
/* Generated by Together */
|
---|---|
2 |
package com.hardcode.gdbms.engine.values; |
3 |
|
4 |
import com.hardcode.gdbms.engine.instruction.SemanticException; |
5 |
import com.hardcode.gdbms.parser.SQLEngineConstants; |
6 |
|
7 |
import java.util.Date; |
8 |
|
9 |
|
10 |
/**
|
11 |
* Factor?a abstracta de objetos value que dado un tipo b?sico, devuelve el
|
12 |
* wrapper apropiado
|
13 |
*
|
14 |
* @author $author$
|
15 |
* @version $Revision: 538 $
|
16 |
*/
|
17 |
public class ValueFactory { |
18 |
/**
|
19 |
* Crea un objeto de tipo Value a partir de un int
|
20 |
*
|
21 |
* @param n valor que se quiere representar
|
22 |
*
|
23 |
* @return objeto Value con el valor que se pasa como par?metro
|
24 |
*/
|
25 |
public static Value createValue(int n) { |
26 |
IntValue ret = new IntValue();
|
27 |
ret.setValue(n); |
28 |
|
29 |
return ret;
|
30 |
} |
31 |
|
32 |
/**
|
33 |
* Crea un objeto de tipo Value a partir de un long
|
34 |
*
|
35 |
* @param l valor que se quiere representar
|
36 |
*
|
37 |
* @return objeto Value con el valor que se pasa como par?metro
|
38 |
*/
|
39 |
public static Value createValue(long l) { |
40 |
LongValue ret = new LongValue();
|
41 |
ret.setValue(l); |
42 |
|
43 |
return ret;
|
44 |
} |
45 |
|
46 |
/**
|
47 |
* Crea un objeto de tipo Value a partir de un String
|
48 |
*
|
49 |
* @param s valor que se quiere representar
|
50 |
*
|
51 |
* @return objeto Value con el valor que se pasa como par?metro
|
52 |
*/
|
53 |
public static Value createValue(String s) { |
54 |
StringValue ret = new StringValue();
|
55 |
ret.setValue(s); |
56 |
|
57 |
return ret;
|
58 |
} |
59 |
|
60 |
/**
|
61 |
* Crea un objeto de tipo Value a partir de un float
|
62 |
*
|
63 |
* @param f valor que se quiere representar
|
64 |
*
|
65 |
* @return objeto Value con el valor que se pasa como par?metro
|
66 |
*/
|
67 |
public static Value createValue(float f) { |
68 |
FloatValue ret = new FloatValue();
|
69 |
ret.setValue(f); |
70 |
|
71 |
return ret;
|
72 |
} |
73 |
|
74 |
/**
|
75 |
* Crea un objeto de tipo Value a partir de un double
|
76 |
*
|
77 |
* @param d valor que se quiere representar
|
78 |
*
|
79 |
* @return objeto Value con el valor que se pasa como par?metro
|
80 |
*/
|
81 |
public static Value createValue(double d) { |
82 |
DoubleValue ret = new DoubleValue();
|
83 |
ret.setValue(d); |
84 |
|
85 |
return ret;
|
86 |
} |
87 |
|
88 |
/**
|
89 |
* Crea un objeto de tipo Date a partir de un Date
|
90 |
*
|
91 |
* @param d valor que se quiere representar
|
92 |
*
|
93 |
* @return objeto Value con el valor que se pasa como par?metro
|
94 |
*/
|
95 |
public static Value createValue(Date d) { |
96 |
DateValue ret = new DateValue();
|
97 |
ret.setValue(d); |
98 |
|
99 |
return ret;
|
100 |
} |
101 |
|
102 |
/**
|
103 |
* Crea un objeto de tipo Value a partir de un booleano
|
104 |
*
|
105 |
* @param b valor que se quiere representar
|
106 |
*
|
107 |
* @return objeto Value con el valor que se pasa como par?metro
|
108 |
*/
|
109 |
public static Value createValue(boolean b) { |
110 |
BooleanValue ret = new BooleanValue();
|
111 |
ret.setValue(b); |
112 |
|
113 |
return ret;
|
114 |
} |
115 |
|
116 |
/**
|
117 |
* Crea un Value a partir de un literal encontrado en una instrucci?n y su
|
118 |
* tipo
|
119 |
*
|
120 |
* @param text Texto del valor
|
121 |
* @param type Tipo del valor
|
122 |
*
|
123 |
* @return Objeto Value del tipo adecuado
|
124 |
*
|
125 |
* @throws SemanticException Si el tipo del literal no est? soportado
|
126 |
*/
|
127 |
public static Value createValue(String text, int type) |
128 |
throws SemanticException {
|
129 |
switch (type) {
|
130 |
case SQLEngineConstants.STRING_LITERAL:
|
131 |
|
132 |
StringValue r1 = new StringValue();
|
133 |
r1.setValue(text); |
134 |
|
135 |
return r1;
|
136 |
|
137 |
case SQLEngineConstants.INTEGER_LITERAL:
|
138 |
|
139 |
try {
|
140 |
IntValue r2 = new IntValue();
|
141 |
r2.setValue(Integer.parseInt(text));
|
142 |
|
143 |
return r2;
|
144 |
} catch (NumberFormatException e) { |
145 |
LongValue r2 = new LongValue();
|
146 |
r2.setValue(Long.parseLong(text));
|
147 |
|
148 |
return r2;
|
149 |
} |
150 |
|
151 |
case SQLEngineConstants.FLOATING_POINT_LITERAL:
|
152 |
|
153 |
try {
|
154 |
FloatValue r2 = new FloatValue();
|
155 |
r2.setValue(Float.parseFloat(text));
|
156 |
|
157 |
return r2;
|
158 |
} catch (NumberFormatException e) { |
159 |
DoubleValue r2 = new DoubleValue();
|
160 |
r2.setValue(Double.parseDouble(text));
|
161 |
|
162 |
return r2;
|
163 |
} |
164 |
|
165 |
default:
|
166 |
throw new SemanticException("Unexpected literal type: " + text + |
167 |
"->" + type);
|
168 |
} |
169 |
} |
170 |
// Se usa para recuperar Value de un XMLEntity
|
171 |
public static Value createValue(String text, String className) throws SemanticException |
172 |
{ |
173 |
if (className.equals("com.hardcode.gdbms.engine.values.BooleanValue")) |
174 |
return createValue(Boolean.getBoolean(text)); |
175 |
if (className.equals("com.hardcode.gdbms.engine.values.DateValue")) |
176 |
return createValue(Date.parse(text)); |
177 |
if (className.equals("com.hardcode.gdbms.engine.values.DoubleValue")) |
178 |
return createValue(Double.parseDouble(text)); |
179 |
if (className.equals("com.hardcode.gdbms.engine.values.FloatValue")) |
180 |
return createValue(Float.parseFloat(text)); |
181 |
if (className.equals("com.hardcode.gdbms.engine.values.IntValue")) |
182 |
return createValue(Integer.parseInt(text)); |
183 |
if (className.equals("com.hardcode.gdbms.engine.values.LongValue")) |
184 |
return createValue(Long.parseLong(text)); |
185 |
if (className.equals("com.hardcode.gdbms.engine.values.StringValue")) |
186 |
return createValue(text);
|
187 |
// default:
|
188 |
throw new SemanticException("Unexpected className in createValue (GDBMS) text: " + text + |
189 |
"-> className: " + className);
|
190 |
|
191 |
} |
192 |
|
193 |
} |