root / branches / v2_0_0_prep / libraries / libFMap_data / src / org / gvsig / fmap / data / feature / impl / DefaultFeatureIndex.java @ 24187
History | View | Annotate | Download (6.73 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.io.File; |
32 |
import java.util.Iterator; |
33 |
import java.util.List; |
34 |
|
35 |
import org.gvsig.fmap.data.DataTypes; |
36 |
import org.gvsig.fmap.data.exceptions.DataException; |
37 |
import org.gvsig.fmap.data.exceptions.InitializeException; |
38 |
import org.gvsig.fmap.data.feature.Feature; |
39 |
import org.gvsig.fmap.data.feature.FeatureAttributeDescriptor; |
40 |
import org.gvsig.fmap.data.feature.FeatureReference; |
41 |
import org.gvsig.fmap.data.feature.FeatureSet; |
42 |
import org.gvsig.fmap.data.feature.FeatureType; |
43 |
import org.gvsig.fmap.data.feature.exceptions.DataIndexException; |
44 |
import org.gvsig.fmap.data.feature.spi.DefaultLongList; |
45 |
import org.gvsig.fmap.data.feature.spi.FeatureSetProvider; |
46 |
import org.gvsig.fmap.data.feature.spi.FeatureStoreProviderServices; |
47 |
import org.gvsig.fmap.data.feature.spi.index.FeatureIndexProvider; |
48 |
import org.gvsig.fmap.data.feature.spi.index.FeatureIndexProviderServices; |
49 |
import org.gvsig.tools.exception.NotYetImplemented; |
50 |
|
51 |
/**
|
52 |
* Default feature index provider services.
|
53 |
*
|
54 |
* @author jyarza
|
55 |
*
|
56 |
*/
|
57 |
public class DefaultFeatureIndex implements FeatureIndexProviderServices { |
58 |
|
59 |
private final FeatureStoreProviderServices featureStore; |
60 |
private final FeatureType featureType; |
61 |
private final String attributeName; |
62 |
private final String indexName; |
63 |
private final int dataType; |
64 |
private final FeatureIndexProvider indexProvider; |
65 |
|
66 |
public DefaultFeatureIndex(FeatureStoreProviderServices featureStore, FeatureType featureType, FeatureIndexProvider indexProvider, String attributeName, String indexName) { |
67 |
if (featureStore == null) { |
68 |
throw new IllegalArgumentException("featureStore cannot be null."); |
69 |
} |
70 |
if (featureType == null) { |
71 |
throw new IllegalArgumentException("featureType cannot be null."); |
72 |
} |
73 |
if (attributeName == null) { |
74 |
throw new IllegalArgumentException("attributeName cannot be null."); |
75 |
} |
76 |
if (indexName == null) { |
77 |
throw new IllegalArgumentException("indexName cannot be null."); |
78 |
} |
79 |
|
80 |
// FIXME Esto debe ir al provider
|
81 |
if (featureStore.getProvider().getFeatureReferenceIdType() != DataTypes.INT) {
|
82 |
throw new IllegalArgumentException(); |
83 |
} |
84 |
|
85 |
FeatureAttributeDescriptor attr = featureType.getAttributeDescriptor(attributeName); |
86 |
if (attr == null) { |
87 |
throw new IllegalArgumentException("Attribute " + attributeName +" not found in FeatureType " + featureType.toString()); |
88 |
} |
89 |
|
90 |
this.featureStore = featureStore;
|
91 |
this.featureType = featureType;
|
92 |
this.attributeName = attributeName;
|
93 |
this.indexName = indexName;
|
94 |
this.dataType = attr.getDataType();
|
95 |
this.indexProvider = indexProvider;
|
96 |
} |
97 |
|
98 |
public final FeatureAttributeDescriptor getFeatureAttributeDescriptor() { |
99 |
return featureType.getAttributeDescriptor(attributeName);
|
100 |
} |
101 |
|
102 |
public final FeatureStoreProviderServices getFeatureStore() { |
103 |
return featureStore;
|
104 |
} |
105 |
|
106 |
public final FeatureType getFeatureType() { |
107 |
return featureType;
|
108 |
} |
109 |
|
110 |
public final String getName() { |
111 |
return indexName;
|
112 |
} |
113 |
|
114 |
public final String getAttributeName() { |
115 |
return attributeName;
|
116 |
} |
117 |
|
118 |
public final int getDataType() { |
119 |
return dataType;
|
120 |
} |
121 |
|
122 |
/**
|
123 |
* Fills this index with all the data in its FeatureStore
|
124 |
*/
|
125 |
public final void fill() throws DataIndexException { |
126 |
try {
|
127 |
insert(getFeatureStore().getFeatureSet()); |
128 |
} catch (DataException e) {
|
129 |
throw new DataIndexException(e); |
130 |
} |
131 |
} |
132 |
|
133 |
public final void insert(FeatureSet data) throws DataException { |
134 |
Iterator it = data.iterator();
|
135 |
while (it.hasNext()) {
|
136 |
Feature feat = (Feature) it.next(); |
137 |
insert(feat); |
138 |
} |
139 |
} |
140 |
|
141 |
public void insert(Feature feat) { |
142 |
try {
|
143 |
indexProvider.insert(feat.get(attributeName), feat.getReference()); |
144 |
} catch (NullPointerException e) { |
145 |
throw new IllegalArgumentException("Feature does not contain a column with name " + attributeName); |
146 |
} catch (ClassCastException e) { |
147 |
throw new IllegalArgumentException("Attribute data type is not valid."); |
148 |
} |
149 |
} |
150 |
|
151 |
public FeatureSetProvider getMatchFeatureSet(Object value) |
152 |
throws DataIndexException {
|
153 |
return new IndexFeatureSet(this, new DefaultLongList(indexProvider |
154 |
.match(value))); |
155 |
} |
156 |
|
157 |
public FeatureSetProvider getRangeFeatureSet(Object value1, Object value2) |
158 |
throws DataIndexException {
|
159 |
return new IndexFeatureSet(this, new DefaultLongList(indexProvider |
160 |
.match(value1, value2))); |
161 |
} |
162 |
|
163 |
public FeatureSetProvider getNearestFeatureSet(int count, Object value) |
164 |
throws DataIndexException {
|
165 |
return new IndexFeatureSet(this, new DefaultLongList(indexProvider |
166 |
.nearest(count, value))); |
167 |
} |
168 |
|
169 |
public FeatureSetProvider getNearestFeatureSet(int count, int tolerance, |
170 |
Object value) throws DataIndexException { |
171 |
// TODO Ver si indice soporta tolerancia / Implementar con tolerancia
|
172 |
return new IndexFeatureSet(this, new DefaultLongList(indexProvider |
173 |
.nearest(count, value))); |
174 |
} |
175 |
|
176 |
public void initialize() throws InitializeException { |
177 |
indexProvider.setFeatureIndexProviderServices(this);
|
178 |
indexProvider.initialize(); |
179 |
} |
180 |
|
181 |
public void delete(Object value, FeatureReference fref) { |
182 |
indexProvider.delete(value, fref); |
183 |
} |
184 |
|
185 |
public void delete(Feature feat) { |
186 |
indexProvider.delete(feat.get(this.attributeName), feat.getReference());
|
187 |
} |
188 |
|
189 |
public void delete(FeatureSet data) throws DataIndexException { |
190 |
try {
|
191 |
Iterator it = data.iterator();
|
192 |
while (it.hasNext()) {
|
193 |
Feature feat = (Feature) it.next(); |
194 |
delete(feat); |
195 |
} |
196 |
} catch (DataException e) {
|
197 |
throw new DataIndexException(e); |
198 |
} |
199 |
} |
200 |
|
201 |
public String[] getAttributeNames() { |
202 |
// TODO Auto-generated method stub
|
203 |
return null; |
204 |
} |
205 |
|
206 |
public String getNewFileName(String prefix, String sufix) { |
207 |
int n=0; |
208 |
File file = new File(prefix + getName(), sufix); |
209 |
while (file.exists()) {
|
210 |
n++; |
211 |
file = new File(prefix + getName() + n, sufix); |
212 |
} |
213 |
return file.getAbsolutePath();
|
214 |
} |
215 |
|
216 |
public String getFileName() { |
217 |
// TODO Auto-generated method stub
|
218 |
return null; |
219 |
} |
220 |
|
221 |
public String getTemporaryFileName() { |
222 |
// TODO Auto-generated method stub
|
223 |
return null; |
224 |
} |
225 |
|
226 |
|
227 |
} |
228 |
|