Skip to main content
summaryrefslogtreecommitdiffstats
blob: 12dcc76816eaa242ab1f7d6cbfd774e843460abb (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
/*******************************************************************************
 * 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.common.utility.internal.iterables;

import java.util.Iterator;

import org.eclipse.jpt.common.utility.internal.StringTools;
import org.eclipse.jpt.common.utility.internal.iterators.ChainIterator;

/**
 * A <code>ChainIterable</code> provides a pluggable {@link Iterable}
 * that loops over a chain of arbitrarily linked objects. The chain
 * should be null-terminated (i.e. a call to the {@link #nextLink(Object)}
 * method should return <code>null</code> when it is passed the last
 * link of the chain).
 * To use, supply a starting link and supply a {@link ChainIterator.Linker} or
 * subclass <code>ChainIterable</code> and override the
 * {@link #nextLink(Object)} method.
 * The starting link will be the first object returned by the iterable's iterator.
 * If the starting link is <code>null</code>, the iterable will be empty.
 * Note this iterable does not support <code>null</code> elements.
 * 
 * @param <E> the type of elements returned by the iterable's iterator
 * 
 * @see ChainIterator
 */
public class ChainIterable<E>
	implements Iterable<E>
{
	private final E startLink;
	private final ChainIterator.Linker<E> linker;


	/**
	 * Construct an iterable with the specified starting link
	 * and a default linker that calls back to the iterable.
	 * Use this constructor if you want to override the
	 * {@link #nextLink(Object)} method instead of building
	 * a {@link ChainIterator.Linker}.
	 */
	public ChainIterable(E startLink) {
		super();
		this.startLink = startLink;
		this.linker = this.buildDefaultLinker();
	}

	/**
	 * Construct an iterator with the specified starting link
	 * and linker.
	 */
	public ChainIterable(E startLink, ChainIterator.Linker<E> linker) {
		super();
		this.startLink = startLink;
		this.linker = linker;
	}

	protected ChainIterator.Linker<E> buildDefaultLinker() {
		return new DefaultLinker();
	}

	public Iterator<E> iterator() {
		return new ChainIterator<E>(this.startLink, this.linker);
	}

	/**
	 * Return the next link in the chain; null if there are no more links.
	 * <p>
	 * This method can be overridden by a subclass as an alternative to
	 * building a {@link ChainIterator.Linker}.
	 */
	protected E nextLink(@SuppressWarnings("unused") E currentLink) {
		throw new RuntimeException("This method was not overridden."); //$NON-NLS-1$
	}

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


	//********** default linker **********

	protected class DefaultLinker implements ChainIterator.Linker<E> {
		public E nextLink(E currentLink) {
			return ChainIterable.this.nextLink(currentLink);
		}
	}

}

Back to the top