001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.commons.io.filefilter;
018
019import java.io.File;
020import java.io.FileFilter;
021import java.io.FilenameFilter;
022import java.io.Serializable;
023import java.util.Objects;
024
025/**
026 * This class turns a Java FileFilter or FilenameFilter into an IO FileFilter.
027 * <h2>Deprecating Serialization</h2>
028 * <p>
029 * <em>Serialization is deprecated and will be removed in 3.0.</em>
030 * </p>
031 *
032 * @since 1.0
033 * @see FileFilterUtils#asFileFilter(FileFilter)
034 * @see FileFilterUtils#asFileFilter(FilenameFilter)
035 */
036public class DelegateFileFilter extends AbstractFileFilter implements Serializable {
037
038    private static final long serialVersionUID = -8723373124984771318L;
039    /** The File filter */
040    private final FileFilter fileFilter;
041    /** The Filename filter */
042    private final FilenameFilter filenameFilter;
043
044    /**
045     * Constructs a delegate file filter around an existing FileFilter.
046     *
047     * @param fileFilter  the filter to decorate
048     */
049    public DelegateFileFilter(final FileFilter fileFilter) {
050        Objects.requireNonNull(fileFilter, "filter");
051        this.fileFilter = fileFilter;
052        this.filenameFilter = null;
053    }
054
055    /**
056     * Constructs a delegate file filter around an existing FilenameFilter.
057     *
058     * @param filenameFilter  the filter to decorate
059     */
060    public DelegateFileFilter(final FilenameFilter filenameFilter) {
061        Objects.requireNonNull(filenameFilter, "filter");
062        this.filenameFilter = filenameFilter;
063        this.fileFilter = null;
064    }
065
066    /**
067     * Checks the filter.
068     *
069     * @param file  the file to check
070     * @return true if the filter matches
071     */
072    @Override
073    public boolean accept(final File file) {
074        if (fileFilter != null) {
075            return fileFilter.accept(file);
076        }
077        return super.accept(file);
078    }
079
080    /**
081     * Checks the filter.
082     *
083     * @param dir  the directory
084     * @param name  the file name in the directory
085     * @return true if the filter matches
086     */
087    @Override
088    public boolean accept(final File dir, final String name) {
089        if (filenameFilter != null) {
090            return filenameFilter.accept(dir, name);
091        }
092        return super.accept(dir, name);
093    }
094
095    /**
096     * Provide a String representation of this file filter.
097     *
098     * @return a String representation
099     */
100    @Override
101    public String toString() {
102        final String delegate = fileFilter != null ? fileFilter.toString() : filenameFilter.toString();
103        return super.toString() + "(" + delegate + ")";
104    }
105
106}