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