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 @ 43521
History | View | Annotate | Download (5.2 KB)
1 | 43512 | jjdelcerro | 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.Range; |
||
7 | import org.gvsig.expressionevaluator.Function; |
||
8 | import org.gvsig.fmap.geom.Geometry; |
||
9 | 43520 | jjdelcerro | import org.gvsig.fmap.geom.primitive.Point; |
10 | 43512 | jjdelcerro | |
11 | public abstract class AbstractFunction implements Function { |
||
12 | 43521 | jjdelcerro | |
13 | 43512 | jjdelcerro | private final String name; |
14 | private final String group; |
||
15 | private final Range argc; |
||
16 | private final String description; |
||
17 | private final String[] descriptionArgs; |
||
18 | private List<String> alias; |
||
19 | 43521 | jjdelcerro | private String template; |
20 | 43512 | jjdelcerro | |
21 | 43521 | jjdelcerro | protected AbstractFunction(String group, String name, Range argc, String description, String template, String[] descriptionArgs) { |
22 | 43512 | jjdelcerro | this.name = name;
|
23 | this.group = group;
|
||
24 | this.argc = argc;
|
||
25 | this.description = description;
|
||
26 | 43521 | jjdelcerro | this.template = template;
|
27 | 43512 | jjdelcerro | this.descriptionArgs = descriptionArgs;
|
28 | } |
||
29 | |||
30 | 43521 | jjdelcerro | protected AbstractFunction(String group, String name, Range argc, String desription, String template) { |
31 | this(group, name, argc, desription, template, null); |
||
32 | } |
||
33 | |||
34 | 43512 | jjdelcerro | protected AbstractFunction(String group, String name, Range argc) { |
35 | 43521 | jjdelcerro | this(group, name, argc, null, null, null); |
36 | 43512 | jjdelcerro | } |
37 | |||
38 | @Override
|
||
39 | public String name() { |
||
40 | return this.name; |
||
41 | } |
||
42 | |||
43 | @Override
|
||
44 | public String group() { |
||
45 | return this.group; |
||
46 | } |
||
47 | |||
48 | @Override
|
||
49 | public Range argc() {
|
||
50 | return argc;
|
||
51 | } |
||
52 | |||
53 | @Override
|
||
54 | public String description() { |
||
55 | return description;
|
||
56 | } |
||
57 | |||
58 | @Override
|
||
59 | public String[] descriptionArgs() { |
||
60 | return descriptionArgs;
|
||
61 | } |
||
62 | |||
63 | @Override
|
||
64 | public void addAlias(String name) { |
||
65 | if( this.alias == null ) { |
||
66 | this.alias = new ArrayList<>(); |
||
67 | } |
||
68 | this.alias.add(name);
|
||
69 | } |
||
70 | |||
71 | @Override
|
||
72 | public List<String> alias() { |
||
73 | return this.alias; |
||
74 | } |
||
75 | |||
76 | 43521 | jjdelcerro | @Override
|
77 | public String template() { |
||
78 | return this.template; |
||
79 | } |
||
80 | |||
81 | @Override
|
||
82 | public boolean isOperator() { |
||
83 | return false; |
||
84 | } |
||
85 | |||
86 | 43512 | jjdelcerro | protected int getInt(Object args[], int n) { |
87 | if( args.length < n ) {
|
||
88 | throw new IllegalArgumentException("Required argument "+n+" and only found " + args.length +" arguments in call to '"+name()+"'."); |
||
89 | } |
||
90 | if( !(args[n] instanceof Number) ) { |
||
91 | String type = (args[n]==null)? "null" : args[n].getClass().getCanonicalName(); |
||
92 | throw new IllegalArgumentException("Type not allowed for argument " + n + " in '" + name() + "' function, expected Number and got " + type + "."); |
||
93 | } |
||
94 | return ((Number)args[n]).intValue(); |
||
95 | } |
||
96 | |||
97 | protected long getLong(Object args[], int n) { |
||
98 | if( args.length < n ) {
|
||
99 | throw new IllegalArgumentException("Required argument "+n+" and only found " + args.length +" arguments in call to '"+name()+"'."); |
||
100 | } |
||
101 | if( !(args[n] instanceof Number) ) { |
||
102 | String type = (args[n]==null)? "null" : args[n].getClass().getCanonicalName(); |
||
103 | throw new IllegalArgumentException("Type not allowed for argument " + n + " in '" + name() + "' function, expected Number and got " + type + "."); |
||
104 | } |
||
105 | return ((Number)args[n]).longValue(); |
||
106 | } |
||
107 | |||
108 | protected double getDouble(Object args[], int n) { |
||
109 | if( args.length < n ) {
|
||
110 | throw new IllegalArgumentException("Required argument "+n+" and only found " + args.length +" arguments in call to '"+name()+"'."); |
||
111 | } |
||
112 | if( !(args[n] instanceof Number) ) { |
||
113 | String type = (args[n]==null)? "null" : args[n].getClass().getCanonicalName(); |
||
114 | throw new IllegalArgumentException("Type not allowed for argument " + n + " in '" + name() + "' function, expected Number and got " + type + "."); |
||
115 | } |
||
116 | return ((Number)args[n]).doubleValue(); |
||
117 | } |
||
118 | |||
119 | protected String getStr(Object args[], int n) { |
||
120 | if( args.length < n ) {
|
||
121 | throw new IllegalArgumentException("Required argument "+n+" and only found " + args.length +" arguments in call to '"+name()+"'."); |
||
122 | } |
||
123 | return Objects.toString(args[n], ""); |
||
124 | } |
||
125 | |||
126 | protected Geometry getGeom(Object[] args, int n) { |
||
127 | if( args.length < n ) {
|
||
128 | throw new IllegalArgumentException("Required argument "+n+" and only found " + args.length +" arguments in call to '"+name()+"'."); |
||
129 | } |
||
130 | if( !(args[n] instanceof Geometry) ) { |
||
131 | String type = (args[n]==null)? "null" : args[n].getClass().getCanonicalName(); |
||
132 | throw new IllegalArgumentException("Type not allowed for argument " + n + " in '" + name() + "' function, expected Geometry and got " + type + "."); |
||
133 | } |
||
134 | return (Geometry)args[n];
|
||
135 | } |
||
136 | 43520 | jjdelcerro | |
137 | protected Point getPoint(Object[] args, int n) { |
||
138 | if( args.length < n ) {
|
||
139 | throw new IllegalArgumentException("Required argument "+n+" and only found " + args.length +" arguments in call to '"+name()+"'."); |
||
140 | } |
||
141 | if( !(args[n] instanceof Point) ) { |
||
142 | String type = (args[n]==null)? "null" : args[n].getClass().getCanonicalName(); |
||
143 | throw new IllegalArgumentException("Type not allowed for argument " + n + " in '" + name() + "' function, expected Point and got " + type + "."); |
||
144 | } |
||
145 | return (Point)args[n]; |
||
146 | } |
||
147 | 43512 | jjdelcerro | } |