package com.jscape.util.collections;

import com.jscape.util.functor.UnaryFunction;
import com.jscape.util.functor.UnaryPredicate;

import java.util.Collection;

/**
 * @author Alex Usun
 */
public class FindCollection<T>
        extends UnaryFunction<Collection<T>, T> {

    /**
     * Predicate.
     */
    private final UnaryPredicate<? super T> predicate;
    /**
     * Default value.
     */
    private final T defaultValue;

    /**
     * @param predicate    predicate
     * @param defaultValue defaultValue
     */
    public FindCollection(UnaryPredicate<? super T> predicate, T defaultValue) {
        this.predicate = predicate;
        this.defaultValue = defaultValue;
    }

    /**
     * @param predicate predicate
     */
    public FindCollection(UnaryPredicate<? super T> predicate) {
        this(predicate, null);
    }

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