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

History | View | Annotate | Download (4.03 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

    
29
import org.gvsig.fmap.dal.exception.DataException;
30
import org.gvsig.tools.dispose.DisposableIterator;
31
import org.gvsig.fmap.dal.feature.Feature;
32
import org.gvsig.fmap.dal.feature.impl.DefaultFeature;
33
import org.gvsig.fmap.dal.feature.spi.FeatureProvider;
34
import org.gvsig.tools.dispose.impl.AbstractDisposable;
35
import org.gvsig.tools.exception.BaseException;
36

    
37

    
38
class DefaultIterator extends AbstractDisposable implements DisposableIterator {
39

    
40
        protected Iterator iterator;
41
        protected DefaultFeatureSet fset;
42
        protected Feature lastFeature = null;
43

    
44
        public DefaultIterator(DefaultFeatureSet featureSet) {
45
                this.fset = featureSet;
46
        }
47

    
48
        public DefaultIterator(DefaultFeatureSet featureSet, long index)
49
                        throws DataException {
50
                this.fset = featureSet;
51
                if (index > 0) {
52
                        if (featureSet.provider.canIterateFromIndex()) {
53
                                try {
54
                                        this.iterator = featureSet.provider.iterator(index);
55
                                } catch (UnsupportedOperationException e) {
56
                                        this.iterator = featureSet.provider.iterator();
57
                                        skypto(index);
58
                                }
59
                        } else {
60
                                this.iterator = featureSet.provider.iterator();
61
                                skypto(index);
62
                        }
63
                } else {
64
                        this.iterator = featureSet.provider.iterator();
65
                }
66
        }
67

    
68
        protected void skypto(long index) {
69
                // TODO: Comprobar si esta bien la condicion de n<=
70
                for (long n = 0; n <= index && this.getIterator().hasNext(); n++, this
71
                                .getIterator()
72
                                .next()) {
73
                        ;
74
                }
75
        }
76

    
77
        public boolean hasNext() {
78
                fset.checkSourceStoreModified();
79
                return this.getIterator().hasNext();
80
        }
81

    
82
        public Object next() {
83
                fset.checkSourceStoreModified();
84
                lastFeature = null;
85
                if (!this.hasNext()) {
86
                        throw new NoSuchElementException();
87
                }
88
                try {
89
                        lastFeature = this.createFeature((FeatureProvider) this.getIterator()
90
                                        .next());
91
                        return lastFeature;
92
                } catch (DataException e) {
93
                        RuntimeException ex = new RuntimeException();
94
                        e.initCause(e);
95
                        throw ex;
96
                }
97
        }
98

    
99
        public void remove() {
100
                if (!fset.store.isEditing()) {
101
                        throw new UnsupportedOperationException();
102
                }
103
                if (lastFeature == null) {
104
                        throw new IllegalStateException();
105
                }
106
                try {
107
                        this.fset.delete(this.lastFeature);
108
                } catch (DataException e) {
109
                        // FIXME Cambiar excepcion a una Runtime de DAL
110
                        throw new RuntimeException(e);
111
                }
112
                lastFeature = null;
113
        }
114

    
115
        protected DefaultFeature createFeature(FeatureProvider fData)
116
                        throws DataException {
117
                fData.setNew(false);
118
                if (this.fset.transform.isEmpty()) {
119
                        return new DefaultFeature(fset.store, fData);
120
                } else {
121
                        return (DefaultFeature) this.fset.transform.applyTransform(
122
                                        new DefaultFeature(fset.store, fData), fset
123
                                                        .getDefaultFeatureType());
124
                }
125
        }
126

    
127
        protected Iterator getIterator() {
128
                return this.iterator;
129
        }
130

    
131
        protected boolean isDeletedOrHasToSkip(FeatureProvider data) {
132
                return false;
133
        }
134

    
135
        protected void doNext() throws DataException {
136

    
137
        }
138

    
139
        protected void doDispose() throws BaseException {
140
                if (iterator instanceof DisposableIterator){
141
                        ((DisposableIterator)this.iterator).dispose();
142
                }
143
                this.iterator = null;
144
                this.fset = null;
145
                this.lastFeature = null;
146
        }
147

    
148
}