OrFileFilter.java revision 8978aac1977408b05e386ae846c30920c7faa0a6
1/* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17package org.apache.commons.io.filefilter; 18 19import java.io.File; 20import java.io.Serializable; 21import java.util.ArrayList; 22import java.util.Collections; 23import java.util.Iterator; 24import java.util.List; 25 26/** 27 * A {@link java.io.FileFilter} providing conditional OR logic across a list of 28 * file filters. This filter returns <code>true</code> if any filters in the 29 * list return <code>true</code>. Otherwise, it returns <code>false</code>. 30 * Checking of the file filter list stops when the first filter returns 31 * <code>true</code>. 32 * 33 * @since Commons IO 1.0 34 * @version $Revision: 606381 $ $Date: 2007-12-22 02:03:16 +0000 (Sat, 22 Dec 2007) $ 35 * 36 * @author Steven Caswell 37 */ 38public class OrFileFilter 39 extends AbstractFileFilter 40 implements ConditionalFileFilter, Serializable { 41 42 /** The list of file filters. */ 43 private List fileFilters; 44 45 /** 46 * Constructs a new instance of <code>OrFileFilter</code>. 47 * 48 * @since Commons IO 1.1 49 */ 50 public OrFileFilter() { 51 this.fileFilters = new ArrayList(); 52 } 53 54 /** 55 * Constructs a new instance of <code>OrFileFilter</code> 56 * with the specified filters. 57 * 58 * @param fileFilters the file filters for this filter, copied, null ignored 59 * @since Commons IO 1.1 60 */ 61 public OrFileFilter(final List fileFilters) { 62 if (fileFilters == null) { 63 this.fileFilters = new ArrayList(); 64 } else { 65 this.fileFilters = new ArrayList(fileFilters); 66 } 67 } 68 69 /** 70 * Constructs a new file filter that ORs the result of two other filters. 71 * 72 * @param filter1 the first filter, must not be null 73 * @param filter2 the second filter, must not be null 74 * @throws IllegalArgumentException if either filter is null 75 */ 76 public OrFileFilter(IOFileFilter filter1, IOFileFilter filter2) { 77 if (filter1 == null || filter2 == null) { 78 throw new IllegalArgumentException("The filters must not be null"); 79 } 80 this.fileFilters = new ArrayList(); 81 addFileFilter(filter1); 82 addFileFilter(filter2); 83 } 84 85 /** 86 * {@inheritDoc} 87 */ 88 public void addFileFilter(final IOFileFilter ioFileFilter) { 89 this.fileFilters.add(ioFileFilter); 90 } 91 92 /** 93 * {@inheritDoc} 94 */ 95 public List getFileFilters() { 96 return Collections.unmodifiableList(this.fileFilters); 97 } 98 99 /** 100 * {@inheritDoc} 101 */ 102 public boolean removeFileFilter(IOFileFilter ioFileFilter) { 103 return this.fileFilters.remove(ioFileFilter); 104 } 105 106 /** 107 * {@inheritDoc} 108 */ 109 public void setFileFilters(final List fileFilters) { 110 this.fileFilters = fileFilters; 111 } 112 113 /** 114 * {@inheritDoc} 115 */ 116 public boolean accept(final File file) { 117 for (Iterator iter = this.fileFilters.iterator(); iter.hasNext();) { 118 IOFileFilter fileFilter = (IOFileFilter) iter.next(); 119 if (fileFilter.accept(file)) { 120 return true; 121 } 122 } 123 return false; 124 } 125 126 /** 127 * {@inheritDoc} 128 */ 129 public boolean accept(final File file, final String name) { 130 for (Iterator iter = this.fileFilters.iterator(); iter.hasNext();) { 131 IOFileFilter fileFilter = (IOFileFilter) iter.next(); 132 if (fileFilter.accept(file, name)) { 133 return true; 134 } 135 } 136 return false; 137 } 138 139 /** 140 * Provide a String representaion of this file filter. 141 * 142 * @return a String representaion 143 */ 144 public String toString() { 145 StringBuffer buffer = new StringBuffer(); 146 buffer.append(super.toString()); 147 buffer.append("("); 148 if (fileFilters != null) { 149 for (int i = 0; i < fileFilters.size(); i++) { 150 if (i > 0) { 151 buffer.append(","); 152 } 153 Object filter = fileFilters.get(i); 154 buffer.append(filter == null ? "null" : filter.toString()); 155 } 156 } 157 buffer.append(")"); 158 return buffer.toString(); 159 } 160 161} 162