001: /*
002: * Created on 03.03.2005 for PIROL
003: *
004: * SVN header information:
005: * $Author: javamap $
006: * $Rev: 856 $
007: * $Date: 2007-06-18 21:15:27 -0700 (Mon, 18 Jun 2007) $
008: * $Id: AttributeInfo.java 856 2007-06-19 04:15:27Z javamap $
009: */
010: package de.fho.jump.pirol.utilities.attributes;
011:
012: import com.vividsolutions.jump.feature.AttributeType;
013: import com.vividsolutions.jump.feature.FeatureSchema;
014:
015: /**
016: * Class to store all information for a given attribute, so it can easily passed
017: * to e.g. to methods of the FeatureCollectionTools
018: *
019: * @author Ole Rahn
020: * <br>
021: * <br>FH Osnabrück - University of Applied Sciences Osnabrück,
022: * <br>Project: PIROL (2005),
023: * <br>Subproject: Daten- und Wissensmanagement
024: *
025: * @version $Rev: 856 $
026: *
027: * @see de.fhOsnabrueck.jump.pirol.utilities.FeatureCollectionTools
028: */
029: public class AttributeInfo implements Comparable {
030:
031: protected AttributeType attributeType = null;
032: protected String attributeName = null;
033: protected String uniqueAttributeName = null;
034: protected String unitIdentifier = null;
035: protected Object nullValue = null;
036:
037: /**
038: * For attributes that have been loaded from the DB
039: */
040: protected int dataBaseId = -1;
041:
042: /**
043: * attribute's index in a (given?) FeatureSchema
044: */
045: protected int index = -1;
046:
047: public AttributeInfo(AttributeType attributeType,
048: String attributeName, Object nullValue) {
049: this (attributeType, attributeName);
050: this .nullValue = nullValue;
051: }
052:
053: public AttributeInfo(AttributeType attributeType,
054: String attributeName) {
055: super ();
056: this .attributeType = attributeType;
057: this .attributeName = attributeName;
058: }
059:
060: public AttributeInfo(String attributeName, Object nullValue) {
061: super ();
062: this .attributeName = attributeName;
063: this .nullValue = nullValue;
064: }
065:
066: public String getAttributeName() {
067: return attributeName;
068: }
069:
070: public AttributeType getAttributeType() {
071: return attributeType;
072: }
073:
074: public int getIndex() {
075: return index;
076: }
077:
078: public void setIndex(int index) {
079: this .index = index;
080: }
081:
082: public Object getNullValue() {
083: return nullValue;
084: }
085:
086: public String getUniqueAttributeName() {
087: if (uniqueAttributeName == null)
088: return this .getAttributeName();
089: return uniqueAttributeName;
090: }
091:
092: public void setUniqueAttributeName(String uniqueAttributeName) {
093: this .uniqueAttributeName = uniqueAttributeName;
094: }
095:
096: public void setAttributeName(String attributeName) {
097: this .attributeName = attributeName;
098: }
099:
100: public void setAttributeType(AttributeType attributeType) {
101: this .attributeType = attributeType;
102: }
103:
104: public void setNullValue(Object nullValue) {
105: if (this .attributeType != null) {
106: if ((this .attributeType.equals(AttributeType.DOUBLE) && !Double.class
107: .isInstance(nullValue))
108: || (this .attributeType
109: .equals(AttributeType.INTEGER) && !Integer.class
110: .isInstance(nullValue)))
111: throw new IllegalArgumentException(
112: "default value is of a wrong type: "
113: + nullValue.getClass().getName());
114: else if (this .attributeType.equals(AttributeType.STRING)
115: && !String.class.isInstance(nullValue))
116: nullValue = new String(nullValue.toString());
117: }
118:
119: this .nullValue = nullValue;
120: }
121:
122: public String getUnitIdentifier() {
123: return unitIdentifier;
124: }
125:
126: public void setUnitIdentifier(String unitIdentifier) {
127: this .unitIdentifier = unitIdentifier;
128: }
129:
130: /**
131: * convenient method to convert a FeatureSchema into an AttributeInfo array
132: *@param fs the FeatureSchema
133: *@return an array of AttributeInfos matching the FeatureSchema
134: */
135: public static AttributeInfo[] schema2AttributeInfoArray(
136: FeatureSchema fs) {
137: AttributeInfo[] result = new AttributeInfo[fs
138: .getAttributeCount()];
139:
140: for (int i = 0; i < result.length; i++) {
141: result[i] = new AttributeInfo(fs.getAttributeType(i), fs
142: .getAttributeName(i));
143: result[i].setIndex(i);
144: }
145:
146: return result;
147: }
148:
149: /**
150: * convenient method to convert an AttributeInfo array into a FeatureSchema
151: *@param attributeInfos an array of AttributeInfos matching wanted in the FeatureSchema
152: *@return the desired FeatureSchema
153: */
154: public static FeatureSchema attributeInfoArray2FeatureSchema(
155: AttributeInfo[] attributeInfos) {
156: FeatureSchema result = new FeatureSchema();
157:
158: for (int i = 0; i < attributeInfos.length; i++) {
159: result.addAttribute(attributeInfos[i]
160: .getUniqueAttributeName(), attributeInfos[i]
161: .getAttributeType());
162: }
163:
164: return result;
165: }
166:
167: /**
168: *@inheritDoc
169: */
170: public String toString() {
171: return this .getUniqueAttributeName();
172: }
173:
174: /**
175: * In order for this to work, the attribute indices of this AttributeInfo object and of the other
176: * one has to be set, correctly!
177: *@param theOtherObject
178: *@return see <code>Comparable</code> for
179: */
180: public int compareTo(Object theOtherObject) {
181: int iTheOtherIndex = ((AttributeInfo) theOtherObject)
182: .getIndex();
183: int iThisIndex = this .getIndex();
184:
185: if (Math.min(iThisIndex, iTheOtherIndex) > -1) {
186: // since both indices may be -1 --> not set...
187: Integer theOtherIndex = new Integer(iTheOtherIndex);
188: Integer this Index = new Integer(iThisIndex);
189: return this Index.compareTo(theOtherIndex);
190: }
191: // ... we may want to throw an exception...
192: throw new IllegalStateException(
193: "at least one index is not set correctly: "
194: + iThisIndex + ", " + iTheOtherIndex);
195: }
196:
197: public int getDataBaseId() {
198: return dataBaseId;
199: }
200:
201: public void setDataBaseId(int dataBaseId) {
202: this.dataBaseId = dataBaseId;
203: }
204:
205: }
|