Statistics
| Revision:

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 / AbstractSymbolTable.java @ 45620

History | View | Annotate | Download (7.75 KB)

1
package org.gvsig.expressionevaluator.spi;
2

    
3
import java.util.ArrayList;
4
import java.util.Collection;
5
import java.util.Collections;
6
import java.util.HashMap;
7
import java.util.HashSet;
8
import java.util.Iterator;
9
import java.util.List;
10
import java.util.Map;
11
import java.util.Set;
12
import org.apache.commons.lang3.StringUtils;
13
import org.gvsig.expressionevaluator.Function;
14
import org.gvsig.expressionevaluator.SymbolTable;
15

    
16
/**
17
 *
18
 * @author jjdelcerro
19
 */
20
public abstract class AbstractSymbolTable implements SymbolTable {
21

    
22
    private final String name;
23

    
24
//    public class ScriptFunction extends AbstractFunction {
25
//
26
//        public ScriptFunction(String funcName) {
27
//            super("Script", funcName, Range.between(1, Integer.MAX_VALUE));
28
//        }
29
//
30
//        @Override
31
//        public Object call(Interpreter interpreter, Object[] args) throws Exception {
32
//            for (Script script : scripts()) {
33
//                try {
34
//                    return script.invokeFunction(this.name(), args);
35
//                } catch (NoSuchMethodException ex) {
36
//                    // Si la funcion no existe en el script pasamos al siguiente
37
//                    // script.
38
//                }
39
//            }
40
//            throw new NoSuchMethodException("Can't locate funcion '" + this.name() + "'.");
41
//        }
42
//
43
//    }
44

    
45
    private Map<String, Object> vars;
46

    
47
    protected final List<SymbolTable> symbolTables;
48
    protected Map<String, Function> functions;
49
    protected Map<String, Function> functionAlias;
50
//    protected List<Script> scripts;
51

    
52
    public AbstractSymbolTable() {
53
        this(null);
54
    }
55
    
56
    public AbstractSymbolTable(String name) {
57
        this.name = name;
58
        this.symbolTables = new ArrayList<>();
59
        this.vars = null;
60
        this.functions = null;
61
        this.functionAlias = null;
62
    }
63

    
64
    @Override
65
    public String getName() {
66
        return name;
67
    }
68

    
69
    public void addFunction(Function function) {
70
        if (function == null) {
71
            throw new IllegalArgumentException("function can't be null");
72
        }
73
        this.getFunctions().put(function.name().toUpperCase(), function);
74
        List<String> aliases = function.aliases();
75
        if( aliases == null || aliases.isEmpty() ) {
76
            return;
77
        }
78
        Map<String, Function> theFunctionAlias = this.getFunctionAlias();
79
        for (String alias : aliases) {
80
            if( alias!=null ) {
81
                theFunctionAlias.put(alias, function);
82
            }
83
        }
84
    }
85
    
86
    public void addFunctions(Collection<Function> functions) {
87
        for (Function function : functions) {
88
            this.addFunction(function);
89
        }
90
    }
91

    
92
    public void removeFunction(String name) {
93
        if( StringUtils.isEmpty(name) ) {
94
            throw new IllegalArgumentException("name can't be null");
95
        }
96
        this.functions.remove(name.toUpperCase());
97
    }
98
    
99
    @Override
100
    public boolean addSymbolTable(SymbolTable symbolTable) {
101
        if( symbolTable==null ) {
102
          throw new IllegalArgumentException("Invalid symbol table (null)");
103
        }
104
        if (this.symbolTables.contains(symbolTable)) {
105
            return false;
106
        }
107
        this.symbolTables.add(0,symbolTable);
108
        return true;
109
    }
110

    
111
    @Override
112
    public boolean containsSymbolTable(SymbolTable symbolTable) {
113
        return this.symbolTables.contains(symbolTable);
114
    }
115
    
116
    @Override
117
    public boolean removeSymbolTable(SymbolTable symbolTable) {
118
        boolean n = this.symbolTables.remove(symbolTable);
119
        return n;
120
    }
121
    
122
    protected Map<String, Object> getVars() {
123
        if (this.vars == null) {
124
            this.vars = new HashMap<>();
125
        }
126
        return this.vars;
127
    }
128

    
129
    public void setVar(String name, Object value) {
130
        if( StringUtils.isEmpty(name) ) {
131
            throw new IllegalArgumentException("name can't be null");
132
        }
133
        this.getVars().put(name.toUpperCase(), value);
134
    }
135

    
136
    public void removeVar(String name) {
137
        if( StringUtils.isEmpty(name) ) {
138
            throw new IllegalArgumentException("name can't be null");
139
        }
140
        this.getVars().remove(name.toUpperCase());
141
    }
142

    
143
    protected Map<String, Function> getFunctions() {
144
        if (this.functions == null) {
145
            this.functions = new HashMap<>();
146
        }
147
        return this.functions;
148
    }
149

    
150
    protected Map<String, Function> getFunctionAlias() {
151
        if (this.functionAlias == null) {
152
            this.functionAlias = new HashMap<>();
153
        }
154
        return this.functionAlias;
155
    }
156

    
157
    @Override
158
    public boolean exists(String name) {
159
        if (StringUtils.isEmpty(name)) {
160
            return false;
161
        }
162
        if (this.getVars().containsKey(name.toUpperCase())) {
163
            return true;
164
        }
165
        for (SymbolTable other : this.symbolTables) {
166
            if (other.exists(name)) {
167
                return true;
168
            }
169
        }
170
        return false;
171
    }
172

    
173
    @Override
174
    public Object value(String name) {
175
        if (StringUtils.isEmpty(name)) {
176
            return null;
177
        }
178
        name = name.toUpperCase();
179
        if (this.getVars().containsKey(name)) {
180
            return this.getVars().get(name);
181
        }
182
        for (SymbolTable other : this.symbolTables) {
183
            if (other.exists(name)) {
184
                return other.value(name);
185
            }
186
        }
187
        return null;
188
    }
189

    
190
    @Override
191
    public Function function(String name) {
192
        if (StringUtils.isEmpty(name)) {
193
            return null;
194
        }
195
        if (this.functions != null) {
196
            name = name.toUpperCase();
197
            if (this.functions.containsKey(name)) {
198
                return this.functions.get(name);
199
            }
200
        }
201
        if (this.functionAlias != null) {
202
            name = name.toUpperCase();
203
            if (this.functionAlias.containsKey(name)) {
204
                return this.functionAlias.get(name);
205
            }
206
        }
207
        for (SymbolTable other : this.symbolTables) {
208
            Function fn = other.function(name);
209
            if (fn != null) {
210
                return fn;
211
            }
212
        }
213
        return null;
214
    }
215

    
216
    @Override
217
    public Collection<String> variables() {
218
        Set<String> theVars = new HashSet<>();
219
        for (SymbolTable symbolTable : this.symbolTables) {
220
            theVars.addAll(symbolTable.variables());
221
        }
222
        theVars.addAll(this.localvariables());
223
        return Collections.unmodifiableCollection(theVars);
224
    }
225

    
226
    @Override
227
    public Collection<Function> functions() {
228
        Set<Function> theFunctions = new HashSet<>();
229
        for (SymbolTable symbolTable : this.symbolTables) {
230
            theFunctions.addAll(symbolTable.functions());
231
        }
232
        if (this.functions != null) {
233
            theFunctions.addAll(this.functions.values());
234
        }
235
        return Collections.unmodifiableCollection(theFunctions);
236
    }
237

    
238
    @Override
239
    public Collection<Function> localfunctions() {
240
        if( this.functions == null ) {
241
            return Collections.EMPTY_LIST;
242
        }
243
        return Collections.unmodifiableCollection(this.functions.values());
244
    }
245

    
246
    @Override
247
    public Collection<String> localvariables() {
248
        if( this.getVars().size() == 0) {
249
            return Collections.EMPTY_LIST;
250
        }
251
        return Collections.unmodifiableCollection(this.vars.keySet());
252
    }
253

    
254
    @Override
255
    public Iterator<Function> iterator() {
256
        return this.functions().iterator();
257
    }
258

    
259
    @Override
260
    public SymbolTable clone() throws CloneNotSupportedException {
261
        SymbolTable other = (SymbolTable) super.clone();
262
        return other;
263
    }
264

    
265
    @Override
266
    public boolean isSQLCompatible(String name) {
267
        Function f = function(name);
268
        if( f!=null ) {
269
            return f.isSQLCompatible();
270
        }
271
        return true;
272
    }
273

    
274
}