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 AND logic across a list of
028 * file filters. This filter returns <code>true</code> if all 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>false</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 AndFileFilter
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>AndFileFilter</code>.
047 *
048 * @since Commons IO 1.1
049 */
050 public AndFileFilter() {
051 this.fileFilters = new ArrayList();
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 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 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();
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(final IOFileFilter ioFileFilter) {
103 return this.fileFilters.remove(ioFileFilter);
104 }
105
106 /**
107 * {@inheritDoc}
108 */
109 public void setFileFilters(final List fileFilters) {
110 this.fileFilters = new ArrayList(fileFilters);
111 }
112
113 /**
114 * {@inheritDoc}
115 */
116 public boolean accept(final File file) {
117 if (this.fileFilters.size() == 0) {
118 return false;
119 }
120 for (Iterator iter = this.fileFilters.iterator(); iter.hasNext();) {
121 IOFileFilter fileFilter = (IOFileFilter) iter.next();
122 if (!fileFilter.accept(file)) {
123 return false;
124 }
125 }
126 return true;
127 }
128
129 /**
130 * {@inheritDoc}
131 */
132 public boolean accept(final File file, final String name) {
133 if (this.fileFilters.size() == 0) {
134 return false;
135 }
136 for (Iterator iter = this.fileFilters.iterator(); iter.hasNext();) {
137 IOFileFilter fileFilter = (IOFileFilter) iter.next();
138 if (!fileFilter.accept(file, name)) {
139 return false;
140 }
141 }
142 return true;
143 }
144
145 /**
146 * Provide a String representaion of this file filter.
147 *
148 * @return a String representaion
149 */
150 public String toString() {
151 StringBuffer buffer = new StringBuffer();
152 buffer.append(super.toString());
153 buffer.append("(");
154 if (fileFilters != null) {
155 for (int i = 0; i < fileFilters.size(); i++) {
156 if (i > 0) {
157 buffer.append(",");
158 }
159 Object filter = fileFilters.get(i);
160 buffer.append(filter == null ? "null" : filter.toString());
161 }
162 }
163 buffer.append(")");
164 return buffer.toString();
165 }
166
167 }