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.csv / src / main / java / org / gvsig / fmap / dal / store / csv / CSVSetProvider.java @ 47436

History | View | Annotate | Download (7.34 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.csv;
25

    
26
import java.util.Iterator;
27
import java.util.List;
28
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
public class CSVSetProvider extends AbstractFeatureSetProvider {
44

    
45
    private static class CSVFeatureProvider extends AbstractFeatureProviderLoadedOnDemand {
46

    
47
        protected CSVStoreProvider store;
48

    
49
        public CSVFeatureProvider(CSVStoreProvider store, FeatureType type) {
50
            super(type);
51
            this.store = store;
52
        }
53

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

    
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 CSVIterator 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 CSVIterator(CSVStoreProvider 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 CSVStoreProvider getFeatureStoreProvider() {
110
            return (CSVStoreProvider) 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
            CSVFeatureProvider data = new CSVFeatureProvider(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 CSVIterator {
152

    
153
        protected FeatureProvider data;
154

    
155
        public FastCSVIterator(CSVStoreProvider store, FeatureQuery query, FeatureType type,
156
                long startOn) throws DataException {
157
            super(store, query, type, startOn);
158
            this.data = new CSVFeatureProvider(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 CSVSetProvider(CSVStoreProvider store, FeatureQuery query,
183
            FeatureType featureType)
184
            throws DataException {
185
        super(store, query, featureType);
186
    }
187

    
188
    @Override
189
    protected CSVStoreProvider getStore() {
190
        return (CSVStoreProvider) 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 CSVIterator(
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
}