001package jmri.util;
002
003import java.util.Enumeration;
004import java.util.Iterator;
005
006/**
007 * Provide an {@link Iterable} interface for an Enumeration
008 * <p>
009 * From
010 * <a href="http://www.javaspecialists.eu/archive/Issue107.html">JavaSpecialists
011 * issue 107</a>
012 *
013 * @author Randall Wood
014 * @param <T> the supported type
015 */
016public class IterableEnumeration<T> implements Iterable<T> {
017
018    private final Enumeration<T> en;
019
020    public IterableEnumeration(Enumeration<T> en) {
021        this.en = en;
022    }
023
024    @Override
025    public Iterator<T> iterator() {
026        return new Iterator<T>() {
027
028            @Override
029            public boolean hasNext() {
030                return en.hasMoreElements();
031            }
032
033            @Override
034            public T next() {
035                return en.nextElement();
036            }
037
038            @Override
039            public void remove() {
040                throw new UnsupportedOperationException();
041            }
042        };
043    }
044
045    public static <T> Iterable<T> make(Enumeration<T> en) {
046        return new IterableEnumeration<>(en);
047    }
048}