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] the pattern
035/// * `boolean` whether to include directories (optional, defaults to `false`)
036///
037/// Implementations of this interface must ensure that the content
038/// of the file tree is not evaluated before a terminal operation
039/// is performed on the [Stream] returned by [#entries]. The delayed
040/// evaluation includes resolving a relative path for root against
041/// the project's directory.
042///
043/// @param <T> the contained type
044///
045public interface FileTree<T extends FileResource> extends Resources<T> {
046
047    /// Includes directories in the file tree if they match the pattern
048    /// used when creating the file tree, and are not excluded (i.e. don't 
049    /// match the exclude pattern).
050    ///
051    /// @return the file tree
052    ///
053    FileTree<T> withDirectories();
054
055    /// Add a file name pattern to exclude from the tree.
056    ///
057    /// @param pattern the pattern
058    /// @return the file tree
059    ///
060    FileTree<T> exclude(String pattern);
061
062    /// Returns the root of the file tree containing the files.
063    ///
064    /// @param relativize whether to return a path relative to the project's
065    /// directory
066    /// @return the path
067    ///
068    Path root(boolean relativize);
069
070    /// Returns the root of the file tree searched for files
071    /// as an absolute path.
072    ///
073    /// @return the path
074    ///
075    default Path root() {
076        return root(false);
077    }
078
079    /// Returns the paths of the files in this file tree relative to
080    /// its root.
081    ///
082    /// @return the stream
083    ///
084    Stream<Path> entries();
085
086    /// Re-scans the file tree for changes.
087    ///
088    /// @return the file tree
089    ///
090    @Override
091    FileTree<T> clear();
092
093    /// Deletes all files in this file tree and directories that are
094    /// empty after deletion of the files (expect for root, which is
095    /// not deleted).
096    ///
097    /// @return the file tree
098    ///
099    FileTree<T> delete();
100}