001/*-
002 * #%L
003 * HAPI FHIR - Core Library
004 * %%
005 * Copyright (C) 2014 - 2025 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.executor;
021
022import ca.uhn.fhir.i18n.Msg;
023import ca.uhn.fhir.interceptor.api.IBaseInterceptorBroadcaster.IInterceptorFilterHook;
024
025import java.util.function.Supplier;
026
027/**
028 * Wraps a Supplier with advice from a filter hook.
029 */
030public class SupplierFilterHookWrapper<T> implements Supplier<T> {
031        private final IInterceptorFilterHook myAdvice;
032        private final Supplier<T> myTarget;
033        private final Supplier<String> myMessageSupplier;
034
035        public SupplierFilterHookWrapper(
036                        Supplier<T> theTarget, IInterceptorFilterHook theAdvice, Supplier<String> theCauseDescriptionSupplier) {
037                myAdvice = theAdvice;
038                myTarget = theTarget;
039                myMessageSupplier = theCauseDescriptionSupplier;
040        }
041
042        @Override
043        public T get() {
044                SupplierRunnable<T> trackingSupplierWrapper = new SupplierRunnable<>(myTarget);
045
046                myAdvice.wrapCall(trackingSupplierWrapper);
047
048                if (!trackingSupplierWrapper.wasExecuted()) {
049                        throw new IllegalStateException(
050                                        Msg.code(2648) + "Supplier was not executed in filter produced by " + myMessageSupplier.get());
051                }
052
053                return trackingSupplierWrapper.getResult();
054        }
055
056        /**
057         * Adapt a Supplier to Runnable.
058         * We use a Runnable for a simpler api for callers.
059         * @param <T>
060         */
061        static class SupplierRunnable<T> implements Runnable {
062                private final Supplier<T> myTarget;
063                private boolean myExecutedFlag = false;
064                private T myResult = null;
065
066                SupplierRunnable(Supplier<T> theTarget) {
067                        myTarget = theTarget;
068                }
069
070                public void run() {
071                        myExecutedFlag = true;
072                        myResult = myTarget.get();
073                }
074
075                public boolean wasExecuted() {
076                        return myExecutedFlag;
077                }
078
079                public T getResult() {
080                        return myResult;
081                }
082        }
083}