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