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 org.jspecify.annotations.NonNull;
020import org.jspecify.annotations.Nullable;
021
022import javax.annotation.concurrent.ThreadSafe;
023import java.util.Optional;
024
025import static java.util.Objects.requireNonNull;
026
027/** Immutable description of the first shutdown component to terminate prematurely. */
028@ThreadSafe
029public final class UnexpectedShutdownComponentTermination {
030        @NonNull
031        private final ShutdownComponentType shutdownComponentType;
032        @Nullable
033        private final Throwable cause;
034
035        UnexpectedShutdownComponentTermination(
036                        @NonNull ShutdownComponentType shutdownComponentType,
037                        @Nullable Throwable cause) {
038                this.shutdownComponentType = requireNonNull(shutdownComponentType);
039                this.cause = cause;
040        }
041
042        /** @return type of the first shutdown component to terminate prematurely */
043        @NonNull
044        public ShutdownComponentType getShutdownComponentType() {
045                return this.shutdownComponentType;
046        }
047
048        /**
049         * Returns the component-supplied failure cause when one was signaled.
050         *
051         * @return original termination cause, otherwise empty
052         */
053        @NonNull
054        public Optional<@NonNull Throwable> getCause() {
055                return Optional.ofNullable(this.cause);
056        }
057}