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