svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.library / org.gvsig.expressionevaluator / org.gvsig.expressionevaluator.lib / org.gvsig.expressionevaluator.lib.api / src / main / java / org / gvsig / expressionevaluator / spi / AbstractFunction.java @ 43989
History | View | Annotate | Download (8.18 KB)
1 |
package org.gvsig.expressionevaluator.spi; |
---|---|
2 |
|
3 |
import java.util.ArrayList; |
4 |
import java.util.List; |
5 |
import java.util.Objects; |
6 |
import org.apache.commons.lang3.BooleanUtils; |
7 |
import org.apache.commons.lang3.Range; |
8 |
import org.apache.commons.math.util.MathUtils; |
9 |
import org.gvsig.expressionevaluator.Code; |
10 |
import org.gvsig.expressionevaluator.Code.Caller.Arguments; |
11 |
import org.gvsig.expressionevaluator.Function; |
12 |
import org.gvsig.expressionevaluator.Interpreter; |
13 |
import org.gvsig.fmap.geom.Geometry; |
14 |
import org.gvsig.fmap.geom.primitive.Point; |
15 |
|
16 |
public abstract class AbstractFunction implements Function { |
17 |
|
18 |
private final String name; |
19 |
private final String group; |
20 |
private final Range argc; |
21 |
private final String description; |
22 |
private final String[] descriptionArgs; |
23 |
private List<String> alias; |
24 |
private String template; |
25 |
private String returnType; |
26 |
private boolean sqlCompatible; |
27 |
|
28 |
protected AbstractFunction(String group, String name, Range argc, String description, String template, String[] descriptionArgs, String returnType, boolean sqlCompatible) { |
29 |
this.name = name;
|
30 |
this.group = group;
|
31 |
this.argc = argc;
|
32 |
this.description = description;
|
33 |
this.template = template;
|
34 |
this.descriptionArgs = descriptionArgs;
|
35 |
this.returnType = returnType;
|
36 |
this.sqlCompatible = sqlCompatible;
|
37 |
} |
38 |
protected AbstractFunction(String group, String name, Range argc, String description, String template, String[] descriptionArgs, String returnType) { |
39 |
this(group, name, argc, description, template, descriptionArgs, returnType, false); |
40 |
} |
41 |
|
42 |
protected AbstractFunction(String group, String name, Range argc, String description, String template, String[] descriptionArgs) { |
43 |
this(group, name, argc, description, template, null, null); |
44 |
} |
45 |
|
46 |
protected AbstractFunction(String group, String name, Range argc, String description, String template) { |
47 |
this(group, name, argc, description, template, null, null); |
48 |
} |
49 |
|
50 |
protected AbstractFunction(String group, String name, Range argc) { |
51 |
this(group, name, argc, null, null, null, null); |
52 |
} |
53 |
|
54 |
@Override
|
55 |
public String name() { |
56 |
return this.name; |
57 |
} |
58 |
|
59 |
@Override
|
60 |
public String returnType() { |
61 |
return this.returnType; |
62 |
} |
63 |
|
64 |
@Override
|
65 |
public String group() { |
66 |
return this.group; |
67 |
} |
68 |
|
69 |
@Override
|
70 |
public Range argc() {
|
71 |
return argc;
|
72 |
} |
73 |
|
74 |
@Override
|
75 |
public String description() { |
76 |
return description;
|
77 |
} |
78 |
|
79 |
@Override
|
80 |
public String[] descriptionArgs() { |
81 |
return descriptionArgs;
|
82 |
} |
83 |
|
84 |
@Override
|
85 |
public void addAlias(String name) { |
86 |
if( this.alias == null ) { |
87 |
this.alias = new ArrayList<>(); |
88 |
} |
89 |
this.alias.add(name);
|
90 |
} |
91 |
|
92 |
@Override
|
93 |
public List<String> alias() { |
94 |
return this.alias; |
95 |
} |
96 |
|
97 |
@Override
|
98 |
public String template() { |
99 |
return this.template; |
100 |
} |
101 |
|
102 |
@Override
|
103 |
public boolean isOperator() { |
104 |
return false; |
105 |
} |
106 |
|
107 |
@Override
|
108 |
public boolean useArgumentsInsteadObjects() { |
109 |
return false; |
110 |
} |
111 |
|
112 |
@Override
|
113 |
public boolean isSQLCompatible() { |
114 |
return sqlCompatible;
|
115 |
} |
116 |
|
117 |
@Override
|
118 |
public Object call(Interpreter interpreter, Arguments args) throws Exception { |
119 |
return null; |
120 |
} |
121 |
|
122 |
protected int getInt(Object args[], int n) { |
123 |
if( args.length < n ) {
|
124 |
throw new IllegalArgumentException("Required argument "+n+" and only found " + args.length +" arguments in call to '"+name()+"'."); |
125 |
} |
126 |
if( !(args[n] instanceof Number) ) { |
127 |
String type = (args[n]==null)? "null" : args[n].getClass().getCanonicalName(); |
128 |
throw new IllegalArgumentException("Type not allowed for argument " + n + " in '" + name() + "' function, expected Number and got " + type + "."); |
129 |
} |
130 |
return ((Number)args[n]).intValue(); |
131 |
} |
132 |
|
133 |
protected long getLong(Object args[], int n) { |
134 |
if( args.length < n ) {
|
135 |
throw new IllegalArgumentException("Required argument "+n+" and only found " + args.length +" arguments in call to '"+name()+"'."); |
136 |
} |
137 |
if( !(args[n] instanceof Number) ) { |
138 |
String type = (args[n]==null)? "null" : args[n].getClass().getCanonicalName(); |
139 |
throw new IllegalArgumentException("Type not allowed for argument " + n + " in '" + name() + "' function, expected Number and got " + type + "."); |
140 |
} |
141 |
return ((Number)args[n]).longValue(); |
142 |
} |
143 |
|
144 |
protected double getDouble(Object args[], int n) { |
145 |
if( args.length < n ) {
|
146 |
throw new IllegalArgumentException("Required argument "+n+" and only found " + args.length +" arguments in call to '"+name()+"'."); |
147 |
} |
148 |
if( !(args[n] instanceof Number) ) { |
149 |
String type = (args[n]==null)? "null" : args[n].getClass().getCanonicalName(); |
150 |
throw new IllegalArgumentException("Type not allowed for argument " + n + " in '" + name() + "' function, expected Number and got " + type + "."); |
151 |
} |
152 |
return ((Number)args[n]).doubleValue(); |
153 |
} |
154 |
|
155 |
protected String getStr(Object args[], int n) { |
156 |
if( args.length < n ) {
|
157 |
throw new IllegalArgumentException("Required argument "+n+" and only found " + args.length +" arguments in call to '"+name()+"'."); |
158 |
} |
159 |
return Objects.toString(args[n], ""); |
160 |
} |
161 |
|
162 |
protected Object getObject(Object args[], int n) { |
163 |
if( args.length < n ) {
|
164 |
throw new IllegalArgumentException("Required argument "+n+" and only found " + args.length +" arguments in call to '"+name()+"'."); |
165 |
} |
166 |
return args[n];
|
167 |
} |
168 |
|
169 |
protected Object getObject(Interpreter interpreter, Arguments args, int n) { |
170 |
if( args.count() < n ) {
|
171 |
throw new IllegalArgumentException("Required argument "+n+" and only found " + args.count() +" arguments in call to '"+name()+"'."); |
172 |
} |
173 |
Code arg = args.get(n); |
174 |
Object value = interpreter.run(arg);
|
175 |
return value;
|
176 |
} |
177 |
|
178 |
protected Geometry getGeom(Object[] args, int n) { |
179 |
if( args.length < n ) {
|
180 |
throw new IllegalArgumentException("Required argument "+n+" and only found " + args.length +" arguments in call to '"+name()+"'."); |
181 |
} |
182 |
if( !(args[n] instanceof Geometry) ) { |
183 |
String type = (args[n]==null)? "null" : args[n].getClass().getCanonicalName(); |
184 |
throw new IllegalArgumentException("Type not allowed for argument " + n + " in '" + name() + "' function, expected Geometry and got " + type + "."); |
185 |
} |
186 |
return (Geometry)args[n];
|
187 |
} |
188 |
|
189 |
protected Point getPoint(Object[] args, int n) { |
190 |
if( args.length < n ) {
|
191 |
throw new IllegalArgumentException("Required argument "+n+" and only found " + args.length +" arguments in call to '"+name()+"'."); |
192 |
} |
193 |
if( !(args[n] instanceof Point) ) { |
194 |
String type = (args[n]==null)? "null" : args[n].getClass().getCanonicalName(); |
195 |
throw new IllegalArgumentException("Type not allowed for argument " + n + " in '" + name() + "' function, expected Point and got " + type + "."); |
196 |
} |
197 |
return (Point)args[n]; |
198 |
} |
199 |
|
200 |
protected boolean getBoolean(Object args[], int n, Double accuracy) { |
201 |
if( args.length < n ) {
|
202 |
throw new IllegalArgumentException("Required argument "+n+" and only found " + args.length +" arguments in call to '"+name()+"'."); |
203 |
} |
204 |
Object value = args[n];
|
205 |
return toBoolean(value, accuracy);
|
206 |
} |
207 |
|
208 |
protected boolean getBoolean(Interpreter interpreter, Arguments args, int n) { |
209 |
Object value = getObject(interpreter, args, n);
|
210 |
return toBoolean(value, interpreter.getAccuracy());
|
211 |
} |
212 |
|
213 |
protected boolean toBoolean(Object value, Double accuracy) { |
214 |
if( value == null ) { |
215 |
return false; |
216 |
} |
217 |
if( value instanceof Boolean ) { |
218 |
return (Boolean)value; |
219 |
} |
220 |
if( value instanceof Number ) { |
221 |
return MathUtils.compareTo(
|
222 |
((Number) value).doubleValue(),
|
223 |
0,
|
224 |
accuracy==null? MathUtils.EPSILON:accuracy
|
225 |
) == 0;
|
226 |
} |
227 |
return BooleanUtils.toBoolean(value.toString());
|
228 |
} |
229 |
} |