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
040    /** The File filter */
041    private transient final FileFilter fileFilter;
042    /** The Filename filter */
043    private transient final FilenameFilter fileNameFilter;
044
045    /**
046     * Constructs a delegate file filter around an existing FileFilter.
047     *
048     * @param fileFilter  the filter to decorate
049     */
050    public DelegateFileFilter(final FileFilter fileFilter) {
051        Objects.requireNonNull(fileFilter, "filter");
052        this.fileFilter = fileFilter;
053        this.fileNameFilter = null;
054    }
055
056    /**
057     * Constructs a delegate file filter around an existing FilenameFilter.
058     *
059     * @param fileNameFilter  the filter to decorate
060     */
061    public DelegateFileFilter(final FilenameFilter fileNameFilter) {
062        Objects.requireNonNull(fileNameFilter, "filter");
063        this.fileNameFilter = fileNameFilter;
064        this.fileFilter = null;
065    }
066
067    /**
068     * Checks the filter.
069     *
070     * @param file  the file to check
071     * @return true if the filter matches
072     */
073    @Override
074    public boolean accept(final File file) {
075        if (fileFilter != null) {
076            return fileFilter.accept(file);
077        }
078        return super.accept(file);
079    }
080
081    /**
082     * Checks the filter.
083     *
084     * @param dir  the directory
085     * @param name  the file name in the directory
086     * @return true if the filter matches
087     */
088    @Override
089    public boolean accept(final File dir, final String name) {
090        if (fileNameFilter != null) {
091            return fileNameFilter.accept(dir, name);
092        }
093        return super.accept(dir, name);
094    }
095
096    /**
097     * Provide a String representation of this file filter.
098     *
099     * @return a String representation
100     */
101    @Override
102    public String toString() {
103        final String delegate = fileFilter != null ? fileFilter.toString() : fileNameFilter.toString();
104        return super.toString() + "(" + delegate + ")";
105    }
106
107}