Last active
October 8, 2019 16:13
-
-
Save 28/923c8a03f766c4f9c5527e5239d26624 to your computer and use it in GitHub Desktop.
A construct that replaces multiple if x ==/!= null statements in a row when trying to produce a value of the same type in different ways.
This file contains hidden or 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.function.Predicate; | |
| import java.util.function.Supplier; | |
| import java.util.stream.Stream; | |
| public class ConditionalComposedProducer { | |
| /** | |
| * Takes a variable number of {@code {@link Supplier}}s that supply the same | |
| * nullable type {@code T} and a {@code {@link Predicate}}. | |
| * Iterate through all {@code {@link Supplier}}s and applies the predicate | |
| * on the results. The first {@code {@link Supplier}} that produces the value that | |
| * satisfies the predicate will have its value returned. If none of the values satisfy | |
| * the predicate {@code null} is returned. | |
| * <p> | |
| * Uses a for-loop with condition checking. | |
| * | |
| * @param condition - a predicate that needs to be satisfied in order for the | |
| * producer to return a value. | |
| * @param suppliers - a variable number of {@code {@link Supplier}}s that | |
| * produce values. | |
| * @param <T> - the return type. | |
| * @return - the first value produced by the {@code {@link Supplier}}s that | |
| * satisfies the predicate or {@code null}. | |
| */ | |
| public static <T> T produce1(Predicate<T> condition, Supplier<T>... suppliers) { | |
| for (Supplier<T> supplier : suppliers) { | |
| T t = supplier.get(); | |
| if (condition.test(t)) | |
| return t; | |
| } | |
| return null; | |
| } | |
| /** | |
| * Takes a variable number of {@code {@link Supplier}}s that supply the same | |
| * nullable type {@code T} and a {@code {@link Predicate}}. | |
| * Iterate through all {@code {@link Supplier}}s and applies the predicate | |
| * on the results. The first {@code {@link Supplier}} that produces the value that | |
| * satisfies the predicate will have its value returned. If none of the values satisfy | |
| * the predicate {@code null} is returned. | |
| * <p> | |
| * Uses a stream that is filtered and short circuited with findFirst. | |
| * | |
| * @param condition - a predicate that needs to be satisfied in order for the | |
| * producer to return a value. | |
| * @param suppliers - a variable number of {@code {@link Supplier}}s that | |
| * produce values. | |
| * @param <T> - the return type. | |
| * @return - the first value produced by the {@code {@link Supplier}}s that | |
| * satisfies the predicate or {@code null}. | |
| */ | |
| public static <T> T produce2(Predicate<T> condition, Supplier<T>... suppliers) { | |
| return Stream.of(suppliers).filter(s -> condition.test(s.get())).findFirst().orElse(() -> null).get(); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment