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 @ 45696
History | View | Annotate | Download (7.74 KB)
1 | 43089 | jjdelcerro | /**
|
---|---|---|---|
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 | 45100 | jjdelcerro | import java.util.Iterator; |
27 | 44655 | jjdelcerro | import javax.json.Json; |
28 | import javax.json.JsonArray; |
||
29 | import javax.json.JsonArrayBuilder; |
||
30 | import javax.json.JsonObject; |
||
31 | 43089 | jjdelcerro | 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 | 44655 | jjdelcerro | import org.gvsig.fmap.dal.feature.impl.DefaultFeature; |
37 | 43089 | jjdelcerro | import org.gvsig.fmap.dal.feature.impl.dynobjectutils.DynObjectSetFeatureSetFacade; |
38 | 45696 | jjdelcerro | import org.gvsig.json.JsonObjectBuilder; |
39 | 43089 | jjdelcerro | 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 | 43358 | jjdelcerro | import org.slf4j.Logger; |
47 | import org.slf4j.LoggerFactory; |
||
48 | 43089 | jjdelcerro | |
49 | |||
50 | public abstract class AbstractFeatureSet |
||
51 | extends AbstractIndexedVisitable
|
||
52 | implements FeatureSet {
|
||
53 | |||
54 | 45195 | omartinez | private static class DisposableFeatureSetIterableImpl implements DisposableFeatureSetIterable { |
55 | 45100 | jjdelcerro | |
56 | private final FeatureSet fset; |
||
57 | private final DisposableIterator it; |
||
58 | |||
59 | 45195 | omartinez | public DisposableFeatureSetIterableImpl(FeatureSet fset, DisposableIterator it) {
|
60 | 45100 | jjdelcerro | this.fset = fset;
|
61 | 45195 | omartinez | // No se debe de hacer el bind. DisposeUtils.bind(fset);
|
62 | 45100 | jjdelcerro | 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 | 45195 | omartinez | |
86 | @Override
|
||
87 | 45425 | jjdelcerro | public boolean isEmpty() { |
88 | 45195 | omartinez | return this.fset.isEmpty(); |
89 | } |
||
90 | |||
91 | @Override
|
||
92 | public long size64() { |
||
93 | return this.fset.size64(); |
||
94 | } |
||
95 | 45541 | fdiaz | |
96 | @Override
|
||
97 | public FeatureSet getFeatureSet() {
|
||
98 | return this.fset; |
||
99 | } |
||
100 | 45100 | jjdelcerro | |
101 | 45541 | fdiaz | |
102 | 45100 | jjdelcerro | } |
103 | |||
104 | 43358 | jjdelcerro | protected static final Logger LOG = LoggerFactory.getLogger(AbstractFeatureSet.class); |
105 | |||
106 | 43089 | jjdelcerro | public abstract FeatureStore getFeatureStore(); |
107 | |||
108 | @Override
|
||
109 | 43358 | jjdelcerro | 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 | 43089 | jjdelcerro | protected void doAccept(Visitor visitor, long firstValueIndex) |
123 | throws VisitCanceledException, BaseException {
|
||
124 | 43358 | jjdelcerro | 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 | 43089 | jjdelcerro | } |
136 | 43358 | jjdelcerro | } finally {
|
137 | iterator.dispose(); |
||
138 | 43089 | jjdelcerro | } |
139 | } |
||
140 | 43358 | jjdelcerro | |
141 | 43089 | jjdelcerro | @Override
|
142 | public Feature first() {
|
||
143 | DisposableIterator it = null;
|
||
144 | try {
|
||
145 | it = this.iterator();
|
||
146 | if( it == null ) { |
||
147 | return null; |
||
148 | } |
||
149 | 44100 | jjdelcerro | if( it.hasNext() ) {
|
150 | Feature f = (Feature) it.next(); |
||
151 | return f;
|
||
152 | } |
||
153 | return null; |
||
154 | 43089 | jjdelcerro | } finally {
|
155 | DisposeUtils.disposeQuietly(it); |
||
156 | } |
||
157 | } |
||
158 | |||
159 | @Override
|
||
160 | 45425 | jjdelcerro | public boolean isEmpty() { |
161 | return this.size64() == 0; |
||
162 | 43089 | jjdelcerro | } |
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 | 45195 | omartinez | throw new RuntimeException("Can't obtain iterator.",ex); |
175 | 43089 | jjdelcerro | } |
176 | } |
||
177 | 45100 | jjdelcerro | |
178 | @Override
|
||
179 | 45195 | omartinez | public DisposableFeatureSetIterable iterable() {
|
180 | return iterable(true); |
||
181 | 45100 | jjdelcerro | } |
182 | 43089 | jjdelcerro | |
183 | @Override
|
||
184 | 45195 | omartinez | public DisposableFeatureSetIterable iterable(boolean disposeFeatureSet) { |
185 | if (!disposeFeatureSet) {
|
||
186 | DisposeUtils.bind(this);
|
||
187 | } |
||
188 | return new DisposableFeatureSetIterableImpl(this, this.iterator()); |
||
189 | } |
||
190 | @Override
|
||
191 | 43089 | jjdelcerro | 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 | 44390 | jjdelcerro | |
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 | 45696 | jjdelcerro | @Override
|
228 | public JsonArray toJson() {
|
||
229 | JsonArrayBuilder builder = this.toJsonBuilder();
|
||
230 | return builder.build();
|
||
231 | } |
||
232 | |||
233 | @Override
|
||
234 | public JsonArrayBuilder toJsonBuilder() {
|
||
235 | 44655 | jjdelcerro | // 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 | 45425 | jjdelcerro | JsonObject fjson = f.toJson(); |
245 | 44655 | jjdelcerro | builder.add(fjson); |
246 | } |
||
247 | }); |
||
248 | 45696 | jjdelcerro | return builder;
|
249 | 44655 | jjdelcerro | } catch (Exception ex) { |
250 | throw new RuntimeException("Can't create JSON array.",ex); |
||
251 | } |
||
252 | } |
||
253 | 45696 | jjdelcerro | |
254 | @Deprecated
|
||
255 | public JsonArray toJSON() {
|
||
256 | return this.toJson(); |
||
257 | } |
||
258 | 43089 | jjdelcerro | } |