File:  [Public] / java / classes / org / w3c / util / ArrayEnumeration.java
Revision 1.4: download - view: text, annotated - select for diffs
Fri Oct 18 13:42:30 2013 UTC (11 years, 10 months ago) by ylafon
Branches: MAIN
CVS tags: HEAD
generics + raw types + serializer

// ArrayEnumeration.java
// $Id: ArrayEnumeration.java,v 1.4 2013/10/18 13:42:30 ylafon Exp $
// (c) COPYRIGHT MIT and INRIA, 1996.
// Please first read the full copyright statement in file COPYRIGHT.html

package org.w3c.util;

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

/**
 * Iterates through array skipping nulls.
 */
public class ArrayEnumeration<V> implements Enumeration<V>, Iterator<V> {
    private int nelems;
    private int elemCount;
    private int arrayIdx;
    private V[] array;

    public ArrayEnumeration(V[] array) {
        this(array, array.length);
    }

    public ArrayEnumeration(V[] array, int nelems) {
        arrayIdx = elemCount = 0;
        this.nelems = nelems;
        this.array = array;
    }

    public final boolean hasMoreElements() {
        return elemCount < nelems;
    }

    public final V nextElement() {
        while (array[arrayIdx] == null && arrayIdx < array.length)
            arrayIdx++;

        if (arrayIdx >= array.length)
            throw new NoSuchElementException();

        elemCount++;
        return array[arrayIdx++];
    }

    public final boolean hasNext() {
        return elemCount < nelems;
    }

    public V next() {
        return nextElement();
    }

    public void remove() {
        // do nothing
    }
}

Webmaster