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 @ 44296
History | View | Annotate | Download (7.75 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 | 44086 | jjdelcerro | * This program is free software; you can redistribute it and/or modify it under
|
7 | * the terms of the GNU General Public License as published by the Free Software
|
||
8 | * Foundation; either version 3 of the License, or (at your option) any later
|
||
9 | * version.
|
||
10 | 40435 | jjdelcerro | *
|
11 | 44086 | jjdelcerro | * This program is distributed in the hope that it will be useful, but WITHOUT
|
12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
||
13 | * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
|
||
14 | * details.
|
||
15 | 40435 | jjdelcerro | *
|
16 | 44086 | jjdelcerro | * You should have received a copy of the GNU General Public License along with
|
17 | * this program; if not, write to the Free Software Foundation, Inc., 51
|
||
18 | * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||
19 | 40435 | jjdelcerro | *
|
20 | 44086 | jjdelcerro | * For any additional information, do not hesitate to contact us at info AT
|
21 | * gvsig.com, or visit our website www.gvsig.com.
|
||
22 | 40435 | jjdelcerro | */
|
23 | package org.gvsig.fmap.dal.feature.spi; |
||
24 | |||
25 | 44086 | jjdelcerro | import org.gvsig.fmap.dal.DataTypes; |
26 | 40435 | jjdelcerro | 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.geom.Geometry; |
||
30 | import org.gvsig.fmap.geom.primitive.Envelope; |
||
31 | 44086 | jjdelcerro | import org.gvsig.timesupport.Time; |
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 | 44086 | jjdelcerro | 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 | 40435 | jjdelcerro | |
49 | 44086 | jjdelcerro | 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 | 40435 | jjdelcerro | |
57 | 44086 | jjdelcerro | this.envelope = null; |
58 | this.defaultGeometry = null; |
||
59 | this.oid = null; |
||
60 | } |
||
61 | 40435 | jjdelcerro | |
62 | 44086 | jjdelcerro | public DefaultFeatureProvider(FeatureType type, Object oid) { |
63 | this(type);
|
||
64 | this.oid = oid;
|
||
65 | } |
||
66 | 40435 | jjdelcerro | |
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 | 44086 | jjdelcerro | return true; |
79 | 41215 | jjdelcerro | } |
80 | 43729 | fdiaz | return false; |
81 | } |
||
82 | |||
83 | 44086 | jjdelcerro | @Override
|
84 | public void set(int i, Object value) { |
||
85 | 43420 | jjdelcerro | FeatureAttributeDescriptor attribute = featureType.getAttributeDescriptor(i); |
86 | 44203 | jjdelcerro | // if (this.isReadOnly(i)) {
|
87 | // return;
|
||
88 | // }
|
||
89 | 44086 | jjdelcerro | if (featureType.getDefaultGeometryAttributeIndex() == i) {
|
90 | if (value instanceof Geometry) { |
||
91 | 43420 | jjdelcerro | defaultGeometry = (Geometry) value; |
92 | } else {
|
||
93 | try {
|
||
94 | defaultGeometry = (Geometry) ToolsLocator.getDataTypesManager().coerce(DataTypes.GEOMETRY, value); |
||
95 | } catch (CoercionException ex) {
|
||
96 | |||
97 | } |
||
98 | } |
||
99 | envelope = null;
|
||
100 | 44086 | jjdelcerro | } |
101 | if (value == null) { |
||
102 | nulls[i] = true;
|
||
103 | values[i] = attribute.getDefaultValue(); |
||
104 | } else {
|
||
105 | values[i] = value; |
||
106 | nulls[i] = false;
|
||
107 | } |
||
108 | } |
||
109 | 40435 | jjdelcerro | |
110 | 44086 | jjdelcerro | /*
|
111 | * (non-Javadoc)
|
||
112 | *
|
||
113 | * @see org.gvsig.fmap.dal.feature.spi.FeatureProvider#set(java.lang.String,
|
||
114 | * java.lang.Object)
|
||
115 | */
|
||
116 | public void set(String name, Object value) { |
||
117 | set(featureType.getIndex(name), value); |
||
118 | } |
||
119 | 40435 | jjdelcerro | |
120 | 44086 | jjdelcerro | /*
|
121 | * (non-Javadoc)
|
||
122 | *
|
||
123 | * @see org.gvsig.fmap.dal.feature.spi.FeatureProvider#get(int)
|
||
124 | */
|
||
125 | public Object get(int i) { |
||
126 | return values[i];
|
||
127 | } |
||
128 | 40435 | jjdelcerro | |
129 | 44086 | jjdelcerro | /*
|
130 | * (non-Javadoc)
|
||
131 | *
|
||
132 | * @see org.gvsig.fmap.dal.feature.spi.FeatureProvider#get(java.lang.String)
|
||
133 | */
|
||
134 | public Object get(String name) { |
||
135 | FeatureAttributeDescriptor featureAttributeDescriptor = featureType.getAttributeDescriptor(name); |
||
136 | return values[featureAttributeDescriptor.getIndex()];
|
||
137 | } |
||
138 | 40435 | jjdelcerro | |
139 | 44086 | jjdelcerro | /*
|
140 | * (non-Javadoc)
|
||
141 | *
|
||
142 | * @see org.gvsig.fmap.dal.feature.spi.FeatureProvider#getType()
|
||
143 | */
|
||
144 | public FeatureType getType() {
|
||
145 | return featureType;
|
||
146 | } |
||
147 | 40435 | jjdelcerro | |
148 | 44086 | jjdelcerro | /*
|
149 | * (non-Javadoc)
|
||
150 | *
|
||
151 | * @see org.gvsig.fmap.dal.feature.spi.FeatureProvider#getCopy()
|
||
152 | */
|
||
153 | public FeatureProvider getCopy() {
|
||
154 | DefaultFeatureProvider data = new DefaultFeatureProvider(
|
||
155 | this.getType());
|
||
156 | return getCopy(data);
|
||
157 | } |
||
158 | 40435 | jjdelcerro | |
159 | 44086 | jjdelcerro | /**
|
160 | * Copy values from current instance to <code>data</code>
|
||
161 | */
|
||
162 | protected FeatureProvider getCopy(DefaultFeatureProvider data) {
|
||
163 | data.oid = this.oid;
|
||
164 | System.arraycopy(this.values, 0, data.values, 0, this.values.length); |
||
165 | data.defaultGeometry = this.defaultGeometry;
|
||
166 | data.envelope = this.envelope;
|
||
167 | data.isNew = this.isNew;
|
||
168 | return data;
|
||
169 | } |
||
170 | 40435 | jjdelcerro | |
171 | 44086 | jjdelcerro | /*
|
172 | * (non-Javadoc)
|
||
173 | *
|
||
174 | * @see org.gvsig.fmap.dal.feature.spi.FeatureProvider#getDefaultEnvelope()
|
||
175 | */
|
||
176 | public Envelope getDefaultEnvelope() {
|
||
177 | if (envelope == null && getDefaultGeometry() != null) { |
||
178 | envelope = getDefaultGeometry().getEnvelope(); |
||
179 | } |
||
180 | return envelope;
|
||
181 | } |
||
182 | 40435 | jjdelcerro | |
183 | 44086 | jjdelcerro | /*
|
184 | * (non-Javadoc)
|
||
185 | *
|
||
186 | * @see org.gvsig.fmap.dal.feature.spi.FeatureProvider#getDefaultGeometry()
|
||
187 | */
|
||
188 | public Geometry getDefaultGeometry() {
|
||
189 | return this.defaultGeometry; |
||
190 | } |
||
191 | 40435 | jjdelcerro | |
192 | 44086 | jjdelcerro | @Override
|
193 | public Time getDefaultTime() { |
||
194 | int index = featureType.getDefaultTimeAttributeIndex();
|
||
195 | Object value = this.get(index); |
||
196 | if (! (value instanceof Time) ) { |
||
197 | try {
|
||
198 | value = (Time) ToolsLocator.getDataTypesManager().coerce(DataTypes.INSTANT, value);
|
||
199 | } catch (CoercionException ex) {
|
||
200 | 40435 | jjdelcerro | |
201 | 44086 | jjdelcerro | } |
202 | } |
||
203 | return (Time) value; |
||
204 | } |
||
205 | 40435 | jjdelcerro | |
206 | 44086 | jjdelcerro | /*
|
207 | * (non-Javadoc)
|
||
208 | *
|
||
209 | * @see
|
||
210 | * org.gvsig.fmap.dal.feature.spi.FeatureProvider#setDefaultEnvelope(org
|
||
211 | * .gvsig.fmap.geom.primitive.Envelope)
|
||
212 | */
|
||
213 | public void setDefaultEnvelope(Envelope envelope) { |
||
214 | this.envelope = envelope;
|
||
215 | } |
||
216 | 40435 | jjdelcerro | |
217 | 44086 | jjdelcerro | @Override
|
218 | public void setDefaultGeometry(Geometry geom) { |
||
219 | int i = featureType.getDefaultGeometryAttributeIndex();
|
||
220 | this.set(i, geom);
|
||
221 | } |
||
222 | 40435 | jjdelcerro | |
223 | 44086 | jjdelcerro | @Override
|
224 | public void setDefaultTime(Time time) { |
||
225 | int i = featureType.getDefaultTimeAttributeIndex();
|
||
226 | this.set(i, time);
|
||
227 | } |
||
228 | 40435 | jjdelcerro | |
229 | |||
230 | 44086 | jjdelcerro | public boolean isNull(int i) { |
231 | return nulls[i];
|
||
232 | } |
||
233 | 40435 | jjdelcerro | |
234 | 44086 | jjdelcerro | /*
|
235 | * (non-Javadoc)
|
||
236 | *
|
||
237 | * @see
|
||
238 | * org.gvsig.fmap.dal.feature.spi.FeatureProvider#isNull(java.lang.String)
|
||
239 | */
|
||
240 | public boolean isNull(String name) { |
||
241 | int i = featureType.getIndex(name);
|
||
242 | return isNull(i);
|
||
243 | } |
||
244 | |||
245 | /*
|
||
246 | * (non-Javadoc)
|
||
247 | *
|
||
248 | * @see org.gvsig.fmap.dal.feature.spi.FeatureProvider#getOID()
|
||
249 | */
|
||
250 | public Object getOID() { |
||
251 | return this.oid; |
||
252 | } |
||
253 | |||
254 | /*
|
||
255 | * (non-Javadoc)
|
||
256 | *
|
||
257 | * @see
|
||
258 | * org.gvsig.fmap.dal.feature.spi.FeatureProvider#setOID(java.lang.Object)
|
||
259 | */
|
||
260 | public void setOID(Object oid) { |
||
261 | this.oid = oid;
|
||
262 | } |
||
263 | |||
264 | /*
|
||
265 | * (non-Javadoc)
|
||
266 | *
|
||
267 | * @see org.gvsig.fmap.dal.feature.spi.FeatureProvider#isNew()
|
||
268 | */
|
||
269 | public boolean isNew() { |
||
270 | return isNew;
|
||
271 | } |
||
272 | |||
273 | /*
|
||
274 | * (non-Javadoc)
|
||
275 | *
|
||
276 | * @see org.gvsig.fmap.dal.feature.spi.FeatureProvider#setNew(boolean)
|
||
277 | */
|
||
278 | public void setNew(boolean isNew) { |
||
279 | this.isNew = isNew;
|
||
280 | } |
||
281 | 40435 | jjdelcerro | } |