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 */
017 package org.apache.commons.io.filefilter;
018
019 import java.io.File;
020 import java.io.FileFilter;
021 import java.io.FilenameFilter;
022 import java.io.Serializable;
023
024 /**
025 * This class turns a Java FileFilter or FilenameFilter into an IO FileFilter.
026 *
027 * @since Commons IO 1.0
028 * @version $Revision: 1005099 $ $Date: 2010-10-06 17:13:01 +0100 (Wed, 06 Oct 2010) $
029 *
030 * @author Stephen Colebourne
031 * @see FileFilterUtils#asFileFilter(FileFilter)
032 * @see FileFilterUtils#asFileFilter(FilenameFilter)
033 */
034 public class DelegateFileFilter extends AbstractFileFilter implements Serializable {
035
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(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(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(File file) {
075 if (fileFilter != null) {
076 return fileFilter.accept(file);
077 } else {
078 return super.accept(file);
079 }
080 }
081
082 /**
083 * Checks the filter.
084 *
085 * @param dir the directory
086 * @param name the filename in the directory
087 * @return true if the filter matches
088 */
089 @Override
090 public boolean accept(File dir, String name) {
091 if (filenameFilter != null) {
092 return filenameFilter.accept(dir, name);
093 } else {
094 return super.accept(dir, name);
095 }
096 }
097
098 /**
099 * Provide a String representaion of this file filter.
100 *
101 * @return a String representaion
102 */
103 @Override
104 public String toString() {
105 String delegate = (fileFilter != null ? fileFilter.toString() : filenameFilter.toString());
106 return super.toString() + "(" + delegate + ")";
107 }
108
109 }