    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;
    023
    024/**
    025 * This class turns a Java FileFilter or FilenameFilter into an IO FileFilter.
    026 *
    027 * @since 1.0
    028 *
    029 *
    030 * @see FileFilterUtils#asFileFilter(FileFilter)
    031 * @see FileFilterUtils#asFileFilter(FilenameFilter)
    032 */
    033public class DelegateFileFilter extends AbstractFileFilter implements Serializable {
    034
    035    private static final long serialVersionUID = -8723373124984771318L;
    036    /** The Filename filter */
    037    private final FilenameFilter filenameFilter;
    038    /** The File filter */
    039    private final FileFilter fileFilter;
    040
    041    /**
    042     * Constructs a delegate file filter around an existing FilenameFilter.
    043     *
    044     * @param filter  the filter to decorate
    045     */
    046    public DelegateFileFilter(final FilenameFilter filter) {
    047        if (filter == null) {
    048            throw new IllegalArgumentException("The FilenameFilter must not be null");
    049        }
    050        this.filenameFilter = filter;
    051        this.fileFilter = null;
    052    }
    053
    054    /**
    055     * Constructs a delegate file filter around an existing FileFilter.
    056     *
    057     * @param filter  the filter to decorate
    058     */
    059    public DelegateFileFilter(final FileFilter filter) {
    060        if (filter == null) {
    061            throw new IllegalArgumentException("The FileFilter must not be null");
    062        }
    063        this.fileFilter = filter;
    064        this.filenameFilter = 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}