段小言
段小言

Reputation: 22

About return lambda expressions

I can't understand what does the method return mean, and I don't know where the builder and name come from

@ConditionalOnBean(DecompressorRegistry.class)
@Bean
GrpcChannelConfigurer decompressionChannelConfigurer(final DecompressorRegistry registry) {
    return (builder, name) -> builder.decompressorRegistry(registry);
}

GrpcChannelConfigurer class :

@FunctionalInterface
public interface GrpcChannelConfigurer extends BiConsumer<ManagedChannelBuilder<?>, String> {
@Override
default GrpcChannelConfigurer andThen(final BiConsumer<? super ManagedChannelBuilder<?>, ? super String> after) {
    requireNonNull(after, "after");
    return (l, r) -> {
        accept(l, r);
        after.accept(l, r);
    };
}
}

Upvotes: 0

Views: 84

Answers (1)

tgdavies
tgdavies

Reputation: 11454

The BiConsumer interface looks like this:

public interface BiConsumer<T, U> {

    /**
     * Performs this operation on the given arguments.
     *
     * @param t the first input argument
     * @param u the second input argument
     */
    void accept(T t, U u);
 ...
}

The code (builder, name) -> builder.decompressorRegistry(registry); is an implementation of accept. It is equivalent to writing:

class MyGrpcChannelConfigurer implements GrpcChannelConfigurer {

  void accept(ManagedChannelBuilder builder, String name) {
    builder.decompressorRegistry(registry)
  }
}

Saying return (builder, name) -> ... is equivalent to return new MyGrpcChannelConfigurer();

Upvotes: 1

Related Questions