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.dbf / src / main / java / org / gvsig / fmap / dal / store / dbf / DBFSetProvider.java @ 42811

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

    
26
import java.util.NoSuchElementException;
27

    
28
import org.gvsig.fmap.dal.exception.DataException;
29
import org.gvsig.fmap.dal.exception.ReadRuntimeException;
30
import org.gvsig.fmap.dal.feature.FeatureQuery;
31
import org.gvsig.fmap.dal.feature.FeatureType;
32
import org.gvsig.fmap.dal.feature.spi.AbstractFeatureProviderIterator;
33
import org.gvsig.fmap.dal.feature.spi.AbstractFeatureSetProvider;
34
import org.gvsig.fmap.dal.feature.spi.FeatureProvider;
35
import org.gvsig.tools.exception.BaseException;
36

    
37
/**
38
 * @author jjdelcerro
39
 *
40
 */
41
public class DBFSetProvider extends AbstractFeatureSetProvider {
42

    
43
        public DBFSetProvider(DBFStoreProvider store, FeatureQuery query,
44
                        FeatureType featureType)
45
                        throws DataException {
46
                super(store, query, featureType);
47
        }
48

    
49
        public boolean canFilter() {
50
                return false;
51
        }
52

    
53
        public boolean canIterateFromIndex() {
54
                return true;
55
        }
56

    
57
        public boolean canOrder() {
58
                return false;
59
        }
60

    
61
        public long getSize() throws DataException {
62
                return getStore().getFeatureCount();
63
        }
64

    
65
        public boolean isEmpty() throws DataException {
66
                return getSize() == 0;
67
        }
68

    
69
        @Override
70
        protected void doDispose() throws BaseException {
71
        }
72

    
73
        protected AbstractFeatureProviderIterator createIterator(long index)
74
                        throws DataException {
75
                return new DBFIterator((DBFStoreProvider) getStore(), getFeatureType(),
76
                                index);
77
        }
78

    
79
        protected AbstractFeatureProviderIterator createFastIterator(long index)
80
                        throws DataException {
81
                return new FastDBFIterator((DBFStoreProvider) getStore(),
82
                                getFeatureType(), index);
83
        }
84

    
85
        protected class DBFIterator extends AbstractFeatureProviderIterator {
86
                protected long index;
87
                protected FeatureType type;
88
                protected long count;
89

    
90
                public DBFIterator(DBFStoreProvider store, FeatureType type,
91
                                long startOn) throws DataException {
92
                        super(store);
93
                        this.index = startOn;
94
                        this.type = type;
95
                        this.count = store.getFeatureCount();
96
                }
97

    
98
                protected boolean internalHasNext() {
99
                        return this.count > index;
100
                }
101

    
102
                protected Object internalNext() {
103
                        if (index >= this.count) {
104
                                throw new NoSuchElementException();
105
                        }
106
                        try {
107

    
108
                                FeatureProvider ret =
109
                                                getDBFStoreProvider().getFeatureProviderByIndex(index,
110
                                                                this.type);
111
                                index++;
112
                                return ret;
113
                        } catch (DataException e) {
114
                                throw new ReadRuntimeException(getNameForMessages(), index,e);
115
                        }
116
                }
117

    
118
                public void remove() {
119
                        throw new UnsupportedOperationException();
120
                }
121

    
122
                protected void internalDispose() {
123
                        type = null;
124
                }
125

    
126
                protected DBFStoreProvider getDBFStoreProvider() {
127
                        return (DBFStoreProvider) getFeatureStoreProvider();
128
                }
129
                
130
                protected String getNameForMessages() {
131
                    // Solo con proposito de mostrar en mensajes de error.
132
                    try {
133
                        return getFeatureStoreProvider().getName();
134
                    } catch(Exception ex) {
135
                        return "unknown";
136
                    }
137
                }
138

    
139
                @Override
140
                protected void doDispose() throws BaseException {
141
                        // Nothing to do
142
                }
143
        }
144

    
145
        protected class FastDBFIterator extends DBFIterator {
146
                protected FeatureProvider data;
147

    
148
                public FastDBFIterator(DBFStoreProvider store, FeatureType type,
149
                                long startOn) throws DataException {
150
                        super(store, type, startOn);
151
                        this.data = getFeatureStoreProvider().createFeatureProvider(type);
152
                }
153

    
154
                protected Object internalNext() {
155
                        if (index >= this.count) {
156
                                throw new NoSuchElementException();
157
                        }
158

    
159
                        try {
160
                                getDBFStoreProvider().initFeatureProviderByIndex(this.data,
161
                                                index, type);
162
                        } catch (DataException e) {
163
                                throw new ReadRuntimeException(getNameForMessages(), index, e);
164
                        }
165
                        index++;
166
                        return this.data;
167
                }
168

    
169
                protected void internalDispose() {
170
                        super.internalDispose();
171
                        data = null;
172
                }
173
        }
174
}