svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.compat.cdc / org.gvsig.fmap.dal / org.gvsig.fmap.dal.spi / src / main / java / org / gvsig / fmap / dal / feature / spi / DefaultFeatureProvider.java @ 41342
History | View | Annotate | Download (6.28 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.spi; |
25 |
|
26 |
import org.gvsig.fmap.dal.feature.EditableFeatureType; |
27 |
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor; |
28 |
import org.gvsig.fmap.dal.feature.FeatureType; |
29 |
import org.gvsig.fmap.dal.feature.exception.SetReadOnlyAttributeException; |
30 |
import org.gvsig.fmap.geom.Geometry; |
31 |
import org.gvsig.fmap.geom.primitive.Envelope; |
32 |
|
33 |
/**
|
34 |
* Default implementation for {@link FeatureProvider}
|
35 |
*
|
36 |
*/
|
37 |
public class DefaultFeatureProvider implements FeatureProvider { |
38 |
|
39 |
protected FeatureType featureType;
|
40 |
protected boolean[] nulls; |
41 |
protected Object[] values; |
42 |
protected Geometry defaultGeometry;
|
43 |
protected Envelope envelope;
|
44 |
private Object oid; |
45 |
private boolean isNew=false; |
46 |
|
47 |
public DefaultFeatureProvider(FeatureType type) {
|
48 |
if (type instanceof EditableFeatureType) { |
49 |
throw new IllegalArgumentException("type can't by editable."); |
50 |
} |
51 |
this.featureType = type;
|
52 |
this.values = new Object[featureType.size()]; |
53 |
this.nulls = new boolean[featureType.size()]; |
54 |
|
55 |
this.envelope = null; |
56 |
this.defaultGeometry = null; |
57 |
this.oid = null; |
58 |
} |
59 |
|
60 |
public DefaultFeatureProvider(FeatureType type, Object oid) { |
61 |
this(type);
|
62 |
this.oid = oid;
|
63 |
} |
64 |
|
65 |
public boolean isReadOnly(int i) { |
66 |
FeatureAttributeDescriptor attribute = featureType.getAttributeDescriptor(i); |
67 |
if (attribute.getEvaluator() != null) { |
68 |
return true; |
69 |
} |
70 |
return false; |
71 |
} |
72 |
|
73 |
/*
|
74 |
* (non-Javadoc)
|
75 |
*
|
76 |
* @see org.gvsig.fmap.dal.feature.spi.FeatureProvider#set(int,
|
77 |
* java.lang.Object)
|
78 |
*/
|
79 |
public void set(int i, Object value) { |
80 |
FeatureAttributeDescriptor attribute = featureType.getAttributeDescriptor(i); |
81 |
if( this.isReadOnly(i) ) { |
82 |
return;
|
83 |
} |
84 |
if (featureType.getDefaultGeometryAttributeIndex() == i) {
|
85 |
defaultGeometry = (Geometry) value; |
86 |
envelope = null;
|
87 |
} |
88 |
if (value == null) { |
89 |
nulls[i] = true;
|
90 |
values[i] = attribute.getDefaultValue(); |
91 |
} else {
|
92 |
values[i] = value; |
93 |
nulls[i] = false;
|
94 |
} |
95 |
} |
96 |
|
97 |
/*
|
98 |
* (non-Javadoc)
|
99 |
*
|
100 |
* @see org.gvsig.fmap.dal.feature.spi.FeatureProvider#set(java.lang.String,
|
101 |
* java.lang.Object)
|
102 |
*/
|
103 |
public void set(String name, Object value) { |
104 |
set(featureType.getIndex(name), value); |
105 |
} |
106 |
|
107 |
/*
|
108 |
* (non-Javadoc)
|
109 |
*
|
110 |
* @see org.gvsig.fmap.dal.feature.spi.FeatureProvider#get(int)
|
111 |
*/
|
112 |
public Object get(int i) { |
113 |
return values[i];
|
114 |
} |
115 |
|
116 |
/*
|
117 |
* (non-Javadoc)
|
118 |
*
|
119 |
* @see org.gvsig.fmap.dal.feature.spi.FeatureProvider#get(java.lang.String)
|
120 |
*/
|
121 |
public Object get(String name) { |
122 |
FeatureAttributeDescriptor featureAttributeDescriptor = featureType.getAttributeDescriptor(name); |
123 |
return values[featureAttributeDescriptor.getIndex()];
|
124 |
} |
125 |
|
126 |
/*
|
127 |
* (non-Javadoc)
|
128 |
*
|
129 |
* @see org.gvsig.fmap.dal.feature.spi.FeatureProvider#getType()
|
130 |
*/
|
131 |
public FeatureType getType() {
|
132 |
return featureType;
|
133 |
} |
134 |
|
135 |
/*
|
136 |
* (non-Javadoc)
|
137 |
*
|
138 |
* @see org.gvsig.fmap.dal.feature.spi.FeatureProvider#getCopy()
|
139 |
*/
|
140 |
public FeatureProvider getCopy() {
|
141 |
DefaultFeatureProvider data = new DefaultFeatureProvider(
|
142 |
this.getType());
|
143 |
return getCopy(data);
|
144 |
} |
145 |
|
146 |
/**
|
147 |
* Copy values from current instance to <code>data</code>
|
148 |
*/
|
149 |
protected FeatureProvider getCopy(DefaultFeatureProvider data) {
|
150 |
data.oid = this.oid;
|
151 |
System.arraycopy(this.values, 0, data.values, 0, this.values.length); |
152 |
data.defaultGeometry = this.defaultGeometry;
|
153 |
data.envelope = this.envelope;
|
154 |
data.isNew=this.isNew;
|
155 |
return data;
|
156 |
} |
157 |
|
158 |
/*
|
159 |
* (non-Javadoc)
|
160 |
*
|
161 |
* @see org.gvsig.fmap.dal.feature.spi.FeatureProvider#getDefaultEnvelope()
|
162 |
*/
|
163 |
public Envelope getDefaultEnvelope() {
|
164 |
if (envelope == null && getDefaultGeometry() != null) { |
165 |
envelope = getDefaultGeometry().getEnvelope(); |
166 |
} |
167 |
return envelope;
|
168 |
} |
169 |
|
170 |
/*
|
171 |
* (non-Javadoc)
|
172 |
*
|
173 |
* @see org.gvsig.fmap.dal.feature.spi.FeatureProvider#getDefaultGeometry()
|
174 |
*/
|
175 |
public Geometry getDefaultGeometry() {
|
176 |
return this.defaultGeometry; |
177 |
} |
178 |
|
179 |
/*
|
180 |
* (non-Javadoc)
|
181 |
*
|
182 |
* @see
|
183 |
* org.gvsig.fmap.dal.feature.spi.FeatureProvider#setDefaultEnvelope(org
|
184 |
* .gvsig.fmap.geom.primitive.Envelope)
|
185 |
*/
|
186 |
public void setDefaultEnvelope(Envelope envelope) { |
187 |
this.envelope = envelope;
|
188 |
} |
189 |
|
190 |
/*
|
191 |
* (non-Javadoc)
|
192 |
*
|
193 |
* @see
|
194 |
* org.gvsig.fmap.dal.feature.spi.FeatureProvider#setDefaultGeometry(org
|
195 |
* .gvsig.fmap.geom.Geometry)
|
196 |
*/
|
197 |
public void setDefaultGeometry(Geometry geom) { |
198 |
int i = featureType.getDefaultGeometryAttributeIndex();
|
199 |
this.set(i, geom);
|
200 |
} |
201 |
|
202 |
/*
|
203 |
* (non-Javadoc)
|
204 |
*
|
205 |
* @see org.gvsig.fmap.dal.feature.spi.FeatureProvider#isNull(int)
|
206 |
*/
|
207 |
public boolean isNull(int i) { |
208 |
return nulls[i];
|
209 |
} |
210 |
|
211 |
/*
|
212 |
* (non-Javadoc)
|
213 |
*
|
214 |
* @see
|
215 |
* org.gvsig.fmap.dal.feature.spi.FeatureProvider#isNull(java.lang.String)
|
216 |
*/
|
217 |
public boolean isNull(String name) { |
218 |
int i = featureType.getIndex(name);
|
219 |
return isNull(i);
|
220 |
} |
221 |
|
222 |
/*
|
223 |
* (non-Javadoc)
|
224 |
*
|
225 |
* @see org.gvsig.fmap.dal.feature.spi.FeatureProvider#getOID()
|
226 |
*/
|
227 |
public Object getOID() { |
228 |
return this.oid; |
229 |
} |
230 |
|
231 |
/*
|
232 |
* (non-Javadoc)
|
233 |
*
|
234 |
* @see
|
235 |
* org.gvsig.fmap.dal.feature.spi.FeatureProvider#setOID(java.lang.Object)
|
236 |
*/
|
237 |
public void setOID(Object oid) { |
238 |
this.oid = oid;
|
239 |
} |
240 |
|
241 |
/*
|
242 |
* (non-Javadoc)
|
243 |
*
|
244 |
* @see org.gvsig.fmap.dal.feature.spi.FeatureProvider#isNew()
|
245 |
*/
|
246 |
public boolean isNew(){ |
247 |
return isNew;
|
248 |
} |
249 |
|
250 |
/*
|
251 |
* (non-Javadoc)
|
252 |
*
|
253 |
* @see org.gvsig.fmap.dal.feature.spi.FeatureProvider#setNew(boolean)
|
254 |
*/
|
255 |
public void setNew(boolean isNew){ |
256 |
this.isNew=isNew;
|
257 |
} |
258 |
} |