Skip to content

Commit 480ab12

Browse files
author
Gary Gregory
committed
Simplify construction implementation.
1 parent 94be75e commit 480ab12

1 file changed

Lines changed: 47 additions & 23 deletions

File tree

src/main/java/org/apache/commons/io/filefilter/RegexFileFilter.java

Lines changed: 47 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,40 @@
6868
public class RegexFileFilter extends AbstractFileFilter implements Serializable {
6969

7070
private static final long serialVersionUID = 4269646126155225062L;
71+
72+
/**
73+
* Compiles the given pattern source.
74+
*
75+
* @param pattern the source pattern
76+
* @param flags the compilation flags.
77+
* @return a new Pattern.
78+
*/
79+
private static Pattern compile(final String pattern, final int flags) {
80+
if (pattern == null) {
81+
throw new IllegalArgumentException("Pattern is missing");
82+
}
83+
return Pattern.compile(pattern, flags);
84+
}
85+
86+
/**
87+
* Converts IOCase to Pattern compilation flags.
88+
*
89+
* @param caseSensitivity case-sensitivity.
90+
* @return Pattern compilation flags.
91+
*/
92+
private static int toFlags(final IOCase caseSensitivity) {
93+
int flags = 0;
94+
if (caseSensitivity != null && !caseSensitivity.isCaseSensitive()) {
95+
flags = Pattern.CASE_INSENSITIVE;
96+
}
97+
return flags;
98+
}
99+
71100
/** The regular expression pattern that will be used to match file names */
72101
private final Pattern pattern;
73102

74103
/**
75-
* Construct a new regular expression filter for a compiled regular expression
104+
* Constructs a new regular expression filter for a compiled regular expression
76105
*
77106
* @param pattern regular expression to match
78107
* @throws IllegalArgumentException if the pattern is null
@@ -81,54 +110,39 @@ public RegexFileFilter(final Pattern pattern) {
81110
if (pattern == null) {
82111
throw new IllegalArgumentException("Pattern is missing");
83112
}
84-
85113
this.pattern = pattern;
86114
}
87115

88116
/**
89-
* Construct a new regular expression filter.
117+
* Constructs a new regular expression filter.
90118
*
91119
* @param pattern regular string expression to match
92120
* @throws IllegalArgumentException if the pattern is null
93121
*/
94122
public RegexFileFilter(final String pattern) {
95-
if (pattern == null) {
96-
throw new IllegalArgumentException("Pattern is missing");
97-
}
98-
99-
this.pattern = Pattern.compile(pattern);
123+
this(pattern, 0);
100124
}
101125

102126
/**
103-
* Construct a new regular expression filter with the specified flags.
127+
* Constructs a new regular expression filter with the specified flags.
104128
*
105129
* @param pattern regular string expression to match
106130
* @param flags pattern flags - e.g. {@link Pattern#CASE_INSENSITIVE}
107131
* @throws IllegalArgumentException if the pattern is null
108132
*/
109133
public RegexFileFilter(final String pattern, final int flags) {
110-
if (pattern == null) {
111-
throw new IllegalArgumentException("Pattern is missing");
112-
}
113-
this.pattern = Pattern.compile(pattern, flags);
134+
this(compile(pattern, flags));
114135
}
115136

116137
/**
117-
* Construct a new regular expression filter with the specified flags case sensitivity.
138+
* Constructs a new regular expression filter with the specified flags case sensitivity.
118139
*
119140
* @param pattern regular string expression to match
120141
* @param caseSensitivity how to handle case sensitivity, null means case-sensitive
121142
* @throws IllegalArgumentException if the pattern is null
122143
*/
123144
public RegexFileFilter(final String pattern, final IOCase caseSensitivity) {
124-
if (pattern == null) {
125-
throw new IllegalArgumentException("Pattern is missing");
126-
}
127-
int flags = 0;
128-
if (caseSensitivity != null && !caseSensitivity.isCaseSensitive()) {
129-
flags = Pattern.CASE_INSENSITIVE;
130-
}
131-
this.pattern = Pattern.compile(pattern, flags);
145+
this(compile(pattern, toFlags(caseSensitivity)));
132146
}
133147

134148
/**
@@ -152,7 +166,17 @@ public boolean accept(final File dir, final String name) {
152166
*/
153167
@Override
154168
public FileVisitResult accept(final Path path, final BasicFileAttributes attributes) {
155-
return toFileVisitResult(pattern.matcher(path.toString()).matches(), path);
169+
return toFileVisitResult(pattern.matcher(path.getFileName().toString()).matches(), path);
170+
}
171+
172+
/**
173+
* Returns a debug string.
174+
*
175+
* @since 2.10.0
176+
*/
177+
@Override
178+
public String toString() {
179+
return "RegexFileFilter [pattern=" + pattern + "]";
156180
}
157181

158182
}

0 commit comments

Comments
 (0)