Skip to content

Instantly share code, notes, and snippets.

@culmat
Created April 28, 2017 10:20
Show Gist options
  • Save culmat/e16ad09c99c79076d4025ba89e515b9d to your computer and use it in GitHub Desktop.
Save culmat/e16ad09c99c79076d4025ba89e515b9d to your computer and use it in GitHub Desktop.
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