1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * https://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17 package org.apache.commons.io.filefilter;
18
19 import java.io.File;
20 import java.io.FileFilter;
21 import java.io.FilenameFilter;
22 import java.io.Serializable;
23 import java.util.Objects;
24
25 /**
26 * This class turns a Java FileFilter or FilenameFilter into an IO FileFilter.
27 * <h2>Deprecating Serialization</h2>
28 * <p>
29 * <em>Serialization is deprecated and will be removed in 3.0.</em>
30 * </p>
31 *
32 * @since 1.0
33 * @see FileFilterUtils#asFileFilter(FileFilter)
34 * @see FileFilterUtils#asFileFilter(FilenameFilter)
35 */
36 public class DelegateFileFilter extends AbstractFileFilter implements Serializable {
37
38 private static final long serialVersionUID = -8723373124984771318L;
39
40 /** The File filter */
41 private final transient FileFilter fileFilter;
42
43 /** The Filename filter */
44 private final transient FilenameFilter fileNameFilter;
45
46 /**
47 * Constructs a delegate file filter around an existing FileFilter.
48 *
49 * @param fileFilter the filter to decorate.
50 */
51 public DelegateFileFilter(final FileFilter fileFilter) {
52 Objects.requireNonNull(fileFilter, "filter");
53 this.fileFilter = fileFilter;
54 this.fileNameFilter = null;
55 }
56
57 /**
58 * Constructs a delegate file filter around an existing FilenameFilter.
59 *
60 * @param fileNameFilter the filter to decorate.
61 */
62 public DelegateFileFilter(final FilenameFilter fileNameFilter) {
63 Objects.requireNonNull(fileNameFilter, "filter");
64 this.fileNameFilter = fileNameFilter;
65 this.fileFilter = null;
66 }
67
68 /**
69 * Tests the filter.
70 *
71 * @param file the file to check.
72 * @return true if the filter matches.
73 */
74 @Override
75 public boolean accept(final File file) {
76 if (fileFilter != null) {
77 return fileFilter.accept(file);
78 }
79 return super.accept(file);
80 }
81
82 /**
83 * Tests the filter.
84 *
85 * @param dir the directory.
86 * @param name the file name in the directory.
87 * @return true if the filter matches.
88 */
89 @Override
90 public boolean accept(final File dir, final String name) {
91 if (fileNameFilter != null) {
92 return fileNameFilter.accept(dir, name);
93 }
94 return super.accept(dir, name);
95 }
96
97 /**
98 * Provide a String representation of this file filter.
99 *
100 * @return a String representation.
101 */
102 @Override
103 public String toString() {
104 final String delegate = Objects.toString(fileFilter, Objects.toString(fileNameFilter, null));
105 return super.toString() + "(" + delegate + ")";
106 }
107
108 }