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. 049 /// 050 /// @param resources the resources to add 051 /// @return the resources 052 /// 053 default Resources<T> addAll(Stream<? extends T> resources) { 054 resources.forEach(this::add); 055 return this; 056 } 057 058 /// As of. 059 /// 060 /// @return the instant 061 /// 062 @Override 063 default Instant asOf() { 064 return stream().map(Resource::asOf).reduce(Instant.MIN, (latest, 065 next) -> next.isAfter(latest) ? next : latest); 066 } 067 068 /// Checks if is empty. 069 /// 070 /// @return true, if is empty 071 /// 072 boolean isEmpty(); 073 074 /// Retrieves the resources as a stream. 075 /// 076 /// @return the stream 077 /// 078 Stream<T> stream(); 079 080 /// Clears the contained resources. 081 /// 082 /// @return the resources 083 /// 084 Resources<T> clear(); 085}