Statistics
| Revision:

svn-gvsig-desktop / branches / v2_0_0_prep / libraries / libFMap_dal / src / org / gvsig / fmap / dal / feature / impl / IndexFeatureSet.java @ 26252

History | View | Annotate | Download (5.82 KB)

1
/* gvSIG. Geographic Information System of the Valencian Government
2
*
3
* Copyright (C) 2007-2008 Infrastructures and Transports Department
4
* of the Valencian Government (CIT)
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 2
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
*/
22

    
23
/*
24
* AUTHORS (In addition to CIT):
25
* 2008 {{Company}}   {{Task}}
26
*/
27

    
28

    
29
package org.gvsig.fmap.dal.feature.impl;
30

    
31
import java.util.ArrayList;
32
import java.util.Collections;
33
import java.util.Iterator;
34
import java.util.List;
35

    
36
import org.gvsig.fmap.dal.DataStore;
37
import org.gvsig.fmap.dal.exception.DataException;
38
import org.gvsig.fmap.dal.exception.ReadRuntimeException;
39
import org.gvsig.fmap.dal.feature.EditableFeature;
40
import org.gvsig.fmap.dal.feature.Feature;
41
import org.gvsig.fmap.dal.feature.FeatureReference;
42
import org.gvsig.fmap.dal.feature.FeatureSet;
43
import org.gvsig.fmap.dal.feature.FeatureType;
44
import org.gvsig.fmap.dal.feature.spi.FeatureData;
45
import org.gvsig.fmap.dal.feature.spi.FeatureReferenceProviderServices;
46
import org.gvsig.fmap.dal.feature.spi.FeatureSetProvider;
47
import org.gvsig.fmap.dal.feature.spi.FeatureStoreProvider;
48
import org.gvsig.fmap.dal.feature.spi.FeatureStoreProviderServices;
49
import org.gvsig.fmap.dal.feature.spi.LongList;
50
import org.gvsig.fmap.dal.feature.spi.index.FeatureIndexProviderServices;
51
import org.gvsig.tools.exception.BaseException;
52
import org.gvsig.tools.visitor.Visitor;
53

    
54

    
55
public class IndexFeatureSet implements FeatureSet, FeatureSetProvider {
56

    
57
        LongList featureReferences = null;
58
        FeatureStoreProvider storeProvider = null;
59
        FeatureStoreProviderServices store = null;
60
        FeatureIndexProviderServices index = null;
61
        List featureTypes = null;
62

    
63
        public class IndexIterator implements Iterator {
64
                Iterator it = null;
65

    
66
                public IndexIterator(Iterator it) {
67
                        this.it = it;
68
                }
69

    
70
                public boolean hasNext() {
71
                        return it.hasNext();
72
                }
73

    
74
                public Object next() {
75
                        Object oid = it.next();
76
                        FeatureReference ref = new DefaultFeatureReference(store, oid);
77
                        try {
78
                                return store.getFeatureByReference(ref);
79
                        } catch (DataException e) {
80
                                throw new ReadRuntimeException(store.getName(), e);
81
                        }
82
                }
83

    
84
                public void remove() {
85
                        throw new UnsupportedOperationException();
86
                }
87
        }
88

    
89
        public class FastIndexIterator implements Iterator {
90
                Iterator it = null;
91
                DefaultFeature feature = null;
92

    
93
                public FastIndexIterator(Iterator it) throws DataException {
94
                        this.it = it;
95
                        feature = (DefaultFeature) store.createFeature(storeProvider
96
                                        .createFeatureData(index.getFeatureType()));
97
                }
98

    
99
                public boolean hasNext() {
100
                        return it.hasNext();
101
                }
102

    
103
                public Object next() {
104
                        Object oid = it.next();
105
                        try {
106
                                //                                Long longer=new Long(((Integer)oid).longValue());
107
                                FeatureReference ref = new DefaultFeatureReference(store, oid);
108
                                FeatureData data = storeProvider
109
                                                .getFeatureDataByReference((FeatureReferenceProviderServices) ref);
110

    
111
                                feature.setData(data);
112

    
113
                                return feature;
114
                        } catch (DataException e) {
115
                                throw new ReadRuntimeException(store.getName(), e);
116
                        }
117
                }
118

    
119
                public void remove() {
120
                        throw new UnsupportedOperationException();
121
                }
122
        }
123

    
124
        public IndexFeatureSet(FeatureIndexProviderServices index, LongList featureReferences) {
125
                this.featureReferences = featureReferences;
126
                this.store = index.getFeatureStore();
127
                this.storeProvider = store.getProvider();
128
                this.index = index;
129
        }
130

    
131
        public boolean canFilter() {
132
                return false;
133
        }
134

    
135
        public boolean canIterateFromIndex() {
136
                return true;
137
        }
138

    
139
        public boolean canOrder() {
140
                return false;
141
        }
142

    
143
        public Iterator fastIterator(long index) throws DataException {
144
                if (store.isEditing()) {
145
                        return this.iterator(index);
146
                }
147
                return new FastIndexIterator(this.featureReferences.iterator(index));
148
        }
149

    
150
        public Iterator fastIterator() throws DataException {
151
                if (store.isEditing()) {
152
                        return this.iterator();
153
                }
154
                return new FastIndexIterator(this.featureReferences.iterator());
155
        }
156

    
157
        public long getSize() throws DataException {
158
                return featureReferences.getSize();
159
        }
160

    
161
        public boolean isEmpty() throws DataException {
162
                return featureReferences.isEmpty();
163
        }
164

    
165
        public Iterator iterator() throws DataException {
166
                return new IndexIterator(this.featureReferences.iterator());
167
        }
168

    
169
        public Iterator iterator(long index) throws DataException {
170
                return new IndexIterator(this.featureReferences.iterator(index));
171
        }
172

    
173
        public void delete(Feature feature) throws DataException {
174
                index.delete(feature);
175
                store.delete(feature);
176
        }
177

    
178
        public FeatureType getDefaultFeatureType() {
179
                return index.getFeatureType();
180
        }
181

    
182
        public List getFeatureTypes() {
183
                List types = new ArrayList();
184
                types.add(index.getFeatureType());
185
                return Collections.unmodifiableList(types);
186
        }
187

    
188
        public void insert(EditableFeature feature) throws DataException {
189
                index.insert(feature);
190
                store.insert(feature);
191
        }
192

    
193
        public void update(EditableFeature feature) throws DataException {
194
                store.update(feature);
195
                // we need to re-index the feature, since its shape might have changed
196
                index.delete(feature);
197
                index.insert(feature);
198
        }
199

    
200
        public void dispose() {
201

    
202
        }
203

    
204
        public boolean isFromStore(DataStore store) {
205
                return this.store.equals(store);
206
        }
207

    
208
        public void accept(Visitor visitor) throws BaseException {
209
                Iterator iterator = iterator();
210

    
211
                while (iterator.hasNext()) {
212
                        Feature feature = (Feature) iterator.next();
213
                        visitor.visit(feature);
214
                }
215
        }
216

    
217
}
218