/** * Invoke the callback instances where the callback method returns a result. * @param invoker the invoker used to invoke the callback * @param <R> the result type * @return the results of the invocation (may be an empty stream if no callbacks * could be called) */ public <R> Stream<R> invokeAnd(Function<C, R> invoker) { Function<C, InvocationResult<R>> mapper = (callbackInstance) -> invoke( callbackInstance, () -> invoker.apply(callbackInstance)); return this.callbackInstances.stream().map(mapper) .filter(InvocationResult::hasResult).map(InvocationResult::get); }
/** * Start a call to callback instances, dealing with common generic type concerns and * exceptions. * @param callbackType the callback type (a {@link FunctionalInterface functional * interface}) * @param callbackInstances the callback instances (elements may be lambdas) * @param argument the primary argument passed to the callbacks * @param additionalArguments any additional arguments passed to the callbacks * @param <C> the callback type * @param <A> the primary argument type * @return a {@link Callbacks} instance that can be invoked. */ public static <C, A> Callbacks<C, A> callbacks(Class<C> callbackType, Collection<? extends C> callbackInstances, A argument, Object... additionalArguments) { Assert.notNull(callbackType, "CallbackType must not be null"); Assert.notNull(callbackInstances, "CallbackInstances must not be null"); return new Callbacks<>(callbackType, callbackInstances, argument, additionalArguments); }
@SuppressWarnings("unchecked") private void customize(MeterRegistry registry) { LambdaSafe .callbacks(MeterRegistryCustomizer.class, asOrderedList(this.customizers), registry) .withLogger(MeterRegistryConfigurer.class) .invoke((customizer) -> customizer.customize(registry)); }
@SuppressWarnings("unchecked") private void invokeCustomizers(T factory, List<WebServerFactoryCustomizer<?>> customizers) { LambdaSafe.callbacks(WebServerFactoryCustomizer.class, customizers, factory) .invoke((customizer) -> customizer.customize(factory)); }
@SuppressWarnings({ "unchecked" }) private MeterBinder getMeterBinder(Cache cache, Tags tags) { Tags cacheTags = tags.and(getAdditionalTags(cache)); return LambdaSafe .callbacks(CacheMeterBinderProvider.class, this.binderProviders, cache) .withLogger(CacheMetricsRegistrar.class) .invokeAnd((binderProvider) -> binderProvider.getMeterBinder(cache, cacheTags)) .filter(Objects::nonNull).findFirst().orElse(null); }
/** * Invoke the callback instances where the callback method returns void. * @param invoker the invoker used to invoke the callback */ public void invoke(Consumer<C> invoker) { this.callbackInstances.forEach((callbackInstance) -> { invoke(callbackInstance, () -> { invoker.accept(callbackInstance); return null; }); }); }
@SuppressWarnings("unchecked") private void postProcessBeforeInitialization(WebServerFactory webServerFactory) { LambdaSafe .callbacks(WebServerFactoryCustomizer.class, getCustomizers(), webServerFactory) .withLogger(WebServerFactoryCustomizerBeanPostProcessor.class) .invoke((customizer) -> customizer.customize(webServerFactory)); }