Statistics
| Revision:

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

History | View | Annotate | Download (6.09 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.DisposableIterator;
40
import org.gvsig.fmap.dal.feature.EditableFeature;
41
import org.gvsig.fmap.dal.feature.Feature;
42
import org.gvsig.fmap.dal.feature.FeatureReference;
43
import org.gvsig.fmap.dal.feature.FeatureSet;
44
import org.gvsig.fmap.dal.feature.FeatureType;
45
import org.gvsig.fmap.dal.feature.spi.FeatureProvider;
46
import org.gvsig.fmap.dal.feature.spi.FeatureReferenceProviderServices;
47
import org.gvsig.fmap.dal.feature.spi.FeatureSetProvider;
48
import org.gvsig.fmap.dal.feature.spi.FeatureStoreProvider;
49
import org.gvsig.fmap.dal.feature.spi.FeatureStoreProviderServices;
50
import org.gvsig.fmap.dal.feature.spi.LongList;
51
import org.gvsig.fmap.dal.feature.spi.index.FeatureIndexProviderServices;
52
import org.gvsig.tools.exception.BaseException;
53
import org.gvsig.tools.visitor.Visitor;
54

    
55

    
56
public class IndexFeatureSet implements FeatureSet, FeatureSetProvider {
57

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

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

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

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

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

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

    
89
                public void dispose() {
90
                        this.it = null;
91
                }
92
        }
93

    
94
        public class FastIndexIterator implements DisposableIterator {
95
                Iterator it = null;
96
                DefaultFeature feature = null;
97

    
98
                public FastIndexIterator(Iterator it) throws DataException {
99
                        this.it = it;
100
                        feature = (DefaultFeature) store.createFeature(storeProvider
101
                                        .createFeatureData(index.getFeatureType()));
102
                }
103

    
104
                public boolean hasNext() {
105
                        return it.hasNext();
106
                }
107

    
108
                public Object next() {
109
                        Object oid = it.next();
110
                        try {
111
                                //                                Long longer=new Long(((Integer)oid).longValue());
112
                                FeatureReference ref = new DefaultFeatureReference(store, oid);
113
                                FeatureProvider data = storeProvider
114
                                                .getFeatureDataByReference((FeatureReferenceProviderServices) ref);
115

    
116
                                feature.setData(data);
117

    
118
                                return feature;
119
                        } catch (DataException e) {
120
                                throw new ReadRuntimeException(store.getName(), e);
121
                        }
122
                }
123

    
124
                public void remove() {
125
                        throw new UnsupportedOperationException();
126
                }
127

    
128
                public void dispose() {
129
                        this.it = null;
130
                        this.feature = null;
131

    
132
                }
133
        }
134

    
135
        public IndexFeatureSet(FeatureIndexProviderServices index, LongList featureReferences) {
136
                this.featureReferences = featureReferences;
137
                this.store = index.getFeatureStore();
138
                this.storeProvider = store.getProvider();
139
                this.index = index;
140
        }
141

    
142
        public boolean canFilter() {
143
                return false;
144
        }
145

    
146
        public boolean canIterateFromIndex() {
147
                return true;
148
        }
149

    
150
        public boolean canOrder() {
151
                return false;
152
        }
153

    
154
        public DisposableIterator fastIterator(long index) throws DataException {
155
                if (store.isEditing()) {
156
                        return this.iterator(index);
157
                }
158
                return new FastIndexIterator(this.featureReferences.iterator(index));
159
        }
160

    
161
        public DisposableIterator fastIterator() throws DataException {
162
                if (store.isEditing()) {
163
                        return this.iterator();
164
                }
165
                return new FastIndexIterator(this.featureReferences.iterator());
166
        }
167

    
168
        public long getSize() throws DataException {
169
                return featureReferences.getSize();
170
        }
171

    
172
        public boolean isEmpty() throws DataException {
173
                return featureReferences.isEmpty();
174
        }
175

    
176
        public DisposableIterator iterator() throws DataException {
177
                return new IndexIterator(this.featureReferences.iterator());
178
        }
179

    
180
        public DisposableIterator iterator(long index) throws DataException {
181
                return new IndexIterator(this.featureReferences.iterator(index));
182
        }
183

    
184
        public void delete(Feature feature) throws DataException {
185
                index.delete(feature);
186
                store.delete(feature);
187
        }
188

    
189
        public FeatureType getDefaultFeatureType() {
190
                return index.getFeatureType();
191
        }
192

    
193
        public List getFeatureTypes() {
194
                List types = new ArrayList();
195
                types.add(index.getFeatureType());
196
                return Collections.unmodifiableList(types);
197
        }
198

    
199
        public void insert(EditableFeature feature) throws DataException {
200
                index.insert(feature);
201
                store.insert(feature);
202
        }
203

    
204
        public void update(EditableFeature feature) throws DataException {
205
                store.update(feature);
206
                // we need to re-index the feature, since its shape might have changed
207
                index.delete(feature);
208
                index.insert(feature);
209
        }
210

    
211
        public void dispose() {
212

    
213
        }
214

    
215
        public boolean isFromStore(DataStore store) {
216
                return this.store.equals(store);
217
        }
218

    
219
        public void accept(Visitor visitor) throws BaseException {
220
                DisposableIterator iterator = iterator();
221

    
222
                while (iterator.hasNext()) {
223
                        Feature feature = (Feature) iterator.next();
224
                        visitor.visit(feature);
225
                }
226
                iterator.dispose();
227
        }
228

    
229
}
230