View Javadoc
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.0-M1
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       * </p>
54       *
55       * @param e  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       * @since 4.1
62       */
63      public static <T> T get(final Enumeration<T> e, final int index) {
64          CollectionUtils.checkIndexBounds(index);
65          int i = index;
66          while (e.hasMoreElements()) {
67              i--;
68              if (i == -1) {
69                  return e.nextElement();
70              }
71              e.nextElement();
72          }
73          throw new IndexOutOfBoundsException("Entry does not exist: " + i);
74      }
75  
76      /**
77       * Creates a list based on an enumeration.
78       *
79       * <p>As the enumeration is traversed, an ArrayList of its values is
80       * created. The new list is returned.</p>
81       *
82       * @param <E> the element type
83       * @param enumeration  the enumeration to traverse, which should not be {@code null}.
84       * @return a list containing all elements of the given enumeration
85       * @throws NullPointerException if the enumeration parameter is {@code null}.
86       */
87      public static <E> List<E> toList(final Enumeration<? extends E> enumeration) {
88          return IteratorUtils.toList(new EnumerationIterator<>(enumeration));
89      }
90  
91      /**
92       * Override toList(Enumeration) for StringTokenizer as it implements Enumeration&lt;Object&gt;
93       * for the sake of backward compatibility.
94       *
95       * @param stringTokenizer  the tokenizer to convert to a {@link List}&lt;{@link String}&gt;
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      * Don't allow instances.
108      */
109     private EnumerationUtils() {
110         // no instances.
111     }
112 
113 }