14
14
import java .nio .file .Path ;
15
15
import java .time .Instant ;
16
16
import java .util .Collection ;
17
+ import java .util .Comparator ;
17
18
import java .util .List ;
18
19
import java .util .Optional ;
19
20
import java .util .function .Function ;
@@ -74,26 +75,41 @@ public static URL toUrl(final Path file) {
74
75
75
76
/**
76
77
* Searches a file system starting from a given directory looking for a file or directory with a
77
- * matching name.
78
+ * matching name. If multiple matching paths are found, returns the one closest to the root (via
79
+ * minimum length of the absolute path).
78
80
*
79
- * @param searchRoot The directory whose contents we will search (and sub-directories)
81
+ * @param searchRoot The directory whose contents we will search (and subdirectories).
80
82
* @param maxDepth The maximum number of subdirectories to search. Zero means only search the
81
83
* 'searchRoot' directory.
82
84
* @param fileName The name of the file to be search for.
83
85
* @return A file matching the given name or empty if not found.
84
86
*/
85
- public Optional <Path > findAny (final Path searchRoot , final int maxDepth , final String fileName ) {
87
+ public Optional <Path > findClosestToRoot (
88
+ final Path searchRoot , final int maxDepth , final String fileName ) {
86
89
return find (searchRoot , maxDepth , fileName ).stream ().findAny ();
87
90
}
88
91
89
- public Collection <Path > find (final Path searchRoot , final int maxDepth , final String fileName ) {
92
+ /**
93
+ * Searches a file system starting from a given directory looking for files or directories with
94
+ * matching names. The resulting list will be in ascending order by absolute path length.
95
+ *
96
+ * @param searchRoot The directory whose contents we will search (and subdirectories).
97
+ * @param maxDepth The maximum number of subdirectories to search. Zero means only search the
98
+ * 'searchRoot' directory.
99
+ * @param fileName The name of the file to be search for.
100
+ * @return A list of files matching the given name or an empty list if not.
101
+ */
102
+ public List <Path > find (final Path searchRoot , final int maxDepth , final String fileName ) {
90
103
Preconditions .checkArgument (Files .isDirectory (searchRoot ), searchRoot .toAbsolutePath ());
91
104
Preconditions .checkArgument (Files .exists (searchRoot ), searchRoot .toAbsolutePath ());
92
105
Preconditions .checkArgument (maxDepth > -1 );
93
106
Preconditions .checkArgument (!fileName .isBlank ());
94
107
try (Stream <Path > files = Files .walk (searchRoot , maxDepth )) {
95
108
return files
96
109
.filter (f -> f .getFileName ().toString ().equals (fileName ))
110
+ // Sort by path length (shortest to longest), so that the ordering is deterministic and
111
+ // paths closer to the root are earlier in the list.
112
+ .sorted (Comparator .comparingInt (f -> f .toAbsolutePath ().toString ().length ()))
97
113
.collect (Collectors .toList ());
98
114
} catch (final IOException e ) {
99
115
log .error (
@@ -243,7 +259,7 @@ public static void deleteDirectory(final Path path) throws IOException {
243
259
}
244
260
245
261
/**
246
- * Does an overwrite of one folder onto another and rolls back if there any errors. The rollback
262
+ * Does an overwrite of one folder onto another and rolls back if there were errors. The rollback
247
263
* is done by first moving the destination folder to a backup location. If there are any errors
248
264
* then we delete whatever we copied and move the backup location back to the destination
249
265
* location.
@@ -287,8 +303,7 @@ static boolean replaceFolder(
287
303
return true ;
288
304
}
289
305
290
- // otherwise create a backup of the destination folder before we replace it
291
-
306
+ // otherwise, create a backup of the destination folder before we replace it
292
307
final Path backupFolder ;
293
308
try {
294
309
backupFolder = Files .createTempDirectory ("temp-dir" ).resolve (dest .getFileName ());
@@ -298,7 +313,7 @@ static boolean replaceFolder(
298
313
}
299
314
300
315
try {
301
- // make a complete backup by moving the dest folder to backup
316
+ // make a complete backup by moving the dest folder to back up
302
317
fileMoveOperation .move (dest , backupFolder );
303
318
304
319
// do the folder move
0 commit comments