001: /*
002: * GeoTools - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2003-2006, GeoTools Project Managment Committee (PMC)
005: * (C) 2002, Centre for Computational Geography
006: *
007: * This library is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU Lesser General Public
009: * License as published by the Free Software Foundation;
010: * version 2.1 of the License.
011: *
012: * This library is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: */
017: package org.geotools.index;
018:
019: import java.util.ArrayList;
020:
021: /**
022: * DOCUMENT ME!
023: *
024: * @author Tommaso Nolli
025: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/plugin/shapefile/src/main/java/org/geotools/index/Data.java $
026: */
027: public class Data {
028: private DataDefinition def;
029: private ArrayList values;
030:
031: /**
032: * DOCUMENT ME!
033: *
034: * @param def
035: */
036: public Data(DataDefinition def) {
037: this .def = def;
038: this .values = new ArrayList();
039: }
040:
041: /**
042: * Check to see if a <code>Data</code> respects its
043: * <code>DataDefinition</code>
044: *
045: */
046: public final boolean isValid() {
047: if (this .getValuesCount() != this .def.getFieldsCount()) {
048: return false;
049: }
050:
051: boolean ret = true;
052:
053: for (int i = 0; i < this .def.getFieldsCount(); i++) {
054: if (!this .def.getField(i).getFieldClass().isInstance(
055: this .getValue(i))) {
056: ret = false;
057:
058: break;
059: }
060: }
061:
062: return ret;
063: }
064:
065: /**
066: * DOCUMENT ME!
067: *
068: * @param val
069: *
070: * @return - this Data object
071: *
072: * @throws TreeException
073: */
074: public Data addValue(Object val) throws TreeException {
075: if (this .values.size() == def.getFieldsCount()) {
076: throw new TreeException("Max number of values reached!");
077: }
078:
079: int pos = this .values.size();
080:
081: if (!val.getClass().equals(def.getField(pos).getFieldClass())) {
082: throw new TreeException("Wrong class type, was expecting "
083: + def.getField(pos).getFieldClass());
084: }
085:
086: this .values.add(val);
087:
088: return this ;
089: }
090:
091: /**
092: * Return the KeyDefinition
093: *
094: */
095: public DataDefinition getDefinition() {
096: return this .def;
097: }
098:
099: /**
100: * DOCUMENT ME!
101: *
102: */
103: public int getValuesCount() {
104: return this .values.size();
105: }
106:
107: /**
108: * DOCUMENT ME!
109: *
110: * @param i
111: *
112: */
113: public Object getValue(int i) {
114: return this .values.get(i);
115: }
116:
117: /**
118: * @see java.lang.Object#toString()
119: */
120: public String toString() {
121: StringBuffer ret = new StringBuffer();
122:
123: for (int i = 0; i < this .values.size(); i++) {
124: if (i > 0) {
125: ret.append(" - ");
126: }
127:
128: ret.append(this .def.getField(i).getFieldClass());
129: ret.append(": ");
130: ret.append(this.values.get(i));
131: }
132:
133: return ret.toString();
134: }
135: }
|