package com.jscape.util.collections;

import com.jscape.util.functor.Predicate;
import com.jscape.util.functor.UnaryFunction;
import com.jscape.util.functor.UnaryProcedure;

import java.util.Collection;

/**
 * @author Alex Usun
 */
public class ForEachCollection<T, U extends UnaryProcedure<? super T>>
        extends UnaryFunction<Collection<T>, U> {

    /**
     * Procedure.
     */
    private final U procedure;
    /**
     * Predicate.
     */
    private final Predicate predicate;

    /**
     * @param procedure procedure
     * @param predicate predicate
     */
    public ForEachCollection(U procedure, Predicate predicate) {
        this.procedure = procedure;
        this.predicate = predicate;
    }

    /**
     * @param procedure procedure
     */
    public ForEachCollection(U procedure) {
        this(procedure, Predicate.TRUE);
    }

    /**
     * @param value value
     *
     * @return U
     */
    public U evaluate(Collection<T> value) {
        return Algorithms.forEach(value, this.procedure, this.predicate);
    }
}