逐步走向响应式编程(三)-常见函数式接口- Function<T, R>

前面介绍了函数式接口PredicateConsumer,本文继续介绍另一函数式接口Function<T, R>,此接口同样也位于 java.util.function包中,它的作用可以看作是消费者和生产者的结合,即消费某种原材料然后生产出某种产品,继续先分析一下源码。

源码解析

package java.util.function;

import java.util.Objects;

/**
 * Represents a function that accepts one argument and produces a result.
 * 此接口中的方法接收一个参数并输出一个结果
 *
 * <p>This is a <a href="package-summary.html">functional interface</a>
 * whose functional method is {@link #apply(Object)}.
 *
 * @param <T> the type of the input to the function
 * @param <R> the type of the result of the function
 *
 * @since 1.8
 */
@FunctionalInterface
public interface Function<T, R> {
   

    /**
     * Applies this function to the given argument.
     * 此方法对输入的参数进行处理,并得到一个结果
     * @param t the function argument
     * @return the function result
     */
    R apply(T t);

    /**
     * Returns a composed function that first applies the {@code before}
     * function to its input, and then applies this function to the result.
     * If evaluation of either function throws an exception, it is relayed to
     * the caller of the composed function.
     * 此方法将两个function进行组合,得到一个新的function,组合的过程中如果有一个function出现了异常,异常由调用者来处理。组合逻辑是,将其中一个function以参数的形式传入(before),在compose内部获取before的结果,并传给第二个function组成一个新的function并返回
     * @param <V> the type of input to the {@code before} function, and to the
     *           composed function
     * @param before the function to apply before this function is applied
     * 这句话好难理解,简单来就说就是将一个function当作一个参数
     * @return a composed function that first applies the {@code before}
     * function and then applies this function
     * 将before function的结果传给当前function,并返回。
     * @throws NullPointerException if before is null
     *
     * @see #andThen(Function)
     */
    default <V> Function<V, R> compose(Function<? super V, ? extends T> before) {
   
        Objects.requireNonNull(before);
        return (V v) -> apply(before.apply(v));
    }

    /**
     * Returns a composed function that first applies this function to
     * its input, and then applies the {@code after} function to the result.
     * If evaluation of either function throws an exception, it is relayed to
     * the caller of the composed function.
     *此函数和compose函数有点类似,但顺序不同。此函数是先执行调用者,再执行andThen的参数,即将调用者的结果传给参数中的function使用,并返回一个function.
     * @param <V> the type of output of the {@code after} function, and of the
     *           composed function
     * @param after the function to apply after this function is applied
     * @return a composed function that first applies this function and then
     * applies the {@code after} function
     * @throws NullPointerException if after is null
     *
     * @see #compose(Function)
     */
    default <V> Function<T, V> andThen(Function<? super R, ? extends V> after) {
   
        Objects.requireNonNull(after);
        return (T t) -> after.apply(apply(t
评论 11
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

浦江之猿

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值