/
DirectoryTreeV1.java
332 lines (301 loc) · 10 KB
/
DirectoryTreeV1.java
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
package com.gourd.erwa.util;
import com.gourd.erwa.annotation.NotNull;
import java.io.File;
import java.io.FileFilter;
import java.util.*;
import java.util.function.Function;
/**
* 目录树生成工具.
* <p>
* {@linkplain #setDeep(int) 设置查询目录深度}
* {@linkplain #setFileFilter(FileFilter)} 设置查询文件筛选过滤器}
* {@linkplain #showLength()} 显示文件大小内容}
* {@linkplain #showModify() 显示文件修改时间内容}
* {@linkplain #showPermission() 显示文件权限内容}
* {@linkplain #addAppendContent(Function) 自定义显示文件内容信息}
*
* @author wei.Li
* @version JDK1.8
* @since 1
*/
public class DirectoryTreeV1 {
/* 换行符*/
private static final String LINE_SEPARATOR = System.getProperty("line.separator");
/* 空字符串*/
private static final String EMPTY = "";
/* 文件连接符*/
private static final String VERTICAL = "│ ", INTERMEDIATE = "├─", END = "└─";
/* 目录间距*/
private static final String SPACING = "\t";
/* 结果集收集*/
private final StringBuilder r = new StringBuilder();
/* 默认查询文件目录深度,默认为Integer.MAX_VALUE */
private int deep = Integer.MAX_VALUE;
/* 文件筛选过滤器*/
private FileFilter fileFilter = pathname -> true;
/* 写出文件名及其他信息,默认只输出文件名称*/
private AppendTo displayContent = new AppendTo(file -> " " + file.getName());
private DirectoryTreeV1() {
}
private DirectoryTreeV1(DirectoryTreeV1 directoryTreeV1) {
this.fileFilter = directoryTreeV1.fileFilter;
}
/**
* Create tree tree.
*
* @param generateFile the generate file
* @return the tree
*/
public static DirectoryTreeV1 create(File generateFile) {
return new DirectoryTreeV1();
}
/**
* 目录读取子文件.
*
* @param file the file
* @return the list
*/
@NotNull
List<File> fetchFiles(File file) {
final File[] files = file.listFiles(this.fileFilter);
return files == null ? Collections.emptyList() : Arrays.asList(files);
}
/**
* 自定义文件筛选过滤器.
*
* @param customFilter the custom file filter
* @return the file filter
*/
public DirectoryTreeV1 setFileFilter(FileFilter customFilter) {
this.fileFilter = customFilter;
return this;
}
/**
* 设置显示文件目录深度.
*
* @param deep the deep
* @return the deep
*/
public DirectoryTreeV1 setDeep(int deep) {
this.deep = deep;
return this;
}
/**
* 自定义排序显示结果 tree.
*
* @param comparable the comparable
* @return the tree
*/
public DirectoryTreeV1 sort(final Comparator<File> comparable) {
return new DirectoryTreeV1(this) {
@Override
List<File> fetchFiles(File file) {
final List<File> files = super.fetchFiles(file);
files.sort(comparable);
return files;
}
};
}
/**
* 显示文件大小.
*
* @return the tree
*/
public DirectoryTreeV1 showLength() {
this.displayContent.add(file -> ("[" + file.length() + "B" + "]"));
return this;
}
/**
* 显示文件修改时间.
*
* @return the tree
*/
public DirectoryTreeV1 showModify() {
this.displayContent.add(file -> ("[" + new Date(file.lastModified()) + "]"));
return this;
}
/**
* 显示文件权限.
*
* @return the tree
*/
public DirectoryTreeV1 showPermission() {
this.displayContent.add(
file -> ("["
+ (file.canRead() ? "r-" : "--")
+ (file.canWrite() ? "w-" : "--")
+ (file.canExecute() ? "x-" : "--")
+ "]")
);
return this;
}
/**
* 自定义添加读取 file 解析内容到输出内容.
*
* @param appendContent the append content
* @return the tree
*/
public DirectoryTreeV1 addAppendContent(Function<File, String> appendContent) {
this.displayContent.add(appendContent);
return this;
}
/**
* 生成文件.
*
* @return 结果内容
*/
public final String generate(File generateFile) {
if (generateFile.exists()) {
this.generateHandle(generateFile, EMPTY, 0);
} else {
System.err.println(generateFile.getPath() + " not found!");
}
return this.r.toString();
}
private void generateHandle(File file, String prefix, int deep) {
final List<File> files = this.fetchFiles(file);
if (files.isEmpty()) {
return;
}
deep++;
final int length = files.size();
for (int i = 0; i < length; i++) {
final File f = files.get(i);
final boolean isLast = (i >= length - 1);
this.r.append(prefix).append(isLast ? END : INTERMEDIATE);
this.appendDisplayContent(f);
this.r.append(LINE_SEPARATOR);
if (f.isDirectory() && deep <= this.deep) {
this.generateHandle(f, prefix + (!(length <= 1 || isLast) ? VERTICAL : EMPTY) + SPACING, deep);
}
}
}
/**
* 处理定义文件内容
*
* @param f f
*/
private void appendDisplayContent(File f) {
final List<Function<File, String>> appendContents = displayContent.appendContents;
for (Function<File, String> to : appendContents) {
this.r.append(to.apply(f));
}
}
/**
* 可累积显示 tree 中具体文件属性内容
*/
private static class AppendTo {
private final List<Function<File, String>> appendContents = new ArrayList<>();
AppendTo(Function<File, String> appendTo) {
if (appendTo != null) {
this.appendContents.add(appendTo);
}
}
void add(Function<File, String> to) {
if (to != null) {
this.appendContents.add(0, to);
}
}
}
}
/**
* The type Main Test.
*/
class MainTest {
public static void main(String[] args) {
final File generateFile = new File("/lw/workfile/intellij_work/analyzer/src/main/java/com/fusionskye/ezsonar");
final String generate = DirectoryTreeV1.create(generateFile)
.setDeep(20)
.setFileFilter(pathname -> (!(pathname.isHidden() || pathname.getName().contains("java"))))
/*.showLength()
.showModify()
.showPermission()
.addAppendContent(new DirectoryTreeV1.AppendContent() {
@Override
public String appendContent(File file) {
return "[" + file.getPath() + "]";
}
})*/
.generate(generateFile);
System.out.println(generate);
}
}
/*
├─ buildNumber.properties
├─ design.iml
├─ pom.xml
└─ src
├─ main
│ ├─ java
│ │ └─ com
│ │ └─ gourd
│ │ └─ erwa
│ │ └─ design
│ │ ├─ abstractfactory
│ │ │ └─ AbstractFactoryClient.java
│ │ ├─ adapter
│ │ │ └─ AdapterClient.java
│ │ ├─ builder
│ │ │ ├─ BuilderClient01.java
│ │ │ ├─ BuilderClient02.java
│ │ │ └─ builderClient02UML.jpg
│ │ ├─ delegate
│ │ │ ├─ ABitComplicated.java
│ │ │ ├─ package-info.java
│ │ │ └─ RealPrinter.java
│ │ ├─ enjoy
│ │ │ ├─ DataSourcesImpl.java
│ │ │ ├─ DataSourcesInterface.java
│ │ │ ├─ DataSourcesType.java
│ │ │ ├─ EnjoyFactory.java
│ │ │ └─ read.md
│ │ ├─ factory
│ │ │ └─ FactoryClient.java
│ │ ├─ listener
│ │ │ ├─ DemoEvent.java
│ │ │ ├─ DemoListener.java
│ │ │ ├─ Lights.java
│ │ │ └─ TestDemo.java
│ │ ├─ observer
│ │ │ ├─ BeingWatched.java
│ │ │ ├─ ObserverDemo.java
│ │ │ ├─ read.md
│ │ │ └─ Watcher.java
│ │ ├─ package-info.java
│ │ ├─ prototype
│ │ │ ├─ prototype.uml
│ │ │ └─ PrototypeClient.java
│ │ ├─ proxy
│ │ │ ├─ cglib
│ │ │ │ ├─ BookFacadeCglib.java
│ │ │ │ ├─ BookFacadeCglibProxy.java
│ │ │ │ └─ Test_Cglib.java
│ │ │ ├─ guava
│ │ │ ├─ jdk
│ │ │ │ ├─ BookFacadeJDKProxy.java
│ │ │ │ ├─ IBookFacadeJDK.java
│ │ │ │ ├─ IBookFacadeJDKImpl.java
│ │ │ │ ├─ testGuava.java
│ │ │ │ └─ testJDK.java
│ │ │ └─ read.md
│ │ ├─ responsibility
│ │ │ ├─ read.md
│ │ │ ├─ ResponsibilityClient01.java
│ │ │ └─ ResponsibilityClient02.java
│ │ ├─ singleton
│ │ │ ├─ LazilySingleton.java
│ │ │ ├─ read.md
│ │ │ ├─ ResultSingleton.java
│ │ │ ├─ Singleton.java
│ │ │ └─ Test.java
│ │ ├─ strategy
│ │ │ ├─ read.md
│ │ │ └─ StrategyClient_.java
│ │ └─ template
│ │ ├─ read.md
│ │ └─ TemplateClient.java
│ └─ resources
└─ test
└─ java
└─ GroupTest.java
*/