Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.compat.cdc / org.gvsig.fmap.dal / org.gvsig.fmap.dal.impl / src / main / java / org / gvsig / fmap / dal / feature / impl / featureset / DefaultIterator.java @ 46133

History | View | Annotate | Download (4.79 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.feature.impl.featureset;
25

    
26
import java.util.Iterator;
27
import java.util.NoSuchElementException;
28
import org.gvsig.fmap.dal.exception.DataException;
29
import org.gvsig.fmap.dal.feature.Feature;
30
import org.gvsig.fmap.dal.feature.impl.DefaultFeature;
31
import org.gvsig.fmap.dal.feature.spi.FeatureProvider;
32
import org.gvsig.tools.dispose.DisposableIterator;
33
import org.gvsig.tools.dispose.DisposeUtils;
34
import org.gvsig.tools.dispose.impl.AbstractDisposable;
35
import org.gvsig.tools.exception.BaseException;
36
import org.slf4j.Logger;
37
import org.slf4j.LoggerFactory;
38

    
39

    
40
class DefaultIterator extends AbstractDisposable implements DisposableIterator {
41

    
42
    protected static final Logger LOGGER = LoggerFactory.getLogger(DefaultIterator.class);
43
        protected Iterator<FeatureProvider> iterator;
44
        protected DefaultFeatureSet fset;
45
        protected Feature lastFeature = null;
46

    
47
        public DefaultIterator(DefaultFeatureSet featureSet) {
48
                this.fset = featureSet;
49
                DisposeUtils.bind(this.fset);
50
        }
51

    
52
        public DefaultIterator(DefaultFeatureSet featureSet, long index, long elements)
53
                        throws DataException {
54
                this.fset = featureSet;
55
                DisposeUtils.bind(this.fset);
56
                if (index > 0) {
57
                        if (featureSet.provider.canIterateFromIndex()) {
58
                                try {
59
                                        this.iterator = featureSet.provider.iterator(index, elements);
60
                                } catch (UnsupportedOperationException e) {
61
                                        this.iterator = featureSet.provider.iterator();
62
                                        skypto(index);
63
                                }
64
                        } else {
65
                                this.iterator = featureSet.provider.iterator();
66
                                skypto(index);
67
                        }
68
                } else {
69
                        this.iterator = featureSet.provider.iterator();
70
                }
71
        }
72

    
73
        protected void skypto(long index) {
74
                // TODO: Comprobar si esta bien la condicion de n<=
75
                for (long n = 0; n <= index && this.getIterator().hasNext(); n++, this
76
                                .getIterator()
77
                                .next()) {
78
                        ;
79
                }
80
        }
81

    
82
        @Override
83
        public boolean hasNext() {
84
            if (this.fset == null) {
85
                return false;
86
            }
87
            fset.checkSourceStoreModified();
88
            if (this.getIterator().hasNext()) {
89
                return true;
90
            }
91
            try {
92
                this.dispose();
93
            } catch (Exception ex) {
94
                throw new RuntimeException("Can't dispose iterator.",ex);
95
            }
96
            return false;
97
        }
98

    
99
        @Override
100
        public Object next() {
101
                if( fset == null ) {
102
                        throw new NoSuchElementException();
103
                }
104
                fset.checkSourceStoreModified();
105
                lastFeature = null;
106
                if (!this.hasNext()) {
107
                        throw new NoSuchElementException();
108
                }
109
                try {
110
                        lastFeature = this.createFeature((FeatureProvider) this.getIterator()
111
                                        .next());
112
                        return lastFeature;
113
                } catch (DataException e) {
114
                        throw new RuntimeException(e);
115
                }
116
        }
117

    
118
        public void remove() {
119
                if (!fset.store.isEditing()) {
120
                        throw new UnsupportedOperationException();
121
                }
122
                if (lastFeature == null) {
123
                        throw new IllegalStateException();
124
                }
125
                try {
126
                        this.fset.delete(this.lastFeature);
127
                } catch (DataException e) {
128
                        // FIXME Cambiar excepcion a una Runtime de DAL
129
                        throw new RuntimeException(e);
130
                }
131
                lastFeature = null;
132
        }
133

    
134
        protected DefaultFeature createFeature(FeatureProvider fData)
135
                        throws DataException {
136
                fData.setNew(false);
137
                if (this.fset.transform.isEmpty()) {
138
                        return new DefaultFeature(fset.store, fData);
139
                } else {
140
                        return (DefaultFeature) this.fset.transform.applyTransform(
141
                                        new DefaultFeature(fset.store, fData), fset
142
                                                        .getDefaultFeatureType());
143
                }
144
        }
145

    
146
        protected Iterator getIterator() {
147
                return this.iterator;
148
        }
149

    
150
        protected boolean skipFeature(FeatureProvider data) {
151
                return false;
152
        }
153

    
154
        protected void doNext() throws DataException {
155

    
156
        }
157

    
158
        protected void doDispose() throws BaseException {
159
                DisposeUtils.disposeQuietly(this.iterator);
160
                this.iterator = null;
161
                DisposeUtils.disposeQuietly(this.fset);
162
                this.fset = null;
163
                this.lastFeature = null;
164
        }
165

    
166
}