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

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

    
47

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

    
52
    protected static final Logger LOG = LoggerFactory.getLogger(AbstractFeatureSet.class);
53

    
54
    public abstract FeatureStore getFeatureStore();
55
    
56
    @Override
57
    public final void accept(Visitor visitor, long firstValueIndex, long elements) throws BaseException {
58
        try {
59
            doAccept(visitor, firstValueIndex, elements);
60
        } catch (VisitCanceledException ex) {
61
            // The visit has been cancelled by the visitor, so we finish here.
62
            LOG.debug(
63
                    "The visit, beggining on position {}, has been cancelled "
64
                    + "by the visitor: {}", new Long(firstValueIndex),
65
                    visitor);
66
        }
67
    }
68
    
69
    @Override
70
    protected void doAccept(Visitor visitor, long firstValueIndex)
71
        throws VisitCanceledException, BaseException {
72
        doAccept(visitor, firstValueIndex, 0);
73
    }
74

    
75
    protected void doAccept(Visitor visitor, long firstValueIndex, long elements)
76
        throws VisitCanceledException, BaseException {
77
        DisposableIterator iterator = fastIterator(firstValueIndex, elements);
78

    
79
        try {
80
            while (iterator.hasNext()) {
81
                Feature feature = (Feature) iterator.next();
82
                visitor.visit(feature);
83
            }
84
        } finally {
85
            iterator.dispose();
86
        }
87
    }
88
      
89
    @Override
90
    public Feature first() {
91
        DisposableIterator it = null;
92
        try {
93
            it = this.iterator();
94
            if( it == null ) {
95
                return null;
96
            }
97
            if( it.hasNext() ) {
98
                Feature f = (Feature) it.next();
99
                return f;
100
            }
101
            return null;
102
        } finally {
103
            DisposeUtils.disposeQuietly(it);
104
        }
105
    }
106

    
107
    @Override
108
    public boolean isEmpty() throws DataException {
109
        return this.getSize() == 0;
110
    }
111

    
112
    @Override
113
    public DisposableIterator fastIterator() throws DataException {
114
        return this.fastIterator(0);
115
    }   
116

    
117
    @Override
118
    public DisposableIterator iterator() {
119
        try {
120
            return this.fastIterator(0);
121
        } catch (DataException ex) {
122
            throw new RuntimeException("Can't obtain itertor.",ex);
123
        }
124
    }
125
    
126
    @Override
127
    public DynObjectSet getDynObjectSet() {
128
        return this.getDynObjectSet(true);
129
    }
130

    
131
    @Override
132
    public DynObjectSet getDynObjectSet(boolean fast) {
133
        return new DynObjectSetFeatureSetFacade(this, this.getFeatureStore(), fast);
134
    }
135

    
136
    @Override
137
    public boolean isFromStore(DataStore store) {
138
        return this.getFeatureStore().equals(store);
139
    }    
140

    
141
    @Override
142
    public long size64() {
143
        try {
144
            return this.getSize();
145
        } catch (DataException ex) {
146
            throw new RuntimeException("Can't get size",ex);
147
        }
148
    }
149

    
150
    @Override
151
    public int size() {
152
        try {
153
            long sz = this.getSize();
154
            if( sz > Integer.MAX_VALUE ) {
155
                return Integer.MAX_VALUE;
156
            }
157
            return (int) sz;
158
        } catch (DataException ex) {
159
            throw new RuntimeException("Can't get size",ex);
160
        }
161
    }
162

    
163
    public JsonArray toJSON() {
164
        // TODO: estaria bien hacer una implementacion alternativa que devolviese
165
        // un JsonArray basado en el FeatureSet/FeaturePagingHelper, asi no 
166
        // tendria que construirse en memoria el JSON entero.
167
        try {
168
            JsonArrayBuilder builder = Json.createArrayBuilder();
169
            this.accept(new Visitor() {
170
                @Override
171
                public void visit(Object obj) throws VisitCanceledException, BaseException {
172
                    DefaultFeature f = (DefaultFeature) obj;
173
                    JsonObject fjson = f.toJSON();
174
                    builder.add(fjson);
175
                }
176
            });
177
            return builder.build();        
178
        } catch (Exception ex) {
179
            throw new RuntimeException("Can't create JSON array.",ex);
180
        }
181
    }
182
}