Statistics
| Revision:

svn-gvsig-desktop / branches / v2_0_0_prep / libraries / libFMap_data / src / org / gvsig / fmap / data / feature / impl / DefaultFeatureIndex.java @ 24017

History | View | Annotate | Download (7.43 KB)

1
/* gvSIG. Geographic Information System of the Valencian Government
2
*
3
* Copyright (C) 2007-2008 Infrastructures and Transports Department
4
* of the Valencian Government (CIT)
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 2
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
*/
22

    
23
/*
24
* AUTHORS (In addition to CIT):
25
* 2008 {{Company}}   {{Task}}
26
*/
27

    
28

    
29
package org.gvsig.fmap.data.feature.impl;
30

    
31
import java.util.Iterator;
32
import java.util.List;
33

    
34
import org.gvsig.fmap.data.DataTypes;
35
import org.gvsig.fmap.data.exceptions.DataException;
36
import org.gvsig.fmap.data.feature.Feature;
37
import org.gvsig.fmap.data.feature.FeatureAttributeDescriptor;
38
import org.gvsig.fmap.data.feature.FeatureReference;
39
import org.gvsig.fmap.data.feature.FeatureSet;
40
import org.gvsig.fmap.data.feature.FeatureType;
41
import org.gvsig.fmap.data.feature.exceptions.DataIndexException;
42
import org.gvsig.fmap.data.feature.spi.DefaultLongList;
43
import org.gvsig.fmap.data.feature.spi.FeatureSetProvider;
44
import org.gvsig.fmap.data.feature.spi.FeatureStoreProviderServices;
45
import org.gvsig.fmap.data.feature.spi.index.FeatureIndexProvider;
46
import org.gvsig.fmap.data.feature.spi.index.FeatureIndexProviderServices;
47
import org.gvsig.tools.exception.NotYetImplemented;
48

    
49
/**
50
 * Abstract index provider.
51
 *
52
 * @author jyarza
53
 *
54
 */
55
public class DefaultFeatureIndex implements FeatureIndexProviderServices {
56

    
57
        private final FeatureStoreProviderServices featureStore;
58
        private final FeatureType featureType;
59
        private final String attributeName;
60
        private final String name;
61
        private final int dataType;
62
        private final FeatureIndexProvider indexProvider;
63

    
64
        public DefaultFeatureIndex(FeatureStoreProviderServices featureStore, FeatureType featureType, FeatureIndexProvider indexProvider, String attributeName, String indexName) {
65
                if (featureStore == null) {
66
                        throw new IllegalArgumentException("featureStore cannot be null.");
67
                }
68
                if (featureType == null) {
69
                        throw new IllegalArgumentException("featureType cannot be null.");
70
                }
71
                if (attributeName == null) {
72
                        throw new IllegalArgumentException("attributeName cannot be null.");
73
                }
74
                if (indexName == null) {
75
                        throw new IllegalArgumentException("indexName cannot be null.");
76
                }
77

    
78
                if (featureStore.getProvider().getFeatureReferenceIdType() != DataTypes.INT) {
79
                        throw new IllegalArgumentException();
80
                }
81

    
82
                FeatureAttributeDescriptor attr = featureType.getAttributeDescriptor(attributeName);
83
                if (attr == null) {
84
                        throw new IllegalArgumentException("Attribute " + attributeName +" not found in FeatureType " + featureType.toString());
85
                }
86

    
87
                this.featureStore = featureStore;
88
                this.featureType = featureType;
89
                this.attributeName = attributeName;
90
                this.name = indexName;
91
                this.dataType = attr.getDataType();
92
                this.indexProvider = indexProvider;
93
        }
94

    
95
        public final FeatureAttributeDescriptor getFeatureAttributeDescriptor() {
96
                return featureType.getAttributeDescriptor(attributeName);
97
        }
98

    
99
        public final FeatureStoreProviderServices getFeatureStore() {
100
                return featureStore;
101
        }
102

    
103
        public final FeatureType getFeatureType() {
104
                return featureType;
105
        }
106

    
107
        public final String getName() {
108
                return name;
109
        }
110

    
111
        public final String getAttributeName() {
112
                return attributeName;
113
        }
114

    
115
        public final int getDataType() {
116
                return dataType;
117
        }
118

    
119
        /**
120
         * Fills this index with all the data in its FeatureStore
121
         */
122
        public final void fill() throws DataIndexException {
123
                try {
124
                        insert(getFeatureStore().getFeatureSet());
125
                } catch (DataException e) {
126
                        throw new DataIndexException(e);
127
                }
128
        }
129

    
130
        public final void insert(FeatureSet data) throws DataException {
131
                Iterator it = data.iterator();
132
                while (it.hasNext()) {
133
                        Feature feat = (Feature) it.next();
134
                        insert(feat);
135
                }
136
        }
137

    
138
        public void insert(Feature feat) {
139
                try {
140
                        indexProvider.insert(feat.get(attributeName), feat.getReference());
141
                } catch (NullPointerException e) {
142
                        throw new IllegalArgumentException("Feature does not contain a column with name " + attributeName);
143
                } catch (ClassCastException e) {
144
                        throw new IllegalArgumentException("Attribute data type is not valid.");
145
                }
146
        }
147

    
148
        public FeatureSetProvider getMatchFeatureSet(Object value)
149
                        throws DataIndexException {
150
                return new IndexFeatureSet(this, new DefaultLongList(indexProvider
151
                                .match(value)));
152
        }
153

    
154
        public FeatureSetProvider getRangeFeatureSet(Object value1, Object value2)
155
                        throws DataIndexException {
156
                return new IndexFeatureSet(this, new DefaultLongList(indexProvider
157
                                .match(value1, value2)));
158
        }
159

    
160
        public FeatureSetProvider getNearestFeatureSet(int count, Object value)
161
                        throws DataIndexException {
162
                return new IndexFeatureSet(this, new DefaultLongList(indexProvider
163
                                .nearest(count, value)));
164
        }
165

    
166
        public FeatureSetProvider getNearestFeatureSet(int count, int tolerance,
167
                        Object value) throws DataIndexException {
168
                throw new NotYetImplemented();
169
        }
170

    
171
        /**
172
         * Performs a search in the index. The type of search depends on the passed
173
         * data
174
         *
175
         * @deprecated
176
         * */
177
        public FeatureSetProvider getFeatureSet(String attrName, Object[] data) throws DataIndexException {
178
                return new IndexFeatureSet(this, new DefaultLongList(this.match(data)));
179
        }
180

    
181
        /**
182
         * Performs a search in the index and returns a list with the elements that
183
         * match the parameters
184
         *
185
         * @deprecated
186
         */
187
        private List match(Object[] values) throws DataIndexException {
188

    
189
                if (values == null) {
190
                        throw new IllegalArgumentException("values can't be null.");
191
                }
192
                if (values.length == 0) {
193
                        throw new IllegalArgumentException("values must contain at least 1 value.");
194
                }
195

    
196
                List result = null;
197
                if (values.length == 1) {
198
                        result = indexProvider.match(values[0]);
199
                }
200
                if (values.length == 2) {
201
                        if (values[0] instanceof Integer && !(values[1] instanceof Integer)) {
202
                                result = indexProvider.nearest(((Integer)values[0]).intValue(), values[1]);
203
                        } else {
204
                                result = indexProvider.match(values[0], values[1]);
205
                        }
206
                }
207
                return result;
208
        }
209

    
210

    
211
        public void initialize() {
212
                indexProvider.initialize();
213
        }
214

    
215
        public void delete(Object o, FeatureReference fref) {
216
                indexProvider.delete(o, fref);
217
        }
218

    
219
        public void delete(Feature feat) {
220
                // TODO Auto-generated method stub
221
        }
222

    
223
        public void delete(FeatureSet data) throws DataIndexException {
224
                // TODO Auto-generated method stub
225

    
226
        }
227

    
228
        public String[] getAttributeNames() {
229
                // TODO Auto-generated method stub
230
                return null;
231
        }
232

    
233
        /**
234
         * FIXME ver que hacemos con estas constantes
235
         * Convenience constants to identify sets of providers classified by supported data types.
236
         */
237
        public interface PROVIDERS {
238

    
239
                public interface GEOMETRY {
240
                        public static final String GT2_QUADTREE = "QuadTreeGt2Factory";
241
                        public static final String JSI_RTREE = "RTreeJsiFactory";
242
                        public static final String JTS_QUADTREE = "QuadTreeJtsFactory";
243
                        public static final String SPATIALINDEX_RTREE = "RTreeSptLibFactory";
244
                        public static final String DEFAULT = GT2_QUADTREE;
245
                }
246

    
247
                public interface DATE {
248
                        // added as example
249
                }
250

    
251
                public interface INTEGER {
252
                        // added as example
253
                }
254
        }
255

    
256

    
257
}
258