Statistics
| Revision:

root / branches / v2_0_0_prep / libraries / libFMap_dalfile / src / org / gvsig / fmap / dal / store / dbf / DBFSetProvider.java @ 27672

History | View | Annotate | Download (4.1 KB)

1
/**
2
 *
3
 */
4
package org.gvsig.fmap.dal.store.dbf;
5

    
6
import java.util.NoSuchElementException;
7

    
8
import org.gvsig.fmap.dal.exception.DataException;
9
import org.gvsig.fmap.dal.exception.ReadRuntimeException;
10
import org.gvsig.fmap.dal.feature.DisposableIterator;
11
import org.gvsig.fmap.dal.feature.FeatureQuery;
12
import org.gvsig.fmap.dal.feature.FeatureType;
13
import org.gvsig.fmap.dal.feature.spi.FeatureData;
14
import org.gvsig.fmap.dal.feature.spi.FeatureSetProvider;
15

    
16
/**
17
 * @author jjdelcerro
18
 *
19
 */
20
public class DBFSetProvider implements FeatureSetProvider {
21

    
22
        DBFStoreProvider store;
23
        FeatureQuery query;
24
        FeatureType featureType;
25

    
26
        public DBFSetProvider(DBFStoreProvider store, FeatureQuery query,
27
                        FeatureType featureType)
28
                        throws DataException {
29
                this.store = store;
30
                this.query = query;
31
                this.featureType = featureType;
32
        }
33

    
34
        /* (non-Javadoc)
35
         * @see org.gvsig.fmap.dal.feature.spi.FeatureSetProvider#canFilter()
36
         */
37
        public boolean canFilter() {
38
                return false;
39
        }
40

    
41
        /* (non-Javadoc)
42
         * @see org.gvsig.fmap.dal.feature.spi.FeatureSetProvider#canIterateFromIndex()
43
         */
44
        public boolean canIterateFromIndex() {
45
                return true;
46
        }
47

    
48
        /* (non-Javadoc)
49
         * @see org.gvsig.fmap.dal.feature.spi.FeatureSetProvider#canOrder()
50
         */
51
        public boolean canOrder() {
52
                return false;
53
        }
54

    
55
        /* (non-Javadoc)
56
         * @see org.gvsig.fmap.dal.feature.spi.FeatureSetProvider#fastIterator(long)
57
         */
58
        public DisposableIterator fastIterator(long index) throws DataException {
59
                return new FastDBFIterartor(this.store, this.featureType,
60
                                index);
61
        }
62

    
63
        /* (non-Javadoc)
64
         * @see org.gvsig.fmap.dal.feature.spi.FeatureSetProvider#fastIterator()
65
         */
66
        public DisposableIterator fastIterator() throws DataException {
67
                return this.fastIterator(0);
68
        }
69

    
70
        /* (non-Javadoc)
71
         * @see org.gvsig.fmap.dal.feature.spi.FeatureSetProvider#getSize()
72
         */
73
        public long getSize() throws DataException {
74
                return this.store.getFeatureCount();
75
        }
76

    
77
        /* (non-Javadoc)
78
         * @see org.gvsig.fmap.dal.feature.spi.FeatureSetProvider#isEmpty()
79
         */
80
        public boolean isEmpty() throws DataException {
81
                return this.store.getFeatureCount() == 0;
82
        }
83

    
84
        public void dispose() {
85
                this.store = null;
86
                this.featureType = null;
87
                this.query = null;
88
        }
89

    
90
        /* (non-Javadoc)
91
         * @see org.gvsig.fmap.dal.feature.spi.FeatureSetProvider#iterator()
92
         */
93
        public DisposableIterator iterator() throws DataException {
94
                return this.iterator(0);
95
        }
96

    
97
        /* (non-Javadoc)
98
         * @see org.gvsig.fmap.dal.feature.spi.FeatureSetProvider#iterator(long)
99
         */
100
        public DisposableIterator iterator(long index) throws DataException {
101
                return new DBFIterartor(this.store, this.featureType, index);
102
        }
103

    
104
        protected class DBFIterartor implements DisposableIterator {
105
                protected long index;
106
                protected DBFStoreProvider store;
107
                protected FeatureType type;
108
                protected long count;
109

    
110
                public DBFIterartor(DBFStoreProvider store, FeatureType type,
111
                                long startOn) throws DataException {
112
                        this.store = store;
113
                        this.index = startOn;
114
                        this.type = type;
115
                        this.count = this.store.getFeatureCount();
116
                }
117

    
118
                public boolean hasNext() {
119
                        return this.count > index;
120
                }
121

    
122
                public Object next() {
123
                        if (index >= this.count) {
124
                                throw new NoSuchElementException();
125
                        }
126
                        try {
127

    
128
                                FeatureData ret = this.store.getFeatureDataByIndex(index,
129
                                                this.type);
130
                                index++;
131
                                return ret;
132
                        } catch (DataException e) {
133
                                throw new ReadRuntimeException(this.store.getName(), e);
134
                        }
135
                }
136

    
137
                public void remove() {
138
                        throw new UnsupportedOperationException();
139
                }
140

    
141
                public void dispose() {
142
                        store = null;
143
                        type = null;
144
                }
145
        }
146

    
147
        protected class FastDBFIterartor extends DBFIterartor {
148
                protected FeatureData data;
149

    
150
                public FastDBFIterartor(DBFStoreProvider store, FeatureType type,
151
                                long startOn) throws DataException {
152
                        super(store, type, startOn);
153
                        this.data = this.store.createFeatureData(type);
154
                }
155

    
156
                public Object next() {
157
                        if (index >= this.count) {
158
                                throw new NoSuchElementException();
159
                        }
160

    
161
                        try {
162
                                this.store.initFeatureDataByIndex(this.data, index, type);
163
                        } catch (DataException e) {
164
                                throw new ReadRuntimeException("next", e);
165
                        }
166
                        index++;
167
                        return this.data;
168
                }
169

    
170
                public void dispose() {
171
                        super.dispose();
172
                        data = null;
173
                }
174

    
175
        }
176
}