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;
020
021import javax.annotation.concurrent.ThreadSafe;
022
023/**
024 * Opens a resource for a streaming operation, allowing checked acquisition failures.
025 * <p>
026 * Each invocation must return an independently owned resource. A reusable streaming descriptor may invoke the same
027 * factory concurrently for different responses; the factory is responsible for the safety of its captured state.
028 * The receiving API defines when acquisition occurs and how the returned resource is closed or aborted.
029 * <p>
030 * If acquisition fails before returning a resource, the factory is responsible for cleaning up any partially
031 * acquired state. Implementing {@link AutoCloseable} alone does not guarantee that a resource can safely be
032 * closed while another thread is using it.
033 *
034 * @param <T> the resource type
035 * @author <a href="https://www.revetkn.com">Mark Allen</a>
036 */
037@ThreadSafe
038@FunctionalInterface
039public interface StreamResourceFactory<T extends @NonNull AutoCloseable> {
040        /**
041         * Opens a resource for one streaming operation.
042         *
043         * @return the newly owned resource
044         * @throws Exception if acquisition fails
045         */
046        @NonNull
047        T open() throws Exception;
048}