
package net.sf.plugfy.util;

import java.lang.reflect.Method;

/**
 * utility methods
 *
 * @author hendrik
 */
public class Util {

    private Util() {
        // hide constructor
    }

    /**
     * finds the first element in an array which meets a condition
     *
     * @param list list
     * @param predicate predicate
     * @return first matching element
     */
    public static <T> T find(T[] list, Predicate<T> predicate) {
        for (T t : list) {
            if (predicate.apply(t)) {
                return t;
            }
        }
        return null;
    }

    /**
     * calls an optional method
     *
     * @param object object to call the method on
     * @param methodName name of method to call
     */
    public static void callOptionalMethod(Object object, String methodName) {
        try {
            Method m = object.getClass().getMethod(methodName);
            m.invoke(object);
        } catch (Exception e) {
            // ignore
        }
    }
}
