package org.zwobble.mammoth.internal.util;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.Iterables;
import com.google.common.collect.Ordering;

import java.util.List;
import java.util.Optional;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;

import static org.zwobble.mammoth.internal.util.MammothIterables.stream;

public class MammothLists {
    public static <T> List<T> list() {
        return ImmutableList.of();
    }

    public static <T> List<T> list(T value1) {
        return ImmutableList.of(value1);
    }

    public static <T> List<T> list(T value1, T value2) {
        return ImmutableList.of(value1, value2);
    }

    public static <T> List<T> list(T value1, T value2, T value3) {
        return ImmutableList.of(value1, value2, value3);
    }

    public static <T> List<T> cons(T head, Iterable<T> tail) {
        return eagerConcat(list(head), tail);
    }

    public static <T> List<T> eagerConcat(Iterable<T> first, Iterable<T> second) {
        return ImmutableList.copyOf(Iterables.concat(first, second));
    }

    public static <T> List<T> eagerFilter(Iterable<T> iterable, Predicate<T> predicate) {
        return stream(iterable).filter(predicate).collect(Collectors.toList());
    }

    public static <T, R> List<R> eagerMap(Iterable<T> iterable, Function<T, R> function) {
        return stream(iterable).map(function).collect(Collectors.toList());
    }

    public static <T, R> List<R> eagerFlatMap(Iterable<T> iterable, Function<T, Iterable<R>> function) {
        return stream(iterable)
            .flatMap(element -> stream(function.apply(element)))
            .collect(Collectors.toList());
    }

    public static <T, R extends Comparable<R>> List<T> orderedBy(Iterable<T> iterable, Function<T, R> getKey) {
        return orderBy(getKey).sortedCopy(iterable);
    }

    private static <T, R extends Comparable<R>> Ordering<T> orderBy(Function<T, R> getKey) {
        return Ordering.from((first, second) -> getKey.apply(first).compareTo(getKey.apply(second)));
    }

    public static <T> List<T> toList(Iterable<T> iterable) {
        return stream(iterable).collect(Collectors.toList());
    }

    public static <T> Optional<T> tryGetLast(List<T> list) {
        if (list.isEmpty()) {
            return Optional.empty();
        } else {
            return Optional.of(list.get(list.size() - 1));
        }
    }

}

@@ -89,52 +89,8 @@
 st;%0A
-import com.google.common.collect.Iterables;%0A
 impo
@@ -287,16 +287,48 @@
 lectors;
+%0Aimport java.util.stream.Stream;
 %0A%0Aimport
@@ -1061,59 +1061,79 @@
 urn 
-ImmutableList.copyOf(Iterables.concat
+Stream.concat(stream
 (first
+)
 , s
-econd
+tream(second)).collect(Collectors.toList(
 ));%0A

Use streams to implement eagerConcat