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.StringTokenizer; 023 024import org.apache.commons.collections4.iterators.EnumerationIterator; 025 026/** 027 * Provides utility methods for {@link Enumeration} instances. 028 * 029 * @since 3.0 030 * @version $Id: EnumerationUtils.html 972421 2015-11-14 20:00:04Z tn $ 031 */ 032public class EnumerationUtils { 033 034 /** 035 * EnumerationUtils is not normally instantiated. 036 */ 037 private EnumerationUtils() {} 038 039 /** 040 * Creates a list based on an enumeration. 041 * 042 * <p>As the enumeration is traversed, an ArrayList of its values is 043 * created. The new list is returned.</p> 044 * 045 * @param <E> the element type 046 * @param enumeration the enumeration to traverse, which should not be <code>null</code>. 047 * @return a list containing all elements of the given enumeration 048 * @throws NullPointerException if the enumeration parameter is <code>null</code>. 049 */ 050 public static <E> List<E> toList(final Enumeration<? extends E> enumeration) { 051 return IteratorUtils.toList(new EnumerationIterator<E>(enumeration)); 052 } 053 054 /** 055 * Override toList(Enumeration) for StringTokenizer as it implements Enumeration<Object> 056 * for the sake of backward compatibility. 057 * 058 * @param stringTokenizer the tokenizer to convert to a {@link List}<{@link String}> 059 * @return a list containing all tokens of the given StringTokenizer 060 */ 061 public static List<String> toList(final StringTokenizer stringTokenizer) { 062 final List<String> result = new ArrayList<String>(stringTokenizer.countTokens()); 063 while (stringTokenizer.hasMoreTokens()) { 064 result.add(stringTokenizer.nextToken()); 065 } 066 return result; 067 } 068}