/*
 * @(#)FilteredFigureEnumerator.java 5.1
 *
 */

package CH.ifa.draw.standard;

import java.util.NoSuchElementException;
import java.util.function.Predicate;

import de.renew.draw.storables.ontology.Figure;
import de.renew.draw.storables.ontology.FigureEnumeration;
import de.renew.draw.storables.ontology.FigureFilter;


/**
 * An Enumeration for a Vector of Figures.
 * @deprecated This class is not to be used externally. Please use the method {@link de.renew.draw.storables.api.StorableApi#createFilteredFigureEnumerator(FigureEnumeration, Predicate)} instead.
 */
@Deprecated(since = "5.0", forRemoval = true)
public final class FilteredFigureEnumerator implements FigureEnumeration {
    final FigureEnumeration _enumeration;
    final FigureFilter _filter;
    private Figure _head;


    /**
     * Constructs a FilteredFigureEnumerator that yields only figures matching the filter.
     * @param enumeration source enumeration
     * @param filter filter condition
     * @deprecated This method is not to be used externally. Please use the method {@link de.renew.draw.storables.api.StorableApi#createFilteredFigureEnumerator(FigureEnumeration, Predicate)} instead.
     */
    @Deprecated
    public FilteredFigureEnumerator(FigureEnumeration enumeration, FigureFilter filter) {
        _enumeration = enumeration;
        _filter = filter;
        next();
    }

    private void next() {
        while (_enumeration.hasMoreElements()) {
            _head = _enumeration.nextFigure();
            if (_filter.isAcceptedFigure(_head)) {
                return;
            }
        }
        _head = null;
    }

    /**
     * Returns true if the enumeration contains more elements; false
     * if it is empty.
     */
    @Override
    public boolean hasMoreElements() {
        return _head != null;
    }

    /**
     * Returns the next element of the enumeration. Calls to this
     * method will enumerate successive elements.
     * @exception NoSuchElementException If no more elements exist.
     */
    @Override
    public Figure nextElement() {
        return nextFigure();
    }

    /**
     * Returns the next element of the enumeration. Calls to this
     * method will enumerate successive elements.
     * @exception NoSuchElementException If no more elements exist.
     */
    @Override
    public Figure nextFigure() {
        Figure last = _head;
        next();
        return last;
    }
}