Created
May 24, 2016 21:33
-
-
Save denov/a7eac36a3cda041f8afeabcef09d16fc to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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