Statistics
| Revision:

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

History | View | Annotate | Download (6.6 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

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

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

    
63
        public DefaultFeatureIndex(FeatureStoreProviderServices featureStore, FeatureType featureType, FeatureIndexProvider indexProvider, String attributeName, String indexName) {
64
                if (featureStore == null) throw new IllegalArgumentException("featureStore cannot be null.");
65
                if (featureType == null) throw new IllegalArgumentException("featureType cannot be null.");
66
                if (attributeName == null) throw new IllegalArgumentException("attributeName cannot be null.");
67
                if (indexName == null) throw new IllegalArgumentException("indexName cannot be null.");
68
                
69
                if (featureStore.getProvider().getFeatureReferenceIdType() != DataTypes.INT) {
70
                        throw new IllegalArgumentException();
71
                }
72
                
73
                FeatureAttributeDescriptor attr = featureType.getAttributeDescriptor(attributeName);
74
                if (attr == null) 
75
                        throw new IllegalArgumentException("Attribute " + attributeName +" not found in FeatureType " + featureType.toString());
76
                        
77
                this.featureStore = featureStore;
78
                this.featureType = featureType;
79
                this.attributeName = attributeName;
80
                this.name = indexName;
81
                this.dataType = attr.getDataType();
82
                this.indexProvider = indexProvider;
83
        }
84

    
85
        public final FeatureAttributeDescriptor getFeatureAttributeDescriptor() {
86
                return featureType.getAttributeDescriptor(attributeName);
87
        }
88

    
89
        public final FeatureStoreProviderServices getFeatureStore() {
90
                return featureStore;
91
        }
92

    
93
        public final FeatureType getFeatureType() {
94
                return featureType;
95
        }
96

    
97
        public final String getName() {
98
                return name;
99
        }
100
        
101
        public final String getAttributeName() {
102
                return attributeName;
103
        }
104
        
105
        public final int getDataType() {
106
                return dataType;
107
        }
108

    
109
        /**
110
         * Fills this index with all the data in its FeatureStore
111
         */
112
        public final void fill() throws DataIndexException {
113
                try {
114
                        insert(getFeatureStore().getFeatureSet());
115
                } catch (DataException e) {
116
                        throw new DataIndexException(e);
117
                }
118
        }
119

    
120
        public final void insert(FeatureSet data) throws DataException {
121
                Iterator it = data.iterator();
122
                while (it.hasNext()) {
123
                        Feature feat = (Feature) it.next();
124
                        insert(feat);
125
                }
126
        }
127

    
128
        public void insert(Feature feat) {
129
                try {
130
                        indexProvider.insert(feat.get(attributeName), feat.getReference());
131
                } catch (NullPointerException e) {
132
                        throw new IllegalArgumentException("Feature does not contain a column with name " + attributeName);
133
                } catch (ClassCastException e) {
134
                        throw new IllegalArgumentException("Attribute data type is not valid.");
135
                }
136
        }
137

    
138
        /** Performs a search in the index. The type of search depends on the passed data 
139
         * 
140
         *  TODO explain 
141
         * */
142
        public FeatureSetProvider getFeatureSet(String attrName, Object[] data) throws DataIndexException {
143
                return new IndexFeatureSet(this, new DefaultLongList(this.match(data)));
144
        }
145
        
146
        /** Performs a search in the index and returns a list with the elements that match the parameters */
147
        private List match(Object[] values) throws DataIndexException {
148
                
149
                if (values == null) throw new IllegalArgumentException("values can't be null.");
150
                if (values.length == 0) throw new IllegalArgumentException("values must contain at least 1 value.");
151
                
152
                List result = null;
153
                if (values.length == 1) {
154
                        result = indexProvider.match(values[0]);
155
                }
156
                if (values.length == 2) {
157
                        if (values[0] instanceof Integer && !(values[1] instanceof Integer)) {
158
                                result = indexProvider.nearest(((Integer)values[0]).intValue(), values[1]);
159
                        } else {
160
                                result = indexProvider.match(values[0], values[1]);        
161
                        }                        
162
                }
163
                return result;
164
        }
165
        
166
        
167
        public void initialize() {
168
                indexProvider.initialize();
169
        }
170
        
171

    
172

    
173
        public void delete(Object o, FeatureReference fref) {
174
                indexProvider.delete(o, fref);
175
                
176
        }
177

    
178
        public void delete(Feature feat) {
179
                // TODO Auto-generated method stub
180
                
181
        }
182

    
183
        public void delete(FeatureSet data) throws DataIndexException {
184
                // TODO Auto-generated method stub
185
                
186
        }
187

    
188
        public String[] getAttributeNames() {
189
                // TODO Auto-generated method stub
190
                return null;
191
        }
192

    
193
        
194
        /**
195
         * FIXME ver que hacemos con estas constantes
196
         * Convenience constants to identify sets of providers classified by supported data types. 
197
         */
198
        public interface PROVIDERS {
199
                
200
                public interface GEOMETRY {
201
                        public static final String GT2_QUADTREE = "QuadTreeGt2Factory";
202
                        public static final String JSI_RTREE = "RTreeJsiFactory";
203
                        public static final String JTS_QUADTREE = "QuadTreeJtsFactory";
204
                        public static final String SPATIALINDEX_RTREE = "RTreeSptLibFactory";
205
                        public static final String DEFAULT = GT2_QUADTREE;
206
                }
207
                
208
                public interface DATE {
209
                        // added as example
210
                }
211
                
212
                public interface INTEGER {
213
                        // added as example
214
                }                
215
        }        
216
        
217
}
218