001/*-
002 * #%L
003 * HAPI FHIR - Core Library
004 * %%
005 * Copyright (C) 2014 - 2024 Smile CDR, Inc.
006 * %%
007 * Licensed under the Apache License, Version 2.0 (the "License");
008 * you may not use this file except in compliance with the License.
009 * You may obtain a copy of the License at
010 *
011 *      http://www.apache.org/licenses/LICENSE-2.0
012 *
013 * Unless required by applicable law or agreed to in writing, software
014 * distributed under the License is distributed on an "AS IS" BASIS,
015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016 * See the License for the specific language governing permissions and
017 * limitations under the License.
018 * #L%
019 */
020package ca.uhn.fhir.interceptor.api;
021
022import java.util.function.Supplier;
023
024public interface IBaseInterceptorBroadcaster<POINTCUT extends IPointcut> {
025
026        /**
027         * Invoke registered interceptor hook methods for the given Pointcut.
028         *
029         * @return Returns <code>false</code> if any of the invoked hook methods returned
030         * <code>false</code>, and returns <code>true</code> otherwise.
031         */
032        boolean callHooks(POINTCUT thePointcut, HookParams theParams);
033
034        /**
035         * A supplier-based callHooks() for lazy construction of the HookParameters.
036         * @return false if any hook methods return false, return true otherwise.
037         */
038        default boolean ifHasCallHooks(POINTCUT thePointcut, Supplier<HookParams> theParamsSupplier) {
039                if (hasHooks(thePointcut)) {
040                        HookParams params = theParamsSupplier.get();
041                        return callHooks(thePointcut, params);
042                }
043                return true; // callHooks returns true when none present;
044        }
045
046        /**
047         * Invoke registered interceptor hook methods for the given Pointcut. This method
048         * should only be called for pointcuts that return a type other than
049         * <code>void</code> or <code>boolean</code>
050         *
051         * @return Returns the object returned by the first hook method that did not return <code>null</code>
052         */
053        Object callHooksAndReturnObject(POINTCUT thePointcut, HookParams theParams);
054
055        /**
056         * A supplier-based version of callHooksAndReturnObject for lazy construction of the params.
057         *
058         * @return Returns the object returned by the first hook method that did not return <code>null</code> or <code>null</code>
059         */
060        default Object ifHasCallHooksAndReturnObject(POINTCUT thePointcut, Supplier<HookParams> theParams) {
061                if (hasHooks(thePointcut)) {
062                        HookParams params = theParams.get();
063                        return callHooksAndReturnObject(thePointcut, params);
064                }
065                return null;
066        }
067
068        /**
069         * Does this broadcaster have any hooks for the given pointcut?
070         *
071         * @param thePointcut The poointcut
072         * @return Does this broadcaster have any hooks for the given pointcut?
073         * @since 4.0.0
074         */
075        boolean hasHooks(POINTCUT thePointcut);
076}