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 *      http://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 */
017package org.apache.commons.collections4;
018
019import java.util.ArrayList;
020import java.util.Enumeration;
021import java.util.List;
022import java.util.Set;
023import java.util.StringTokenizer;
024
025import org.apache.commons.collections4.iterators.EnumerationIterator;
026import org.apache.commons.collections4.iterators.IteratorIterable;
027
028/**
029 * Provides utility methods for {@link Enumeration} instances.
030 *
031 * @since 3.0
032 */
033public class EnumerationUtils {
034
035    /**
036     * Creates an {@link Iterable} that wraps an {@link Enumeration}. The returned {@link Iterable} can be used for a
037     * 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     * @since 4.5.0-M1
043     */
044    public static <T> Iterable<T> asIterable(final Enumeration<T> enumeration) {
045        return new IteratorIterable<>(new EnumerationIterator<>(enumeration));
046    }
047
048    /**
049     * Gets the {@code index}-th value in the {@link Enumeration}, throwing
050     * {@code IndexOutOfBoundsException} if there is no such element.
051     * <p>
052     * The Enumeration is advanced to {@code index} (or to the end, if
053     * {@code index} exceeds the number of entries) as a side effect of this method.
054     * </p>
055     *
056     * @param e  the enumeration to get a value from
057     * @param index  the index to get
058     * @param <T> the type of object in the {@link Enumeration}
059     * @return the object at the specified index
060     * @throws IndexOutOfBoundsException if the index is invalid
061     * @throws IllegalArgumentException if the object type is invalid
062     * @since 4.1
063     */
064    public static <T> T get(final Enumeration<T> e, final int index) {
065        CollectionUtils.checkIndexBounds(index);
066        int i = index;
067        while (e.hasMoreElements()) {
068            i--;
069            if (i == -1) {
070                return e.nextElement();
071            }
072            e.nextElement();
073        }
074        throw new IndexOutOfBoundsException("Entry does not exist: " + i);
075    }
076
077    /**
078     * Creates a list based on an enumeration.
079     *
080     * <p>As the enumeration is traversed, an ArrayList of its values is
081     * created. The new list is returned.</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;
094     * for the sake of backward compatibility.
095     *
096     * @param stringTokenizer  the tokenizer to convert to a {@link List}&lt;{@link String}&gt;
097     * @return a list containing all tokens of the given StringTokenizer
098     */
099    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}