Enumeration as Iterable - Java java.util

Java examples for java.util:Enumeration

Description

Enumeration as Iterable

Demo Code


//package com.java2s;

import java.util.Enumeration;

import java.util.Iterator;

public class Main {
    public static <T> Iterable<T> asIterable(final Enumeration<T> e) {
        if (e == null)
            return null;
        return new Iterable<T>() {
            @Override/*from ww  w.  j av a 2s .  c o  m*/
            public Iterator<T> iterator() {
                return new Iterator<T>() {
                    @Override
                    public boolean hasNext() {
                        return e.hasMoreElements();
                    }

                    @SuppressWarnings({ "IteratorNextCanNotThrowNoSuchElementException" })
                    @Override
                    public T next() {
                        return e.nextElement();
                    }

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

Related Tutorials