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 @ 43420
History | View | Annotate | Download (6.62 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 | 41215 | jjdelcerro | public boolean isReadOnly(int i) { |
68 | FeatureAttributeDescriptor attribute = featureType.getAttributeDescriptor(i); |
||
69 | if (attribute.getEvaluator() != null) { |
||
70 | return true; |
||
71 | } |
||
72 | return false; |
||
73 | } |
||
74 | |||
75 | 40435 | jjdelcerro | /*
|
76 | * (non-Javadoc)
|
||
77 | *
|
||
78 | * @see org.gvsig.fmap.dal.feature.spi.FeatureProvider#set(int,
|
||
79 | * java.lang.Object)
|
||
80 | */
|
||
81 | public void set(int i, Object value) { |
||
82 | 43420 | jjdelcerro | FeatureAttributeDescriptor attribute = featureType.getAttributeDescriptor(i); |
83 | if( this.isReadOnly(i) ) { |
||
84 | 41212 | jjdelcerro | return;
|
85 | } |
||
86 | 40435 | jjdelcerro | if (featureType.getDefaultGeometryAttributeIndex() == i) {
|
87 | 43420 | jjdelcerro | if( value instanceof Geometry ) { |
88 | defaultGeometry = (Geometry) value; |
||
89 | } else {
|
||
90 | try {
|
||
91 | defaultGeometry = (Geometry) ToolsLocator.getDataTypesManager().coerce(DataTypes.GEOMETRY, value); |
||
92 | } catch (CoercionException ex) {
|
||
93 | |||
94 | } |
||
95 | } |
||
96 | envelope = null;
|
||
97 | 40435 | jjdelcerro | } |
98 | if (value == null) { |
||
99 | nulls[i] = true;
|
||
100 | 41212 | jjdelcerro | values[i] = attribute.getDefaultValue(); |
101 | 40435 | jjdelcerro | } else {
|
102 | values[i] = value; |
||
103 | nulls[i] = false;
|
||
104 | } |
||
105 | } |
||
106 | |||
107 | /*
|
||
108 | * (non-Javadoc)
|
||
109 | *
|
||
110 | * @see org.gvsig.fmap.dal.feature.spi.FeatureProvider#set(java.lang.String,
|
||
111 | * java.lang.Object)
|
||
112 | */
|
||
113 | public void set(String name, Object value) { |
||
114 | set(featureType.getIndex(name), value); |
||
115 | } |
||
116 | |||
117 | /*
|
||
118 | * (non-Javadoc)
|
||
119 | *
|
||
120 | * @see org.gvsig.fmap.dal.feature.spi.FeatureProvider#get(int)
|
||
121 | */
|
||
122 | public Object get(int i) { |
||
123 | return values[i];
|
||
124 | } |
||
125 | |||
126 | /*
|
||
127 | * (non-Javadoc)
|
||
128 | *
|
||
129 | * @see org.gvsig.fmap.dal.feature.spi.FeatureProvider#get(java.lang.String)
|
||
130 | */
|
||
131 | public Object get(String name) { |
||
132 | FeatureAttributeDescriptor featureAttributeDescriptor = featureType.getAttributeDescriptor(name); |
||
133 | return values[featureAttributeDescriptor.getIndex()];
|
||
134 | } |
||
135 | |||
136 | /*
|
||
137 | * (non-Javadoc)
|
||
138 | *
|
||
139 | * @see org.gvsig.fmap.dal.feature.spi.FeatureProvider#getType()
|
||
140 | */
|
||
141 | public FeatureType getType() {
|
||
142 | return featureType;
|
||
143 | } |
||
144 | |||
145 | /*
|
||
146 | * (non-Javadoc)
|
||
147 | *
|
||
148 | * @see org.gvsig.fmap.dal.feature.spi.FeatureProvider#getCopy()
|
||
149 | */
|
||
150 | public FeatureProvider getCopy() {
|
||
151 | DefaultFeatureProvider data = new DefaultFeatureProvider(
|
||
152 | this.getType());
|
||
153 | return getCopy(data);
|
||
154 | } |
||
155 | |||
156 | /**
|
||
157 | * Copy values from current instance to <code>data</code>
|
||
158 | */
|
||
159 | protected FeatureProvider getCopy(DefaultFeatureProvider data) {
|
||
160 | data.oid = this.oid;
|
||
161 | System.arraycopy(this.values, 0, data.values, 0, this.values.length); |
||
162 | data.defaultGeometry = this.defaultGeometry;
|
||
163 | data.envelope = this.envelope;
|
||
164 | data.isNew=this.isNew;
|
||
165 | return data;
|
||
166 | } |
||
167 | |||
168 | /*
|
||
169 | * (non-Javadoc)
|
||
170 | *
|
||
171 | * @see org.gvsig.fmap.dal.feature.spi.FeatureProvider#getDefaultEnvelope()
|
||
172 | */
|
||
173 | public Envelope getDefaultEnvelope() {
|
||
174 | 41058 | jjdelcerro | if (envelope == null && getDefaultGeometry() != null) { |
175 | envelope = getDefaultGeometry().getEnvelope(); |
||
176 | 40435 | jjdelcerro | } |
177 | return envelope;
|
||
178 | } |
||
179 | |||
180 | /*
|
||
181 | * (non-Javadoc)
|
||
182 | *
|
||
183 | * @see org.gvsig.fmap.dal.feature.spi.FeatureProvider#getDefaultGeometry()
|
||
184 | */
|
||
185 | public Geometry getDefaultGeometry() {
|
||
186 | 41342 | jjdelcerro | return this.defaultGeometry; |
187 | 40435 | jjdelcerro | } |
188 | |||
189 | /*
|
||
190 | * (non-Javadoc)
|
||
191 | *
|
||
192 | * @see
|
||
193 | * org.gvsig.fmap.dal.feature.spi.FeatureProvider#setDefaultEnvelope(org
|
||
194 | * .gvsig.fmap.geom.primitive.Envelope)
|
||
195 | */
|
||
196 | public void setDefaultEnvelope(Envelope envelope) { |
||
197 | this.envelope = envelope;
|
||
198 | } |
||
199 | |||
200 | /*
|
||
201 | * (non-Javadoc)
|
||
202 | *
|
||
203 | * @see
|
||
204 | * org.gvsig.fmap.dal.feature.spi.FeatureProvider#setDefaultGeometry(org
|
||
205 | * .gvsig.fmap.geom.Geometry)
|
||
206 | */
|
||
207 | public void setDefaultGeometry(Geometry geom) { |
||
208 | int i = featureType.getDefaultGeometryAttributeIndex();
|
||
209 | 41212 | jjdelcerro | this.set(i, geom);
|
210 | 40435 | jjdelcerro | } |
211 | |||
212 | /*
|
||
213 | * (non-Javadoc)
|
||
214 | *
|
||
215 | * @see org.gvsig.fmap.dal.feature.spi.FeatureProvider#isNull(int)
|
||
216 | */
|
||
217 | public boolean isNull(int i) { |
||
218 | return nulls[i];
|
||
219 | } |
||
220 | |||
221 | /*
|
||
222 | * (non-Javadoc)
|
||
223 | *
|
||
224 | * @see
|
||
225 | * org.gvsig.fmap.dal.feature.spi.FeatureProvider#isNull(java.lang.String)
|
||
226 | */
|
||
227 | public boolean isNull(String name) { |
||
228 | int i = featureType.getIndex(name);
|
||
229 | return isNull(i);
|
||
230 | } |
||
231 | |||
232 | /*
|
||
233 | * (non-Javadoc)
|
||
234 | *
|
||
235 | * @see org.gvsig.fmap.dal.feature.spi.FeatureProvider#getOID()
|
||
236 | */
|
||
237 | public Object getOID() { |
||
238 | return this.oid; |
||
239 | } |
||
240 | |||
241 | /*
|
||
242 | * (non-Javadoc)
|
||
243 | *
|
||
244 | * @see
|
||
245 | * org.gvsig.fmap.dal.feature.spi.FeatureProvider#setOID(java.lang.Object)
|
||
246 | */
|
||
247 | public void setOID(Object oid) { |
||
248 | this.oid = oid;
|
||
249 | } |
||
250 | |||
251 | /*
|
||
252 | * (non-Javadoc)
|
||
253 | *
|
||
254 | * @see org.gvsig.fmap.dal.feature.spi.FeatureProvider#isNew()
|
||
255 | */
|
||
256 | public boolean isNew(){ |
||
257 | return isNew;
|
||
258 | } |
||
259 | |||
260 | /*
|
||
261 | * (non-Javadoc)
|
||
262 | *
|
||
263 | * @see org.gvsig.fmap.dal.feature.spi.FeatureProvider#setNew(boolean)
|
||
264 | */
|
||
265 | public void setNew(boolean isNew){ |
||
266 | this.isNew=isNew;
|
||
267 | } |
||
268 | } |