001: package org.geotools.feature.iso;
002:
003: import java.util.List;
004:
005: import org.geotools.feature.IllegalAttributeException;
006: import org.geotools.resources.Utilities;
007: import org.opengis.feature.Attribute;
008: import org.opengis.feature.type.AttributeDescriptor;
009: import org.opengis.feature.type.AttributeType;
010: import org.opengis.feature.type.Name;
011: import org.opengis.feature.type.PropertyDescriptor;
012:
013: /**
014: * Simple, mutable class to store attributes.
015: *
016: * @author Rob Hranac, VFNY
017: * @author Chris Holmes, TOPP
018: * @author Ian Schneider
019: * @author Jody Garnett
020: * @author Gabriel Roldan
021: * @version $Id: AttributeImpl.java 28577 2008-01-03 15:44:29Z groldan $
022: */
023: public class AttributeImpl implements Attribute {
024:
025: protected Object content;
026:
027: protected AttributeDescriptor DESCRIPTOR;
028:
029: protected final AttributeType TYPE;
030:
031: protected final String ID;
032:
033: public AttributeImpl(Object content,
034: AttributeDescriptor descriptor, String id) {
035: this (content, (AttributeType) descriptor.type(), id);
036:
037: DESCRIPTOR = descriptor;
038: }
039:
040: public AttributeImpl(Object content, AttributeType type, String id) {
041: // if (type.isAbstract()) {
042: // throw new UnsupportedOperationException(type.getName()
043: // + " is abstract");
044: // }
045: TYPE = type;
046: ID = id;
047: setValue(content);
048: }
049:
050: public String getID() {
051: return ID;
052: }
053:
054: public Object getValue() {
055: return content;
056: }
057:
058: public AttributeDescriptor getDescriptor() {
059: return DESCRIPTOR;
060: }
061:
062: public PropertyDescriptor descriptor() {
063: return getDescriptor();
064: }
065:
066: public AttributeType getType() {
067: return TYPE;
068: }
069:
070: public Name name() {
071: return DESCRIPTOR != null ? DESCRIPTOR.getName() : null;
072: }
073:
074: public boolean nillable() {
075: if (getDescriptor() != null) {
076: return getDescriptor().isNillable();
077: }
078:
079: return true;
080: }
081:
082: /**
083: *
084: * @throws IllegalArgumentException
085: * @throws IllegalStateException
086: * if the value has been parsed and validated, yet this
087: * Attribute does not passes the restrictions imposed by its
088: * AttributeType
089: */
090: public void setValue(Object newValue)
091: throws IllegalArgumentException, IllegalStateException {
092:
093: newValue = parse(newValue);
094:
095: try {
096: Types.validate(getType(), this , newValue);
097: } catch (IllegalAttributeException e) {
098: throw (IllegalArgumentException) new IllegalArgumentException()
099: .initCause(e);
100: }
101:
102: content = newValue;
103: }
104:
105: /**
106: * Override of hashCode.
107: *
108: * @return hashCode for this object.
109: */
110: public int hashCode() {
111: return 37 * (DESCRIPTOR == null ? 0 : DESCRIPTOR.hashCode())
112: + (37 * (TYPE == null ? 0 : TYPE.hashCode()))
113: + (37 * (ID == null ? 0 : ID.hashCode()))
114: + (37 * (content == null ? 0 : content.hashCode()));
115: }
116:
117: /**
118: * Override of equals.
119: *
120: * @param other
121: * the object to be tested for equality.
122: *
123: * @return whether other is equal to this attribute Type.
124: */
125: public boolean equals(Object other) {
126: if (!(other instanceof AttributeImpl)) {
127: return false;
128: }
129:
130: AttributeImpl att = (AttributeImpl) other;
131:
132: if (!Utilities.equals(DESCRIPTOR, att.DESCRIPTOR))
133: return false;
134:
135: if (!Utilities.equals(TYPE, att.TYPE))
136: return false;
137:
138: if (!Utilities.equals(ID, att.ID))
139: return false;
140:
141: if (!Utilities.equals(content, att.content))
142: return false;
143:
144: return true;
145: }
146:
147: /**
148: * Allows this Attribute to convert an argument to its prefered storage
149: * type. If no parsing is possible, returns the original value. If a parse
150: * is attempted, yet fails (i.e. a poor decimal format) throw the Exception.
151: * This is mostly for use internally in Features, but implementors should
152: * simply follow the rules to be safe.
153: *
154: * @param value
155: * the object to attempt parsing of.
156: *
157: * @return <code>value</code> converted to the preferred storage of this
158: * <code>AttributeType</code>. If no parsing was possible then
159: * the same object is returned.
160: *
161: * @throws IllegalArgumentException
162: * if parsing is attempted and is unsuccessful.
163: */
164: protected Object parse(Object value)
165: throws IllegalArgumentException {
166: return value;
167: }
168:
169: public Object operation(Name arg0, List arg1) {
170: throw new UnsupportedOperationException(
171: "operation not supported yet");
172: }
173:
174: public String toString() {
175: StringBuffer sb = new StringBuffer("Attribute[");
176: sb.append(DESCRIPTOR == null ? "" : DESCRIPTOR.getName()
177: .getLocalPart());
178: sb.append(":");
179: sb.append(TYPE.getName().getLocalPart());
180: sb.append(":@");
181: sb.append(ID == null ? "" : ID);
182: sb.append(":");
183: sb.append(content);
184: sb.append("]");
185: // LOGGER.fine("converting value for unbound Attribute (possibly null value) " + sb.toString() );
186: return (content == null ? "" : content.toString());
187: }
188: }
|