001: package org.geotools.feature.iso.simple;
002:
003: import java.util.Iterator;
004: import java.util.List;
005:
006: import org.geotools.feature.iso.FeatureImpl;
007: import org.opengis.feature.Attribute;
008: import org.opengis.feature.GeometryAttribute;
009: import org.opengis.feature.simple.SimpleFeature;
010: import org.opengis.feature.simple.SimpleFeatureType;
011: import org.opengis.feature.type.AttributeDescriptor;
012: import org.opengis.feature.type.AttributeType;
013:
014: /**
015: * An implementation of the SimpleFeature convience methods ontop of
016: * FeatureImpl.
017: *
018: * @author Justin
019: */
020: public class SimpleFeatureImpl extends FeatureImpl implements
021: SimpleFeature {
022:
023: public SimpleFeatureImpl(List values, AttributeDescriptor desc,
024: String id) {
025: super (values, desc, id);
026: }
027:
028: public SimpleFeatureImpl(List values, SimpleFeatureType type,
029: String id) {
030: super (values, type, id);
031: }
032:
033: /**
034: * Create a Feature with the following content.
035: *
036: * @param values
037: * Values in agreement with provided type
038: * @param type
039: * Type of feature to be created
040: * @param id
041: * Feature ID
042: */
043: public SimpleFeatureImpl(SimpleFeatureType type, String id,
044: Object[] values) {
045: this (SimpleFeatureFactoryImpl.attributes(type, values), type,
046: id);
047: }
048:
049: /**
050: * Retrive value by attribute name.
051: *
052: * @param name
053: * @return Attribute Value associated with name
054: */
055: public Object getValue(String name) {
056: for (Iterator itr = super .properties.iterator(); itr.hasNext();) {
057: Attribute att = (Attribute) itr.next();
058: AttributeType type = att.getType();
059: String attName = type.getName().getLocalPart();
060: if (attName.equals(name)) {
061: return att.getValue();
062: }
063: }
064: return null;
065: }
066:
067: public Object getValue(AttributeType type) {
068: if (!super .types().contains(type)) {
069: throw new IllegalArgumentException(
070: "this feature content model has no type " + type);
071: }
072: for (Iterator itr = super .properties.iterator(); itr.hasNext();) {
073: Attribute att = (Attribute) itr.next();
074: if (att.getType().equals(type)) {
075: return att.getValue();
076: }
077: }
078: throw new Error();
079: }
080:
081: /**
082: * Access attribute by "index" indicated by SimpleFeatureType.
083: *
084: * @param index
085: * @return
086: */
087: public Object getValue(int index) {
088: Attribute att = (Attribute) super .properties.get(index);
089: return att == null ? null : att.getValue();
090: // return values().get(index);
091: }
092:
093: /**
094: * Modify attribute with "name" indicated by SimpleFeatureType.
095: *
096: * @param name
097: * @param value
098: */
099: public void setValue(String name, Object value) {
100: AttributeType type = ((SimpleFeatureType) getType())
101: .getType(name);
102: List/* <AttributeType> */types = ((SimpleFeatureType) getType())
103: .getTypes();
104: int idx = types.indexOf(type);
105: if (idx == -1) {
106: throw new IllegalArgumentException(name
107: + " is not a feature attribute");
108: }
109: setValue(idx, value);
110: }
111:
112: /**
113: * Modify attribute at the "index" indicated by SimpleFeatureType.
114: *
115: * @param index
116: * @param value
117: */
118: public void setValue(int index, Object value) {
119: List/* <Attribute> */contents = (List) super .getValue();
120: Attribute attribute = (Attribute) contents.get(index);
121: attribute.setValue(value);
122: this .setValue(contents);
123: }
124:
125: public List getAttributes() {
126: return (List) getValue();
127: }
128:
129: public int getNumberOfAttributes() {
130: return types().size();
131: }
132:
133: public List getTypes() {
134: return super .types();
135: }
136:
137: public Object getDefaultGeometryValue() {
138: return getDefaultGeometry() != null ? getDefaultGeometry()
139: .getValue() : null;
140: }
141:
142: public void defaultGeometry(Object geometry) {
143: if (getDefaultGeometry() != null) {
144: getDefaultGeometry().setValue(geometry);
145: }
146: }
147:
148: public Object operation(String arg0, Object arg1) {
149: throw new UnsupportedOperationException(
150: "operation not supported yet");
151: }
152:
153: public void setDefaultGeometryValue(Object geometry) {
154: GeometryAttribute defaultGeometry = getDefaultGeometry();
155: defaultGeometry.setValue(geometry);
156: }
157:
158: public void setValue(List /* <Attribute> */values) {
159: super .setValue(values);
160: }
161:
162: public void setValues(List values) {
163: super .setValue(values);
164: }
165:
166: public void setValues(Object[] values) {
167: if (values == null) {
168: super .setValue(null);
169: return;
170: }
171: List properties = super .properties;
172: for (int i = 0; i < values.length; i++) {
173: Attribute att = (Attribute) properties.get(i);
174: att.setValue(values[i]);
175: }
176: }
177: }
|