1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.settings4j.settings;
21
22 import java.util.ArrayList;
23 import java.util.List;
24 import java.util.regex.Pattern;
25
26 import org.settings4j.Filter;
27
28
29
30
31
32
33 public class DefaultFilter implements Filter {
34
35 private final List<Pattern> includePatterns = new ArrayList<Pattern>();
36 private final List<Pattern> excludePatterns = new ArrayList<Pattern>();
37
38 @Override
39 public void addExclude(final String pattern) {
40 try {
41 final Pattern p = Pattern.compile(pattern);
42 this.excludePatterns.add(p);
43 } catch (final RuntimeException e) {
44 throw new IllegalArgumentException(String.format("cannot compile exclude filter pattern '%s': %s", pattern, e.getMessage()), e);
45 }
46 }
47
48 @Override
49 public void addInclude(final String pattern) {
50 try {
51 final Pattern p = Pattern.compile(pattern);
52 this.includePatterns.add(p);
53 } catch (final RuntimeException e) {
54 throw new IllegalArgumentException(String.format("cannot compile include filter pattern '%s': %s", pattern, e.getMessage()), e);
55 }
56 }
57
58 @Override
59 public boolean isValid(final String key) {
60
61 for (final Pattern pattern : this.excludePatterns) {
62 if (pattern.matcher(key).matches()) {
63 return false;
64 }
65 }
66
67
68 if (this.includePatterns.isEmpty()) {
69 return true;
70 }
71
72
73 for (final Pattern pattern : this.includePatterns) {
74 if (pattern.matcher(key).matches()) {
75 return true;
76 }
77 }
78
79
80 return false;
81 }
82
83 }