001    /**
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    package org.apache.camel.component.flatpack;
018    
019    import java.util.AbstractList;
020    import java.util.Iterator;
021    import java.util.Map;
022    
023    import net.sf.flatpack.DataSet;
024    
025    /**
026     * @version $Revision: 16256 $
027     */
028    public class DataSetList extends AbstractList<Map<String, Object>> {
029        private final DataSet dataSet;
030    
031        public DataSetList(DataSet dataSet) {
032            this.dataSet = dataSet;
033        }
034    
035        public Map<String, Object> get(int index) {
036            dataSet.absolute(index);
037            return FlatpackConverter.toMap(dataSet);
038        }
039    
040        public int size() {
041            return dataSet.getRowCount();
042        }
043    
044        @Override
045        public Iterator<Map<String, Object>> iterator() {
046            dataSet.goTop();
047            return new Iterator<Map<String, Object>>() {
048                public boolean hasNext() {
049                    return dataSet.next();
050                }
051    
052                public Map<String, Object> next() {
053                    // because of a limitation in split() we need to create an object for the current position
054                    // otherwise strangeness occurs when the same object is used to represent each row
055                    return FlatpackConverter.toMap(dataSet);
056                }
057    
058                public void remove() {
059                    throw new UnsupportedOperationException("remove() not supported");
060                }
061            };
062        }
063    }