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 @ 46133
History | View | Annotate | Download (7.74 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.json.JsonObjectBuilder; |
39 |
import org.gvsig.tools.dispose.DisposableIterator; |
40 |
import org.gvsig.tools.dispose.DisposeUtils; |
41 |
import org.gvsig.tools.dynobject.DynObjectSet; |
42 |
import org.gvsig.tools.exception.BaseException; |
43 |
import org.gvsig.tools.visitor.VisitCanceledException; |
44 |
import org.gvsig.tools.visitor.Visitor; |
45 |
import org.gvsig.tools.visitor.impl.AbstractIndexedVisitable; |
46 |
import org.slf4j.Logger; |
47 |
import org.slf4j.LoggerFactory; |
48 |
|
49 |
|
50 |
public abstract class AbstractFeatureSet |
51 |
extends AbstractIndexedVisitable
|
52 |
implements FeatureSet {
|
53 |
|
54 |
private static class DisposableFeatureSetIterableImpl implements DisposableFeatureSetIterable { |
55 |
|
56 |
private final FeatureSet fset; |
57 |
private final DisposableIterator it; |
58 |
|
59 |
public DisposableFeatureSetIterableImpl(FeatureSet fset, DisposableIterator it) {
|
60 |
this.fset = fset;
|
61 |
// No se debe de hacer el bind. DisposeUtils.bind(fset);
|
62 |
this.it = it;
|
63 |
} |
64 |
|
65 |
@Override
|
66 |
public boolean hasNext() { |
67 |
return this.it.hasNext(); |
68 |
} |
69 |
|
70 |
@Override
|
71 |
public Feature next() {
|
72 |
return (Feature) this.it.next(); |
73 |
} |
74 |
|
75 |
@Override
|
76 |
public Iterator<Feature> iterator() { |
77 |
return this.it; |
78 |
} |
79 |
|
80 |
@Override
|
81 |
public void dispose() { |
82 |
DisposeUtils.disposeQuietly(this.it);
|
83 |
DisposeUtils.disposeQuietly(this.fset);
|
84 |
} |
85 |
|
86 |
@Override
|
87 |
public boolean isEmpty() { |
88 |
return this.fset.isEmpty(); |
89 |
} |
90 |
|
91 |
@Override
|
92 |
public long size64() { |
93 |
return this.fset.size64(); |
94 |
} |
95 |
|
96 |
@Override
|
97 |
public FeatureSet getFeatureSet() {
|
98 |
return this.fset; |
99 |
} |
100 |
|
101 |
|
102 |
} |
103 |
|
104 |
protected static final Logger LOG = LoggerFactory.getLogger(AbstractFeatureSet.class); |
105 |
|
106 |
public abstract FeatureStore getFeatureStore(); |
107 |
|
108 |
@Override
|
109 |
public final void accept(Visitor visitor, long firstValueIndex, long elements) throws BaseException { |
110 |
try {
|
111 |
doAccept(visitor, firstValueIndex, elements); |
112 |
} catch (VisitCanceledException ex) {
|
113 |
// The visit has been cancelled by the visitor, so we finish here.
|
114 |
LOG.debug( |
115 |
"The visit, beggining on position {}, has been cancelled "
|
116 |
+ "by the visitor: {}", new Long(firstValueIndex), |
117 |
visitor); |
118 |
} |
119 |
} |
120 |
|
121 |
@Override
|
122 |
protected void doAccept(Visitor visitor, long firstValueIndex) |
123 |
throws VisitCanceledException, BaseException {
|
124 |
doAccept(visitor, firstValueIndex, 0);
|
125 |
} |
126 |
|
127 |
protected void doAccept(Visitor visitor, long firstValueIndex, long elements) |
128 |
throws VisitCanceledException, BaseException {
|
129 |
DisposableIterator iterator = fastIterator(firstValueIndex, elements); |
130 |
|
131 |
try {
|
132 |
while (iterator.hasNext()) {
|
133 |
Feature feature = (Feature) iterator.next(); |
134 |
visitor.visit(feature); |
135 |
} |
136 |
} finally {
|
137 |
iterator.dispose(); |
138 |
} |
139 |
} |
140 |
|
141 |
@Override
|
142 |
public Feature first() {
|
143 |
DisposableIterator it = null;
|
144 |
try {
|
145 |
it = this.iterator();
|
146 |
if( it == null ) { |
147 |
return null; |
148 |
} |
149 |
if( it.hasNext() ) {
|
150 |
Feature f = (Feature) it.next(); |
151 |
return f;
|
152 |
} |
153 |
return null; |
154 |
} finally {
|
155 |
DisposeUtils.disposeQuietly(it); |
156 |
} |
157 |
} |
158 |
|
159 |
@Override
|
160 |
public boolean isEmpty() { |
161 |
return this.size64() == 0; |
162 |
} |
163 |
|
164 |
@Override
|
165 |
public DisposableIterator fastIterator() throws DataException { |
166 |
return this.fastIterator(0); |
167 |
} |
168 |
|
169 |
@Override
|
170 |
public DisposableIterator iterator() {
|
171 |
try {
|
172 |
return this.fastIterator(0); |
173 |
} catch (DataException ex) {
|
174 |
throw new RuntimeException("Can't obtain iterator.",ex); |
175 |
} |
176 |
} |
177 |
|
178 |
@Override
|
179 |
public DisposableFeatureSetIterable iterable() {
|
180 |
return iterable(true); |
181 |
} |
182 |
|
183 |
@Override
|
184 |
public DisposableFeatureSetIterable iterable(boolean disposeFeatureSet) { |
185 |
if (!disposeFeatureSet) {
|
186 |
DisposeUtils.bind(this);
|
187 |
} |
188 |
return new DisposableFeatureSetIterableImpl(this, this.iterator()); |
189 |
} |
190 |
@Override
|
191 |
public DynObjectSet getDynObjectSet() {
|
192 |
return this.getDynObjectSet(true); |
193 |
} |
194 |
|
195 |
@Override
|
196 |
public DynObjectSet getDynObjectSet(boolean fast) { |
197 |
return new DynObjectSetFeatureSetFacade(this, this.getFeatureStore(), fast); |
198 |
} |
199 |
|
200 |
@Override
|
201 |
public boolean isFromStore(DataStore store) { |
202 |
return this.getFeatureStore().equals(store); |
203 |
} |
204 |
|
205 |
@Override
|
206 |
public long size64() { |
207 |
try {
|
208 |
return this.getSize(); |
209 |
} catch (DataException ex) {
|
210 |
throw new RuntimeException("Can't get size",ex); |
211 |
} |
212 |
} |
213 |
|
214 |
@Override
|
215 |
public int size() { |
216 |
try {
|
217 |
long sz = this.getSize(); |
218 |
if( sz > Integer.MAX_VALUE ) { |
219 |
return Integer.MAX_VALUE; |
220 |
} |
221 |
return (int) sz; |
222 |
} catch (DataException ex) {
|
223 |
throw new RuntimeException("Can't get size",ex); |
224 |
} |
225 |
} |
226 |
|
227 |
@Override
|
228 |
public JsonArray toJson() {
|
229 |
JsonArrayBuilder builder = this.toJsonBuilder();
|
230 |
return builder.build();
|
231 |
} |
232 |
|
233 |
@Override
|
234 |
public JsonArrayBuilder toJsonBuilder() {
|
235 |
// TODO: estaria bien hacer una implementacion alternativa que devolviese
|
236 |
// un JsonArray basado en el FeatureSet/FeaturePagingHelper, asi no
|
237 |
// tendria que construirse en memoria el JSON entero.
|
238 |
try {
|
239 |
JsonArrayBuilder builder = Json.createArrayBuilder(); |
240 |
this.accept(new Visitor() { |
241 |
@Override
|
242 |
public void visit(Object obj) throws VisitCanceledException, BaseException { |
243 |
DefaultFeature f = (DefaultFeature) obj; |
244 |
JsonObject fjson = f.toJson(); |
245 |
builder.add(fjson); |
246 |
} |
247 |
}); |
248 |
return builder;
|
249 |
} catch (Exception ex) { |
250 |
throw new RuntimeException("Can't create JSON array.",ex); |
251 |
} |
252 |
} |
253 |
|
254 |
@Deprecated
|
255 |
public JsonArray toJSON() {
|
256 |
return this.toJson(); |
257 |
} |
258 |
} |