/*
 * Copyright (c) 2009 JSCAPE
 * 1147 S. 53rd Pl., Mesa, Arizona, 85206, U.S.A.
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of
 * JSCAPE. ("Confidential Information").  You shall not disclose such
 * Confidential Information and shall use it only in accordance with
 * the terms of the license agreement you entered into with JSCAPE.
 *
 */

package com.jscape.util.reflection;

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

import java.lang.reflect.Constructor;

import static com.jscape.util.Parameters.assertNotNull;

/**
 * @author Alex Usun
 */
public class ConstructorFinder
        extends UnaryFunction<Class, Constructor> {

    private final UnaryPredicate<? super Constructor> predicate;

    public static Constructor find(Class aClass, UnaryPredicate<? super Constructor> predicate) {
        if (aClass == null) {
            return null;
        }

        return Algorithms.find(aClass.getDeclaredConstructors(), predicate);
    }

    public ConstructorFinder(UnaryPredicate<? super Constructor> predicate) {
        assertNotNull(predicate);
        this.predicate = predicate;
    }

    public Constructor evaluate(Class value) {
        return find(value, this.predicate);
    }

    @Override
    public String toString() {
        return "ConstructorFinder {"
                + "predicate=" + this.predicate
                + '}';
    }
}