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.Iterator; 024 import java.util.List; 025 026 /** 027 * A {@link java.io.FileFilter} providing conditional OR logic across a list of 028 * file filters. This filter returns <code>true</code> if any filters in the 029 * list return <code>true</code>. Otherwise, it returns <code>false</code>. 030 * Checking of the file filter list stops when the first filter returns 031 * <code>true</code>. 032 * 033 * @since Commons IO 1.0 034 * @version $Revision: 606381 $ $Date: 2007-12-22 02:03:16 +0000 (Sat, 22 Dec 2007) $ 035 * 036 * @author Steven Caswell 037 */ 038 public class OrFileFilter 039 extends AbstractFileFilter 040 implements ConditionalFileFilter, Serializable { 041 042 /** The list of file filters. */ 043 private List fileFilters; 044 045 /** 046 * Constructs a new instance of <code>OrFileFilter</code>. 047 * 048 * @since Commons IO 1.1 049 */ 050 public OrFileFilter() { 051 this.fileFilters = new ArrayList(); 052 } 053 054 /** 055 * Constructs a new instance of <code>OrFileFilter</code> 056 * with the specified filters. 057 * 058 * @param fileFilters the file filters for this filter, copied, null ignored 059 * @since Commons IO 1.1 060 */ 061 public OrFileFilter(final List fileFilters) { 062 if (fileFilters == null) { 063 this.fileFilters = new ArrayList(); 064 } else { 065 this.fileFilters = new ArrayList(fileFilters); 066 } 067 } 068 069 /** 070 * Constructs a new file filter that ORs 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 OrFileFilter(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(); 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 getFileFilters() { 096 return Collections.unmodifiableList(this.fileFilters); 097 } 098 099 /** 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 }