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

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

    
42

    
43
public abstract class AbstractFeatureSet 
44
    extends AbstractIndexedVisitable 
45
    implements FeatureSet {
46

    
47
    protected static final Logger LOG = LoggerFactory.getLogger(AbstractFeatureSet.class);
48

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

    
70
    protected void doAccept(Visitor visitor, long firstValueIndex, long elements)
71
        throws VisitCanceledException, BaseException {
72
        DisposableIterator iterator = fastIterator(firstValueIndex, elements);
73

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

    
102
    @Override
103
    public boolean isEmpty() throws DataException {
104
        return this.getSize() == 0;
105
    }
106

    
107
    @Override
108
    public DisposableIterator fastIterator() throws DataException {
109
        return this.fastIterator(0);
110
    }   
111

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

    
126
    @Override
127
    public DynObjectSet getDynObjectSet(boolean fast) {
128
        return new DynObjectSetFeatureSetFacade(this, this.getFeatureStore(), fast);
129
    }
130

    
131
    @Override
132
    public boolean isFromStore(DataStore store) {
133
        return this.getFeatureStore().equals(store);
134
    }    
135
}