一、前言
通过apahce的commons-io包中org.apache.commons.io.filefilter.FileFilterUtils文件过滤工具类基于java.io.FileFilter文件过滤器接口对文件基本基于过滤器filter的处理、前缀过滤prefixFileFilter、文件名称过滤nameFileFilter、兼容两种过滤器处理orFileFilter、大小范围处理sizeRangeFileFilter、文件只读处理makeFileOnly等操作。
二、源码说明
1.FileFilterUtils
package org.apache.commons.io.filefilter;@b@@b@import java.io.File;@b@import java.io.FileFilter;@b@import java.io.FilenameFilter;@b@import java.util.ArrayList;@b@import java.util.Arrays;@b@import java.util.Collection;@b@import java.util.Date;@b@import java.util.HashSet;@b@import java.util.List;@b@import java.util.Set;@b@import org.apache.commons.io.IOCase;@b@@b@public class FileFilterUtils@b@{@b@ private static final IOFileFilter cvsFilter = notFileFilter(and(new IOFileFilter[] { directoryFileFilter(), nameFileFilter("CVS") }));@b@ private static final IOFileFilter svnFilter = notFileFilter(and(new IOFileFilter[] { directoryFileFilter(), nameFileFilter(".svn") }));@b@@b@ public static File[] filter(IOFileFilter filter, File[] files)@b@ {@b@ if (filter == null)@b@ throw new IllegalArgumentException("file filter is null");@b@@b@ if (files == null)@b@ return new File[0];@b@@b@ List acceptedFiles = new ArrayList();@b@ File[] arr$ = files; int len$ = arr$.length; for (int i$ = 0; i$ < len$; ++i$) { File file = arr$[i$];@b@ if (file == null)@b@ throw new IllegalArgumentException("file array contains null");@b@@b@ if (filter.accept(file))@b@ acceptedFiles.add(file);@b@ }@b@@b@ return ((File[])acceptedFiles.toArray(new File[acceptedFiles.size()]));@b@ }@b@@b@ public static File[] filter(IOFileFilter filter, Iterable<File> files)@b@ {@b@ List acceptedFiles = filterList(filter, files);@b@ return ((File[])acceptedFiles.toArray(new File[acceptedFiles.size()]));@b@ }@b@@b@ public static List<File> filterList(IOFileFilter filter, Iterable<File> files)@b@ {@b@ return ((List)filter(filter, files, new ArrayList()));@b@ }@b@@b@ public static List<File> filterList(IOFileFilter filter, File[] files)@b@ {@b@ File[] acceptedFiles = filter(filter, files);@b@ return Arrays.asList(acceptedFiles);@b@ }@b@@b@ public static Set<File> filterSet(IOFileFilter filter, File[] files)@b@ {@b@ File[] acceptedFiles = filter(filter, files);@b@ return new HashSet(Arrays.asList(acceptedFiles));@b@ }@b@@b@ public static Set<File> filterSet(IOFileFilter filter, Iterable<File> files)@b@ {@b@ return ((Set)filter(filter, files, new HashSet()));@b@ }@b@@b@ private static <T extends Collection<File>> T filter(IOFileFilter filter, Iterable<File> files, T acceptedFiles)@b@ {@b@ if (filter == null)@b@ throw new IllegalArgumentException("file filter is null");@b@@b@ if (files != null)@b@ for (File file : files) {@b@ if (file == null)@b@ throw new IllegalArgumentException("file collection contains null");@b@@b@ if (filter.accept(file))@b@ acceptedFiles.add(file);@b@ }@b@@b@@b@ return acceptedFiles;@b@ }@b@@b@ public static IOFileFilter prefixFileFilter(String prefix)@b@ {@b@ return new PrefixFileFilter(prefix);@b@ }@b@@b@ public static IOFileFilter prefixFileFilter(String prefix, IOCase caseSensitivity)@b@ {@b@ return new PrefixFileFilter(prefix, caseSensitivity);@b@ }@b@@b@ public static IOFileFilter suffixFileFilter(String suffix)@b@ {@b@ return new SuffixFileFilter(suffix);@b@ }@b@@b@ public static IOFileFilter suffixFileFilter(String suffix, IOCase caseSensitivity)@b@ {@b@ return new SuffixFileFilter(suffix, caseSensitivity);@b@ }@b@@b@ public static IOFileFilter nameFileFilter(String name)@b@ {@b@ return new NameFileFilter(name);@b@ }@b@@b@ public static IOFileFilter nameFileFilter(String name, IOCase caseSensitivity)@b@ {@b@ return new NameFileFilter(name, caseSensitivity);@b@ }@b@@b@ public static IOFileFilter directoryFileFilter()@b@ {@b@ return DirectoryFileFilter.DIRECTORY;@b@ }@b@@b@ public static IOFileFilter fileFileFilter()@b@ {@b@ return FileFileFilter.FILE;@b@ }@b@@b@ @Deprecated@b@ public static IOFileFilter andFileFilter(IOFileFilter filter1, IOFileFilter filter2)@b@ {@b@ return new AndFileFilter(filter1, filter2);@b@ }@b@@b@ @Deprecated@b@ public static IOFileFilter orFileFilter(IOFileFilter filter1, IOFileFilter filter2)@b@ {@b@ return new OrFileFilter(filter1, filter2);@b@ }@b@@b@ public static IOFileFilter and(IOFileFilter[] filters)@b@ {@b@ return new AndFileFilter(toList(filters));@b@ }@b@@b@ public static IOFileFilter or(IOFileFilter[] filters)@b@ {@b@ return new OrFileFilter(toList(filters));@b@ }@b@@b@ public static List<IOFileFilter> toList(IOFileFilter[] filters)@b@ {@b@ if (filters == null)@b@ throw new IllegalArgumentException("The filters must not be null");@b@@b@ List list = new ArrayList(filters.length);@b@ for (int i = 0; i < filters.length; ++i) {@b@ if (filters[i] == null)@b@ throw new IllegalArgumentException("The filter[" + i + "] is null");@b@@b@ list.add(filters[i]);@b@ }@b@ return list;@b@ }@b@@b@ public static IOFileFilter notFileFilter(IOFileFilter filter)@b@ {@b@ return new NotFileFilter(filter);@b@ }@b@@b@ public static IOFileFilter trueFileFilter()@b@ {@b@ return TrueFileFilter.TRUE;@b@ }@b@@b@ public static IOFileFilter falseFileFilter()@b@ {@b@ return FalseFileFilter.FALSE;@b@ }@b@@b@ public static IOFileFilter asFileFilter(FileFilter filter)@b@ {@b@ return new DelegateFileFilter(filter);@b@ }@b@@b@ public static IOFileFilter asFileFilter(FilenameFilter filter)@b@ {@b@ return new DelegateFileFilter(filter);@b@ }@b@@b@ public static IOFileFilter ageFileFilter(long cutoff)@b@ {@b@ return new AgeFileFilter(cutoff);@b@ }@b@@b@ public static IOFileFilter ageFileFilter(long cutoff, boolean acceptOlder)@b@ {@b@ return new AgeFileFilter(cutoff, acceptOlder);@b@ }@b@@b@ public static IOFileFilter ageFileFilter(Date cutoffDate)@b@ {@b@ return new AgeFileFilter(cutoffDate);@b@ }@b@@b@ public static IOFileFilter ageFileFilter(Date cutoffDate, boolean acceptOlder)@b@ {@b@ return new AgeFileFilter(cutoffDate, acceptOlder);@b@ }@b@@b@ public static IOFileFilter ageFileFilter(File cutoffReference)@b@ {@b@ return new AgeFileFilter(cutoffReference);@b@ }@b@@b@ public static IOFileFilter ageFileFilter(File cutoffReference, boolean acceptOlder)@b@ {@b@ return new AgeFileFilter(cutoffReference, acceptOlder);@b@ }@b@@b@ public static IOFileFilter sizeFileFilter(long threshold)@b@ {@b@ return new SizeFileFilter(threshold);@b@ }@b@@b@ public static IOFileFilter sizeFileFilter(long threshold, boolean acceptLarger)@b@ {@b@ return new SizeFileFilter(threshold, acceptLarger);@b@ }@b@@b@ public static IOFileFilter sizeRangeFileFilter(long minSizeInclusive, long maxSizeInclusive)@b@ {@b@ IOFileFilter minimumFilter = new SizeFileFilter(minSizeInclusive, true);@b@ IOFileFilter maximumFilter = new SizeFileFilter(maxSizeInclusive + 1L, false);@b@ return new AndFileFilter(minimumFilter, maximumFilter);@b@ }@b@@b@ public static IOFileFilter magicNumberFileFilter(String magicNumber)@b@ {@b@ return new MagicNumberFileFilter(magicNumber);@b@ }@b@@b@ public static IOFileFilter magicNumberFileFilter(String magicNumber, long offset)@b@ {@b@ return new MagicNumberFileFilter(magicNumber, offset);@b@ }@b@@b@ public static IOFileFilter magicNumberFileFilter(byte[] magicNumber)@b@ {@b@ return new MagicNumberFileFilter(magicNumber);@b@ }@b@@b@ public static IOFileFilter magicNumberFileFilter(byte[] magicNumber, long offset)@b@ {@b@ return new MagicNumberFileFilter(magicNumber, offset);@b@ }@b@@b@ public static IOFileFilter makeCVSAware(IOFileFilter filter)@b@ {@b@ if (filter == null)@b@ return cvsFilter;@b@@b@ return and(new IOFileFilter[] { filter, cvsFilter });@b@ }@b@@b@ public static IOFileFilter makeSVNAware(IOFileFilter filter)@b@ {@b@ if (filter == null)@b@ return svnFilter;@b@@b@ return and(new IOFileFilter[] { filter, svnFilter });@b@ }@b@@b@ public static IOFileFilter makeDirectoryOnly(IOFileFilter filter)@b@ {@b@ if (filter == null)@b@ return DirectoryFileFilter.DIRECTORY;@b@@b@ return new AndFileFilter(DirectoryFileFilter.DIRECTORY, filter);@b@ }@b@@b@ public static IOFileFilter makeFileOnly(IOFileFilter filter)@b@ {@b@ if (filter == null)@b@ return FileFileFilter.FILE;@b@@b@ return new AndFileFilter(FileFileFilter.FILE, filter);@b@ }@b@}
2.IOFileFilter接口
package org.apache.commons.io.filefilter;@b@@b@import java.io.File;@b@import java.io.FileFilter;@b@import java.io.FilenameFilter;@b@@b@public abstract interface IOFileFilter extends FileFilter, FilenameFilter@b@{@b@ public abstract boolean accept(File paramFile);@b@@b@ public abstract boolean accept(File paramFile, String paramString);@b@}
3.PrefixFileFilter
package org.apache.commons.io.filefilter;@b@@b@import java.io.File;@b@import java.io.Serializable;@b@import java.util.List;@b@import org.apache.commons.io.IOCase;@b@@b@public class PrefixFileFilter extends AbstractFileFilter@b@ implements Serializable@b@{@b@ private final String[] prefixes;@b@ private final IOCase caseSensitivity;@b@@b@ public PrefixFileFilter(String prefix)@b@ {@b@ this(prefix, IOCase.SENSITIVE);@b@ }@b@@b@ public PrefixFileFilter(String prefix, IOCase caseSensitivity)@b@ {@b@ if (prefix == null)@b@ throw new IllegalArgumentException("The prefix must not be null");@b@@b@ this.prefixes = new String[] { prefix };@b@ this.caseSensitivity = ((caseSensitivity == null) ? IOCase.SENSITIVE : caseSensitivity);@b@ }@b@@b@ public PrefixFileFilter(String[] prefixes)@b@ {@b@ this(prefixes, IOCase.SENSITIVE);@b@ }@b@@b@ public PrefixFileFilter(String[] prefixes, IOCase caseSensitivity)@b@ {@b@ if (prefixes == null)@b@ throw new IllegalArgumentException("The array of prefixes must not be null");@b@@b@ this.prefixes = new String[prefixes.length];@b@ System.arraycopy(prefixes, 0, this.prefixes, 0, prefixes.length);@b@ this.caseSensitivity = ((caseSensitivity == null) ? IOCase.SENSITIVE : caseSensitivity);@b@ }@b@@b@ public PrefixFileFilter(List<String> prefixes)@b@ {@b@ this(prefixes, IOCase.SENSITIVE);@b@ }@b@@b@ public PrefixFileFilter(List<String> prefixes, IOCase caseSensitivity)@b@ {@b@ if (prefixes == null)@b@ throw new IllegalArgumentException("The list of prefixes must not be null");@b@@b@ this.prefixes = ((String[])prefixes.toArray(new String[prefixes.size()]));@b@ this.caseSensitivity = ((caseSensitivity == null) ? IOCase.SENSITIVE : caseSensitivity);@b@ }@b@@b@ public boolean accept(File file)@b@ {@b@ String name = file.getName();@b@ String[] arr$ = this.prefixes; int len$ = arr$.length; for (int i$ = 0; i$ < len$; ++i$) { String prefix = arr$[i$];@b@ if (this.caseSensitivity.checkStartsWith(name, prefix))@b@ return true;@b@ }@b@@b@ return false;@b@ }@b@@b@ public boolean accept(File file, String name)@b@ {@b@ String[] arr$ = this.prefixes; int len$ = arr$.length; for (int i$ = 0; i$ < len$; ++i$) { String prefix = arr$[i$];@b@ if (this.caseSensitivity.checkStartsWith(name, prefix))@b@ return true;@b@ }@b@@b@ return false;@b@ }@b@@b@ public String toString()@b@ {@b@ int i;@b@ StringBuilder buffer = new StringBuilder();@b@ buffer.append(super.toString());@b@ buffer.append("(");@b@ if (this.prefixes != null)@b@ for (i = 0; i < this.prefixes.length; ++i) {@b@ if (i > 0)@b@ buffer.append(",");@b@@b@ buffer.append(this.prefixes[i]);@b@ }@b@@b@ buffer.append(")");@b@ return buffer.toString();@b@ }@b@}
4.NameFileFilter
package org.apache.commons.io.filefilter;@b@@b@import java.io.File;@b@import java.io.Serializable;@b@import java.util.List;@b@import org.apache.commons.io.IOCase;@b@@b@public class NameFileFilter extends AbstractFileFilter@b@ implements Serializable@b@{@b@ private final String[] names;@b@ private final IOCase caseSensitivity;@b@@b@ public NameFileFilter(String name)@b@ {@b@ this(name, null);@b@ }@b@@b@ public NameFileFilter(String name, IOCase caseSensitivity)@b@ {@b@ if (name == null)@b@ throw new IllegalArgumentException("The wildcard must not be null");@b@@b@ this.names = new String[] { name };@b@ this.caseSensitivity = ((caseSensitivity == null) ? IOCase.SENSITIVE : caseSensitivity);@b@ }@b@@b@ public NameFileFilter(String[] names)@b@ {@b@ this(names, null);@b@ }@b@@b@ public NameFileFilter(String[] names, IOCase caseSensitivity)@b@ {@b@ if (names == null)@b@ throw new IllegalArgumentException("The array of names must not be null");@b@@b@ this.names = new String[names.length];@b@ System.arraycopy(names, 0, this.names, 0, names.length);@b@ this.caseSensitivity = ((caseSensitivity == null) ? IOCase.SENSITIVE : caseSensitivity);@b@ }@b@@b@ public NameFileFilter(List<String> names)@b@ {@b@ this(names, null);@b@ }@b@@b@ public NameFileFilter(List<String> names, IOCase caseSensitivity)@b@ {@b@ if (names == null)@b@ throw new IllegalArgumentException("The list of names must not be null");@b@@b@ this.names = ((String[])names.toArray(new String[names.size()]));@b@ this.caseSensitivity = ((caseSensitivity == null) ? IOCase.SENSITIVE : caseSensitivity);@b@ }@b@@b@ public boolean accept(File file)@b@ {@b@ String name = file.getName();@b@ String[] arr$ = this.names; int len$ = arr$.length; for (int i$ = 0; i$ < len$; ++i$) { String name2 = arr$[i$];@b@ if (this.caseSensitivity.checkEquals(name, name2))@b@ return true;@b@ }@b@@b@ return false;@b@ }@b@@b@ public boolean accept(File file, String name)@b@ {@b@ String[] arr$ = this.names; int len$ = arr$.length; for (int i$ = 0; i$ < len$; ++i$) { String name2 = arr$[i$];@b@ if (this.caseSensitivity.checkEquals(name, name2))@b@ return true;@b@ }@b@@b@ return false;@b@ }@b@@b@ public String toString()@b@ {@b@ int i;@b@ StringBuilder buffer = new StringBuilder();@b@ buffer.append(super.toString());@b@ buffer.append("(");@b@ if (this.names != null)@b@ for (i = 0; i < this.names.length; ++i) {@b@ if (i > 0)@b@ buffer.append(",");@b@@b@ buffer.append(this.names[i]);@b@ }@b@@b@ buffer.append(")");@b@ return buffer.toString();@b@ }@b@}
5.SuffixFileFilter
package org.apache.commons.io.filefilter;@b@@b@import java.io.File;@b@import java.io.Serializable;@b@import java.util.List;@b@import org.apache.commons.io.IOCase;@b@@b@public class SuffixFileFilter extends AbstractFileFilter@b@ implements Serializable@b@{@b@ private final String[] suffixes;@b@ private final IOCase caseSensitivity;@b@@b@ public SuffixFileFilter(String suffix)@b@ {@b@ this(suffix, IOCase.SENSITIVE);@b@ }@b@@b@ public SuffixFileFilter(String suffix, IOCase caseSensitivity)@b@ {@b@ if (suffix == null)@b@ throw new IllegalArgumentException("The suffix must not be null");@b@@b@ this.suffixes = new String[] { suffix };@b@ this.caseSensitivity = ((caseSensitivity == null) ? IOCase.SENSITIVE : caseSensitivity);@b@ }@b@@b@ public SuffixFileFilter(String[] suffixes)@b@ {@b@ this(suffixes, IOCase.SENSITIVE);@b@ }@b@@b@ public SuffixFileFilter(String[] suffixes, IOCase caseSensitivity)@b@ {@b@ if (suffixes == null)@b@ throw new IllegalArgumentException("The array of suffixes must not be null");@b@@b@ this.suffixes = new String[suffixes.length];@b@ System.arraycopy(suffixes, 0, this.suffixes, 0, suffixes.length);@b@ this.caseSensitivity = ((caseSensitivity == null) ? IOCase.SENSITIVE : caseSensitivity);@b@ }@b@@b@ public SuffixFileFilter(List<String> suffixes)@b@ {@b@ this(suffixes, IOCase.SENSITIVE);@b@ }@b@@b@ public SuffixFileFilter(List<String> suffixes, IOCase caseSensitivity)@b@ {@b@ if (suffixes == null)@b@ throw new IllegalArgumentException("The list of suffixes must not be null");@b@@b@ this.suffixes = ((String[])suffixes.toArray(new String[suffixes.size()]));@b@ this.caseSensitivity = ((caseSensitivity == null) ? IOCase.SENSITIVE : caseSensitivity);@b@ }@b@@b@ public boolean accept(File file)@b@ {@b@ String name = file.getName();@b@ String[] arr$ = this.suffixes; int len$ = arr$.length; for (int i$ = 0; i$ < len$; ++i$) { String suffix = arr$[i$];@b@ if (this.caseSensitivity.checkEndsWith(name, suffix))@b@ return true;@b@ }@b@@b@ return false;@b@ }@b@@b@ public boolean accept(File file, String name)@b@ {@b@ String[] arr$ = this.suffixes; int len$ = arr$.length; for (int i$ = 0; i$ < len$; ++i$) { String suffix = arr$[i$];@b@ if (this.caseSensitivity.checkEndsWith(name, suffix))@b@ return true;@b@ }@b@@b@ return false;@b@ }@b@@b@ public String toString()@b@ {@b@ int i;@b@ StringBuilder buffer = new StringBuilder();@b@ buffer.append(super.toString());@b@ buffer.append("(");@b@ if (this.suffixes != null)@b@ for (i = 0; i < this.suffixes.length; ++i) {@b@ if (i > 0)@b@ buffer.append(",");@b@@b@ buffer.append(this.suffixes[i]);@b@ }@b@@b@ buffer.append(")");@b@ return buffer.toString();@b@ }@b@}
6.SizeFileFilter
package org.apache.commons.io.filefilter;@b@@b@import java.io.File;@b@import java.io.Serializable;@b@@b@public class SizeFileFilter extends AbstractFileFilter@b@ implements Serializable@b@{@b@ private final long size;@b@ private final boolean acceptLarger;@b@@b@ public SizeFileFilter(long size)@b@ {@b@ this(size, true);@b@ }@b@@b@ public SizeFileFilter(long size, boolean acceptLarger)@b@ {@b@ if (size < 0L)@b@ throw new IllegalArgumentException("The size must be non-negative");@b@@b@ this.size = size;@b@ this.acceptLarger = acceptLarger;@b@ }@b@@b@ public boolean accept(File file)@b@ {@b@ boolean smaller = file.length() < this.size;@b@ return ((this.acceptLarger) ? false : (!(smaller)) ? true : smaller);@b@ }@b@@b@ public String toString()@b@ {@b@ String condition = (this.acceptLarger) ? ">=" : "<";@b@ return super.toString() + "(" + condition + this.size + ")";@b@ }@b@}
7.MagicNumberFileFilter
package org.apache.commons.io.filefilter;@b@@b@import java.io.File;@b@import java.io.IOException;@b@import java.io.RandomAccessFile;@b@import java.io.Serializable;@b@import java.util.Arrays;@b@import org.apache.commons.io.IOUtils;@b@@b@public class MagicNumberFileFilter extends AbstractFileFilter@b@ implements Serializable@b@{@b@ private static final long serialVersionUID = -547733176983104172L;@b@ private final byte[] magicNumbers;@b@ private final long byteOffset;@b@@b@ public MagicNumberFileFilter(byte[] magicNumber)@b@ {@b@ this(magicNumber, 0L);@b@ }@b@@b@ public MagicNumberFileFilter(String magicNumber)@b@ {@b@ this(magicNumber, 0L);@b@ }@b@@b@ public MagicNumberFileFilter(String magicNumber, long offset)@b@ {@b@ if (magicNumber == null)@b@ throw new IllegalArgumentException("The magic number cannot be null");@b@@b@ if (magicNumber.length() == 0)@b@ throw new IllegalArgumentException("The magic number must contain at least one byte");@b@@b@ if (offset < 0L) {@b@ throw new IllegalArgumentException("The offset cannot be negative");@b@ }@b@@b@ this.magicNumbers = magicNumber.getBytes();@b@ this.byteOffset = offset;@b@ }@b@@b@ public MagicNumberFileFilter(byte[] magicNumber, long offset)@b@ {@b@ if (magicNumber == null)@b@ throw new IllegalArgumentException("The magic number cannot be null");@b@@b@ if (magicNumber.length == 0)@b@ throw new IllegalArgumentException("The magic number must contain at least one byte");@b@@b@ if (offset < 0L) {@b@ throw new IllegalArgumentException("The offset cannot be negative");@b@ }@b@@b@ this.magicNumbers = new byte[magicNumber.length];@b@ System.arraycopy(magicNumber, 0, this.magicNumbers, 0, magicNumber.length);@b@ this.byteOffset = offset;@b@ }@b@@b@ public boolean accept(File file)@b@ {@b@ if ((file != null) && (file.isFile()) && (file.canRead())) {@b@ RandomAccessFile randomAccessFile = null;@b@ try {@b@ byte[] fileBytes = new byte[this.magicNumbers.length];@b@ randomAccessFile = new RandomAccessFile(file, "r");@b@ randomAccessFile.seek(this.byteOffset);@b@ int read = randomAccessFile.read(fileBytes);@b@ if (read != this.magicNumbers.length) {@b@ bool = false;@b@@b@ return bool;@b@ }@b@ boolean bool = Arrays.equals(this.magicNumbers, fileBytes);@b@@b@ return bool; } catch (IOException ioe) { } finally { IOUtils.closeQuietly(randomAccessFile);@b@ }@b@ }@b@@b@ return false;@b@ }@b@@b@ public String toString()@b@ {@b@ StringBuilder builder = new StringBuilder(super.toString());@b@ builder.append("(");@b@ builder.append(new String(this.magicNumbers));@b@ builder.append(",");@b@ builder.append(this.byteOffset);@b@ builder.append(")");@b@ return builder.toString();@b@ }@b@}
8.DirectoryFileFilter
package org.apache.commons.io.filefilter;@b@@b@import java.io.File;@b@import java.io.Serializable;@b@@b@public class DirectoryFileFilter extends AbstractFileFilter@b@ implements Serializable@b@{@b@ public static final IOFileFilter DIRECTORY = new DirectoryFileFilter();@b@ public static final IOFileFilter INSTANCE = DIRECTORY;@b@@b@ public boolean accept(File file)@b@ {@b@ return file.isDirectory();@b@ }@b@}
9.AndFileFilter
package org.apache.commons.io.filefilter;@b@@b@import java.io.File;@b@import java.io.Serializable;@b@import java.util.ArrayList;@b@import java.util.Collections;@b@import java.util.List;@b@@b@public class AndFileFilter extends AbstractFileFilter@b@ implements ConditionalFileFilter, Serializable@b@{@b@ private final List<IOFileFilter> fileFilters;@b@@b@ public AndFileFilter()@b@ {@b@ this.fileFilters = new ArrayList();@b@ }@b@@b@ public AndFileFilter(List<IOFileFilter> fileFilters)@b@ {@b@ if (fileFilters == null)@b@ this.fileFilters = new ArrayList();@b@ else@b@ this.fileFilters = new ArrayList(fileFilters);@b@ }@b@@b@ public AndFileFilter(IOFileFilter filter1, IOFileFilter filter2)@b@ {@b@ if ((filter1 == null) || (filter2 == null))@b@ throw new IllegalArgumentException("The filters must not be null");@b@@b@ this.fileFilters = new ArrayList(2);@b@ addFileFilter(filter1);@b@ addFileFilter(filter2);@b@ }@b@@b@ public void addFileFilter(IOFileFilter ioFileFilter)@b@ {@b@ this.fileFilters.add(ioFileFilter);@b@ }@b@@b@ public List<IOFileFilter> getFileFilters()@b@ {@b@ return Collections.unmodifiableList(this.fileFilters);@b@ }@b@@b@ public boolean removeFileFilter(IOFileFilter ioFileFilter)@b@ {@b@ return this.fileFilters.remove(ioFileFilter);@b@ }@b@@b@ public void setFileFilters(List<IOFileFilter> fileFilters)@b@ {@b@ this.fileFilters.clear();@b@ this.fileFilters.addAll(fileFilters);@b@ }@b@@b@ public boolean accept(File file)@b@ {@b@ if (this.fileFilters.size() == 0)@b@ return false;@b@@b@ for (IOFileFilter fileFilter : this.fileFilters)@b@ if (!(fileFilter.accept(file)))@b@ return false;@b@@b@@b@ return true;@b@ }@b@@b@ public boolean accept(File file, String name)@b@ {@b@ if (this.fileFilters.size() == 0)@b@ return false;@b@@b@ for (IOFileFilter fileFilter : this.fileFilters)@b@ if (!(fileFilter.accept(file, name)))@b@ return false;@b@@b@@b@ return true;@b@ }@b@@b@ public String toString()@b@ {@b@ int i;@b@ StringBuilder buffer = new StringBuilder();@b@ buffer.append(super.toString());@b@ buffer.append("(");@b@ if (this.fileFilters != null)@b@ for (i = 0; i < this.fileFilters.size(); ++i) {@b@ if (i > 0)@b@ buffer.append(",");@b@@b@ Object filter = this.fileFilters.get(i);@b@ buffer.append((filter == null) ? "null" : filter.toString());@b@ }@b@@b@ buffer.append(")");@b@ return buffer.toString();@b@ }@b@}