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 / csv / CSVSetProvider.java @ 47436
History | View | Annotate | Download (7.34 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 | 45727 | jjdelcerro | package org.gvsig.fmap.dal.store.csv; |
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 | |||
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.tools.exception.BaseException; |
||
38 | |||
39 | /**
|
||
40 | * @author jjdelcerro
|
||
41 | *
|
||
42 | */
|
||
43 | 45727 | jjdelcerro | public class CSVSetProvider extends AbstractFeatureSetProvider { |
44 | 40435 | jjdelcerro | |
45 | 45727 | jjdelcerro | private static class CSVFeatureProvider extends AbstractFeatureProviderLoadedOnDemand { |
46 | 40435 | jjdelcerro | |
47 | 45727 | jjdelcerro | protected CSVStoreProvider store;
|
48 | 40435 | jjdelcerro | |
49 | 45727 | jjdelcerro | public CSVFeatureProvider(CSVStoreProvider store, FeatureType type) {
|
50 | super(type);
|
||
51 | this.store = store;
|
||
52 | } |
||
53 | 40435 | jjdelcerro | |
54 | 45727 | jjdelcerro | @Override
|
55 | protected void doLoad() { |
||
56 | try {
|
||
57 | CSVStoreProvider.RowToFeatureTranslator translator = this.store.getRowToFeatureTranslator();
|
||
58 | long index = this.getOID(); |
||
59 | List<String> row = this.store.getRowByIndex(index); |
||
60 | translator.translate(index, row, this);
|
||
61 | } catch (Exception ex) { |
||
62 | throw new ReadRuntimeException(getStoreFullName(), this.getOID(), ex); |
||
63 | } |
||
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 | private class CSVIterator 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 | 47199 | jjdelcerro | public CSVIterator(CSVStoreProvider 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 | 47199 | jjdelcerro | protected CSVStoreProvider getFeatureStoreProvider() {
|
110 | return (CSVStoreProvider) super.getFeatureStoreProvider(); |
||
111 | } |
||
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 | CSVFeatureProvider data = new CSVFeatureProvider(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 | 45727 | jjdelcerro | private class FastCSVIterator extends CSVIterator { |
152 | 40435 | jjdelcerro | |
153 | 45727 | jjdelcerro | protected FeatureProvider data;
|
154 | 40435 | jjdelcerro | |
155 | 47199 | jjdelcerro | public FastCSVIterator(CSVStoreProvider store, FeatureQuery query, FeatureType type,
|
156 | 45727 | jjdelcerro | long startOn) throws DataException { |
157 | 47199 | jjdelcerro | super(store, query, type, startOn);
|
158 | 45727 | jjdelcerro | this.data = new CSVFeatureProvider(store, type); |
159 | } |
||
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 | 45727 | jjdelcerro | public CSVSetProvider(CSVStoreProvider store, FeatureQuery query,
|
183 | FeatureType featureType) |
||
184 | throws DataException {
|
||
185 | super(store, query, featureType);
|
||
186 | } |
||
187 | 40435 | jjdelcerro | |
188 | 45727 | jjdelcerro | @Override
|
189 | protected CSVStoreProvider getStore() {
|
||
190 | return (CSVStoreProvider) super.getStore(); |
||
191 | } |
||
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 | return new CSVIterator( |
||
234 | 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 | } |