01: /*
02: * Converts an iterator to an enumerator.
03: * Copyright (C) 2004-2006 Stephen Ostermiller
04: * http://ostermiller.org/contact.pl?regarding=Java+Utilities
05: * Copyright (C) 2006 Jonathan Faivre-Vuillin
06: * public dot lp at free dot fr
07: *
08: * This program is free software; you can redistribute it and/or modify
09: * it under the terms of the GNU General Public License as published by
10: * the Free Software Foundation; either version 2 of the License, or
11: * (at your option) any later version.
12: *
13: * This program is distributed in the hope that it will be useful,
14: * but WITHOUT ANY WARRANTY; without even the implied warranty of
15: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16: * GNU General Public License for more details.
17: *
18: * See COPYING.TXT for details.
19: */
20:
21: package com.Ostermiller.util;
22:
23: import java.util.*;
24:
25: /**
26: * Converts an iterator to an enumerator.
27: * <p>
28: * More information about this class is available from <a target="_top" href=
29: * "http://ostermiller.org/utils/Iterator_Enumeration.html">ostermiller.org</a>.
30: *
31: * @author Stephen Ostermiller http://ostermiller.org/contact.pl?regarding=Java+Utilities
32: * @param <ElementType> Type of element being enumerated
33: * @since ostermillerutils 1.03.00
34: */
35: public class IteratorEnumeration<ElementType> implements
36: Enumeration<ElementType> {
37:
38: /**
39: * Iterator being converted to enumeration.
40: */
41: private Iterator<ElementType> iterator;
42:
43: /**
44: * Create an Enumeration from an Iterator.
45: *
46: * @param iterator Iterator to convert to an enumeration.
47: *
48: * @since ostermillerutils 1.03.00
49: */
50: public IteratorEnumeration(Iterator<ElementType> iterator) {
51: this .iterator = iterator;
52: }
53:
54: /**
55: * Tests if this enumeration contains more elements.
56: *
57: * @return true if and only if this enumeration object contains at least
58: * one more element to provide; false otherwise.
59: *
60: * @since ostermillerutils 1.03.00
61: */
62: public boolean hasMoreElements() {
63: return iterator.hasNext();
64: }
65:
66: /**
67: * Returns the next element of this enumeration if this enumeration
68: * object has at least one more element to provide.
69: *
70: * @return the next element of this enumeration.
71: * @throws NoSuchElementException if no more elements exist.
72: *
73: * @since ostermillerutils 1.03.00
74: */
75: public ElementType nextElement() throws NoSuchElementException {
76: return iterator.next();
77: }
78: }
|