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.data;
017:
018: import java.io.IOException;
019:
020: import org.geotools.feature.AttributeType;
021:
022: /**
023: * Attribute Reader that joins.
024: *
025: * @author Ian Schneider
026: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/library/main/src/main/java/org/geotools/data/JoiningAttributeReader.java $
027: * @version $Id: JoiningAttributeReader.java 20651 2006-07-21 07:51:54Z jgarnett $
028: */
029: public class JoiningAttributeReader implements AttributeReader {
030: private AttributeReader[] readers;
031: private int[] index;
032: private AttributeType[] metaData;
033:
034: /**
035: * Creates a new instance of JoiningAttributeReader
036: *
037: * @param readers Readers to join
038: */
039: public JoiningAttributeReader(AttributeReader[] readers) {
040: this .readers = readers;
041:
042: this .metaData = joinMetaData(readers);
043: }
044:
045: private AttributeType[] joinMetaData(AttributeReader[] readers) {
046: int total = 0;
047: index = new int[readers.length];
048:
049: for (int i = 0, ii = readers.length; i < ii; i++) {
050: index[i] = total;
051: total += readers[i].getAttributeCount();
052: }
053:
054: AttributeType[] md = new AttributeType[total];
055: int idx = 0;
056:
057: for (int i = 0, ii = readers.length; i < ii; i++) {
058: for (int j = 0, jj = readers[i].getAttributeCount(); j < jj; j++) {
059: md[idx] = readers[i].getAttributeType(j);
060: idx++;
061: }
062: }
063:
064: return md;
065: }
066:
067: public void close() throws IOException {
068: IOException dse = null;
069:
070: for (int i = 0, ii = readers.length; i < ii; i++) {
071: try {
072: readers[i].close();
073: } catch (IOException e) {
074: dse = e;
075: }
076: }
077:
078: if (dse != null) {
079: throw dse;
080: }
081: }
082:
083: public boolean hasNext() throws IOException {
084: for (int i = 0, ii = readers.length; i < ii; i++) {
085: if (readers[i].hasNext()) {
086: return true;
087: }
088: }
089:
090: return false;
091: }
092:
093: public void next() throws IOException {
094: for (int i = 0, ii = readers.length; i < ii; i++) {
095: if (readers[i].hasNext()) {
096: readers[i].next();
097: }
098: }
099: }
100:
101: public Object read(int idx) throws IOException {
102: AttributeReader reader = null;
103:
104: for (int i = index.length - 1; i >= 0; i--) {
105: if (idx >= index[i]) {
106: idx -= index[i];
107: reader = readers[i];
108:
109: break;
110: }
111: }
112:
113: if (reader == null) {
114: throw new ArrayIndexOutOfBoundsException(idx);
115: }
116:
117: return reader.read(idx);
118: }
119:
120: public int getAttributeCount() {
121: return metaData.length;
122: }
123:
124: public AttributeType getAttributeType(int i) {
125: return metaData[i];
126: }
127: }
|