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 / DynObjectSymbolTable.java @ 45739

History | View | Annotate | Download (4.16 KB)

1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2020 gvSIG Association.
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 3
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24
package org.gvsig.expressionevaluator.spi;
25

    
26
import java.util.AbstractMap;
27
import java.util.AbstractSet;
28
import java.util.Iterator;
29
import java.util.Map;
30
import java.util.Set;
31
import org.apache.commons.lang3.tuple.MutablePair;
32
import org.gvsig.expressionevaluator.ExpressionEvaluatorLocator;
33
import org.gvsig.expressionevaluator.ExpressionEvaluatorManager;
34
import org.gvsig.expressionevaluator.MutableSymbolTable;
35
import org.gvsig.tools.ToolsLocator;
36
import org.gvsig.tools.dynobject.DynObject;
37
import org.gvsig.tools.dynobject.DynObjectManager;
38
import org.gvsig.tools.lang.Cloneable;
39

    
40
/**
41
 *
42
 * @author gvSIG Team
43
 */
44
public class DynObjectSymbolTable extends AbstractSymbolTable implements MutableSymbolTable {
45

    
46
    private DynObject obj;
47
    private Map<String, Object> wrapper;
48
    
49
    public DynObjectSymbolTable(String name, DynObject obj) {
50
        super(name);
51
        this.obj = obj;
52
        this.wrapper = createDynObjectToMapAdapter(obj);
53

    
54
        ExpressionEvaluatorManager manager = ExpressionEvaluatorLocator.getManager();
55
        manager.populateSymbolTable(this);
56
    }
57

    
58
    private static Map createDynObjectToMapAdapter(DynObject obj) {
59
        return new AbstractMap<String, Object>() {
60
            @Override
61
            public Set<Map.Entry<String, Object>> entrySet() {
62
                return new AbstractSet<Map.Entry<String, Object>>() {
63
                    @Override
64
                    public Iterator<Map.Entry<String, Object>> iterator() {
65
                        return new Iterator<Map.Entry<String, Object>>() {
66
                            int n=0;
67
                            @Override
68
                            public boolean hasNext() {
69
                                return n < obj.getDynClass().getDynFields().length;
70
                            }
71

    
72
                            @Override
73
                            public Map.Entry<String, Object> next() {
74
                                String name = obj.getDynClass().getDynFields()[n++].getName();
75
                                return new MutablePair<>(name, obj.getDynValue(name));
76
                            }
77
                        };
78
                    }
79

    
80
                    @Override
81
                    public int size() {
82
                        return obj.getDynClass().getDynFields().length;
83
                    }
84
                };
85
            }
86
        };
87
    }
88
    
89
    @Override
90
    protected Map<String, Object> getVars() {
91
        return this.wrapper;
92
    }
93

    
94
    @Override
95
    public void setVar(String name, Object value) {
96
        this.obj.setDynValue(name, value);
97
    }
98

    
99
    @Override
100
    public void removeVar(String name) {
101
        throw new UnsupportedOperationException("Not supported."); 
102
    }
103

    
104
    @Override
105
    public MutableSymbolTable clone() throws CloneNotSupportedException {
106
        DynObjectSymbolTable other = (DynObjectSymbolTable) super.clone();
107
        DynObjectManager dynObjectManager = ToolsLocator.getDynObjectManager();
108
        other.obj = (DynObject) Cloneable.cloneQuietly(this.obj);
109
        if( other.obj == null ) {
110
            other.obj = dynObjectManager.createDynObject(obj.getDynClass());
111
            dynObjectManager.copy(this.obj, other.obj);
112
        }
113
        other.wrapper = createDynObjectToMapAdapter(other.obj);
114
        return other;
115
    }
116

    
117
}