package net.squanchy.support.lang;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public final class Lists {

    public static <T, R> List<R> map(List<T> list, Func1<T, R> function) {
        if (list == null || list.isEmpty()) {
            return Collections.emptyList();
        }

        List<R> result = new ArrayList<>(list.size());
        for (T t : list) {
            result.add(function.call(t));
        }

        return result;
    }

    public static <T> List<T> filter(List<T> list, Predicate<T> predicate) {
        if (list == null || list.isEmpty()) {
            return Collections.emptyList();
        }

        List<T> result = new ArrayList<>(list.size());
        for (T t : list) {
            if (predicate.call(t)) {
                result.add(t);
            }
        }

        return result;
    }

    public static <T> Optional<T> find(List<T> list, Func1<T, Boolean> predicate) {
        for (T t : list) {
            if (predicate.call(t)) {
                return Optional.of(t);
            }
        }

        return Optional.absent();
    }

    public static <T, R> R reduce(R initial, List<T> list, Func2<R, T, R> reducer) {
        R reducedValue = initial;
        for (T t : list) {
            reducedValue = reducer.call(reducedValue, t);
        }

        return reducedValue;
    }

    public static <T> boolean any(T[] array, Predicate<T> predicate) {
        return any(Arrays.asList(array), predicate);
    }

    public static <T> boolean any(List<T> list, Predicate<T> predicate) {
        for (T t : list) {
            if (predicate.call(t)) {
                return true;
            }
        }

        return false;
    }
}

@@ -1742,11 +1742,229 @@
 e;%0A    %7D
+%0A%0A    public static %3CT%3E boolean all(T%5B%5D array, Predicate%3CT%3E predicate) %7B%0A        for (T t : array)%7B%0A            if (!predicate.call(t))%7B%0A                return false;%0A            %7D%0A        %7D%0A%0A        return true;%0A    %7D
 %0A%7D%0A

add the all utility method for arrays