Skip to content

Commit 338e571

Browse files
committed
Replacing custom Filter class with Predicate
1 parent 0fdb500 commit 338e571

1 file changed

Lines changed: 10 additions & 36 deletions

File tree

java/client/src/org/openqa/selenium/io/FileHandler.java

Lines changed: 10 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import java.io.Reader;
3333
import java.nio.file.Files;
3434
import java.util.List;
35+
import java.util.function.Predicate;
3536

3637
/**
3738
* Utility methods for common filesystem activities
@@ -129,14 +130,16 @@ public static boolean delete(File toDelete) {
129130
}
130131

131132
public static void copy(File from, File to) throws IOException {
132-
copy(from, to, new NoFilter());
133+
copy(from, to, (file) -> true);
133134
}
134135

135136
public static void copy(File source, File dest, String suffix) throws IOException {
136-
copy(source, dest, suffix == null ? new NoFilter() : new FileSuffixFilter(suffix));
137+
copy(source, dest, suffix == null
138+
? (file) -> true
139+
: (file) -> file.isDirectory() || file.getAbsolutePath().endsWith(suffix));
137140
}
138141

139-
private static void copy(File source, File dest, Filter onlyCopy) throws IOException {
142+
private static void copy(File source, File dest, Predicate<File> onlyCopy) throws IOException {
140143
if (!source.exists()) {
141144
return;
142145
}
@@ -148,8 +151,8 @@ private static void copy(File source, File dest, Filter onlyCopy) throws IOExcep
148151
}
149152
}
150153

151-
private static void copyDir(File from, File to, Filter onlyCopy) throws IOException {
152-
if (!onlyCopy.isRequired(from)) {
154+
private static void copyDir(File from, File to, Predicate<File> onlyCopy) throws IOException {
155+
if (!onlyCopy.test(from)) {
153156
return;
154157
}
155158

@@ -168,8 +171,8 @@ private static void copyDir(File from, File to, Filter onlyCopy) throws IOExcept
168171
}
169172
}
170173

171-
private static void copyFile(File from, File to, Filter onlyCopy) throws IOException {
172-
if (!onlyCopy.isRequired(from)) {
174+
private static void copyFile(File from, File to, Predicate<File> onlyCopy) throws IOException {
175+
if (!onlyCopy.test(from)) {
173176
return;
174177
}
175178

@@ -182,35 +185,6 @@ private static void copyFile(File from, File to, Filter onlyCopy) throws IOExcep
182185
}
183186
}
184187

185-
/**
186-
* Used by file operations to determine whether or not to make use of a file.
187-
*/
188-
public interface Filter {
189-
/**
190-
* @param file File to be considered.
191-
* @return Whether or not to make use of the file in this oprtation.
192-
*/
193-
boolean isRequired(File file);
194-
}
195-
196-
private static class FileSuffixFilter implements Filter {
197-
private final String suffix;
198-
199-
public FileSuffixFilter(String suffix) {
200-
this.suffix = suffix;
201-
}
202-
203-
public boolean isRequired(File file) {
204-
return file.isDirectory() || file.getAbsolutePath().endsWith(suffix);
205-
}
206-
}
207-
208-
private static class NoFilter implements Filter {
209-
public boolean isRequired(File file) {
210-
return true;
211-
}
212-
}
213-
214188
public static String readAsString(File toRead) throws IOException {
215189
try (Reader reader = new BufferedReader(new FileReader(toRead))) {
216190
StringBuilder builder = new StringBuilder();

0 commit comments

Comments
 (0)