Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
EnumIterator |
|
| 2.2;2.2 |
1 | /* | |
2 | * Copyright (C) 1998-2000 Semiotek Inc. All Rights Reserved. | |
3 | * | |
4 | * Redistribution and use in source and binary forms, with or without | |
5 | * modification, are permitted under the terms of either of the following | |
6 | * Open Source licenses: | |
7 | * | |
8 | * The GNU General Public License, version 2, or any later version, as | |
9 | * published by the Free Software Foundation | |
10 | * (http://www.fsf.org/copyleft/gpl.html); | |
11 | * | |
12 | * or | |
13 | * | |
14 | * The Semiotek Public License (http://webmacro.org/LICENSE.) | |
15 | * | |
16 | * This software is provided "as is", with NO WARRANTY, not even the | |
17 | * implied warranties of fitness to purpose, or merchantability. You | |
18 | * assume all risks and liabilities associated with its use. | |
19 | * | |
20 | * See www.webmacro.org for more information on the WebMacro project. | |
21 | */ | |
22 | ||
23 | ||
24 | package org.webmacro.util; | |
25 | ||
26 | import java.util.Enumeration; | |
27 | import java.util.Iterator; | |
28 | import java.util.NoSuchElementException; | |
29 | ||
30 | ||
31 | /** | |
32 | * Allow a Java 1.1 enumeration to be used as a JDK 1.2 style Iterator. | |
33 | */ | |
34 | final public class EnumIterator implements Iterator | |
35 | { | |
36 | ||
37 | final Enumeration wrappedEnum; | |
38 | private boolean hasNext; | |
39 | ||
40 | /** | |
41 | * Construct an iterator given an enumeration. | |
42 | */ | |
43 | public EnumIterator (Enumeration e) | |
44 | 2070 | { |
45 | 2070 | wrappedEnum = e; |
46 | 2070 | hasNext = e.hasMoreElements(); |
47 | 2070 | } |
48 | ||
49 | /** | |
50 | * @return true if we have not yet reached the end of the enumeration. | |
51 | */ | |
52 | final public boolean hasNext () | |
53 | { | |
54 | 19616 | return hasNext; |
55 | } | |
56 | ||
57 | /** | |
58 | * Advance the iterator and return the next value. Return null if we | |
59 | * reach the end of the enumeration. | |
60 | * | |
61 | * @throws NoSuchElementException | |
62 | */ | |
63 | final public Object next () | |
64 | { | |
65 | 17548 | if (!hasNext) |
66 | { | |
67 | 0 | throw new NoSuchElementException("advanced past end of list"); |
68 | } | |
69 | 17548 | Object o = wrappedEnum.nextElement(); |
70 | 17546 | hasNext = wrappedEnum.hasMoreElements(); |
71 | 17546 | return o; |
72 | } | |
73 | ||
74 | /** | |
75 | * Unsupported. | |
76 | * @throws UnsupportedOperationException | |
77 | */ | |
78 | final public void remove () | |
79 | { | |
80 | 0 | throw new UnsupportedOperationException(); |
81 | } | |
82 | ||
83 | /** | |
84 | * Test harness | |
85 | */ | |
86 | static public void main (String arg[]) | |
87 | { | |
88 | 0 | java.util.Vector v = new java.util.Vector(arg.length); |
89 | 0 | for (int i = 0; i < arg.length; i++) |
90 | { | |
91 | 0 | v.addElement(arg[i]); |
92 | } | |
93 | ||
94 | try | |
95 | { | |
96 | 0 | Iterator i = new EnumIterator(v.elements()); |
97 | 0 | while (i.hasNext()) |
98 | { | |
99 | 0 | System.out.println("item: " + i.next()); |
100 | } | |
101 | } | |
102 | 0 | catch (Exception e) |
103 | { | |
104 | 0 | e.printStackTrace(); |
105 | 0 | } |
106 | 0 | } |
107 | } | |
108 |