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