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.csv / src / main / java / org / gvsig / fmap / dal / store / simplereader / SimpleReaderSetProvider.java @ 47638

History | View | Annotate | Download (7.66 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 org.gvsig.fmap.dal.store.csv.CSVStoreProvider;
27
import java.util.Iterator;
28
import java.util.List;
29
import java.util.NoSuchElementException;
30
import org.gvsig.fmap.dal.exception.DataException;
31
import org.gvsig.fmap.dal.exception.ReadRuntimeException;
32
import org.gvsig.fmap.dal.feature.FeatureQuery;
33
import org.gvsig.fmap.dal.feature.FeatureType;
34
import org.gvsig.fmap.dal.feature.spi.AbstractFeatureProviderIterator;
35
import org.gvsig.fmap.dal.feature.spi.AbstractFeatureSetProvider;
36
import org.gvsig.fmap.dal.feature.spi.FeatureProvider;
37
import org.gvsig.fmap.dal.store.csv.*;
38
import org.gvsig.tools.exception.BaseException;
39

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

    
46
    private static class SimpleReaderFeatureProvider extends AbstractFeatureProviderLoadedOnDemand {
47

    
48
        protected SimpleReaderStoreProvider store;
49

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

    
55
        @Override
56
        protected void doLoad() {
57
            try {
58
                SimpleReaderStoreProvider.RowToFeatureTranslator translator = this.store.getRowToFeatureTranslator();
59
                long index = this.getOID();
60
                List<String> row = this.store.getRowByIndex(index);
61
                translator.translate(index, row, this);
62
            } catch (Exception ex) {
63
                throw new ReadRuntimeException(getStoreFullName(), this.getOID(), 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
                        store.getSpatialIndex()
105
                );
106
            }
107
        }
108

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

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

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

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

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

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

    
152
    private class FastCSVIterator extends SimpleReaderIterator {
153

    
154
        protected FeatureProvider data;
155

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

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

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

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

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

    
194
    protected String getStoreFullName() {
195
        try {
196
            return this.getStore().getFullName();
197
        } catch (Throwable th) {
198
            return "unknown";
199
        }
200
    }
201

    
202
    @Override
203
    public boolean canFilter() {
204
        return false;
205
    }
206

    
207
    @Override
208
    public boolean canIterateFromIndex() {
209
        return true;
210
    }
211

    
212
    @Override
213
    public boolean canOrder() {
214
        return false;
215
    }
216

    
217
    @Override
218
    public long getSize() throws DataException {
219
        return getStore().getFeatureCount();
220
    }
221

    
222
    @Override
223
    public boolean isEmpty() throws DataException {
224
        return getSize() == 0;
225
    }
226

    
227
    @Override
228
    protected void doDispose() throws BaseException {
229
    }
230

    
231
    @Override
232
    protected AbstractFeatureProviderIterator createIterator(long index)
233
            throws DataException {
234
        return new SimpleReaderIterator(
235
                getStore(),
236
                getQuery(),
237
                getProviderFeatureType(),
238
                index
239
        );
240
    }
241

    
242
    @Override
243
    protected AbstractFeatureProviderIterator createFastIterator(long index)
244
            throws DataException {
245
        return new FastCSVIterator(
246
                getStore(),
247
                getQuery(),
248
                getProviderFeatureType(),
249
                index
250
        );
251
    }
252
}