EnumerationUtils.java

  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. import java.util.ArrayList;
  19. import java.util.Enumeration;
  20. import java.util.List;
  21. import java.util.Set;
  22. import java.util.StringTokenizer;

  23. import org.apache.commons.collections4.iterators.EnumerationIterator;
  24. import org.apache.commons.collections4.iterators.IteratorIterable;

  25. /**
  26.  * Provides utility methods for {@link Enumeration} instances.
  27.  *
  28.  * @since 3.0
  29.  */
  30. public class EnumerationUtils {

  31.     /**
  32.      * Creates an {@link Iterable} that wraps an {@link Enumeration}. The returned {@link Iterable} can be used for a
  33.      * single iteration.
  34.      *
  35.      * @param <T> the element type
  36.      * @param enumeration the enumeration to use, may not be null
  37.      * @return a new, single use {@link Iterable}
  38.      * @since 4.5.0-M1
  39.      */
  40.     public static <T> Iterable<T> asIterable(final Enumeration<T> enumeration) {
  41.         return new IteratorIterable<>(new EnumerationIterator<>(enumeration));
  42.     }

  43.     /**
  44.      * Gets the {@code index}-th value in the {@link Enumeration}, throwing
  45.      * {@code IndexOutOfBoundsException} if there is no such element.
  46.      * <p>
  47.      * The Enumeration is advanced to {@code index} (or to the end, if
  48.      * {@code index} exceeds the number of entries) as a side effect of this method.
  49.      * </p>
  50.      *
  51.      * @param e  the enumeration to get a value from
  52.      * @param index  the index to get
  53.      * @param <T> the type of object in the {@link Enumeration}
  54.      * @return the object at the specified index
  55.      * @throws IndexOutOfBoundsException if the index is invalid
  56.      * @throws IllegalArgumentException if the object type is invalid
  57.      * @since 4.1
  58.      */
  59.     public static <T> T get(final Enumeration<T> e, final int index) {
  60.         CollectionUtils.checkIndexBounds(index);
  61.         int i = index;
  62.         while (e.hasMoreElements()) {
  63.             i--;
  64.             if (i == -1) {
  65.                 return e.nextElement();
  66.             }
  67.             e.nextElement();
  68.         }
  69.         throw new IndexOutOfBoundsException("Entry does not exist: " + i);
  70.     }

  71.     /**
  72.      * Creates a list based on an enumeration.
  73.      *
  74.      * <p>As the enumeration is traversed, an ArrayList of its values is
  75.      * created. The new list is returned.</p>
  76.      *
  77.      * @param <E> the element type
  78.      * @param enumeration  the enumeration to traverse, which should not be {@code null}.
  79.      * @return a list containing all elements of the given enumeration
  80.      * @throws NullPointerException if the enumeration parameter is {@code null}.
  81.      */
  82.     public static <E> List<E> toList(final Enumeration<? extends E> enumeration) {
  83.         return IteratorUtils.toList(new EnumerationIterator<>(enumeration));
  84.     }

  85.     /**
  86.      * Override toList(Enumeration) for StringTokenizer as it implements Enumeration&lt;Object&gt;
  87.      * for the sake of backward compatibility.
  88.      *
  89.      * @param stringTokenizer  the tokenizer to convert to a {@link List}&lt;{@link String}&gt;
  90.      * @return a list containing all tokens of the given StringTokenizer
  91.      */
  92.     public static List<String> toList(final StringTokenizer stringTokenizer) {
  93.         final List<String> result = new ArrayList<>(stringTokenizer.countTokens());
  94.         while (stringTokenizer.hasMoreTokens()) {
  95.             result.add(stringTokenizer.nextToken());
  96.         }
  97.         return result;
  98.     }

  99.     /**
  100.      * Creates a set based on an enumeration.
  101.      *
  102.      * <p>As the enumeration is traversed, an HashSet of its values is
  103.      * created. The new set is returned.</p>
  104.      *
  105.      * @param <E> the element type
  106.      * @param enumeration  the enumeration to traverse, which should not be {@code null}.
  107.      * @return a set containing all elements of the given enumeration.
  108.      * @throws NullPointerException if the enumeration parameter is {@code null}.
  109.      * @since 4.5.0-M4
  110.      */
  111.     public static <E> Set<E> toSet(final Enumeration<? extends E> enumeration) {
  112.         return IteratorUtils.toSet(new EnumerationIterator<>(enumeration));
  113.     }

  114.     /**
  115.      * Don't allow instances.
  116.      */
  117.     private EnumerationUtils() {
  118.         // no instances.
  119.     }

  120. }