Functions { @NonNull @SuppressWarnings({"unchecked", "UnusedParameters"}) public static <F> FList<F, List<F>, List<F>> functionFromListOf( @Nullable final Class<F> from) { return functionCompiler(); } private  Functions(); @NonNull static Function<F, T> staticFunction(@NonNull final T object); @NonNull static Function<F, T> supplierAsFunction(
      @NonNull final Supplier<? extends T> supplier); @NonNull static Function<T, T> identityFunction(); @NonNull @SuppressWarnings({"unchecked", "UnusedParameters"}) static FItem<F, F> functionFrom(@Nullable Class<F> from); @NonNull @SuppressWarnings({"unchecked", "UnusedParameters"}) static FList<F, List<F>, List<F>> functionFromListOf(
      @Nullable final Class<F> from); @SuppressWarnings("unchecked") @NonNull static Function<Throwable, Result<T>> failedResult(); @SuppressWarnings("unchecked") @NonNull static Function<Result<T>, List<T>> resultAsList(); @SuppressWarnings("unchecked") @NonNull static Function<T, List<T>> itemAsList(); @SuppressWarnings("unchecked") @NonNull static Function<Result<List<T>>, List<T>> resultListAsList();  }
@Test public void shouldCreateFunctionFromListToItem() { final Function<List<String>, Integer> function = functionFromListOf(String.class) .limit(3) .map(new StringLength()) .thenApply(new SumOfIntegersInList()); assertThat(function.apply(INPUT_LIST), is(14)); }
@Test public void shouldCreateFunctionFromListToList() { final Function<List<String>, List<Integer>> function = functionFromListOf(String.class) .limit(3) .thenMap(new StringLength()); assertThat(function.apply(INPUT_LIST), contains(4, 7, 3)); }
@Test public void shouldCreateFunctionFromListToSortedList() { final Function<List<String>, List<Integer>> function = functionFromListOf(String.class) .map(new StringLength()) .thenSort(new Comparator<Integer>() { @Override public int compare(Integer lhs, Integer rhs) { return lhs.compareTo(rhs); } }); final List<String> inputList = new ArrayList<>(INPUT_LIST); assertThat(function.apply(inputList), contains(3, 4, 7, 7)); }
@Test public void shouldNotMutateInputListWhenSorting() { final Function<List<String>, List<String>> function = functionFromListOf(String.class) .thenSort(new Comparator<String>() { @Override public int compare(String lhs, String rhs) { return lhs.compareTo(rhs); } }); final List<String> inputList = new ArrayList<>(INPUT_LIST); assertThat(function.apply(inputList), contains("for", "some", "strings", "testing")); assertThat(inputList, is(INPUT_LIST)); }
@Test public void shouldCreateFunctionFromListToListWithZeroLimit() { final Function<List<String>, List<Integer>> function = functionFromListOf(String.class) .limit(0) .thenMap(new StringLength()); assertThat(function.apply(INPUT_LIST), Matchers.<Integer>emptyIterable()); }
@Test public void shouldCreateFunctionFromListToListWithLimitLargerThanInputList() { final Function<List<String>, List<Integer>> function = functionFromListOf(String.class) .limit(INPUT_LIST.size() * 2) .thenMap(new StringLength()); assertThat(function.apply(INPUT_LIST), contains(4, 7, 3, 7)); }
@Test public void shouldReturnEmptyListForFilterOfEmptyList() { final Predicate<String> predicate = mock(Predicate.class); when(predicate.apply(anyString())).thenReturn(true); final Function<List<String>, List<String>> function = functionFromListOf(String.class) .thenFilter(predicate); assertThat(function.apply(new ArrayList<String>()), sameInstance(Collections.<String>emptyList())); }