Skip to main content
summaryrefslogtreecommitdiffstats
blob: d3ffc94c9ffc3c2a77f8e9e576e1a06e557d36f6 (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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/*******************************************************************************
 * Copyright (c) 2009, 2010 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.iterators;

import java.util.List;
import java.util.ListIterator;

import org.eclipse.jpt.utility.internal.StringTools;
import org.eclipse.jpt.utility.internal.iterables.ListIterable;

/**
 * Wrap a list iterator and synchronize all its methods so it can be safely shared
 * among multiple threads.
 * 
 * @param <E> the type of elements returned by the iterator
 */
public class SynchronizedListIterator<E>
	implements ListIterator<E>
{
	private final ListIterator<E> listIterator;

	/** Object to synchronize on. */
	private final Object mutex;


	public SynchronizedListIterator(List<E> list) {
		this(list.listIterator());
	}

	public SynchronizedListIterator(List<E> list, Object mutex) {
		this(list.listIterator(), mutex);
	}

	public SynchronizedListIterator(ListIterable<E> listIterable) {
		this(listIterable.iterator());
	}

	public SynchronizedListIterator(ListIterable<E> listIterable, Object mutex) {
		this(listIterable.iterator(), mutex);
	}

	public SynchronizedListIterator(ListIterator<E> listIterator) {
		super();
		this.listIterator = listIterator;
		this.mutex = this;
	}

	public SynchronizedListIterator(ListIterator<E> listIterator, Object mutex) {
		super();
		this.listIterator = listIterator;
		this.mutex = mutex;
	}

	public synchronized boolean hasNext() {
		synchronized (this.mutex) {
			return this.listIterator.hasNext();
		}
	}

	public synchronized E next() {
		synchronized (this.mutex) {
			return this.listIterator.next();
		}
	}

	public synchronized int nextIndex() {
		synchronized (this.mutex) {
			return this.listIterator.nextIndex();
		}
	}

	public synchronized boolean hasPrevious() {
		synchronized (this.mutex) {
			return this.listIterator.hasPrevious();
		}
	}

	public synchronized E previous() {
		synchronized (this.mutex) {
			return this.listIterator.previous();
		}
	}

	public synchronized int previousIndex() {
		synchronized (this.mutex) {
			return this.listIterator.previousIndex();
		}
	}

	public synchronized void remove() {
		synchronized (this.mutex) {
			this.listIterator.remove();
		}
	}

	public synchronized void add(E e) {
		synchronized (this.mutex) {
			this.listIterator.add(e);
		}
	}

	public synchronized void set(E e) {
		synchronized (this.mutex) {
			this.listIterator.set(e);
		}
	}

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

}

Back to the top