/*
* Geotools - OpenSource mapping toolkit
* (C) 2002, Centre for Computational Geography
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation;
* version 2.1 of the License.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
package org.geotools.index;
import java.util.ArrayList;
/**
* @author Tommaso Nolli
*/
public class Data {
private DataDefinition def;
private ArrayList values;
/**
*
* @param def
*/
public Data(DataDefinition def) {
this.def = def;
this.values = new ArrayList();
}
/**
* Check to see if a Data
respects its
* DataDefinition
* @return
*/
public final boolean isValid() {
if (this.getValuesCount() != this.def.getFieldsCount()) {
return false;
}
boolean ret = true;
for (int i = 0; i < this.def.getFieldsCount(); i++) {
if (!this.def.getField(i).getFieldClass()
.isInstance(this.getValue(i)))
{
ret = false;
break;
}
}
return ret;
}
/**
*
* @param val
* @return - this Data object
* @throws TreeException
*/
public Data addValue(Object val) throws TreeException {
if (this.values.size() == def.getFieldsCount()) {
throw new TreeException("Max number of values reached!");
}
int pos = this.values.size();
if (!val.getClass().equals(def.getField(pos).getFieldClass())) {
throw new TreeException("Wrong class type, was expecting " +
def.getField(pos).getFieldClass());
}
this.values.add(val);
return this;
}
/**
* Return the KeyDefinition
* @return
*/
public DataDefinition getDefinition() {
return this.def;
}
/**
*
* @return
*/
public int getValuesCount() {
return this.values.size();
}
/**
*
* @param i
* @return
*/
public Object getValue(int i) {
return this.values.get(i);
}
/**
* @see java.lang.Object#toString()
*/
public String toString() {
StringBuffer ret = new StringBuffer();
for (int i = 0; i < this.values.size(); i++) {
if (i > 0) {
ret.append(" - ");
}
ret.append(this.def.getField(i).getFieldClass());
ret.append(": ");
ret.append(this.values.get(i));
}
return ret.toString();
}
}