Statistics
| Revision:

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

History | View | Annotate | Download (3.81 KB)

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

    
6
import java.util.Iterator;
7
import java.util.NoSuchElementException;
8

    
9
import org.gvsig.fmap.dal.exception.DataException;
10
import org.gvsig.fmap.dal.exception.ReadRuntimeException;
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
                        throws DataException {
28
                this.store = store;
29
                this.query = query;
30
                this.featureType = store.getProviderServices().getFeatureType(query);
31
        }
32

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

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

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

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

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

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

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

    
83
        /* (non-Javadoc)
84
         * @see org.gvsig.fmap.dal.feature.spi.FeatureSetProvider#iterator()
85
         */
86
        public Iterator iterator() throws DataException {
87
                return this.iterator(0);
88
        }
89

    
90
        /* (non-Javadoc)
91
         * @see org.gvsig.fmap.dal.feature.spi.FeatureSetProvider#iterator(long)
92
         */
93
        public Iterator iterator(long index) throws DataException {
94
                return new DBFIterartor(this.store, this.featureType, index);
95
        }
96

    
97
        protected class DBFIterartor implements Iterator {
98
                protected long index;
99
                protected DBFStoreProvider store;
100
                protected FeatureType type;
101
                protected long count;
102

    
103
                public DBFIterartor(DBFStoreProvider store, FeatureType type,
104
                                long startOn) throws DataException {
105
                        this.store = store;
106
                        this.index = startOn;
107
                        this.type = type;
108
                        this.count = this.store.getFeatureCount();
109
                }
110

    
111
                public boolean hasNext() {
112
                        return this.count > index;
113
                }
114

    
115
                public Object next() {
116
                        if (index >= this.count) {
117
                                throw new NoSuchElementException();
118
                        }
119
                        try {
120

    
121
                                FeatureData ret = this.store.getFeatureDataByIndex(index,
122
                                                this.type);
123
                                index++;
124
                                return ret;
125
                        } catch (DataException e) {
126
                                throw new ReadRuntimeException(this.store.getName(), e);
127
                        }
128
                }
129

    
130
                public void remove() {
131
                        throw new UnsupportedOperationException();
132
                }
133
        }
134

    
135
        protected class FastDBFIterartor extends DBFIterartor {
136
                protected FeatureData data;
137

    
138
                public FastDBFIterartor(DBFStoreProvider store, FeatureType type,
139
                                long startOn) throws DataException {
140
                        super(store, type, startOn);
141
                        this.data = this.store.createFeatureData(type);
142
                }
143

    
144
                public Object next() {
145
                        if (index >= this.count) {
146
                                throw new NoSuchElementException();
147
                        }
148

    
149
                        try {
150
                                this.store.initFeatureDataByIndex(this.data, index, type);
151
                        } catch (DataException e) {
152
                                throw new ReadRuntimeException("next", e);
153
                        }
154
                        index++;
155
                        return this.data;
156
                }
157

    
158

    
159
        }
160

    
161
}