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.Set;
23  import java.util.StringTokenizer;
24  
25  import org.apache.commons.collections4.iterators.EnumerationIterator;
26  import org.apache.commons.collections4.iterators.IteratorIterable;
27  
28  /**
29   * Provides utility methods for {@link Enumeration} instances.
30   *
31   * @since 3.0
32   */
33  public class EnumerationUtils {
34  
35      /**
36       * Creates an {@link Iterable} that wraps an {@link Enumeration}. The returned {@link Iterable} can be used for a
37       * 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       * @since 4.5.0-M1
43       */
44      public static <T> Iterable<T> asIterable(final Enumeration<T> enumeration) {
45          return new IteratorIterable<>(new EnumerationIterator<>(enumeration));
46      }
47  
48      /**
49       * Gets the {@code index}-th value in the {@link Enumeration}, throwing
50       * {@code IndexOutOfBoundsException} if there is no such element.
51       * <p>
52       * The Enumeration is advanced to {@code index} (or to the end, if
53       * {@code index} exceeds the number of entries) as a side effect of this method.
54       * </p>
55       *
56       * @param e  the enumeration to get a value from
57       * @param index  the index to get
58       * @param <T> the type of object in the {@link Enumeration}
59       * @return the object at the specified index
60       * @throws IndexOutOfBoundsException if the index is invalid
61       * @throws IllegalArgumentException if the object type is invalid
62       * @since 4.1
63       */
64      public static <T> T get(final Enumeration<T> e, final int index) {
65          CollectionUtils.checkIndexBounds(index);
66          int i = index;
67          while (e.hasMoreElements()) {
68              i--;
69              if (i == -1) {
70                  return e.nextElement();
71              }
72              e.nextElement();
73          }
74          throw new IndexOutOfBoundsException("Entry does not exist: " + i);
75      }
76  
77      /**
78       * Creates a list based on an enumeration.
79       *
80       * <p>As the enumeration is traversed, an ArrayList of its values is
81       * created. The new list is returned.</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&lt;Object&gt;
94       * for the sake of backward compatibility.
95       *
96       * @param stringTokenizer  the tokenizer to convert to a {@link List}&lt;{@link String}&gt;
97       * @return a list containing all tokens of the given StringTokenizer
98       */
99      public static List<String> toList(final StringTokenizer stringTokenizer) {
100         final List<String> result = new ArrayList<>(stringTokenizer.countTokens());
101         while (stringTokenizer.hasMoreTokens()) {
102             result.add(stringTokenizer.nextToken());
103         }
104         return result;
105     }
106 
107     /**
108      * Creates a set based on an enumeration.
109      *
110      * <p>As the enumeration is traversed, an HashSet of its values is
111      * created. The new set is returned.</p>
112      *
113      * @param <E> the element type
114      * @param enumeration  the enumeration to traverse, which should not be {@code null}.
115      * @return a set containing all elements of the given enumeration.
116      * @throws NullPointerException if the enumeration parameter is {@code null}.
117      * @since 4.5.0-M4
118      */
119     public static <E> Set<E> toSet(final Enumeration<? extends E> enumeration) {
120         return IteratorUtils.toSet(new EnumerationIterator<>(enumeration));
121     }
122 
123     /**
124      * Don't allow instances.
125      */
126     private EnumerationUtils() {
127         // no instances.
128     }
129 
130 }