a
554325746@qq.com
2019-12-24 570a73851c26d810c2597596a8acc8a8d4cde211
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
package com.obsez.android.lib.filechooser.internals;
import java.io.File;
import java.io.FileFilter;
import java.util.regex.Pattern;
/**
 * Created by coco on 6/7/15.
 */
public class RegexFileFilter implements FileFilter {
    boolean m_allowHidden;
    boolean m_onlyDirectory;
    Pattern m_pattern;
    public RegexFileFilter() {
        this(null);
    }
    public RegexFileFilter(Pattern ptn) {
        this(false, false, ptn);
    }
    public RegexFileFilter(boolean dirOnly, boolean hidden, String ptn) {
        m_allowHidden = hidden;
        m_onlyDirectory = dirOnly;
        m_pattern = Pattern.compile(ptn, Pattern.CASE_INSENSITIVE);
    }
    public RegexFileFilter(boolean dirOnly, boolean hidden, String ptn, int flags) {
        m_allowHidden = hidden;
        m_onlyDirectory = dirOnly;
        m_pattern = Pattern.compile(ptn, flags);
    }
    public RegexFileFilter(boolean dirOnly, boolean hidden, Pattern ptn) {
        m_allowHidden = hidden;
        m_onlyDirectory = dirOnly;
        m_pattern = ptn;
    }
    @Override
    public boolean accept(File pathname) {
        if (!m_allowHidden) {
            if (pathname.isHidden()) {
                return false;
            }
        }
        if (m_onlyDirectory) {
            if (!pathname.isDirectory()) {
                return false;
            }
        }
        if (m_pattern == null) {
            return true;
        }
        if (pathname.isDirectory()) {
            return true;
        }
        String name = pathname.getName();
        if (m_pattern.matcher(name).matches()) {
            return true;
        }
        return false;
    }
}