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

History | View | Annotate | Download (4.67 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.DisposeUtils;
35
import org.gvsig.tools.dispose.impl.AbstractDisposable;
36
import org.gvsig.tools.exception.BaseException;
37

    
38

    
39
class DefaultIterator extends AbstractDisposable implements DisposableIterator {
40

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

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

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

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

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

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

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

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

    
144
        protected Iterator getIterator() {
145
                return this.iterator;
146
        }
147

    
148
        protected boolean isDeletedOrHasToSkip(FeatureProvider data) {
149
                return false;
150
        }
151

    
152
        protected void doNext() throws DataException {
153

    
154
        }
155

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

    
166
}