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
9
* http://www.apache.org/licenses/LICENSE-2.0
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.
17
package org.apache.commons.io.filefilter;
20
import java.io.FileFilter;
21
import java.io.FilenameFilter;
22
import java.io.Serializable;
25
* This class turns a Java FileFilter or FilenameFilter into an IO FileFilter.
28
* @version $Id: DelegateFileFilter.java 1304052 2012-03-22 20:55:29Z ggregory $
30
* @see FileFilterUtils#asFileFilter(FileFilter)
31
* @see FileFilterUtils#asFileFilter(FilenameFilter)
33
public class DelegateFileFilter extends AbstractFileFilter implements Serializable {
35
/** The Filename filter */
36
private final FilenameFilter filenameFilter;
37
/** The File filter */
38
private final FileFilter fileFilter;
41
* Constructs a delegate file filter around an existing FilenameFilter.
43
* @param filter the filter to decorate
45
public DelegateFileFilter(FilenameFilter filter) {
47
throw new IllegalArgumentException("The FilenameFilter must not be null");
49
this.filenameFilter = filter;
50
this.fileFilter = null;
54
* Constructs a delegate file filter around an existing FileFilter.
56
* @param filter the filter to decorate
58
public DelegateFileFilter(FileFilter filter) {
60
throw new IllegalArgumentException("The FileFilter must not be null");
62
this.fileFilter = filter;
63
this.filenameFilter = null;
69
* @param file the file to check
70
* @return true if the filter matches
73
public boolean accept(File file) {
74
if (fileFilter != null) {
75
return fileFilter.accept(file);
77
return super.accept(file);
84
* @param dir the directory
85
* @param name the filename in the directory
86
* @return true if the filter matches
89
public boolean accept(File dir, String name) {
90
if (filenameFilter != null) {
91
return filenameFilter.accept(dir, name);
93
return super.accept(dir, name);
98
* Provide a String representaion of this file filter.
100
* @return a String representaion
103
public String toString() {
104
String delegate = fileFilter != null ? fileFilter.toString() : filenameFilter.toString();
105
return super.toString() + "(" + delegate + ")";