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 * http://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 package org.apache.commons.collections4;
18
19 import java.util.ArrayList;
20 import java.util.Enumeration;
21 import java.util.List;
22 import java.util.StringTokenizer;
23
24 import org.apache.commons.collections4.iterators.EnumerationIterator;
25 import org.apache.commons.collections4.iterators.IteratorIterable;
26
27 /**
28 * Provides utility methods for {@link Enumeration} instances.
29 *
30 * @since 3.0
31 */
32 public class EnumerationUtils {
33
34 /**
35 * Creates an {@link Iterable} that wraps an {@link Enumeration}. The returned {@link Iterable} can be used for a
36 * single iteration.
37 *
38 * @param <T> the element type
39 * @param enumeration the enumeration to use, may not be null
40 * @return a new, single use {@link Iterable}
41 * @since 4.5
42 */
43 public static <T> Iterable<T> asIterable(final Enumeration<T> enumeration) {
44 return new IteratorIterable<>(new EnumerationIterator<>(enumeration));
45 }
46
47 /**
48 * Returns the {@code index}-th value in the {@link Enumeration}, throwing
49 * {@code IndexOutOfBoundsException} if there is no such element.
50 * <p>
51 * The Enumeration is advanced to {@code index} (or to the end, if
52 * {@code index} exceeds the number of entries) as a side effect of this method.
53 *
54 * @param e the enumeration to get a value from
55 * @param index the index to get
56 * @param <T> the type of object in the {@link Enumeration}
57 * @return the object at the specified index
58 * @throws IndexOutOfBoundsException if the index is invalid
59 * @throws IllegalArgumentException if the object type is invalid
60 * @since 4.1
61 */
62 public static <T> T get(final Enumeration<T> e, final int index) {
63 CollectionUtils.checkIndexBounds(index);
64 int i = index;
65 while (e.hasMoreElements()) {
66 i--;
67 if (i == -1) {
68 return e.nextElement();
69 }
70 e.nextElement();
71 }
72 throw new IndexOutOfBoundsException("Entry does not exist: " + i);
73 }
74
75 /**
76 * Creates a list based on an enumeration.
77 *
78 * <p>As the enumeration is traversed, an ArrayList of its values is
79 * created. The new list is returned.</p>
80 *
81 * @param <E> the element type
82 * @param enumeration the enumeration to traverse, which should not be {@code null}.
83 * @return a list containing all elements of the given enumeration
84 * @throws NullPointerException if the enumeration parameter is {@code null}.
85 */
86 public static <E> List<E> toList(final Enumeration<? extends E> enumeration) {
87 return IteratorUtils.toList(new EnumerationIterator<>(enumeration));
88 }
89
90 /**
91 * Override toList(Enumeration) for StringTokenizer as it implements Enumeration<Object>
92 * for the sake of backward compatibility.
93 *
94 * @param stringTokenizer the tokenizer to convert to a {@link List}<{@link String}>
95 * @return a list containing all tokens of the given StringTokenizer
96 */
97 public static List<String> toList(final StringTokenizer stringTokenizer) {
98 final List<String> result = new ArrayList<>(stringTokenizer.countTokens());
99 while (stringTokenizer.hasMoreTokens()) {
100 result.add(stringTokenizer.nextToken());
101 }
102 return result;
103 }
104
105 /**
106 * Don't allow instances.
107 */
108 private EnumerationUtils() {
109 // no instances.
110 }
111
112 }