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 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
            try {
58 47634 fdiaz
                SimpleReaderStoreProvider.RowToFeatureTranslator translator = this.store.getRowToFeatureTranslator();
59 45727 jjdelcerro
                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 40435 jjdelcerro
67 45727 jjdelcerro
        @Override
68
        public Long getOID() {
69
            return (Long) super.getOID();
70
        }
71 40435 jjdelcerro
72 45727 jjdelcerro
        public void setOID(long oid) {
73
            super.setOID(oid);
74
        }
75 40435 jjdelcerro
76 45727 jjdelcerro
        private String getStoreFullName() {
77
            try {
78
                return this.store.getFullName();
79
            } catch (Exception ex) {
80
                return "unknown";
81
            }
82
        }
83 40435 jjdelcerro
84 45727 jjdelcerro
    }
85 47634 fdiaz
    private class SimpleReaderIterator extends AbstractFeatureProviderIterator {
86 40435 jjdelcerro
87 45727 jjdelcerro
        protected long index;
88
        protected FeatureType type;
89
        protected long count;
90 47199 jjdelcerro
        protected final FeatureQuery query;
91
        protected Iterator spatialIndexIt;
92 40435 jjdelcerro
93 47634 fdiaz
        public SimpleReaderIterator(SimpleReaderStoreProvider store, FeatureQuery query, FeatureType type,
94 45727 jjdelcerro
                long startOn) throws DataException {
95
            super(store);
96
            this.index = startOn;
97
            this.type = type;
98 47199 jjdelcerro
            this.query = query;
99 45727 jjdelcerro
            this.count = store.getFeatureCount();
100 47199 jjdelcerro
            if( this.query!=null ) {
101
                this.spatialIndexIt = createSpatialIterator(
102
                        store.getFeatureStore().getDefaultFeatureTypeQuietly(),
103
                        query,
104
                        store.getSpatialIndex()
105
                );
106
            }
107 45727 jjdelcerro
        }
108 40435 jjdelcerro
109 45727 jjdelcerro
        @Override
110 47199 jjdelcerro
        protected CSVStoreProvider getFeatureStoreProvider() {
111
            return (CSVStoreProvider) super.getFeatureStoreProvider();
112
        }
113
114
        @Override
115 45727 jjdelcerro
        protected boolean internalHasNext() {
116 47199 jjdelcerro
            if( this.spatialIndexIt != null ) {
117
                return this.spatialIndexIt.hasNext();
118
            }
119 45727 jjdelcerro
            return this.count > index;
120
        }
121 40435 jjdelcerro
122 45727 jjdelcerro
        @Override
123
        protected FeatureProvider internalNext() {
124 47634 fdiaz
            SimpleReaderFeatureProvider data = new SimpleReaderFeatureProvider(getStore(), type);
125 47199 jjdelcerro
            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 45727 jjdelcerro
            return data;
135
        }
136 40435 jjdelcerro
137 45727 jjdelcerro
        @Override
138
        public void remove() {
139
            throw new UnsupportedOperationException();
140
        }
141 40435 jjdelcerro
142 45727 jjdelcerro
        protected void internalDispose() {
143
            type = null;
144
        }
145 40435 jjdelcerro
146 45727 jjdelcerro
        @Override
147
        protected void doDispose() throws BaseException {
148
            // Nothing to do
149
        }
150
    }
151 40435 jjdelcerro
152 47634 fdiaz
    private class FastCSVIterator extends SimpleReaderIterator {
153 40435 jjdelcerro
154 45727 jjdelcerro
        protected FeatureProvider data;
155 40435 jjdelcerro
156 47634 fdiaz
        public FastCSVIterator(SimpleReaderStoreProvider store, FeatureQuery query, FeatureType type,
157 45727 jjdelcerro
                long startOn) throws DataException {
158 47199 jjdelcerro
            super(store, query, type, startOn);
159 47634 fdiaz
            this.data = new SimpleReaderFeatureProvider(store, type);
160 45727 jjdelcerro
        }
161 40435 jjdelcerro
162 45727 jjdelcerro
        @Override
163
        protected FeatureProvider internalNext() {
164 47199 jjdelcerro
            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 45727 jjdelcerro
        }
175 40435 jjdelcerro
176 45727 jjdelcerro
        @Override
177
        protected void internalDispose() {
178
            super.internalDispose();
179
            data = null;
180
        }
181
    }
182 40435 jjdelcerro
183 47634 fdiaz
    public SimpleReaderSetProvider(SimpleReaderStoreProvider store, FeatureQuery query,
184 47557 fdiaz
            FeatureType providerFeatureType, FeatureType featureType)
185 45727 jjdelcerro
            throws DataException {
186 47557 fdiaz
        super(store, query, providerFeatureType, featureType);
187 45727 jjdelcerro
    }
188 40435 jjdelcerro
189 45727 jjdelcerro
    @Override
190 47634 fdiaz
    protected SimpleReaderStoreProvider getStore() {
191
        return (SimpleReaderStoreProvider) super.getStore();
192 45727 jjdelcerro
    }
193 40435 jjdelcerro
194 45727 jjdelcerro
    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 47634 fdiaz
        return new SimpleReaderIterator(
235 45727 jjdelcerro
                getStore(),
236 47199 jjdelcerro
                getQuery(),
237 47436 fdiaz
                getProviderFeatureType(),
238 45727 jjdelcerro
                index
239
        );
240
    }
241
242
    @Override
243
    protected AbstractFeatureProviderIterator createFastIterator(long index)
244
            throws DataException {
245
        return new FastCSVIterator(
246
                getStore(),
247 47199 jjdelcerro
                getQuery(),
248 47436 fdiaz
                getProviderFeatureType(),
249 45727 jjdelcerro
                index
250
        );
251
    }
252 40767 jjdelcerro
}