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