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.Serializable; 021 import java.util.ArrayList; 022 import java.util.Collections; 023 import java.util.List; 024 025 /** 026 * A {@link java.io.FileFilter} providing conditional AND logic across a list of 027 * file filters. This filter returns <code>true</code> if all filters in the 028 * list return <code>true</code>. Otherwise, it returns <code>false</code>. 029 * Checking of the file filter list stops when the first filter returns 030 * <code>false</code>. 031 * 032 * @since Commons IO 1.0 033 * @version $Revision: 1005099 $ $Date: 2010-10-06 12:13:01 -0400 (Wed, 06 Oct 2010) $ 034 * 035 * @author Steven Caswell 036 * @see FileFilterUtils#and(IOFileFilter...) 037 */ 038 public class AndFileFilter 039 extends AbstractFileFilter 040 implements ConditionalFileFilter, Serializable { 041 042 /** The list of file filters. */ 043 private final List<IOFileFilter> fileFilters; 044 045 /** 046 * Constructs a new instance of <code>AndFileFilter</code>. 047 * 048 * @since Commons IO 1.1 049 */ 050 public AndFileFilter() { 051 this.fileFilters = new ArrayList<IOFileFilter>(); 052 } 053 054 /** 055 * Constructs a new instance of <code>AndFileFilter</code> 056 * with the specified list of filters. 057 * 058 * @param fileFilters a List of IOFileFilter instances, copied, null ignored 059 * @since Commons IO 1.1 060 */ 061 public AndFileFilter(final List<IOFileFilter> fileFilters) { 062 if (fileFilters == null) { 063 this.fileFilters = new ArrayList<IOFileFilter>(); 064 } else { 065 this.fileFilters = new ArrayList<IOFileFilter>(fileFilters); 066 } 067 } 068 069 /** 070 * Constructs a new file filter that ANDs the result of two other filters. 071 * 072 * @param filter1 the first filter, must not be null 073 * @param filter2 the second filter, must not be null 074 * @throws IllegalArgumentException if either filter is null 075 */ 076 public AndFileFilter(IOFileFilter filter1, IOFileFilter filter2) { 077 if (filter1 == null || filter2 == null) { 078 throw new IllegalArgumentException("The filters must not be null"); 079 } 080 this.fileFilters = new ArrayList<IOFileFilter>(2); 081 addFileFilter(filter1); 082 addFileFilter(filter2); 083 } 084 085 /** 086 * {@inheritDoc} 087 */ 088 public void addFileFilter(final IOFileFilter ioFileFilter) { 089 this.fileFilters.add(ioFileFilter); 090 } 091 092 /** 093 * {@inheritDoc} 094 */ 095 public List<IOFileFilter> getFileFilters() { 096 return Collections.unmodifiableList(this.fileFilters); 097 } 098 099 /** 100 * {@inheritDoc} 101 */ 102 public boolean removeFileFilter(final IOFileFilter ioFileFilter) { 103 return this.fileFilters.remove(ioFileFilter); 104 } 105 106 /** 107 * {@inheritDoc} 108 */ 109 public void setFileFilters(final List<IOFileFilter> fileFilters) { 110 this.fileFilters.clear(); 111 this.fileFilters.addAll(fileFilters); 112 } 113 114 /** 115 * {@inheritDoc} 116 */ 117 @Override 118 public boolean accept(final File file) { 119 if (this.fileFilters.size() == 0) { 120 return false; 121 } 122 for (IOFileFilter fileFilter : fileFilters) { 123 if (!fileFilter.accept(file)) { 124 return false; 125 } 126 } 127 return true; 128 } 129 130 /** 131 * {@inheritDoc} 132 */ 133 @Override 134 public boolean accept(final File file, final String name) { 135 if (this.fileFilters.size() == 0) { 136 return false; 137 } 138 for (IOFileFilter fileFilter : fileFilters) { 139 if (!fileFilter.accept(file, name)) { 140 return false; 141 } 142 } 143 return true; 144 } 145 146 /** 147 * Provide a String representaion of this file filter. 148 * 149 * @return a String representaion 150 */ 151 @Override 152 public String toString() { 153 StringBuilder buffer = new StringBuilder(); 154 buffer.append(super.toString()); 155 buffer.append("("); 156 if (fileFilters != null) { 157 for (int i = 0; i < fileFilters.size(); i++) { 158 if (i > 0) { 159 buffer.append(","); 160 } 161 Object filter = fileFilters.get(i); 162 buffer.append(filter == null ? "null" : filter.toString()); 163 } 164 } 165 buffer.append(")"); 166 return buffer.toString(); 167 } 168 169 }