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 @ 44058
History | View | Annotate | Download (6.83 KB)
1 | 40559 | jjdelcerro | /**
|
---|---|---|---|
2 | * gvSIG. Desktop Geographic Information System.
|
||
3 | 40435 | jjdelcerro | *
|
4 | 40559 | jjdelcerro | * Copyright (C) 2007-2013 gvSIG Association.
|
5 | 40435 | jjdelcerro | *
|
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 | 40559 | jjdelcerro | * as published by the Free Software Foundation; either version 3
|
9 | 40435 | jjdelcerro | * 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 | 40559 | jjdelcerro | * For any additional information, do not hesitate to contact us
|
22 | * at info AT gvsig.com, or visit our website www.gvsig.com.
|
||
23 | 40435 | jjdelcerro | */
|
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 | 43420 | jjdelcerro | import org.gvsig.fmap.geom.DataTypes; |
30 | 40435 | jjdelcerro | import org.gvsig.fmap.geom.Geometry; |
31 | import org.gvsig.fmap.geom.primitive.Envelope; |
||
32 | 43420 | jjdelcerro | import org.gvsig.tools.ToolsLocator; |
33 | import org.gvsig.tools.dataTypes.CoercionException; |
||
34 | 40435 | jjdelcerro | |
35 | /**
|
||
36 | * Default implementation for {@link FeatureProvider}
|
||
37 | *
|
||
38 | */
|
||
39 | public class DefaultFeatureProvider implements FeatureProvider { |
||
40 | |||
41 | protected FeatureType featureType;
|
||
42 | protected boolean[] nulls; |
||
43 | protected Object[] values; |
||
44 | protected Geometry defaultGeometry;
|
||
45 | protected Envelope envelope;
|
||
46 | private Object oid; |
||
47 | private boolean isNew=false; |
||
48 | |||
49 | public DefaultFeatureProvider(FeatureType type) {
|
||
50 | if (type instanceof EditableFeatureType) { |
||
51 | throw new IllegalArgumentException("type can't by editable."); |
||
52 | } |
||
53 | this.featureType = type;
|
||
54 | this.values = new Object[featureType.size()]; |
||
55 | this.nulls = new boolean[featureType.size()]; |
||
56 | |||
57 | this.envelope = null; |
||
58 | this.defaultGeometry = null; |
||
59 | this.oid = null; |
||
60 | } |
||
61 | |||
62 | public DefaultFeatureProvider(FeatureType type, Object oid) { |
||
63 | this(type);
|
||
64 | this.oid = oid;
|
||
65 | } |
||
66 | |||
67 | 43729 | fdiaz | public DefaultFeatureProvider(FeatureType type, DefaultFeatureProvider data) {
|
68 | this(type);
|
||
69 | this.oid = data.oid;
|
||
70 | this.defaultGeometry = data.defaultGeometry;
|
||
71 | this.envelope = data.envelope;
|
||
72 | this.isNew = data.isNew;
|
||
73 | } |
||
74 | |||
75 | public boolean isReadOnly(int i) { |
||
76 | FeatureAttributeDescriptor attribute = featureType.getAttributeDescriptor(i); |
||
77 | if (attribute.getEvaluator() != null) { |
||
78 | return true; |
||
79 | 41215 | jjdelcerro | } |
80 | 43729 | fdiaz | return false; |
81 | } |
||
82 | |||
83 | 40435 | jjdelcerro | /*
|
84 | * (non-Javadoc)
|
||
85 | *
|
||
86 | * @see org.gvsig.fmap.dal.feature.spi.FeatureProvider#set(int,
|
||
87 | * java.lang.Object)
|
||
88 | */
|
||
89 | public void set(int i, Object value) { |
||
90 | 43420 | jjdelcerro | FeatureAttributeDescriptor attribute = featureType.getAttributeDescriptor(i); |
91 | if( this.isReadOnly(i) ) { |
||
92 | 41212 | jjdelcerro | return;
|
93 | } |
||
94 | 40435 | jjdelcerro | if (featureType.getDefaultGeometryAttributeIndex() == i) {
|
95 | 43420 | jjdelcerro | if( value instanceof Geometry ) { |
96 | defaultGeometry = (Geometry) value; |
||
97 | } else {
|
||
98 | try {
|
||
99 | defaultGeometry = (Geometry) ToolsLocator.getDataTypesManager().coerce(DataTypes.GEOMETRY, value); |
||
100 | } catch (CoercionException ex) {
|
||
101 | |||
102 | } |
||
103 | } |
||
104 | envelope = null;
|
||
105 | 40435 | jjdelcerro | } |
106 | if (value == null) { |
||
107 | nulls[i] = true;
|
||
108 | 41212 | jjdelcerro | values[i] = attribute.getDefaultValue(); |
109 | 40435 | jjdelcerro | } else {
|
110 | values[i] = value; |
||
111 | nulls[i] = false;
|
||
112 | } |
||
113 | } |
||
114 | |||
115 | /*
|
||
116 | * (non-Javadoc)
|
||
117 | *
|
||
118 | * @see org.gvsig.fmap.dal.feature.spi.FeatureProvider#set(java.lang.String,
|
||
119 | * java.lang.Object)
|
||
120 | */
|
||
121 | public void set(String name, Object value) { |
||
122 | set(featureType.getIndex(name), value); |
||
123 | } |
||
124 | |||
125 | /*
|
||
126 | * (non-Javadoc)
|
||
127 | *
|
||
128 | * @see org.gvsig.fmap.dal.feature.spi.FeatureProvider#get(int)
|
||
129 | */
|
||
130 | public Object get(int i) { |
||
131 | return values[i];
|
||
132 | } |
||
133 | |||
134 | /*
|
||
135 | * (non-Javadoc)
|
||
136 | *
|
||
137 | * @see org.gvsig.fmap.dal.feature.spi.FeatureProvider#get(java.lang.String)
|
||
138 | */
|
||
139 | public Object get(String name) { |
||
140 | 43729 | fdiaz | FeatureAttributeDescriptor featureAttributeDescriptor = featureType.getAttributeDescriptor(name); |
141 | 40435 | jjdelcerro | return values[featureAttributeDescriptor.getIndex()];
|
142 | } |
||
143 | |||
144 | /*
|
||
145 | * (non-Javadoc)
|
||
146 | *
|
||
147 | * @see org.gvsig.fmap.dal.feature.spi.FeatureProvider#getType()
|
||
148 | */
|
||
149 | public FeatureType getType() {
|
||
150 | return featureType;
|
||
151 | } |
||
152 | |||
153 | /*
|
||
154 | * (non-Javadoc)
|
||
155 | *
|
||
156 | * @see org.gvsig.fmap.dal.feature.spi.FeatureProvider#getCopy()
|
||
157 | */
|
||
158 | public FeatureProvider getCopy() {
|
||
159 | DefaultFeatureProvider data = new DefaultFeatureProvider(
|
||
160 | this.getType());
|
||
161 | return getCopy(data);
|
||
162 | } |
||
163 | |||
164 | /**
|
||
165 | * Copy values from current instance to <code>data</code>
|
||
166 | */
|
||
167 | protected FeatureProvider getCopy(DefaultFeatureProvider data) {
|
||
168 | data.oid = this.oid;
|
||
169 | System.arraycopy(this.values, 0, data.values, 0, this.values.length); |
||
170 | data.defaultGeometry = this.defaultGeometry;
|
||
171 | data.envelope = this.envelope;
|
||
172 | data.isNew=this.isNew;
|
||
173 | return data;
|
||
174 | } |
||
175 | |||
176 | /*
|
||
177 | * (non-Javadoc)
|
||
178 | 43729 | fdiaz | *
|
179 | 40435 | jjdelcerro | * @see org.gvsig.fmap.dal.feature.spi.FeatureProvider#getDefaultEnvelope()
|
180 | */
|
||
181 | public Envelope getDefaultEnvelope() {
|
||
182 | 41058 | jjdelcerro | if (envelope == null && getDefaultGeometry() != null) { |
183 | envelope = getDefaultGeometry().getEnvelope(); |
||
184 | 40435 | jjdelcerro | } |
185 | return envelope;
|
||
186 | } |
||
187 | |||
188 | /*
|
||
189 | * (non-Javadoc)
|
||
190 | 43729 | fdiaz | *
|
191 | 40435 | jjdelcerro | * @see org.gvsig.fmap.dal.feature.spi.FeatureProvider#getDefaultGeometry()
|
192 | */
|
||
193 | public Geometry getDefaultGeometry() {
|
||
194 | 41342 | jjdelcerro | return this.defaultGeometry; |
195 | 40435 | jjdelcerro | } |
196 | |||
197 | /*
|
||
198 | * (non-Javadoc)
|
||
199 | 43729 | fdiaz | *
|
200 | 40435 | jjdelcerro | * @see
|
201 | * org.gvsig.fmap.dal.feature.spi.FeatureProvider#setDefaultEnvelope(org
|
||
202 | * .gvsig.fmap.geom.primitive.Envelope)
|
||
203 | */
|
||
204 | public void setDefaultEnvelope(Envelope envelope) { |
||
205 | this.envelope = envelope;
|
||
206 | } |
||
207 | |||
208 | /*
|
||
209 | * (non-Javadoc)
|
||
210 | 43729 | fdiaz | *
|
211 | 40435 | jjdelcerro | * @see
|
212 | * org.gvsig.fmap.dal.feature.spi.FeatureProvider#setDefaultGeometry(org
|
||
213 | * .gvsig.fmap.geom.Geometry)
|
||
214 | */
|
||
215 | public void setDefaultGeometry(Geometry geom) { |
||
216 | int i = featureType.getDefaultGeometryAttributeIndex();
|
||
217 | 41212 | jjdelcerro | this.set(i, geom);
|
218 | 40435 | jjdelcerro | } |
219 | |||
220 | /*
|
||
221 | * (non-Javadoc)
|
||
222 | 43729 | fdiaz | *
|
223 | 40435 | jjdelcerro | * @see org.gvsig.fmap.dal.feature.spi.FeatureProvider#isNull(int)
|
224 | */
|
||
225 | public boolean isNull(int i) { |
||
226 | return nulls[i];
|
||
227 | } |
||
228 | |||
229 | /*
|
||
230 | * (non-Javadoc)
|
||
231 | 43729 | fdiaz | *
|
232 | 40435 | jjdelcerro | * @see
|
233 | * org.gvsig.fmap.dal.feature.spi.FeatureProvider#isNull(java.lang.String)
|
||
234 | */
|
||
235 | public boolean isNull(String name) { |
||
236 | int i = featureType.getIndex(name);
|
||
237 | return isNull(i);
|
||
238 | } |
||
239 | |||
240 | /*
|
||
241 | * (non-Javadoc)
|
||
242 | 43729 | fdiaz | *
|
243 | 40435 | jjdelcerro | * @see org.gvsig.fmap.dal.feature.spi.FeatureProvider#getOID()
|
244 | */
|
||
245 | public Object getOID() { |
||
246 | return this.oid; |
||
247 | } |
||
248 | |||
249 | /*
|
||
250 | * (non-Javadoc)
|
||
251 | 43729 | fdiaz | *
|
252 | 40435 | jjdelcerro | * @see
|
253 | * org.gvsig.fmap.dal.feature.spi.FeatureProvider#setOID(java.lang.Object)
|
||
254 | */
|
||
255 | public void setOID(Object oid) { |
||
256 | this.oid = oid;
|
||
257 | } |
||
258 | |||
259 | /*
|
||
260 | * (non-Javadoc)
|
||
261 | 43729 | fdiaz | *
|
262 | 40435 | jjdelcerro | * @see org.gvsig.fmap.dal.feature.spi.FeatureProvider#isNew()
|
263 | */
|
||
264 | public boolean isNew(){ |
||
265 | return isNew;
|
||
266 | } |
||
267 | |||
268 | /*
|
||
269 | * (non-Javadoc)
|
||
270 | 43729 | fdiaz | *
|
271 | 40435 | jjdelcerro | * @see org.gvsig.fmap.dal.feature.spi.FeatureProvider#setNew(boolean)
|
272 | */
|
||
273 | public void setNew(boolean isNew){ |
||
274 | this.isNew=isNew;
|
||
275 | } |
||
276 | } |