Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.plugin / org.gvsig.app / org.gvsig.app.mainplugin / src / main / java / org / gvsig / app / project / symboltables / ListOfFeaturesWrapper.java @ 44207

History | View | Annotate | Download (2.94 KB)

1
package org.gvsig.app.project.symboltables;
2

    
3
import java.util.Iterator;
4
import java.util.List;
5
import java.util.NoSuchElementException;
6
import org.gvsig.expressionevaluator.Expression;
7
import org.gvsig.expressionevaluator.ExpressionEvaluatorLocator;
8
import org.gvsig.expressionevaluator.ExpressionEvaluatorManager;
9
import org.gvsig.expressionevaluator.MutableSymbolTable;
10
import org.gvsig.expressionevaluator.SymbolTable;
11
import org.gvsig.fmap.dal.DALLocator;
12
import org.gvsig.fmap.dal.DataManager;
13
import org.gvsig.fmap.dal.expressionevaluator.FeatureSymbolTable;
14
import org.gvsig.fmap.dal.feature.Feature;
15
import org.gvsig.tools.util.UnmodifiableBasicList;
16
import org.gvsig.tools.util.UnmodifiableBasicListToListAdapter;
17

    
18
/**
19
 *
20
 * @author jjdelcerro
21
 */
22
public class ListOfFeaturesWrapper implements UnmodifiableBasicList<Object> {
23

    
24
    private final List<Feature> features;
25
    private final FeatureSymbolTable featureSymbolTable;
26
    private final MutableSymbolTable symbolTable;
27
    private final Expression expression;
28
    
29
    public ListOfFeaturesWrapper(
30
            List<Feature> features, 
31
            SymbolTable symbolTable, 
32
            String expression
33
        ) {
34
        DataManager dataManager = DALLocator.getDataManager();
35
        ExpressionEvaluatorManager expManager = ExpressionEvaluatorLocator.getManager();
36
        
37
        this.features = features;
38
        this.featureSymbolTable = dataManager.createFeatureSymbolTable();
39
        this.symbolTable = this.featureSymbolTable.createParent();
40
        this.symbolTable.addSymbolTable(symbolTable);
41

    
42
        this.expression = expManager.createExpression();
43
        this.expression.setPhrase(expression);
44
    }
45

    
46
    private Object getValue(Feature feature) {
47
        this.featureSymbolTable.setFeature(feature);
48
        Object value = this.expression.execute(this.symbolTable);
49
        return value;
50
    }
51
    
52
    @Override
53
    public boolean isEmpty() {
54
        return this.features.isEmpty();
55
    }
56

    
57
    @Override
58
    public List<Object> toList() {
59
        List<Object> x = new UnmodifiableBasicListToListAdapter<>(this);
60
        return x;
61
    }
62

    
63
    @Override
64
    public Object get(int position) {
65
        return this.getValue(this.features.get(position));
66
    }
67

    
68
    @Override
69
    public int size() {
70
        return this.features.size();
71
    }
72

    
73
    @Override
74
    public Iterator<Object> iterator() {
75
        return new Itr();
76
    }
77
    
78
    private class Itr implements Iterator<Object> {
79
        int cursor;       // index of next element to return
80

    
81
        Itr() {}
82

    
83
        @Override
84
        public boolean hasNext() {
85
            return cursor != size();
86
        }
87

    
88
        @Override
89
        public Object next() {
90
            int i = cursor;
91
            if (i >= size())
92
                throw new NoSuchElementException();
93
            cursor = i + 1;
94
            return get(i);
95
        }
96

    
97
        @Override
98
        public void remove() {
99
            throw new UnsupportedOperationException("Not supported yet."); 
100
        }
101
    }
102
}