Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.compat.cdc / org.gvsig.fmap.dal / org.gvsig.fmap.dal.file / org.gvsig.fmap.dal.file.lib / src / main / java / org / gvsig / fmap / dal / store / simplereader / SimpleReaderSetProvider.java @ 47665

History | View | Annotate | Download (7.72 KB)

1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2013 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.fmap.dal.store.simplereader;
25

    
26
import java.util.Iterator;
27
import java.util.List;
28
import java.util.NoSuchElementException;
29
import org.gvsig.fmap.dal.exception.DataException;
30
import org.gvsig.fmap.dal.exception.ReadRuntimeException;
31
import org.gvsig.fmap.dal.feature.FeatureQuery;
32
import org.gvsig.fmap.dal.feature.FeatureType;
33
import org.gvsig.fmap.dal.feature.spi.AbstractFeatureProviderIterator;
34
import org.gvsig.fmap.dal.feature.spi.AbstractFeatureSetProvider;
35
import org.gvsig.fmap.dal.feature.spi.FeatureProvider;
36
import org.gvsig.fmap.geom.SpatialIndex;
37
import org.gvsig.tools.exception.BaseException;
38

    
39
/**
40
 * @author jjdelcerro
41
 *
42
 */
43
public class SimpleReaderSetProvider extends AbstractFeatureSetProvider {
44

    
45
    private static class SimpleReaderFeatureProvider extends AbstractFeatureProviderLoadedOnDemand {
46

    
47
        protected SimpleReaderStoreProvider store;
48

    
49
        public SimpleReaderFeatureProvider(SimpleReaderStoreProvider store, FeatureType type) {
50
            super(type);
51
            this.store = store;
52
        }
53

    
54
        @Override
55
        protected void doLoad() {
56
            long index=-1;
57
            try {
58
                index = this.getOID();
59
                SimpleReaderStoreProvider.RowToFeatureTranslator translator = this.store.getRowToFeatureTranslator();
60
                List<String> row = this.store.getRowByIndex(index);
61
                translator.translate(index, row, this);
62
            } catch (Throwable ex) {
63
                throw new ReadRuntimeException(getStoreFullName(), index, ex);
64
            }
65
        }
66

    
67
        @Override
68
        public Long getOID() {
69
            return (Long) super.getOID();
70
        }
71

    
72
        public void setOID(long oid) {
73
            super.setOID(oid); 
74
        }
75

    
76
        private String getStoreFullName() {
77
            try {
78
                return this.store.getFullName();
79
            } catch (Exception ex) {
80
                return "unknown";
81
            }
82
        }
83

    
84
    }
85
    private class SimpleReaderIterator extends AbstractFeatureProviderIterator {
86

    
87
        protected long index;
88
        protected FeatureType type;
89
        protected long count;
90
        protected final FeatureQuery query;
91
        protected Iterator spatialIndexIt;
92

    
93
        public SimpleReaderIterator(SimpleReaderStoreProvider store, FeatureQuery query, FeatureType type,
94
                long startOn) throws DataException {
95
            super(store);
96
            this.index = startOn;
97
            this.type = type;
98
            this.query = query;
99
            this.count = store.getFeatureCount();
100
            if( this.query!=null ) {
101
                this.spatialIndexIt = createSpatialIterator(
102
                        store.getFeatureStore().getDefaultFeatureTypeQuietly(), 
103
                        query
104
                );
105
            }
106
        }
107

    
108
        @Override
109
        protected SimpleReaderStoreProvider getFeatureStoreProvider() {
110
            return (SimpleReaderStoreProvider) super.getFeatureStoreProvider(); 
111
        }
112

    
113
        @Override
114
        protected boolean internalHasNext() {
115
            if( this.spatialIndexIt != null ) {
116
                return this.spatialIndexIt.hasNext();
117
            }
118
            return this.count > index;
119
        }
120

    
121
        @Override
122
        protected FeatureProvider internalNext() {
123
            SimpleReaderFeatureProvider data = new SimpleReaderFeatureProvider(getStore(), type);
124
            if( this.spatialIndexIt != null ) {
125
                Object oid = this.spatialIndexIt.next();                
126
                data.setOID(oid);
127
            } else {
128
                if (index >= this.count) {
129
                    throw new NoSuchElementException();
130
                }
131
                data.setOID(index++);
132
            }   
133
            return data;
134
        }
135

    
136
        @Override
137
        public void remove() {
138
            throw new UnsupportedOperationException();
139
        }
140

    
141
        protected void internalDispose() {
142
            type = null;
143
        }
144

    
145
        @Override
146
        protected void doDispose() throws BaseException {
147
            // Nothing to do
148
        }
149
    }
150

    
151
    private class FastCSVIterator extends SimpleReaderIterator {
152

    
153
        protected FeatureProvider data;
154

    
155
        public FastCSVIterator(SimpleReaderStoreProvider store, FeatureQuery query, FeatureType type,
156
                long startOn) throws DataException {
157
            super(store, query, type, startOn);
158
            this.data = new SimpleReaderFeatureProvider(store, type);
159
        }
160

    
161
        @Override
162
        protected FeatureProvider internalNext() {
163
            if( this.spatialIndexIt != null ) {
164
                Object oid = this.spatialIndexIt.next();                
165
                data.setOID(oid);
166
            } else {
167
                if (index >= this.count) {
168
                    throw new NoSuchElementException();
169
                }
170
                data.setOID(index++);
171
            }   
172
            return data;            
173
        }
174

    
175
        @Override
176
        protected void internalDispose() {
177
            super.internalDispose();
178
            data = null;
179
        }
180
    }
181

    
182
    public SimpleReaderSetProvider(SimpleReaderStoreProvider store, FeatureQuery query,
183
            FeatureType providerFeatureType, FeatureType featureType)
184
            throws DataException {
185
        super(store, query, providerFeatureType, featureType);
186
    }
187

    
188
    @Override
189
    protected SimpleReaderStoreProvider getStore() {
190
        return (SimpleReaderStoreProvider) super.getStore();
191
    }
192

    
193
    @Override
194
    protected SpatialIndex getSpatialIndex(String name) {
195
        return getStore().getSpatialIndex(name);
196
    }
197
    
198
    protected String getStoreFullName() {
199
        try {
200
            return this.getStore().getFullName();
201
        } catch (Throwable th) {
202
            return "unknown";
203
        }
204
    }
205

    
206
    @Override
207
    public boolean canFilter() {
208
        return false;
209
    }
210

    
211
    @Override
212
    public boolean canIterateFromIndex() {
213
        return true;
214
    }
215

    
216
    @Override
217
    public boolean canOrder() {
218
        return false;
219
    }
220

    
221
    @Override
222
    public long getSize() throws DataException {
223
        return getStore().getFeatureCount();
224
    }
225

    
226
    @Override
227
    public boolean isEmpty() throws DataException {
228
        return getSize() == 0;
229
    }
230

    
231
    @Override
232
    protected void doDispose() throws BaseException {
233
    }
234

    
235
    @Override
236
    protected AbstractFeatureProviderIterator createIterator(long index)
237
            throws DataException {
238
        return new SimpleReaderIterator(
239
                getStore(),
240
                getQuery(),
241
                getProviderFeatureType(),
242
                index
243
        );
244
    }
245

    
246
    @Override
247
    protected AbstractFeatureProviderIterator createFastIterator(long index)
248
            throws DataException {
249
        return new FastCSVIterator(
250
                getStore(),
251
                getQuery(),
252
                getProviderFeatureType(),
253
                index
254
        );
255
    }
256
}