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 terminal operation 038/// is performed on the [Stream] returned by [#entries]. 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 Resources<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 /// Add a ant-style path pattern to exclude from the tree. 055 /// 056 /// @param pattern the pattern 057 /// @return the file tree 058 /// 059 FileTree<T> exclude(String pattern); 060 061 /// Returns the root of the file tree containing the files. 062 /// 063 /// @param relativize whether to return a path relative to the project's 064 /// directory 065 /// @return the path 066 /// 067 Path root(boolean relativize); 068 069 /// Returns the root of the file tree searched for files 070 /// as an absolute path. 071 /// 072 /// @return the path 073 /// 074 default Path root() { 075 return root(false); 076 } 077 078 /// Returns the paths of the files in this file tree relative to 079 /// its root. 080 /// 081 /// @return the stream 082 /// 083 Stream<Path> entries(); 084 085 /// Re-scans the file tree for changes. 086 /// 087 /// @return the file tree 088 /// 089 @Override 090 FileTree<T> clear(); 091 092 /// Deletes all files in this file tree and directories that are 093 /// empty after deletion of the files including the root directory. 094 /// 095 @Override 096 void cleanup(); 097 098 /// Creates a new general file tree from the given project and path. 099 /// 100 /// @param project the project 101 /// @param directory the root of the file tree relative to the 102 /// project's directory (or an absolute path) 103 /// @param patterns the patterns 104 /// @return the file tree 105 /// 106 @SuppressWarnings({ "PMD.UseDiamondOperator", "PMD.ShortMethodName" }) 107 static FileTree<FileResource> of( 108 Project project, Path directory, String... patterns) { 109 return ResourceFactory.create( 110 new ResourceType<FileTree<FileResource>>() {}, project, 111 project != null ? project.directory().resolve(directory) 112 : directory, 113 (Object) patterns); 114 } 115 116 /// Creates a new file tree with elements of the given type from the 117 /// given project and path. 118 /// 119 /// @param <T> the generic type 120 /// @param project the project 121 /// @param directory the root of the file tree relative to the 122 /// project's directory (or an absolute path) 123 /// @param patterns the patterns 124 /// @param type the type 125 /// @return the file tree 126 /// 127 @SuppressWarnings({ "unchecked", "PMD.ShortMethodName" }) 128 static <T extends FileResource> FileTree<T> of(Project project, 129 Path directory, Class<T> type, String... patterns) { 130 return (FileTree<T>) ResourceFactory.create( 131 ResourceType.create(FileTree.class, type), project, 132 project != null ? project.directory().resolve(directory) 133 : directory, 134 (Object) patterns); 135 } 136}