001/*
002 * JDrupes Builder
003 * Copyright (C) 2025 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/// Defines a container for a collection of resources. Implementations
025/// must behave as sets (no duplicate entries) and must maintain insertion
026/// order when providing the content through [stream].
027///
028/// @param <T> the contained type
029///
030public interface Resources<T extends Resource> extends Resource {
031
032    /// Adds the given resource.
033    ///
034    /// @param resource the resource
035    /// @return the resources
036    ///
037    Resources<T> add(T resource);
038
039    /// Adds all resources from the given collection.
040    ///
041    /// @param resources the resources to add
042    /// @return the resources
043    ///
044    default Resources<T> addAll(Resources<? extends T> resources) {
045        return addAll(resources.stream());
046    }
047
048    /// Adds all resources from the given stream. This terminates the given
049    /// stream.
050    ///
051    /// @param resources the resources to add
052    /// @return the resources
053    ///
054    default Resources<T> addAll(Stream<? extends T> resources) {
055        resources.forEach(this::add);
056        return this;
057    }
058
059    /// As of.
060    ///
061    /// @return the instant
062    ///
063    @Override
064    default Instant asOf() {
065        return stream().map(Resource::asOf).reduce(Instant.MIN, (latest,
066                next) -> next.isAfter(latest) ? next : latest);
067    }
068
069    /// Checks if is empty.
070    ///
071    /// @return true, if is empty
072    ///
073    boolean isEmpty();
074
075    /// Retrieves the resources as a stream.
076    ///
077    /// @return the stream
078    ///
079    Stream<T> stream();
080
081    /// Clears the contained resources.
082    ///
083    /// @return the resources
084    ///
085    Resources<T> clear();
086}