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

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