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 * @see FileFilterUtils#asFileFilter(FileFilter) 029 * @see FileFilterUtils#asFileFilter(FilenameFilter) 030 */ 031public class DelegateFileFilter extends AbstractFileFilter implements Serializable { 032 033 private static final long serialVersionUID = -8723373124984771318L; 034 /** The File filter */ 035 private final FileFilter fileFilter; 036 /** The Filename filter */ 037 private final FilenameFilter filenameFilter; 038 039 /** 040 * Constructs a delegate file filter around an existing FileFilter. 041 * 042 * @param filter the filter to decorate 043 */ 044 public DelegateFileFilter(final FileFilter filter) { 045 if (filter == null) { 046 throw new IllegalArgumentException("The FileFilter must not be null"); 047 } 048 this.fileFilter = filter; 049 this.filenameFilter = null; 050 } 051 052 /** 053 * Constructs a delegate file filter around an existing FilenameFilter. 054 * 055 * @param filter the filter to decorate 056 */ 057 public DelegateFileFilter(final FilenameFilter filter) { 058 if (filter == null) { 059 throw new IllegalArgumentException("The FilenameFilter must not be null"); 060 } 061 this.filenameFilter = filter; 062 this.fileFilter = null; 063 } 064 065 /** 066 * Checks the filter. 067 * 068 * @param file the file to check 069 * @return true if the filter matches 070 */ 071 @Override 072 public boolean accept(final File file) { 073 if (fileFilter != null) { 074 return fileFilter.accept(file); 075 } 076 return super.accept(file); 077 } 078 079 /** 080 * Checks the filter. 081 * 082 * @param dir the directory 083 * @param name the file name in the directory 084 * @return true if the filter matches 085 */ 086 @Override 087 public boolean accept(final File dir, final String name) { 088 if (filenameFilter != null) { 089 return filenameFilter.accept(dir, name); 090 } 091 return super.accept(dir, name); 092 } 093 094 /** 095 * Provide a String representation of this file filter. 096 * 097 * @return a String representation 098 */ 099 @Override 100 public String toString() { 101 final String delegate = fileFilter != null ? fileFilter.toString() : filenameFilter.toString(); 102 return super.toString() + "(" + delegate + ")"; 103 } 104 105}