001/*
002 * JDrupes Builder
003 * Copyright (C) 2026 Michael N. Lipp
004 * 
005 * This program is free software: you can redistribute it and/or modify
006 * it under the terms of the GNU Affero General Public License as
007 * published by the Free Software Foundation, either version 3 of the
008 * License, or (at your option) any later version.
009 *
010 * This program is distributed in the hope that it will be useful,
011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
013 * GNU Affero General Public License for more details.
014 *
015 * You should have received a copy of the GNU Affero General Public License
016 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
017 */
018
019package org.jdrupes.builder.api;
020
021import java.time.Instant;
022import java.util.stream.Stream;
023
024/// Provides the results from executing a process. The type of resource
025/// related to the execution depends on the provider. It is recommended
026/// to this as a base interface for defining execution results with
027/// the resource type bound to a specific type.
028///
029/// @param <T> the resource type
030///
031public interface ExecResult<T extends Resource> extends Resource, FaultAware {
032
033    /// The exit value.
034    ///
035    /// @return the exit value
036    ///
037    int exitValue();
038
039    /// Sets the instant to be returned by [Resource#asOf].
040    ///
041    /// @param asOf the as of
042    /// @return the exec result
043    ///
044    ExecResult<T> asOf(Instant asOf);
045
046    /// Returns the stream of resources produced by the execution.
047    /// Each invocation of this method must return a new stream.
048    ///
049    /// @return the stream
050    ///
051    default Stream<T> resources() {
052        return Stream.empty();
053    }
054
055    /// Creates a new execution result from the given values.
056    ///
057    /// @param provider the provider
058    /// @param name the name
059    /// @param exitValue the exit value
060    /// @return the exec result
061    ///
062    @SuppressWarnings("PMD.ShortMethodName")
063    static ExecResult<?> of(ResourceProvider provider, String name,
064            int exitValue) {
065        return ResourceFactory.create(
066            new ResourceType<>() {}, provider, name, exitValue);
067    }
068
069    /// Creates a new execution result from the given values.
070    ///
071    /// @param <T> the generic type
072    /// @param provider the provider
073    /// @param name the name
074    /// @param exitValue the exit value
075    /// @param resources the resources
076    /// @return the exec result
077    ///
078    @SuppressWarnings("PMD.ShortMethodName")
079    static <T extends Resource> ExecResult<T> of(ResourceProvider provider,
080            String name, int exitValue, Stream<T> resources) {
081        return ResourceFactory.create(
082            new ResourceType<>() {}, provider, name, exitValue, resources);
083    }
084}