Package: IterableEnumeration$EnumerationIterator

IterableEnumeration$EnumerationIterator

nameinstructionbranchcomplexitylinemethod
IterableEnumeration.EnumerationIterator(IterableEnumeration, Enumeration)
M: 0 C: 9
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
hasNext()
M: 0 C: 4
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
next()
M: 0 C: 4
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
remove()
M: 0 C: 4
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
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.Enumeration;
15: import java.util.Iterator;
16:
17: /**
18: * An <code>IterableEnumeration</code> wraps an {@link Enumeration} in an
19: * implementation of {@link Iterable} thereby allowing the <code>Enumeration</code>
20: * to be used in a for-each loop.
21: * <p/>
22: *
23: * <pre>
24: * Enumeration<Foo> enumeration = ...
25: * IterableEnumeration<Foo> foos = new IterableEnumeration<Foo>(enumeration);
26: *
27: * for (Foo foo : foos) {
28: * // Do something with foo
29: * }
30: * </pre>
31: *
32: * <p/>
33: * <strong>Note:</strong> as <code>Enumerations</code> do not support entry removal
34: * the {@link Iterator} returned by {@link #iterator()} does not support the optional
35: * {@link Iterator#remove() remove} method: invocation of the method will result in an
36: * {@link UnsupportedOperationException} being thrown.
37: *
38: * <strong>Concurrent Semantics</strong><br />
39: * This class is <strong>not</strong> thread-safe.
40: * @param <T> the element type of the enumeration
41: *
42: */
43: public class IterableEnumeration<T> implements Iterable<T> {
44:
45: private final Iterator<T> iterator;
46:
47: /**
48: * Creates a new <code>IterableEnumeration</code> backed by the supplied <code>Enumeration</code>
49: * @param enumeration The <code>Enumeration</code> to be made iterable.
50: */
51: public IterableEnumeration(Enumeration<T> enumeration) {
52: this.iterator = new EnumerationIterator<T>(enumeration);
53: }
54:
55: /**
56: * Returns the <code>Enumeration</code>-backed <code>Iterator</code>.
57: * <p/>
58: * <strong>Note:</strong> as <code>Enumerations</code> do not support entry removal
59: * the returned {@link Iterator} does not support the optional
60: * {@link Iterator#remove() remove} method: invocation of the method will result in an
61: * {@link UnsupportedOperationException} being thrown.
62: */
63: public Iterator<T> iterator() {
64: return iterator;
65: }
66:
67: private class EnumerationIterator<E> implements Iterator<E> {
68:
69: private final Enumeration<E> enumeration;
70:
71: private EnumerationIterator(Enumeration<E> enumeration) {
72: this.enumeration = enumeration;
73: }
74:
75: public boolean hasNext() {
76: return this.enumeration.hasMoreElements();
77: }
78:
79: public E next() {
80: return this.enumeration.nextElement();
81: }
82:
83: public void remove() {
84: throw new UnsupportedOperationException();
85:
86: }
87: }
88: }