Skip to main content
summaryrefslogtreecommitdiffstats
blob: 88cd46faefcba3a0978067a5a16d96598ed37bf6 (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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/*******************************************************************************
 * Copyright (c) 2009 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.utility.internal.iterables;

import java.util.Iterator;

import org.eclipse.jpt.utility.internal.StringTools;
import org.eclipse.jpt.utility.internal.iterators.CompositeIterator;
import org.eclipse.jpt.utility.internal.iterators.TransformationIterator;

/**
 * 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();
		this.iterables = iterables;
	}

	/**
	 * Construct an iterable with the specified object prepended
	 * to the specified iterable.
	 */
	@SuppressWarnings("unchecked")
	public CompositeIterable(E object, Iterable<? extends E> iterable) {
		this(new SingleElementIterable<E>(object), iterable);
	}

	/**
	 * Construct an iterable with the specified object appended
	 * to the specified iterable.
	 */
	@SuppressWarnings("unchecked")
	public CompositeIterable(Iterable<? extends E> iterable, E object) {
		this(iterable, new SingleElementIterable<E>(object));
	}

	/**
	 * Construct an iterable with the specified iterables.
	 */
	public CompositeIterable(Iterable<? extends E>... iterables) {
		this(new ArrayIterable<Iterable<? extends E>>(iterables));
	}

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

	/**
	 * iterator of iterators
	 */
	protected Iterator<? extends Iterator<? extends E>> iterators() {
		return new TransformationIterator<Iterable<? extends E>, Iterator<? extends E>>(this.iterables()) {
			@Override
			protected Iterator<? extends E> transform(Iterable<? extends E> next) {
				return next.iterator();
			}
		};
	}

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

	@Override
	public String toString() {
		return StringTools.buildToStringFor(this, this.iterables);
	}

}

Back to the top