Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: c1677d045b70d53d0108ab607c8fec3b67a1fe1e (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.Collection;
import java.util.Iterator;
import org.eclipse.jpt.common.utility.closure.Closure;
import org.eclipse.jpt.common.utility.internal.iterator.CloneIterator;
import org.eclipse.jpt.common.utility.internal.iterator.IteratorTools;

/**
 * A <code>LiveCloneIterable</code> returns an iterator on a current copy of a
 * collection, allowing for concurrent access to the original collection. A
 * copy of the collection is created every time {@link #iterator()} is
 * called. As a result, the contents of the collection can be different with
 * each call to {@link #iterator()} (i.e. it is "live").
 * <p>
 * The original collection passed to the <code>LiveCloneIterable</code>'s
 * constructor should be thread-safe (e.g. {@link java.util.Vector});
 * otherwise you run the risk of a corrupted collection.
 * <p>
 * By default, the iterator returned by a <code>LiveCloneIterable</code> does not
 * support the {@link Iterator#remove()} operation; this is because it does not
 * have access to the original collection. But if the <code>LiveCloneIterable</code>
 * is supplied with an {@link Closure remove closure} it will delegate the
 * {@link Iterator#remove()} operation to the command.
 * 
 * @param <E> the type of elements returned by the iterable's iterator
 * 
 * @see CloneIterator
 * @see SnapshotCloneIterable
 * @see LiveCloneListIterable
 */
public class LiveCloneIterable<E>
	extends CloneIterable<E>
{
	private final Collection<? extends E> collection;


	/**
	 * Construct a "live" iterable for the specified collection.
	 * The specified command will be used by any generated iterators to
	 * remove objects from the original collection.
	 */
	public LiveCloneIterable(Collection<? extends E> collection, Closure<? super E> removeClosure) {
		super(removeClosure);
		if (collection == null) {
			throw new NullPointerException();
		}
		this.collection = collection;
	}


	// ********** Iterable implementation **********

	public Iterator<E> iterator() {
		return IteratorTools.clone(this.collection, this.removeClosure);
	}

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

Back to the top