1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
   | class IterableEnumeration<T> implements Iterable<T>, Iterator<T> {
 
    private final Enumeration<T> enumeration;
 
    public IterableEnumeration(Enumeration<T> e) {
        this.enumeration = e;
    }
 
    public Iterator<T> iterator() {
        // Attention : contrairement à un vrai Iterable,
        // rappeller cette méthode ne renvoit pas un nouvel Iterator
        return this;
    }
 
    public boolean hasNext() {
        return IterableEnumeration.this.enumeration.hasMoreElements();
    }
 
    public T next() {
        return IterableEnumeration.this.enumeration.nextElement();
    }
 
    public void remove() {
        throw new UnsupportedOperationException("remove() not supported by Enumeration");
    }
 
} | 
Partager