package com.jscape.util;

import java.util.Enumeration;
import java.util.Iterator;

/**
 * @author Alex Usun
 */
public class IterableEnumeration<T>
        implements Iterable<T> {

    private final Enumeration<T> base;

    @SuppressWarnings("unchecked")
    public static <T> Iterable<T> iterable(Enumeration base) {
        return new IterableEnumeration<T>(base);
    }

    public IterableEnumeration(Enumeration<T> base) {
        Assert.notNull(base);
        this.base = base;
    }

    @Override
    public String toString() {
        return "IterableEnumeration {"
                + "base=" + this.base
                + '}';
    }

    public Iterator<T> iterator() {
        return new Iterator<T>() {

            public boolean hasNext() {
                return base.hasMoreElements();
            }

            public T next() {
                return base.nextElement();
            }

            public void remove() {
                throw new UnsupportedOperationException();
            }
        };
    }

}
