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.nio.file.Path;
022import java.util.stream.Stream;
023
024/// The representation of a file tree. A file tree is a collection
025/// of [FileResource]s that are contained in a directory hierarchy
026/// with a common root.
027///
028/// Implementations of this interface must provide a [ResourceFactory]
029/// that supports the invocation of [ResourceFactory#create] with
030/// arguments
031///
032/// * [Project] the project
033/// * [Path] the root directory
034/// * [String]\[\] an array of ant-style path patterns
035///
036/// Implementations of this interface must ensure that the content
037/// of the file tree is not evaluated before a consuming operation
038/// is performed on the [Stream] returned by [#paths]. The delayed
039/// evaluation includes resolving a relative path for root against
040/// the project's directory.
041///
042/// @param <T> the contained type
043///
044public interface FileTree<T extends FileResource> extends InputTree<T> {
045
046    /// Includes directories in the file tree if they match the pattern
047    /// used when creating the file tree, and are not excluded (i.e. don't 
048    /// match the exclude pattern).
049    ///
050    /// @return the file tree
051    ///
052    FileTree<T> withDirectories();
053
054    /// Returns the root of the file tree containing the files.
055    ///
056    /// @param relativize whether to return a path relative to the project's
057    /// directory
058    /// @return the path
059    ///
060    Path root(boolean relativize);
061
062    /// Returns the root of the file tree searched for files
063    /// as an absolute path.
064    ///
065    /// @return the path
066    ///
067    default Path root() {
068        return root(false);
069    }
070
071    /// Returns the paths of the files in this file tree relative to
072    /// its root.
073    ///
074    /// @return the stream
075    ///
076    @Override
077    Stream<Path> paths();
078
079    /// Re-scans the file tree for changes.
080    ///
081    /// @return the file tree
082    ///
083    @Override
084    FileTree<T> clear();
085
086    /// Deletes all files in this file tree and directories that are
087    /// empty after deletion of the files including the root directory.
088    ///
089    @Override
090    void cleanup();
091
092    /// Creates a new general file tree from the given project and path.
093    ///
094    /// @param project the project
095    /// @param directory the root of the file tree relative to the
096    /// project's directory (or an absolute path)
097    /// @param patterns the patterns. If no patterns are given, the
098    /// default pattern "**/*" is used
099    /// @return the file tree
100    ///
101    @SuppressWarnings({ "PMD.UseDiamondOperator", "PMD.ShortMethodName" })
102    static FileTree<FileResource> of(
103            Project project, Path directory, String... patterns) {
104        return ResourceFactory.create(
105            new ResourceType<FileTree<FileResource>>() {}, project,
106            project != null ? project.directory().resolve(directory)
107                : directory,
108            (Object) patterns);
109    }
110
111    /// Creates a new file tree with elements of the given type from the
112    /// given project and path.
113    ///
114    /// @param <T> the generic type
115    /// @param project the project
116    /// @param directory the root of the file tree relative to the
117    /// project's directory (or an absolute path)
118    /// @param patterns the patterns. If no patterns are given, the
119    /// default pattern "**/*" is used
120    /// @param type the type
121    /// @return the file tree
122    ///
123    @SuppressWarnings({ "unchecked", "PMD.ShortMethodName" })
124    static <T extends FileResource> FileTree<T> of(Project project,
125            Path directory, Class<T> type, String... patterns) {
126        return (FileTree<T>) ResourceFactory.create(
127            ResourceType.create(FileTree.class, type), project,
128            project != null ? project.directory().resolve(directory)
129                : directory,
130            (Object) patterns);
131    }
132}