package org.cyclops.cyclopscore.ingredient.collection;

import org.cyclops.commoncapabilities.api.ingredient.IIngredientMatcher;

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

/**
 * An iterator that filters over an instances collection based on an instance with a match condition.
 * @param <T> The instance type.
 * @param <M> The matching condition parameter.
 */
public class FilteredIngredientCollectionIterator<T, M> implements Iterator<T> {

    private final Iterator<T> iterator;
    private final IIngredientMatcher<T, M> matcher;
    private final T instance;
    private final M matchCondition;

    private T next;

    /**
     * Create a new instance.
     * @param iterable An iterable with instances.
     * @param matcher A matcher for the type of instances.
     * @param instance An instance to match.
     * @param matchCondition A match condition to filter by.
     */
    public FilteredIngredientCollectionIterator(Iterable<T> iterable, IIngredientMatcher<T, M> matcher,
                                                T instance, M matchCondition) {
        this.iterator = iterable.iterator();
        this.matcher = matcher;
        this.instance = instance;
        this.matchCondition = matchCondition;
        this.next = null;
    }

    protected T findNext() {
        while (iterator.hasNext()) {
            T next = iterator.next();
            if (matcher.matches(instance, next, matchCondition)) {
                return next;
            }
        }
        return null;
    }

    @Override
    public boolean hasNext() {
        if (next == null) {
            this.next = findNext();
        }
        return next != null;
    }

    @Override
    public T next() {
        if (!hasNext()) {
            throw new NoSuchElementException("Tried reading a finished FilteredIngredientCollectionIterator");
        }
        T next = this.next;
        this.next = null;
        return next;
    }

    @Override
    public void remove() {
        iterator.remove();
    }
}

@@ -1107,39 +1107,551 @@
 this
-.iterator = iterable.iterator()
+(iterable.iterator(), matcher, instance, matchCondition);%0A    %7D%0A%0A    /**%0A     * Create a new instance.%0A     * @param iterator An iterator with instances.%0A     * @param matcher A matcher for the type of instances.%0A     * @param instance An instance to match.%0A     * @param matchCondition A match condition to filter by.%0A     */%0A    public FilteredIngredientCollectionIterator(Iterator%3CT%3E iterator, IIngredientMatcher%3CT, M%3E matcher,%0A                                                T instance, M matchCondition) %7B%0A        this.iterator = iterator
 ;%0A  

Add iterator-based constructor to FilteredIngredientCollectionIterator