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 @ 47652

History | View | Annotate | Download (7.67 KB)

1 40435 jjdelcerro
/**
2 40559 jjdelcerro
 * gvSIG. Desktop Geographic Information System.
3 40435 jjdelcerro
 *
4 40559 jjdelcerro
 * 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 40435 jjdelcerro
 */
24 47634 fdiaz
package org.gvsig.fmap.dal.store.simplereader;
25 40435 jjdelcerro
26 47638 jjdelcerro
import org.gvsig.fmap.dal.store.csv.CSVStoreProvider;
27 47199 jjdelcerro
import java.util.Iterator;
28 45727 jjdelcerro
import java.util.List;
29 40435 jjdelcerro
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 47636 fdiaz
import org.gvsig.fmap.dal.store.csv.*;
38 40435 jjdelcerro
import org.gvsig.tools.exception.BaseException;
39
40
/**
41
 * @author jjdelcerro
42
 *
43
 */
44 47634 fdiaz
public class SimpleReaderSetProvider extends AbstractFeatureSetProvider {
45 40435 jjdelcerro
46 47634 fdiaz
    private static class SimpleReaderFeatureProvider extends AbstractFeatureProviderLoadedOnDemand {
47 40435 jjdelcerro
48 47634 fdiaz
        protected SimpleReaderStoreProvider store;
49 40435 jjdelcerro
50 47634 fdiaz
        public SimpleReaderFeatureProvider(SimpleReaderStoreProvider store, FeatureType type) {
51 45727 jjdelcerro
            super(type);
52
            this.store = store;
53
        }
54 40435 jjdelcerro
55 45727 jjdelcerro
        @Override
56
        protected void doLoad() {
57 47652 fdiaz
            long index=-1;
58 45727 jjdelcerro
            try {
59 47652 fdiaz
                index = this.getOID();
60 47634 fdiaz
                SimpleReaderStoreProvider.RowToFeatureTranslator translator = this.store.getRowToFeatureTranslator();
61 45727 jjdelcerro
                List<String> row = this.store.getRowByIndex(index);
62
                translator.translate(index, row, this);
63 47652 fdiaz
            } catch (Throwable ex) {
64
                throw new ReadRuntimeException(getStoreFullName(), index, ex);
65 45727 jjdelcerro
            }
66
        }
67 40435 jjdelcerro
68 45727 jjdelcerro
        @Override
69
        public Long getOID() {
70
            return (Long) super.getOID();
71
        }
72 40435 jjdelcerro
73 45727 jjdelcerro
        public void setOID(long oid) {
74
            super.setOID(oid);
75
        }
76 40435 jjdelcerro
77 45727 jjdelcerro
        private String getStoreFullName() {
78
            try {
79
                return this.store.getFullName();
80
            } catch (Exception ex) {
81
                return "unknown";
82
            }
83
        }
84 40435 jjdelcerro
85 45727 jjdelcerro
    }
86 47634 fdiaz
    private class SimpleReaderIterator extends AbstractFeatureProviderIterator {
87 40435 jjdelcerro
88 45727 jjdelcerro
        protected long index;
89
        protected FeatureType type;
90
        protected long count;
91 47199 jjdelcerro
        protected final FeatureQuery query;
92
        protected Iterator spatialIndexIt;
93 40435 jjdelcerro
94 47634 fdiaz
        public SimpleReaderIterator(SimpleReaderStoreProvider store, FeatureQuery query, FeatureType type,
95 45727 jjdelcerro
                long startOn) throws DataException {
96
            super(store);
97
            this.index = startOn;
98
            this.type = type;
99 47199 jjdelcerro
            this.query = query;
100 45727 jjdelcerro
            this.count = store.getFeatureCount();
101 47199 jjdelcerro
            if( this.query!=null ) {
102
                this.spatialIndexIt = createSpatialIterator(
103
                        store.getFeatureStore().getDefaultFeatureTypeQuietly(),
104
                        query,
105
                        store.getSpatialIndex()
106
                );
107
            }
108 45727 jjdelcerro
        }
109 40435 jjdelcerro
110 45727 jjdelcerro
        @Override
111 47199 jjdelcerro
        protected CSVStoreProvider getFeatureStoreProvider() {
112
            return (CSVStoreProvider) super.getFeatureStoreProvider();
113
        }
114
115
        @Override
116 45727 jjdelcerro
        protected boolean internalHasNext() {
117 47199 jjdelcerro
            if( this.spatialIndexIt != null ) {
118
                return this.spatialIndexIt.hasNext();
119
            }
120 45727 jjdelcerro
            return this.count > index;
121
        }
122 40435 jjdelcerro
123 45727 jjdelcerro
        @Override
124
        protected FeatureProvider internalNext() {
125 47634 fdiaz
            SimpleReaderFeatureProvider data = new SimpleReaderFeatureProvider(getStore(), type);
126 47199 jjdelcerro
            if( this.spatialIndexIt != null ) {
127
                Object oid = this.spatialIndexIt.next();
128
                data.setOID(oid);
129
            } else {
130
                if (index >= this.count) {
131
                    throw new NoSuchElementException();
132
                }
133
                data.setOID(index++);
134
            }
135 45727 jjdelcerro
            return data;
136
        }
137 40435 jjdelcerro
138 45727 jjdelcerro
        @Override
139
        public void remove() {
140
            throw new UnsupportedOperationException();
141
        }
142 40435 jjdelcerro
143 45727 jjdelcerro
        protected void internalDispose() {
144
            type = null;
145
        }
146 40435 jjdelcerro
147 45727 jjdelcerro
        @Override
148
        protected void doDispose() throws BaseException {
149
            // Nothing to do
150
        }
151
    }
152 40435 jjdelcerro
153 47634 fdiaz
    private class FastCSVIterator extends SimpleReaderIterator {
154 40435 jjdelcerro
155 45727 jjdelcerro
        protected FeatureProvider data;
156 40435 jjdelcerro
157 47634 fdiaz
        public FastCSVIterator(SimpleReaderStoreProvider store, FeatureQuery query, FeatureType type,
158 45727 jjdelcerro
                long startOn) throws DataException {
159 47199 jjdelcerro
            super(store, query, type, startOn);
160 47634 fdiaz
            this.data = new SimpleReaderFeatureProvider(store, type);
161 45727 jjdelcerro
        }
162 40435 jjdelcerro
163 45727 jjdelcerro
        @Override
164
        protected FeatureProvider internalNext() {
165 47199 jjdelcerro
            if( this.spatialIndexIt != null ) {
166
                Object oid = this.spatialIndexIt.next();
167
                data.setOID(oid);
168
            } else {
169
                if (index >= this.count) {
170
                    throw new NoSuchElementException();
171
                }
172
                data.setOID(index++);
173
            }
174
            return data;
175 45727 jjdelcerro
        }
176 40435 jjdelcerro
177 45727 jjdelcerro
        @Override
178
        protected void internalDispose() {
179
            super.internalDispose();
180
            data = null;
181
        }
182
    }
183 40435 jjdelcerro
184 47634 fdiaz
    public SimpleReaderSetProvider(SimpleReaderStoreProvider store, FeatureQuery query,
185 47557 fdiaz
            FeatureType providerFeatureType, FeatureType featureType)
186 45727 jjdelcerro
            throws DataException {
187 47557 fdiaz
        super(store, query, providerFeatureType, featureType);
188 45727 jjdelcerro
    }
189 40435 jjdelcerro
190 45727 jjdelcerro
    @Override
191 47634 fdiaz
    protected SimpleReaderStoreProvider getStore() {
192
        return (SimpleReaderStoreProvider) super.getStore();
193 45727 jjdelcerro
    }
194 40435 jjdelcerro
195 45727 jjdelcerro
    protected String getStoreFullName() {
196
        try {
197
            return this.getStore().getFullName();
198
        } catch (Throwable th) {
199
            return "unknown";
200
        }
201
    }
202
203
    @Override
204
    public boolean canFilter() {
205
        return false;
206
    }
207
208
    @Override
209
    public boolean canIterateFromIndex() {
210
        return true;
211
    }
212
213
    @Override
214
    public boolean canOrder() {
215
        return false;
216
    }
217
218
    @Override
219
    public long getSize() throws DataException {
220
        return getStore().getFeatureCount();
221
    }
222
223
    @Override
224
    public boolean isEmpty() throws DataException {
225
        return getSize() == 0;
226
    }
227
228
    @Override
229
    protected void doDispose() throws BaseException {
230
    }
231
232
    @Override
233
    protected AbstractFeatureProviderIterator createIterator(long index)
234
            throws DataException {
235 47634 fdiaz
        return new SimpleReaderIterator(
236 45727 jjdelcerro
                getStore(),
237 47199 jjdelcerro
                getQuery(),
238 47436 fdiaz
                getProviderFeatureType(),
239 45727 jjdelcerro
                index
240
        );
241
    }
242
243
    @Override
244
    protected AbstractFeatureProviderIterator createFastIterator(long index)
245
            throws DataException {
246
        return new FastCSVIterator(
247
                getStore(),
248 47199 jjdelcerro
                getQuery(),
249 47436 fdiaz
                getProviderFeatureType(),
250 45727 jjdelcerro
                index
251
        );
252
    }
253 40767 jjdelcerro
}