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 1.0
028 * @version $Id: DelegateFileFilter.java 1304052 2012-03-22 20:55:29Z ggregory $
029 *
030 * @see FileFilterUtils#asFileFilter(FileFilter)
031 * @see FileFilterUtils#asFileFilter(FilenameFilter)
032 */
033 public class DelegateFileFilter extends AbstractFileFilter implements Serializable {
034
035 /** The Filename filter */
036 private final FilenameFilter filenameFilter;
037 /** The File filter */
038 private final FileFilter fileFilter;
039
040 /**
041 * Constructs a delegate file filter around an existing FilenameFilter.
042 *
043 * @param filter the filter to decorate
044 */
045 public DelegateFileFilter(FilenameFilter filter) {
046 if (filter == null) {
047 throw new IllegalArgumentException("The FilenameFilter must not be null");
048 }
049 this.filenameFilter = filter;
050 this.fileFilter = null;
051 }
052
053 /**
054 * Constructs a delegate file filter around an existing FileFilter.
055 *
056 * @param filter the filter to decorate
057 */
058 public DelegateFileFilter(FileFilter filter) {
059 if (filter == null) {
060 throw new IllegalArgumentException("The FileFilter must not be null");
061 }
062 this.fileFilter = filter;
063 this.filenameFilter = 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(File file) {
074 if (fileFilter != null) {
075 return fileFilter.accept(file);
076 } else {
077 return super.accept(file);
078 }
079 }
080
081 /**
082 * Checks the filter.
083 *
084 * @param dir the directory
085 * @param name the filename in the directory
086 * @return true if the filter matches
087 */
088 @Override
089 public boolean accept(File dir, String name) {
090 if (filenameFilter != null) {
091 return filenameFilter.accept(dir, name);
092 } else {
093 return super.accept(dir, name);
094 }
095 }
096
097 /**
098 * Provide a String representaion of this file filter.
099 *
100 * @return a String representaion
101 */
102 @Override
103 public String toString() {
104 String delegate = fileFilter != null ? fileFilter.toString() : filenameFilter.toString();
105 return super.toString() + "(" + delegate + ")";
106 }
107
108 }