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