-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLogFilter.java
More file actions
45 lines (40 loc) · 1.19 KB
/
Copy pathLogFilter.java
File metadata and controls
45 lines (40 loc) · 1.19 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
package com.core.utils;
import java.io.File;
import java.io.FilenameFilter;
import java.util.regex.Pattern;
/**
* @author ManhKM on 6/21/2022
* @project Java-Utils
*/
public class LogFilter implements FilenameFilter {
private int type = 0;
private String pattern = "";
private Pattern p = null;
public LogFilter(String pattern) {
this.pattern = pattern.replace("*", ""); // Loai bo cac dau * trong log
boolean isStartWith = pattern.startsWith("*");
boolean isEndWith = pattern.endsWith("*");
if (isStartWith && isEndWith) {
this.type = 1;
} else if (isStartWith) {
this.type = 2;
} else if (isEndWith) {
this.type = 3;
} else {
this.p = Pattern.compile(pattern);
}
}
@Override
public boolean accept(File dir, String name) {
switch(this.type) {
case 1:
return name.indexOf(this.pattern) > -1;
case 2:
return name.endsWith(this.pattern);
case 3:
return name.startsWith(this.pattern);
default:
return this.p.matcher(name).find();
}
}
}