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

History | View | Annotate | Download (4.47 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
        @Override
78
        public boolean hasNext() {
79
            if (this.fset == null) {
80
                return false;
81
            }
82
            fset.checkSourceStoreModified();
83
            if (this.getIterator().hasNext()) {
84
                return true;
85
            }
86
            try {
87
                this.doDispose();
88
            } catch (BaseException ex) {
89
                throw new RuntimeException("Can't dispose iterator.",ex);
90
            }
91
            return false;
92
        }
93

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

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

    
129
        protected DefaultFeature createFeature(FeatureProvider fData)
130
                        throws DataException {
131
                fData.setNew(false);
132
                if (this.fset.transform.isEmpty()) {
133
                        return new DefaultFeature(fset.store, fData);
134
                } else {
135
                        return (DefaultFeature) this.fset.transform.applyTransform(
136
                                        new DefaultFeature(fset.store, fData), fset
137
                                                        .getDefaultFeatureType());
138
                }
139
        }
140

    
141
        protected Iterator getIterator() {
142
                return this.iterator;
143
        }
144

    
145
        protected boolean isDeletedOrHasToSkip(FeatureProvider data) {
146
                return false;
147
        }
148

    
149
        protected void doNext() throws DataException {
150

    
151
        }
152

    
153
        protected void doDispose() throws BaseException {
154
                if (iterator instanceof DisposableIterator){
155
                        ((DisposableIterator)this.iterator).dispose();
156
                }
157
                this.iterator = null;
158
                this.fset = null;
159
                this.lastFeature = null;
160
        }
161

    
162
}