    001/*
    002 * Licensed to the Apache Software Foundation (ASF) under one or more
    003 * contributor license agreements.  See the NOTICE file distributed with
    004 * this work for additional information regarding copyright ownership.
    005 * The ASF licenses this file to You under the Apache License, Version 2.0
    006 * (the "License"); you may not use this file except in compliance with
    007 * the License.  You may obtain a copy of the License at
    008 *
    009 *      https://www.apache.org/licenses/LICENSE-2.0
    010 *
    011 * Unless required by applicable law or agreed to in writing, software
    012 * distributed under the License is distributed on an "AS IS" BASIS,
    013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    014 * See the License for the specific language governing permissions and
    015 * limitations under the License.
    016 */
    017
    018package org.apache.commons.collections4;
    019
    020import java.util.ArrayList;
    021import java.util.Enumeration;
    022import java.util.List;
    023import java.util.Set;
    024import java.util.StringTokenizer;
    025
    026import org.apache.commons.collections4.iterators.EnumerationIterator;
    027import org.apache.commons.collections4.iterators.IteratorIterable;
    028
    029/**
    030 * Provides utility methods for {@link Enumeration} instances.
    031 *
    032 * @since 3.0
    033 */
    034public class EnumerationUtils {
    035
    036    /**
    037     * Creates an {@link Iterable} that wraps an {@link Enumeration}. The returned {@link Iterable} can be used for a single iteration.
    038     *
    039     * @param <T>         The element type.
    040     * @param enumeration The enumeration to use, may not be null.
    041     * @return A new, single use {@link Iterable}.
    042     * @throws NullPointerException if enumeration is null.
    043     * @since 4.5.0-M1
    044     */
    045    public static <T> Iterable<T> asIterable(final Enumeration<T> enumeration) {
    046        return new IteratorIterable<>(new EnumerationIterator<>(enumeration));
    047    }
    048
    049    /**
    050     * Gets the {@code index}-th value in the {@link Enumeration}, throwing {@code IndexOutOfBoundsException} if there is no such element.
    051     * <p>
    052     * 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.
    053     * </p>
    054     *
    055     * @param enumeration     The enumeration to get a value from.
    056     * @param index The index to get.
    057     * @param <T>   The type of object in the {@link Enumeration}.
    058     * @return The object at the specified index.
    059     * @throws IndexOutOfBoundsException if the index is invalid.
    060     * @throws IllegalArgumentException  if the object type is invalid.
    061     * @throws NullPointerException      if enumeration is null.
    062     * @since 4.1
    063     */
    064    public static <T> T get(final Enumeration<T> enumeration, final int index) {
    065        CollectionUtils.checkIndexBounds(index);
    066        int i = index;
    067        while (enumeration.hasMoreElements()) {
    068            i--;
    069            if (i == -1) {
    070                return enumeration.nextElement();
    071            }
    072            enumeration.nextElement();
    073        }
    074        throw new IndexOutOfBoundsException("Entry does not exist: " + i);
    075    }
    076
    077    /**
    078     * Creates a list based on an enumeration.
    079     * <p>
    080     * As the enumeration is traversed, an ArrayList of its values is created. The new list is returned.
    081     * </p>
    082     *
    083     * @param <E>         The element type.
    084     * @param enumeration The enumeration to traverse, which should not be {@code null}.
    085     * @return A list containing all elements of the given enumeration.
    086     * @throws NullPointerException if the enumeration parameter is {@code null}.
    087     */
    088    public static <E> List<E> toList(final Enumeration<? extends E> enumeration) {
    089        return IteratorUtils.toList(new EnumerationIterator<>(enumeration));
    090    }
    091
    092    /**
    093     * Override toList(Enumeration) for StringTokenizer as it implements Enumeration&lt;Object&gt; for the sake of backward compatibility.
    094     *
    095     * @param stringTokenizer The tokenizer to convert to a {@link List}&lt;{@link String}&gt;
    096     * @return A list containing all tokens of the given StringTokenizer.
    097     */
    098    public static List<String> toList(final StringTokenizer stringTokenizer) {
    099        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}