/** * Create a new {@link InvocationResult} instance with the specified value. * @param value the value (may be {@code null}) * @param <R> the result type * @return an {@link InvocationResult} */ public static <R> InvocationResult<R> of(R value) { return new InvocationResult<>(value); }
protected final <R> InvocationResult<R> invoke(C callbackInstance, Supplier<R> supplier) { if (this.filter.match(this.callbackType, callbackInstance, this.argument, this.additionalArguments)) { try { return InvocationResult.of(supplier.get()); } catch (ClassCastException ex) { if (!isLambdaGenericProblem(ex)) { throw ex; } logNonMatchingType(callbackInstance, ex); } } return InvocationResult.noResult(); }
/** * Invoke the callback instance where the callback method returns a result. * @param invoker the invoker used to invoke the callback * @param <R> the result type * @return the result of the invocation (may be {@link InvocationResult#noResult} * if the callback was not invoked) */ public <R> InvocationResult<R> invokeAnd(Function<C, R> invoker) { return invoke(this.callbackInstance, () -> invoker.apply(this.callbackInstance)); }
@SuppressWarnings("unchecked") private void postProcessBeforeInitialization(WebServerFactory webServerFactory) { LambdaSafe .callbacks(WebServerFactoryCustomizer.class, getCustomizers(), webServerFactory) .withLogger(WebServerFactoryCustomizerBeanPostProcessor.class) .invoke((customizer) -> customizer.customize(webServerFactory)); }
@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); }
@SuppressWarnings("unchecked") private boolean isFilterMatch(EndpointFilter<E> filter, E endpoint) { return LambdaSafe.callback(EndpointFilter.class, filter, endpoint) .withLogger(EndpointDiscoverer.class).invokeAnd((f) -> f.match(endpoint)) .get(); }
/** * 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; }); }); }
/** * Use the specified logger to report any lambda failures. * @param logger the logger to use * @return this instance */ public SELF withLogger(Log logger) { Assert.notNull(logger, "Logger must not be null"); this.logger = logger; return self(); }
private boolean isLambdaGenericProblem(ClassCastException ex) { return (ex.getMessage() == null || startsWithArgumentClassName(ex.getMessage())); }
/** * Use the specified logger source to report any lambda failures. * @param loggerSource the logger source to use * @return this instance */ public SELF withLogger(Class<?> loggerSource) { return withLogger(LogFactory.getLog(loggerSource)); }
/** * 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); }
/** * Start a call to a single callback instance, dealing with common generic type * concerns and exceptions. * @param callbackType the callback type (a {@link FunctionalInterface functional * interface}) * @param callbackInstance the callback instance (may be a lambda) * @param argument the primary argument passed to the callback * @param additionalArguments any additional arguments passed to the callback * @param <C> the callback type * @param <A> the primary argument type * @return a {@link Callback} instance that can be invoked. */ public static <C, A> Callback<C, A> callback(Class<C> callbackType, C callbackInstance, A argument, Object... additionalArguments) { Assert.notNull(callbackType, "CallbackType must not be null"); Assert.notNull(callbackInstance, "CallbackInstance must not be null"); return new Callback<>(callbackType, callbackInstance, 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)); }
/** * 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); }
/** * Invoke the callback instance where the callback method returns void. * @param invoker the invoker used to invoke the callback */ public void invoke(Consumer<C> invoker) { invoke(this.callbackInstance, () -> { invoker.accept(this.callbackInstance); return null; }); }
/** * Use a specific filter to determine when a callback should apply. If no explicit * filter is set filter will be attempted using the generic type on the callback * type. * @param filter the filter to use * @return this instance */ public SELF withFilter(Filter<C, A> filter) { Assert.notNull(filter, "Filter must not be null"); this.filter = filter; return self(); }
private boolean startsWithArgumentClassName(String message) { Predicate<Object> startsWith = (argument) -> startsWithArgumentClassName( message, argument); return startsWith.test(this.argument) || Stream.of(this.additionalArguments).anyMatch(startsWith); }