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 / FastOrderedIterator.java @ 40559

History | View | Annotate | Download (3.68 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.ArrayList;
27
import java.util.Collections;
28
import java.util.Iterator;
29
import java.util.List;
30

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

    
38
public class FastOrderedIterator extends DefaultIterator {
39
        DefaultFeature myFeature;
40
        protected Feature lastFeature = null;
41

    
42
        FastOrderedIterator(DefaultFeatureSet featureSet, Iterator iterator, long index) {
43
                super(featureSet);
44
                try {
45
                        this.initializeFeature();
46
                        if (featureSet.orderedData == null) {
47
                                // FIXME QUE PASA CON SIZE > Integer.MAX_VALUE ?????
48
        
49
                                List data = new ArrayList();
50
                                Object item;
51
                                while (iterator.hasNext()) {
52
                                        item = iterator.next();
53
                                        if (item instanceof DefaultFeature){
54
                                                data.add(((DefaultFeature) item).getData().getCopy());
55
                                        } else {
56
                                                data.add(((FeatureProvider)item).getCopy());
57
                                        }
58
                                }
59
                                Collections.sort(data, new FeatureProviderComparator(featureSet.store,
60
                                                featureSet.query.getOrder()));
61
                                featureSet.orderedData = data;
62
                        }
63
        
64
                        if (index < Integer.MAX_VALUE) {
65
                                this.iterator = featureSet.orderedData.listIterator((int) index);
66
                        } else {
67
                                this.iterator = featureSet.orderedData.iterator();
68
                                this.skypto(index);
69
                        }
70
                }
71
                finally {                        
72
                        if (iterator instanceof DisposableIterator) {
73
                                ((DisposableIterator) iterator).dispose();
74
                        }
75
                }
76
        }
77

    
78
        public FastOrderedIterator(DefaultFeatureSet featureSet, long index) {
79
                super(featureSet);
80
                this.initializeFeature();
81

    
82
                if (index < Integer.MAX_VALUE) {
83
                        this.iterator = featureSet.orderedData.listIterator((int) index);
84
                } else {
85
                        this.iterator = featureSet.orderedData.iterator();
86
                        this.skypto(index);
87
                }
88
        }
89

    
90
        protected DefaultFeature createFeature(FeatureProvider fData) {
91
                fData.setNew(false);
92
                this.myFeature.setData(fData);
93
                return this.myFeature;
94
        }
95

    
96
        protected void initializeFeature() {
97
                myFeature = new DefaultFeature(fset.store);
98
        }
99

    
100
        public Object next() {
101
                lastFeature = null;
102
                lastFeature = (Feature) super.next();
103
                return lastFeature;
104
        }
105

    
106
        public void remove() {
107
                if (!fset.store.isEditing()) {
108
                        throw new UnsupportedOperationException();
109
                }
110
                if (this.lastFeature == null) {
111
                        throw new IllegalStateException();
112
                }
113
                try {
114
                        fset.store.delete(this.lastFeature);
115
                } catch (DataException e) {
116
                        // FIXME Cambiar excepcion a una Runtime de DAL
117
                        throw new RuntimeException(e);
118
                }
119
                this.iterator.remove();
120
                this.initializeFeature();
121
        }
122

    
123
        protected void doDispose() throws BaseException {
124
                super.doDispose();
125
                myFeature = null;
126
                lastFeature = null;
127
        }
128
}
129