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