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.core; 020 021import java.util.stream.Stream; 022import org.jdrupes.builder.api.Project; 023import org.jdrupes.builder.api.Resource; 024import org.jdrupes.builder.api.ResourceRequest; 025import org.jdrupes.builder.api.ResourceType; 026import org.jdrupes.builder.api.Resources; 027 028/// A provider of resources to be included in a project. This 029/// implementation can be used for all kinds of resources. Usually 030/// language specific packages derive specializations that bind 031/// this class to a specific type of resource. These specializations 032/// often also offer methods that ease the specification of resources 033/// to be included. 034/// 035/// @param <T> the resource type 036/// 037public class ResourceCollector<T extends Resource> extends AbstractGenerator { 038 039 private final ResourceType<T> type; 040 private final Resources<T> resources; 041 042 /// Instantiates a new resources collector. 043 /// 044 /// @param project the project 045 /// @param type the type of resources to collect 046 /// 047 public ResourceCollector(Project project, ResourceType<T> type) { 048 super(project); 049 this.type = type; 050 resources = Resources.with(type); 051 } 052 053 /// Adds the given file tree with resource directories. 054 /// 055 /// @param resources the resources 056 /// @return the resources collector 057 /// 058 public final ResourceCollector<T> add(T resources) { 059 this.resources.add(resources); 060 return this; 061 } 062 063 /// Adds the given file trees with resource directories. 064 /// 065 /// @param resources the resources 066 /// @return the resources collector 067 /// 068 public final ResourceCollector<T> add(Stream<T> resources) { 069 this.resources.addAll(resources); 070 return this; 071 } 072 073 /// Return the resources to collect. 074 /// 075 /// @return the resources 076 /// 077 public final Resources<T> resources() { 078 return resources; 079 } 080 081 @Override 082 @SuppressWarnings("unchecked") 083 protected <R extends Resource> Stream<R> 084 doProvide(ResourceRequest<R> requested) { 085 if (!requested.accepts(type)) { 086 return Stream.empty(); 087 } 088 return (Stream<R>) resources.stream(); 089 } 090 091}