001/*
002// This software is subject to the terms of the Eclipse Public License v1.0
003// Agreement, available at the following URL:
004// http://www.eclipse.org/legal/epl-v10.html.
005// You must accept the terms of that agreement to use this software.
006//
007// Copyright (C) 2009-2010 Pentaho
008// All Rights Reserved.
009*/
010package mondrian.util;
011
012import java.util.AbstractList;
013import java.util.List;
014
015/**
016 * List composed of several lists.
017 *
018 * @param <T> element type
019 *
020 * @author jhyde
021 */
022public class CompositeList<T> extends AbstractList<T> {
023    private final List<? extends T>[] lists;
024
025    /**
026     * Creates a composite list.
027     *
028     * @param lists Component lists
029     */
030    public CompositeList(
031        List<? extends T>... lists)
032    {
033        this.lists = lists;
034    }
035
036    /**
037     * Creates a composite list, inferring the element type from the arguments.
038     *
039     * @param lists One or more lists
040     * @param <T> element type
041     * @return composite list
042     */
043    public static <T> CompositeList<T> of(
044        List<? extends T>... lists)
045    {
046        return new CompositeList<T>(lists);
047    }
048
049    public T get(int index) {
050        int n = 0;
051        for (List<? extends T> list : lists) {
052            int next = n + list.size();
053            if (index < next) {
054                return list.get(index - n);
055            }
056            n = next;
057        }
058        throw new IndexOutOfBoundsException(
059            "index" + index + " out of bounds in list of size " + n);
060    }
061
062    public int size() {
063        int n = 0;
064        for (List<? extends T> array : lists) {
065            n += array.size();
066        }
067        return n;
068    }
069}
070
071// End CompositeList.java