001/*
002 * Copyright 2022-2026 Revetware LLC.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package com.soklet;
018
019import com.google.errorprone.annotations.CheckReturnValue;
020import org.jspecify.annotations.NonNull;
021import org.jspecify.annotations.Nullable;
022
023import javax.annotation.concurrent.ThreadSafe;
024import java.util.concurrent.CompletionStage;
025
026import static java.util.Objects.requireNonNull;
027
028/**
029 * Framework-created result of termination-owning unary delegate attachment.
030 * Soklet owns the delegate subtree's completion state. The enclosing transport
031 * may retain this immutable handle to drive the child runtime as part of its own
032 * lifecycle response and to observe, but never complete, the subtree's
033 * termination stage.
034 *
035 * @author <a href="https://www.revetkn.com">Mark Allen</a>
036 */
037@ThreadSafe
038public final class TransportDelegateAttachment {
039        @NonNull
040        private final TransportRuntime transportRuntime;
041        @NonNull
042        private final InternalTransportDelegateAttachment internalAttachment;
043
044        TransportDelegateAttachment(@NonNull TransportRuntime transportRuntime,
045                        @NonNull InternalTransportDelegateAttachment internalAttachment) {
046                this.transportRuntime = requireNonNull(transportRuntime);
047                this.internalAttachment = requireNonNull(internalAttachment);
048        }
049
050        /**
051         * Acquires the exact runtime returned by the immediate delegate.
052         *
053         * @return the delegate runtime
054         */
055        @NonNull
056        public TransportRuntime getTransportRuntime() {
057                return this.transportRuntime;
058        }
059
060        /**
061         * Acquires the one cached, read-only minimal stage for the delegate's frozen
062         * subtree. The stage is observational and cannot be completed or otherwise
063         * mutated by callers. A transport must not synchronously wait for it from
064         * {@code attach}, {@link TransportRuntime#start(StartupContext)},
065         * {@link TransportRuntime#shutdownGracefully(ShutdownContext)},
066         * {@link TransportRuntime#shutdownForcibly(ShutdownContext)}, or any
067         * callback/activity
068         * whose completion is part of that subtree's termination proof.
069         *
070         * @return a stage completed only after committed subtree termination proof
071         */
072        @NonNull
073        @CheckReturnValue
074        public CompletionStage<@Nullable Void> whenTerminated() {
075                return this.internalAttachment.whenTerminated();
076        }
077}