Revision 43983 trunk/org.gvsig.desktop/org.gvsig.desktop.library/org.gvsig.expressionevaluator/org.gvsig.expressionevaluator.lib/org.gvsig.expressionevaluator.lib.impl/src/main/java/org/gvsig/expressionevaluator/impl/DefaultSymbolTable.java

View differences:

DefaultSymbolTable.java
1

  
2 1
package org.gvsig.expressionevaluator.impl;
3 2

  
3
import org.gvsig.expressionevaluator.spi.AbstractSymbolTable;
4
import java.util.ArrayList;
5
import java.util.Collection;
4 6
import java.util.HashMap;
5
import java.util.Iterator;
6
import java.util.Map;
7 7
import org.apache.commons.lang3.StringUtils;
8 8
import org.gvsig.expressionevaluator.ExpressionEvaluatorLocator;
9 9
import org.gvsig.expressionevaluator.ExpressionEvaluatorManager;
10 10
import org.gvsig.expressionevaluator.Function;
11
import org.gvsig.expressionevaluator.SymbolTable;
12 11
import org.gvsig.expressionevaluator.MutableSymbolTable;
13
import org.gvsig.expressionevaluator.Scripts;
14
import org.gvsig.expressionevaluator.impl.function.ScriptFunction;
12
import org.gvsig.tools.script.Script;
15 13

  
14
/**
15
 *
16
 * @author jjdelcerro
17
 */
18
public class DefaultSymbolTable 
19
        extends AbstractSymbolTable
20
        implements MutableSymbolTable 
21
    {
16 22

  
17
public class DefaultSymbolTable extends AbstractSymbolTable implements MutableSymbolTable {
18 23

  
19
    private Map<String,Object> vars;
20
    protected DefaultScripts scripts = null;
21
    
22 24
    public DefaultSymbolTable() {
23
        this(null);
24
    }
25

  
26
    public DefaultSymbolTable(SymbolTable delegatedSymbolTable) {
27
        super(delegatedSymbolTable);
25
        super();
28 26
        this.vars = new HashMap<>();
29
        this.scripts = new DefaultScripts();
27
        this.functions = new HashMap<>();
28
        this.scripts = new ArrayList<>();
30 29
        this.init();
31 30
    }
32

  
33
    private void init() {
31
    
32
    protected final void init() {
34 33
        ExpressionEvaluatorManager manager = ExpressionEvaluatorLocator.getManager();
35
        this.addFunctions(manager.getAllFunctions().values());
36
        this.addFunctions(manager.getAllOperators().values());
34
        this.addSymbolTable(manager.getSymbolTable("SQL"));
35
        this.addSymbolTable(manager.getSymbolTable("OGC"));
37 36
    }
38 37
    
39 38
    @Override
40
    public MutableSymbolTable clone() throws CloneNotSupportedException {
41
        DefaultSymbolTable other = (DefaultSymbolTable) super.clone();
42
        other.vars = new HashMap<>(vars);
43
        other.scripts =(DefaultScripts) scripts.clone();
44
        return other;
39
    public void setVar(String name, Object value) {
40
        if( StringUtils.isEmpty(name) ) {
41
            throw new IllegalArgumentException("name can't be null");
42
        }
43
        this.vars.put(name.toUpperCase(), value);
45 44
    }
46 45

  
47 46
    @Override
48
    public Function function(String name) {
49
        if( StringUtils.isEmpty(name) ) {
50
            return null;
47
    public void addFunction(Function function) {
48
        if( function==null ) {
49
            throw new IllegalArgumentException("function can't be null");
51 50
        }
52
        Function fn = super.function(name);
53
        if( fn == null && !this.scripts.isEmpty() ) {
54
            fn = new ScriptFunction(this.scripts, name);
55
        }
56
        return fn;
51
        this.functions.put(function.name().toUpperCase(), function);
57 52
    }
58
    
53

  
59 54
    @Override
60
    public void setVar(String name, Object value) {
61
        this.vars.put(name, value);
55
    public void addFunctions(Collection<Function> functions) {
56
        for (Function function : functions) {
57
            this.addFunction(function);
58
        }
62 59
    }
63 60

  
64 61
    @Override
65
    public boolean exists(String name) {
66
        boolean e = this.vars.containsKey(name);
67
        if( !e ) {
68
            e = this.delegatedSymbolTable.exists(name);
62
    public void removeVar(String name) {
63
        if( StringUtils.isEmpty(name) ) {
64
            throw new IllegalArgumentException("name can't be null");
69 65
        }
70
        return e;
66
        this.vars.remove(name.toUpperCase());
71 67
    }
72 68

  
73 69
    @Override
74
    public Object value(String name) {
75
        if( this.vars.containsKey(name) ) {
76
            return this.vars.get(name);
70
    public void removeFunction(String name) {
71
        if( StringUtils.isEmpty(name) ) {
72
            throw new IllegalArgumentException("name can't be null");
77 73
        }
78
        return this.delegatedSymbolTable.value(name);
74
        this.functions.remove(name.toUpperCase());
79 75
    }
80 76

  
81 77
    @Override
82
    public void removeVar(String name) {
83
        this.vars.remove(name);
78
    public void addScript(Script script) {
79
        if( script == null ) {
80
            throw new IllegalArgumentException("script can't be null");
81
        }
82
        if (this.scripts.contains(script)) {
83
            return;
84
        }
85
        this.scripts.add(script);
84 86
    }
85 87

  
86 88
    @Override
87
    public Iterator<String> itervars() {
88
        return this.vars.keySet().iterator();
89
    public void removeScript(Script script) {
90
        if( script == null ) {
91
            throw new IllegalArgumentException("script can't be null");
92
        }
93
        this.scripts.remove(script);
89 94
    }
90 95

  
91 96
    @Override
92
    public Scripts getScripts() {
93
        return this.scripts;
97
    public MutableSymbolTable clone() throws CloneNotSupportedException {
98
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
94 99
    }
95 100

  
96 101
}

Also available in: Unified diff