01: /*
02: * GeoTools - OpenSource mapping toolkit
03: * http://geotools.org
04: * (C) 2003-2006, GeoTools Project Managment Committee (PMC)
05: *
06: * This library is free software; you can redistribute it and/or
07: * modify it under the terms of the GNU Lesser General Public
08: * License as published by the Free Software Foundation;
09: * version 2.1 of the License.
10: *
11: * This library is distributed in the hope that it will be useful,
12: * but WITHOUT ANY WARRANTY; without even the implied warranty of
13: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14: * Lesser General Public License for more details.
15: */
16: package org.geotools.data;
17:
18: import java.io.IOException;
19:
20: import org.geotools.feature.FeatureType;
21:
22: /**
23: * A Default FIDReader. Just auto-increments an index. May be sufficient for
24: * files, representing rows in a file. For jdbc datasources a
25: * ResultSetFIDReader should be used.
26: *
27: * @author Chris Holmes
28: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/library/main/src/main/java/org/geotools/data/DefaultFIDReader.java $
29: * @version $Id: DefaultFIDReader.java 23707 2007-01-08 10:32:10Z aaime $
30: */
31: public class DefaultFIDReader implements FIDReader {
32: protected static final String CLOSE_MESG = "Close has already been called"
33: + " on this FIDReader";
34:
35: private int len;
36: protected int index = 0;
37: protected StringBuffer buffer;
38:
39: public DefaultFIDReader(String typeName) {
40: buffer = new StringBuffer(typeName);
41: buffer.append('.');
42: len = typeName.length() + 1;
43: }
44:
45: public DefaultFIDReader(FeatureType featureType) {
46: this (featureType.getTypeName());
47: }
48:
49: /**
50: * Release any resources associated with this reader
51: */
52: public void close() {
53: index = -1;
54: }
55:
56: /**
57: * Does another set of attributes exist in this reader?
58: *
59: * @return <code>true</code> if more attributes exist
60: *
61: * @throws IOException If closed
62: */
63: public boolean hasNext() throws IOException {
64: if (index < 0) {
65: throw new IOException(CLOSE_MESG);
66: }
67:
68: return index < Integer.MAX_VALUE;
69: }
70:
71: /**
72: * Read the attribute at the given index.
73: *
74: * @return Attribute at index
75: *
76: * @throws IOException If closed
77: */
78: public String next() throws IOException {
79: if (index < 0) {
80: throw new IOException(CLOSE_MESG);
81: }
82:
83: buffer.setLength(len);
84: buffer.append(++index);
85:
86: return buffer.toString();
87: }
88:
89: }
|