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 @ 45100
History | View | Annotate | Download (6.87 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 DisposableSetIteratorImpl implements DisposableSetIterator { |
54 |
|
55 |
private final FeatureSet fset; |
56 |
private final DisposableIterator it; |
57 |
|
58 |
public DisposableSetIteratorImpl(FeatureSet fset, DisposableIterator it) {
|
59 |
this.fset = fset;
|
60 |
this.it = it;
|
61 |
} |
62 |
|
63 |
@Override
|
64 |
public boolean hasNext() { |
65 |
return this.it.hasNext(); |
66 |
} |
67 |
|
68 |
@Override
|
69 |
public Feature next() {
|
70 |
return (Feature) this.it.next(); |
71 |
} |
72 |
|
73 |
@Override
|
74 |
public Iterator<Feature> iterator() { |
75 |
return this.it; |
76 |
} |
77 |
|
78 |
@Override
|
79 |
public void dispose() { |
80 |
DisposeUtils.disposeQuietly(this.it);
|
81 |
DisposeUtils.disposeQuietly(this.fset);
|
82 |
} |
83 |
|
84 |
} |
85 |
|
86 |
protected static final Logger LOG = LoggerFactory.getLogger(AbstractFeatureSet.class); |
87 |
|
88 |
public abstract FeatureStore getFeatureStore(); |
89 |
|
90 |
@Override
|
91 |
public final void accept(Visitor visitor, long firstValueIndex, long elements) throws BaseException { |
92 |
try {
|
93 |
doAccept(visitor, firstValueIndex, elements); |
94 |
} catch (VisitCanceledException ex) {
|
95 |
// The visit has been cancelled by the visitor, so we finish here.
|
96 |
LOG.debug( |
97 |
"The visit, beggining on position {}, has been cancelled "
|
98 |
+ "by the visitor: {}", new Long(firstValueIndex), |
99 |
visitor); |
100 |
} |
101 |
} |
102 |
|
103 |
@Override
|
104 |
protected void doAccept(Visitor visitor, long firstValueIndex) |
105 |
throws VisitCanceledException, BaseException {
|
106 |
doAccept(visitor, firstValueIndex, 0);
|
107 |
} |
108 |
|
109 |
protected void doAccept(Visitor visitor, long firstValueIndex, long elements) |
110 |
throws VisitCanceledException, BaseException {
|
111 |
DisposableIterator iterator = fastIterator(firstValueIndex, elements); |
112 |
|
113 |
try {
|
114 |
while (iterator.hasNext()) {
|
115 |
Feature feature = (Feature) iterator.next(); |
116 |
visitor.visit(feature); |
117 |
} |
118 |
} finally {
|
119 |
iterator.dispose(); |
120 |
} |
121 |
} |
122 |
|
123 |
@Override
|
124 |
public Feature first() {
|
125 |
DisposableIterator it = null;
|
126 |
try {
|
127 |
it = this.iterator();
|
128 |
if( it == null ) { |
129 |
return null; |
130 |
} |
131 |
if( it.hasNext() ) {
|
132 |
Feature f = (Feature) it.next(); |
133 |
return f;
|
134 |
} |
135 |
return null; |
136 |
} finally {
|
137 |
DisposeUtils.disposeQuietly(it); |
138 |
} |
139 |
} |
140 |
|
141 |
@Override
|
142 |
public boolean isEmpty() throws DataException { |
143 |
return this.getSize() == 0; |
144 |
} |
145 |
|
146 |
@Override
|
147 |
public DisposableIterator fastIterator() throws DataException { |
148 |
return this.fastIterator(0); |
149 |
} |
150 |
|
151 |
@Override
|
152 |
public DisposableIterator iterator() {
|
153 |
try {
|
154 |
return this.fastIterator(0); |
155 |
} catch (DataException ex) {
|
156 |
throw new RuntimeException("Can't obtain itertor.",ex); |
157 |
} |
158 |
} |
159 |
|
160 |
@Override
|
161 |
public DisposableSetIterator setIterator() {
|
162 |
return new DisposableSetIteratorImpl(this, this.iterator()); |
163 |
} |
164 |
|
165 |
@Override
|
166 |
public DynObjectSet getDynObjectSet() {
|
167 |
return this.getDynObjectSet(true); |
168 |
} |
169 |
|
170 |
@Override
|
171 |
public DynObjectSet getDynObjectSet(boolean fast) { |
172 |
return new DynObjectSetFeatureSetFacade(this, this.getFeatureStore(), fast); |
173 |
} |
174 |
|
175 |
@Override
|
176 |
public boolean isFromStore(DataStore store) { |
177 |
return this.getFeatureStore().equals(store); |
178 |
} |
179 |
|
180 |
@Override
|
181 |
public long size64() { |
182 |
try {
|
183 |
return this.getSize(); |
184 |
} catch (DataException ex) {
|
185 |
throw new RuntimeException("Can't get size",ex); |
186 |
} |
187 |
} |
188 |
|
189 |
@Override
|
190 |
public int size() { |
191 |
try {
|
192 |
long sz = this.getSize(); |
193 |
if( sz > Integer.MAX_VALUE ) { |
194 |
return Integer.MAX_VALUE; |
195 |
} |
196 |
return (int) sz; |
197 |
} catch (DataException ex) {
|
198 |
throw new RuntimeException("Can't get size",ex); |
199 |
} |
200 |
} |
201 |
|
202 |
public JsonArray toJSON() {
|
203 |
// TODO: estaria bien hacer una implementacion alternativa que devolviese
|
204 |
// un JsonArray basado en el FeatureSet/FeaturePagingHelper, asi no
|
205 |
// tendria que construirse en memoria el JSON entero.
|
206 |
try {
|
207 |
JsonArrayBuilder builder = Json.createArrayBuilder(); |
208 |
this.accept(new Visitor() { |
209 |
@Override
|
210 |
public void visit(Object obj) throws VisitCanceledException, BaseException { |
211 |
DefaultFeature f = (DefaultFeature) obj; |
212 |
JsonObject fjson = f.toJSON(); |
213 |
builder.add(fjson); |
214 |
} |
215 |
}); |
216 |
return builder.build();
|
217 |
} catch (Exception ex) { |
218 |
throw new RuntimeException("Can't create JSON array.",ex); |
219 |
} |
220 |
} |
221 |
} |