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

History | View | Annotate | Download (7.35 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
    } 
96
    
97
    protected static final Logger LOG = LoggerFactory.getLogger(AbstractFeatureSet.class);
98

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

    
120
    protected void doAccept(Visitor visitor, long firstValueIndex, long elements)
121
        throws VisitCanceledException, BaseException {
122
        DisposableIterator iterator = fastIterator(firstValueIndex, elements);
123

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

    
152
    @Override
153
    public boolean isEmpty() {
154
        return this.size64() == 0;
155
    }
156

    
157
    @Override
158
    public DisposableIterator fastIterator() throws DataException {
159
        return this.fastIterator(0);
160
    }   
161

    
162
    @Override
163
    public DisposableIterator iterator() {
164
        try {
165
            return this.fastIterator(0);
166
        } catch (DataException ex) {
167
            throw new RuntimeException("Can't obtain iterator.",ex);
168
        }
169
    }
170

    
171
    @Override
172
    public DisposableFeatureSetIterable iterable() {
173
        return iterable(true);
174
    }
175
    
176
    @Override
177
    public DisposableFeatureSetIterable iterable(boolean disposeFeatureSet) {
178
        if (!disposeFeatureSet) {
179
            DisposeUtils.bind(this);
180
        }
181
        return new DisposableFeatureSetIterableImpl(this, this.iterator());
182
    }
183
    @Override
184
    public DynObjectSet getDynObjectSet() {
185
        return this.getDynObjectSet(true);
186
    }
187

    
188
    @Override
189
    public DynObjectSet getDynObjectSet(boolean fast) {
190
        return new DynObjectSetFeatureSetFacade(this, this.getFeatureStore(), fast);
191
    }
192

    
193
    @Override
194
    public boolean isFromStore(DataStore store) {
195
        return this.getFeatureStore().equals(store);
196
    }    
197

    
198
    @Override
199
    public long size64() {
200
        try {
201
            return this.getSize();
202
        } catch (DataException ex) {
203
            throw new RuntimeException("Can't get size",ex);
204
        }
205
    }
206

    
207
    @Override
208
    public int size() {
209
        try {
210
            long sz = this.getSize();
211
            if( sz > Integer.MAX_VALUE ) {
212
                return Integer.MAX_VALUE;
213
            }
214
            return (int) sz;
215
        } catch (DataException ex) {
216
            throw new RuntimeException("Can't get size",ex);
217
        }
218
    }
219

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