All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.basking2.sdsai.itrex.iterators.EvaluatingIterator Maven / Gradle / Ivy

There is a newer version: 1.1.23
Show newest version
package com.github.basking2.sdsai.itrex.iterators;

import com.github.basking2.sdsai.itrex.EvaluationContext;
import com.github.basking2.sdsai.itrex.Evaluator;

import java.util.Iterator;

public class EvaluatingIterator implements Iterator {

    private Iterator itr;
    private boolean evaluationEnabled;
    private Evaluator evaluator;
    private EvaluationContext evaluationContext;

    public EvaluatingIterator(final Evaluator evaluator, final EvaluationContext evaluationContext, final Iterator itr) {
        this.evaluator = evaluator;
        this.evaluationContext = evaluationContext;
        this.itr = itr;
        this.evaluationEnabled = true;
    }

    @Override
    public boolean hasNext() {
        return itr.hasNext();
    }

    @Override
    public T next() {
        return next(evaluationContext);
    }

    @SuppressWarnings("unchecked")
    public T next(final EvaluationContext evaluationContext) {
        return evaluationEnabled ? (T)evaluator.evaluate(itr.next(), evaluationContext) : (T)itr.next();
    }

    /**
     * Set whether this iterator should map objects through its evaluator or not.
     *
     * @param evaluationEnabled Enable or disable evlaution using this iterator's {@link Evaluator}.
     */
    public void setEvaluationEnabled(final boolean evaluationEnabled) {
        this.evaluationEnabled = evaluationEnabled;
    }

    /**
     * Advance the iterator without evaluating it.
     *
     * This is useful for conditional executions, such is ["if"].
     */
    public void skip() {
        this.itr.next();
    }

    public EvaluationContext getEvaluationContext() {
        return evaluationContext;
    }
}