001: /*
002: * GeoTools - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2003-2006, GeoTools Project Managment Committee (PMC)
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation;
009: * version 2.1 of the License.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: */
016: package org.geotools.index;
017:
018: import java.nio.charset.Charset;
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/DataDefinition.java $
026: */
027: public class DataDefinition {
028: private Charset charset;
029: private ArrayList fields;
030:
031: public DataDefinition(String charset) {
032: fields = new ArrayList();
033: this .charset = Charset.forName(charset);
034: }
035:
036: public final boolean isValid() {
037: return (this .charset != null) && !this .charset.equals("")
038: && (this .fields.size() > 0);
039: }
040:
041: public int getFieldsCount() {
042: return this .fields.size();
043: }
044:
045: public Field getField(int i) {
046: return (Field) this .fields.get(i);
047: }
048:
049: /**
050: * Well known classes
051: *
052: * <ul>
053: * <li>
054: * Short
055: * </li>
056: * <li>
057: * Integer
058: * </li>
059: * <li>
060: * Long
061: * </li>
062: * <li>
063: * Float
064: * </li>
065: * <li>
066: * Double
067: * </li>
068: * <li>
069: * Date
070: * </li>
071: * </ul>
072: *
073: *
074: * @param clazz
075: *
076: * @throws TreeException DOCUMENT ME!
077: */
078: public void addField(Class clazz) {
079: if (clazz.isAssignableFrom(Short.class)) {
080: this .fields.add(new Field(clazz, 2));
081: } else if (clazz.isAssignableFrom(Integer.class)) {
082: this .fields.add(new Field(clazz, 4));
083: } else if (clazz.isAssignableFrom(Long.class)) {
084: this .fields.add(new Field(clazz, 8));
085: } else if (clazz.isAssignableFrom(Float.class)) {
086: // TODO: Are you sure of 4 bytes?
087: this .fields.add(new Field(clazz, 4));
088: } else if (clazz.isAssignableFrom(Double.class)) {
089: this .fields.add(new Field(clazz, 8));
090: } else {
091: throw new IllegalArgumentException("Unknow len of class "
092: + clazz + "use addField(int)");
093: }
094: }
095:
096: /**
097: * For classes with unknown length; this values will be threated as
098: * <code>String</code>s and truncated at the specified len
099: *
100: * @param len
101: */
102: public void addField(int len) {
103: this .fields.add(new Field(String.class, len));
104: }
105:
106: /**
107: * DOCUMENT ME!
108: *
109: */
110: public Charset getCharset() {
111: return charset;
112: }
113:
114: /**
115: * Gets the max len of the data
116: *
117: */
118: public int getLen() {
119: int len = 0;
120:
121: Field field = null;
122:
123: for (int i = 0; i < this .fields.size(); i++) {
124: field = (Field) this .fields.get(i);
125: len += field.getLen();
126: }
127:
128: return len;
129: }
130:
131: /**
132: * Gets the len of this field after the encoding, this method may be
133: * different from getLen() only if exists strings in the definition
134: *
135: */
136: public int getEncodedLen() {
137: int len = 0;
138:
139: Field field = null;
140:
141: for (int i = 0; i < this .fields.size(); i++) {
142: field = (Field) this .fields.get(i);
143: len += field.getEncodedLen();
144: }
145:
146: return len;
147: }
148:
149: /**
150: * Inner class for Data fields
151: *
152: * @author Tommaso Nolli
153: */
154: public class Field {
155: private Class clazz;
156: private int len;
157:
158: public Field(Class clazz, int len) {
159: this .clazz = clazz;
160: this .len = len;
161: }
162:
163: /**
164: * DOCUMENT ME!
165: *
166: */
167: public Class getFieldClass() {
168: return clazz;
169: }
170:
171: /**
172: * DOCUMENT ME!
173: *
174: */
175: public int getLen() {
176: return len;
177: }
178:
179: /**
180: * DOCUMENT ME!
181: *
182: */
183: public int getEncodedLen() {
184: int ret = this .len;
185:
186: if (this .clazz.equals(String.class)) {
187: ret = (int) charset.newEncoder().maxBytesPerChar()
188: * this.len;
189: }
190:
191: return ret;
192: }
193: }
194: }
|