Created
April 28, 2017 10:20
-
-
Save culmat/e16ad09c99c79076d4025ba89e515b9d 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
package common; | |
import java.util.function.Consumer; | |
import java.util.function.Function; | |
public class Consumers { | |
/** | |
* <pre> | |
*Stream.of(new Point(1, 2), new Point(2, 3)) | |
* | |
* // write | |
* Optional.ofNullable(properties.getProperty("enabled")).ifPresent(call(button::setEnabled,Boolean::valueOf)); | |
* | |
* // instead of | |
* Optional.ofNullable(properties.getProperty("enabled")).ifPresent(prop -> button.setEnabled(Boolean.valueOf(prop))); | |
* </pre> | |
*/ | |
public static <U, T> Consumer<T> call(Consumer<U> consumer, Function<T, U> mapping) { | |
return (tmp)-> consumer.accept(mapping.apply(tmp)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment