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 @ 45739

History | View | Annotate | Download (7.69 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
import org.gvsig.tools.util.GetItemByKey;
16

    
17
/**
18
 *
19
 * @author jjdelcerro
20
 */
21
public abstract class AbstractSymbolTable implements SymbolTable, GetItemByKey<String, Object> {
22

    
23
    private final String name;
24

    
25

    
26
    private Map<String, Object> vars;
27

    
28
    protected final List<SymbolTable> symbolTables;
29
    protected Map<String, Function> functions;
30
    protected Map<String, Function> functionAlias;
31

    
32
    public AbstractSymbolTable() {
33
        this(null);
34
    }
35
    
36
    public AbstractSymbolTable(String name) {
37
        if( name == null ) {
38
            this.name = "Anonymous";
39
        } else {
40
            this.name = name;
41
        }
42
        this.symbolTables = new ArrayList<>();
43
        this.vars = null;
44
        this.functions = null;
45
        this.functionAlias = null;
46
    }
47

    
48
    @Override
49
    public String getName() {
50
        return name;
51
    }
52

    
53
    public void addFunction(Function function) {
54
        if (function == null) {
55
            throw new IllegalArgumentException("function can't be null");
56
        }
57
        this.getFunctions().put(function.name().toUpperCase(), function);
58
        List<String> aliases = function.aliases();
59
        if( aliases == null || aliases.isEmpty() ) {
60
            return;
61
        }
62
        Map<String, Function> theFunctionAlias = this.getFunctionAlias();
63
        for (String alias : aliases) {
64
            if( alias!=null ) {
65
                theFunctionAlias.put(alias, function);
66
            }
67
        }
68
    }
69
    
70
    public void addFunctions(Collection<Function> functions) {
71
        for (Function function : functions) {
72
            this.addFunction(function);
73
        }
74
    }
75

    
76
    public void removeFunction(String name) {
77
        if( StringUtils.isEmpty(name) ) {
78
            throw new IllegalArgumentException("name can't be null");
79
        }
80
        this.functions.remove(name.toUpperCase());
81
    }
82
    
83
    @Override
84
    public boolean addSymbolTable(SymbolTable symbolTable) {
85
        if( symbolTable==null ) {
86
          throw new IllegalArgumentException("Invalid symbol table (null)");
87
        }
88
        if (this.symbolTables.contains(symbolTable)) {
89
            return false;
90
        }
91
        this.symbolTables.add(0,symbolTable);
92
        return true;
93
    }
94

    
95
    @Override
96
    public boolean containsSymbolTable(SymbolTable symbolTable) {
97
        return this.symbolTables.contains(symbolTable);
98
    }
99
    
100
    @Override
101
    public boolean removeSymbolTable(SymbolTable symbolTable) {
102
        boolean n = this.symbolTables.remove(symbolTable);
103
        return n;
104
    }
105
    
106
    protected Map<String, Object> getVars() {
107
        if (this.vars == null) {
108
            this.vars = new HashMap<>();
109
        }
110
        return this.vars;
111
    }
112

    
113
    public void setVar(String name, Object value) {
114
        if( StringUtils.isEmpty(name) ) {
115
            throw new IllegalArgumentException("name can't be null");
116
        }
117
        this.getVars().put(name.toUpperCase(), value);
118
    }
119

    
120
    public void removeVar(String name) {
121
        if( StringUtils.isEmpty(name) ) {
122
            throw new IllegalArgumentException("name can't be null");
123
        }
124
        this.getVars().remove(name.toUpperCase());
125
    }
126

    
127
    protected Map<String, Function> getFunctions() {
128
        if (this.functions == null) {
129
            this.functions = new HashMap<>();
130
        }
131
        return this.functions;
132
    }
133

    
134
    protected Map<String, Function> getFunctionAlias() {
135
        if (this.functionAlias == null) {
136
            this.functionAlias = new HashMap<>();
137
        }
138
        return this.functionAlias;
139
    }
140

    
141
    @Override
142
    public boolean exists(String name) {
143
        if (StringUtils.isEmpty(name)) {
144
            return false;
145
        }
146
        if (this.getVars().containsKey(name.toUpperCase())) {
147
            return true;
148
        }
149
        if( StringUtils.equalsIgnoreCase(name, "$symboltable") )  {
150
            return true;
151
        }
152
        for (SymbolTable other : this.symbolTables) {
153
            if (other.exists(name)) {
154
                return true;
155
            }
156
        }
157
        return false;
158
    }
159

    
160
    @Override
161
    public Object value(String name) {
162
        if (StringUtils.isEmpty(name)) {
163
            return null;
164
        }
165
        name = name.toUpperCase();
166
        if (this.getVars().containsKey(name)) {
167
            return this.getVars().get(name);
168
        }
169
        if( name.equals("$SYMBOLTABLE") ) {
170
            return this;
171
        }
172
        for (SymbolTable other : this.symbolTables) {
173
            if (other.exists(name)) {
174
                return other.value(name);
175
            }
176
            if( StringUtils.equalsIgnoreCase(name, other.getName())) {
177
                return other;
178
            }
179
        }
180
        return null;
181
    }
182

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

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

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

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

    
244
    @Override
245
    public Collection<String> localvariables() {
246
        if( this.getVars().isEmpty()) {
247
            return Collections.EMPTY_LIST;
248
        }
249
        return Collections.unmodifiableCollection(this.vars.keySet());
250
    }
251

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

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

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

    
272
    public List<SymbolTable> getSymbolTables() {
273
        return Collections.unmodifiableList(symbolTables);
274
    }
275

    
276
    @Override
277
    public String toString() {
278
        return this.name;
279
    }
280

    
281
}