Package: SynchronizedIterator

SynchronizedIterator

nameinstructionbranchcomplexitylinemethod
SynchronizedIterator(Iterator, Object)
M: 0 C: 11
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 4
100%
M: 0 C: 1
100%
hasNext()
M: 0 C: 11
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
next()
M: 0 C: 11
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
remove()
M: 0 C: 12
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 3
100%
M: 0 C: 1
100%

Coverage

1: /*******************************************************************************
2: * Copyright (c) 2008, 2010 VMware Inc.
3: * All rights reserved. This program and the accompanying materials
4: * are made available under the terms of the Eclipse Public License v1.0
5: * which accompanies this distribution, and is available at
6: * http://www.eclipse.org/legal/epl-v10.html
7: *
8: * Contributors:
9: * VMware Inc. - initial contribution
10: *******************************************************************************/
11:
12: package org.eclipse.virgo.util.common;
13:
14: import java.util.Iterator;
15:
16: /**
17: * {@link SynchronizedIterator} wraps a given {@link Iterator} and protects access to the iterator by synchronizing on
18: * the monitor of the containing {@link SynchronizedCollection}.
19: * <p />
20: *
21: * <strong>Concurrent Semantics</strong><br />
22: *
23: * This class is thread safe, but ConcurrentModificationException may still be thrown.
24: *
25: * @param <T> the type of element the iterator returns
26: */
27: public class SynchronizedIterator<T> extends SynchronizedObject implements Iterator<T> {
28:
29: private final Object monitor;
30:
31: private final Iterator<T> iterator;
32:
33: public SynchronizedIterator(Iterator<T> iterator, Object monitor) {
34: super(iterator, monitor);
35: this.monitor = monitor;
36: this.iterator = iterator;
37: }
38:
39: /**
40: * {@inheritDoc}
41: */
42: public boolean hasNext() {
43: synchronized (this.monitor) {
44: return this.iterator.hasNext();
45: }
46: }
47:
48: /**
49: * {@inheritDoc}
50: */
51: public T next() {
52: synchronized (this.monitor) {
53: return this.iterator.next();
54: }
55: }
56:
57: /**
58: * {@inheritDoc}
59: */
60: public void remove() {
61: synchronized (this.monitor) {
62: this.iterator.remove();
63: }
64: }
65:
66: }