Skip to content

Instantly share code, notes, and snippets.

@denov
Created May 24, 2016 21:33
Show Gist options
  • Save denov/a7eac36a3cda041f8afeabcef09d16fc to your computer and use it in GitHub Desktop.
Save denov/a7eac36a3cda041f8afeabcef09d16fc to your computer and use it in GitHub Desktop.
import java.util.List;
import java.util.function.Function;
import java.util.stream.Collector;
import java.util.stream.Collectors;
public class StreamUtils {
public static <T> Collector<T, ?, T> singletonCollector() {
return getCollectorFunction(getListTSingleFunction());
}
public static <T> Collector<T, ?, T> firstInListCollector() {
return getCollectorFunction(getListTFirstFunction());
}
public static <T> Collector<T, ?, T> lastInListCollector() {
return getCollectorFunction(getLastTFirstFunction());
}
private static <T> Collector<T, ?, T> getCollectorFunction(Function<List<T>, T> listTFunction) {
return Collectors.collectingAndThen(Collectors.toList(), listTFunction);
}
private static <T> Function<List<T>, T> getListTSingleFunction() {
return list -> {
if (list.size() != 1) {
throw new IllegalStateException();
}
return list.get(0);
};
}
private static <T> Function<List<T>, T> getListTFirstFunction() {
return list -> {
if (list.size() == 0) {
throw new IllegalStateException();
}
return list.get(0);
};
}
private static <T> Function<List<T>, T> getLastTFirstFunction() {
return list -> {
if (list.size() == 0) {
throw new IllegalStateException();
}
return list.get(list.size()-1);
};
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment