forked from objectionary/eo
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEmptyDirectoriesIn.java
More file actions
72 lines (66 loc) · 1.69 KB
/
Copy pathEmptyDirectoriesIn.java
File metadata and controls
72 lines (66 loc) · 1.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/*
* SPDX-FileCopyrightText: Copyright (c) 2016-2025 Objectionary.com
* SPDX-License-Identifier: MIT
*/
package org.eolang.maven;
import com.jcabi.log.Logger;
import java.io.File;
import java.nio.file.Path;
/**
* Delete empty directories in provided root.
*
* @since 0.55
*/
final class EmptyDirectoriesIn {
/**
* Root path.
*/
private final File root;
/**
* Ctor.
* @param root Root directory.
*/
EmptyDirectoriesIn(final Path root) {
this(root.toFile());
}
/**
* Ctor.
* @param root Root directory
*/
EmptyDirectoriesIn(final File root) {
this.root = root;
}
/**
* Clear empty directories in {@code this.root}.
*/
void clear() {
if (!this.root.isDirectory()) {
throw new IllegalStateException(
Logger.format("Provided path %[file]s is not a directory", this.root)
);
}
this.delete(this.root);
}
/**
* Recursively delete empty directories.
* @param dir Directory to delete
* @checkstyle NestedIfDepthCheck (20 lines)
*/
private void delete(final File dir) {
if (!dir.isDirectory()) {
return;
}
final File[] before = dir.listFiles();
if (before != null) {
for (final File file : before) {
if (file.isDirectory()) {
this.delete(file);
}
}
}
final File[] after = dir.listFiles();
if (after != null && after.length == 0 && !dir.equals(this.root) && dir.delete()) {
Logger.debug(EmptyDirectoriesIn.class, "Deleted empty directory %[file]s", dir);
}
}
}