Skip to main content
summaryrefslogtreecommitdiffstats
blob: 8170130fe86b7398f4ec980b326c7c90d5830f30 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/*******************************************************************************
 * Copyright (c) 2009, 2013 Oracle. All rights reserved.
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License v1.0, which accompanies this distribution
 * and is available at http://www.eclipse.org/legal/epl-v10.html.
 * 
 * Contributors:
 *     Oracle - initial API and implementation
 ******************************************************************************/
package org.eclipse.jpt.common.utility.internal.iterable;

import java.util.Iterator;
import org.eclipse.jpt.common.utility.internal.collection.ListTools;
import org.eclipse.jpt.common.utility.internal.iterator.CompositeIterator;
import org.eclipse.jpt.common.utility.internal.iterator.IteratorTools;
import org.eclipse.jpt.common.utility.transformer.Transformer;

/**
 * A <code>CompositeIterable</code> wraps an {@link Iterable}
 * of {@link Iterable}s and makes them appear to be a single
 * {@link Iterable}.
 * 
 * @param <E> the type of elements returned by the iterable's iterator
 * 
 * @see CompositeIterator
 * @see CompositeListIterable
 */
public class CompositeIterable<E>
	implements Iterable<E>
{
	private final Iterable<? extends Iterable<? extends E>> iterables;


	/**
	 * Construct an iterable with the specified collection of iterables.
	 */
	public CompositeIterable(Iterable<? extends Iterable<? extends E>> iterables) {
		super();
		if (iterables == null) {
			throw new NullPointerException();
		}
		this.iterables = iterables;
	}

	/**
	 * combined iterators
	 */
	public Iterator<E> iterator() {
		return new CompositeIterator<E>(this.iterators());
	}

	/**
	 * iterator of iterators
	 */
	protected Iterator<? extends Iterator<? extends E>> iterators() {
		Transformer<Iterable<? extends E>, Iterator<E>> transformer = IterableTools.iteratorTransformer();
		return IteratorTools.transform(this.iterables(), transformer);
	}

	/**
	 * iterator of iterables
	 */
	protected Iterator<? extends Iterable<? extends E>> iterables() {
		return this.iterables.iterator();
	}

	@Override
	public String toString() {
		return ListTools.list(this).toString();
	}
}

Back to the top