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 / AbstractFeatureSet.java @ 45647

History | View | Annotate | Download (7.46 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 javax.json.Json;
28
import javax.json.JsonArray;
29
import javax.json.JsonArrayBuilder;
30
import javax.json.JsonObject;
31
import org.gvsig.fmap.dal.DataStore;
32
import org.gvsig.fmap.dal.exception.DataException;
33
import org.gvsig.fmap.dal.feature.Feature;
34
import org.gvsig.fmap.dal.feature.FeatureSet;
35
import org.gvsig.fmap.dal.feature.FeatureStore;
36
import org.gvsig.fmap.dal.feature.impl.DefaultFeature;
37
import org.gvsig.fmap.dal.feature.impl.dynobjectutils.DynObjectSetFeatureSetFacade;
38
import org.gvsig.tools.dispose.DisposableIterator;
39
import org.gvsig.tools.dispose.DisposeUtils;
40
import org.gvsig.tools.dynobject.DynObjectSet;
41
import org.gvsig.tools.exception.BaseException;
42
import org.gvsig.tools.visitor.VisitCanceledException;
43
import org.gvsig.tools.visitor.Visitor;
44
import org.gvsig.tools.visitor.impl.AbstractIndexedVisitable;
45
import org.slf4j.Logger;
46
import org.slf4j.LoggerFactory;
47

    
48

    
49
public abstract class AbstractFeatureSet 
50
    extends AbstractIndexedVisitable 
51
    implements FeatureSet {
52

    
53
    private static class DisposableFeatureSetIterableImpl implements DisposableFeatureSetIterable {
54

    
55
        private final FeatureSet fset;
56
        private final DisposableIterator it;
57

    
58
        public DisposableFeatureSetIterableImpl(FeatureSet fset, DisposableIterator it) {
59
            this.fset = fset;
60
            // No se debe de hacer el bind. DisposeUtils.bind(fset);
61
            this.it = it;
62
        }
63
        
64
        @Override
65
        public boolean hasNext() {
66
            return this.it.hasNext();
67
        }
68

    
69
        @Override
70
        public Feature next() {
71
            return (Feature) this.it.next();
72
        }
73

    
74
        @Override
75
        public Iterator<Feature> iterator() {
76
            return this.it;
77
        }
78

    
79
        @Override
80
        public void dispose() {
81
            DisposeUtils.disposeQuietly(this.it);
82
            DisposeUtils.disposeQuietly(this.fset);
83
        }
84

    
85
        @Override
86
        public boolean isEmpty() {
87
            return this.fset.isEmpty();
88
        }
89

    
90
        @Override
91
        public long size64() {
92
            return this.fset.size64();
93
        }
94

    
95
        @Override
96
        public FeatureSet getFeatureSet() {
97
            return this.fset;
98
        }
99
        
100
        
101
    } 
102
    
103
    protected static final Logger LOG = LoggerFactory.getLogger(AbstractFeatureSet.class);
104

    
105
    public abstract FeatureStore getFeatureStore();
106
    
107
    @Override
108
    public final void accept(Visitor visitor, long firstValueIndex, long elements) throws BaseException {
109
        try {
110
            doAccept(visitor, firstValueIndex, elements);
111
        } catch (VisitCanceledException ex) {
112
            // The visit has been cancelled by the visitor, so we finish here.
113
            LOG.debug(
114
                    "The visit, beggining on position {}, has been cancelled "
115
                    + "by the visitor: {}", new Long(firstValueIndex),
116
                    visitor);
117
        }
118
    }
119
    
120
    @Override
121
    protected void doAccept(Visitor visitor, long firstValueIndex)
122
        throws VisitCanceledException, BaseException {
123
        doAccept(visitor, firstValueIndex, 0);
124
    }
125

    
126
    protected void doAccept(Visitor visitor, long firstValueIndex, long elements)
127
        throws VisitCanceledException, BaseException {
128
        DisposableIterator iterator = fastIterator(firstValueIndex, elements);
129

    
130
        try {
131
            while (iterator.hasNext()) {
132
                Feature feature = (Feature) iterator.next();
133
                visitor.visit(feature);
134
            }
135
        } finally {
136
            iterator.dispose();
137
        }
138
    }
139
      
140
    @Override
141
    public Feature first() {
142
        DisposableIterator it = null;
143
        try {
144
            it = this.iterator();
145
            if( it == null ) {
146
                return null;
147
            }
148
            if( it.hasNext() ) {
149
                Feature f = (Feature) it.next();
150
                return f;
151
            }
152
            return null;
153
        } finally {
154
            DisposeUtils.disposeQuietly(it);
155
        }
156
    }
157

    
158
    @Override
159
    public boolean isEmpty() {
160
        return this.size64() == 0;
161
    }
162

    
163
    @Override
164
    public DisposableIterator fastIterator() throws DataException {
165
        return this.fastIterator(0);
166
    }   
167

    
168
    @Override
169
    public DisposableIterator iterator() {
170
        try {
171
            return this.fastIterator(0);
172
        } catch (DataException ex) {
173
            throw new RuntimeException("Can't obtain iterator.",ex);
174
        }
175
    }
176

    
177
    @Override
178
    public DisposableFeatureSetIterable iterable() {
179
        return iterable(true);
180
    }
181
    
182
    @Override
183
    public DisposableFeatureSetIterable iterable(boolean disposeFeatureSet) {
184
        if (!disposeFeatureSet) {
185
            DisposeUtils.bind(this);
186
        }
187
        return new DisposableFeatureSetIterableImpl(this, this.iterator());
188
    }
189
    @Override
190
    public DynObjectSet getDynObjectSet() {
191
        return this.getDynObjectSet(true);
192
    }
193

    
194
    @Override
195
    public DynObjectSet getDynObjectSet(boolean fast) {
196
        return new DynObjectSetFeatureSetFacade(this, this.getFeatureStore(), fast);
197
    }
198

    
199
    @Override
200
    public boolean isFromStore(DataStore store) {
201
        return this.getFeatureStore().equals(store);
202
    }    
203

    
204
    @Override
205
    public long size64() {
206
        try {
207
            return this.getSize();
208
        } catch (DataException ex) {
209
            throw new RuntimeException("Can't get size",ex);
210
        }
211
    }
212

    
213
    @Override
214
    public int size() {
215
        try {
216
            long sz = this.getSize();
217
            if( sz > Integer.MAX_VALUE ) {
218
                return Integer.MAX_VALUE;
219
            }
220
            return (int) sz;
221
        } catch (DataException ex) {
222
            throw new RuntimeException("Can't get size",ex);
223
        }
224
    }
225

    
226
    public JsonArray toJSON() {
227
        // TODO: estaria bien hacer una implementacion alternativa que devolviese
228
        // un JsonArray basado en el FeatureSet/FeaturePagingHelper, asi no 
229
        // tendria que construirse en memoria el JSON entero.
230
        try {
231
            JsonArrayBuilder builder = Json.createArrayBuilder();
232
            this.accept(new Visitor() {
233
                @Override
234
                public void visit(Object obj) throws VisitCanceledException, BaseException {
235
                    DefaultFeature f = (DefaultFeature) obj;
236
                    JsonObject fjson = f.toJson();
237
                    builder.add(fjson);
238
                }
239
            });
240
            return builder.build();        
241
        } catch (Exception ex) {
242
            throw new RuntimeException("Can't create JSON array.",ex);
243
        }
244
    }
245
}