1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * https://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18 package org.apache.commons.collections4;
19
20 import java.util.ArrayList;
21 import java.util.Enumeration;
22 import java.util.List;
23 import java.util.Set;
24 import java.util.StringTokenizer;
25
26 import org.apache.commons.collections4.iterators.EnumerationIterator;
27 import org.apache.commons.collections4.iterators.IteratorIterable;
28
29 /**
30 * Provides utility methods for {@link Enumeration} instances.
31 *
32 * @since 3.0
33 */
34 public class EnumerationUtils {
35
36 /**
37 * Creates an {@link Iterable} that wraps an {@link Enumeration}. The returned {@link Iterable} can be used for a single iteration.
38 *
39 * @param <T> The element type.
40 * @param enumeration The enumeration to use, may not be null.
41 * @return A new, single use {@link Iterable}.
42 * @throws NullPointerException if enumeration is null.
43 * @since 4.5.0-M1
44 */
45 public static <T> Iterable<T> asIterable(final Enumeration<T> enumeration) {
46 return new IteratorIterable<>(new EnumerationIterator<>(enumeration));
47 }
48
49 /**
50 * Gets the {@code index}-th value in the {@link Enumeration}, throwing {@code IndexOutOfBoundsException} if there is no such element.
51 * <p>
52 * The Enumeration is advanced to {@code index} (or to the end, if {@code index} exceeds the number of entries) as a side effect of this method.
53 * </p>
54 *
55 * @param enumeration The enumeration to get a value from.
56 * @param index The index to get.
57 * @param <T> The type of object in the {@link Enumeration}.
58 * @return The object at the specified index.
59 * @throws IndexOutOfBoundsException if the index is invalid.
60 * @throws IllegalArgumentException if the object type is invalid.
61 * @throws NullPointerException if enumeration is null.
62 * @since 4.1
63 */
64 public static <T> T get(final Enumeration<T> enumeration, final int index) {
65 CollectionUtils.checkIndexBounds(index);
66 int i = index;
67 while (enumeration.hasMoreElements()) {
68 i--;
69 if (i == -1) {
70 return enumeration.nextElement();
71 }
72 enumeration.nextElement();
73 }
74 throw new IndexOutOfBoundsException("Entry does not exist: " + i);
75 }
76
77 /**
78 * Creates a list based on an enumeration.
79 * <p>
80 * As the enumeration is traversed, an ArrayList of its values is created. The new list is returned.
81 * </p>
82 *
83 * @param <E> The element type.
84 * @param enumeration The enumeration to traverse, which should not be {@code null}.
85 * @return A list containing all elements of the given enumeration.
86 * @throws NullPointerException if the enumeration parameter is {@code null}.
87 */
88 public static <E> List<E> toList(final Enumeration<? extends E> enumeration) {
89 return IteratorUtils.toList(new EnumerationIterator<>(enumeration));
90 }
91
92 /**
93 * Override toList(Enumeration) for StringTokenizer as it implements Enumeration<Object> for the sake of backward compatibility.
94 *
95 * @param stringTokenizer The tokenizer to convert to a {@link List}<{@link String}>
96 * @return A list containing all tokens of the given StringTokenizer.
97 */
98 public static List<String> toList(final StringTokenizer stringTokenizer) {
99 final List<String> result = new ArrayList<>(stringTokenizer.countTokens());
100 while (stringTokenizer.hasMoreTokens()) {
101 result.add(stringTokenizer.nextToken());
102 }
103 return result;
104 }
105
106 /**
107 * Creates a set based on an enumeration.
108 * <p>
109 * As the enumeration is traversed, an HashSet of its values is created. The new set is returned.
110 * </p>
111 *
112 * @param <E> The element type.
113 * @param enumeration The enumeration to traverse, which should not be {@code null}.
114 * @return A set containing all elements of the given enumeration.
115 * @throws NullPointerException if the enumeration parameter is {@code null}.
116 * @since 4.5.0-M4
117 */
118 public static <E> Set<E> toSet(final Enumeration<? extends E> enumeration) {
119 return IteratorUtils.toSet(new EnumerationIterator<>(enumeration));
120 }
121
122 /**
123 * Don't allow instances.
124 */
125 private EnumerationUtils() {
126 // no instances.
127 }
128 }