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.util.ArrayList;
021: import java.util.Collections;
022: import java.util.Iterator;
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: 490425 $ $Date: 2006-12-27 02:25:43 +0100 (Mi, 27 Dez 2006) $
034: *
035: * @author Steven Caswell
036: */
037: public class AndFileFilter extends AbstractFileFilter implements
038: ConditionalFileFilter {
039:
040: /** The list of file filters. */
041: private List fileFilters;
042:
043: /**
044: * Constructs a new instance of <code>AndFileFilter</code>.
045: *
046: * @since Commons IO 1.1
047: */
048: public AndFileFilter() {
049: this .fileFilters = new ArrayList();
050: }
051:
052: /**
053: * Constructs a new instance of <code>AndFileFilter</code>
054: * with the specified list of filters.
055: *
056: * @param fileFilters a List of IOFileFilter instances, copied, null ignored
057: * @since Commons IO 1.1
058: */
059: public AndFileFilter(final List fileFilters) {
060: if (fileFilters == null) {
061: this .fileFilters = new ArrayList();
062: } else {
063: this .fileFilters = new ArrayList(fileFilters);
064: }
065: }
066:
067: /**
068: * Constructs a new file filter that ANDs the result of two other filters.
069: *
070: * @param filter1 the first filter, must not be null
071: * @param filter2 the second filter, must not be null
072: * @throws IllegalArgumentException if either filter is null
073: */
074: public AndFileFilter(IOFileFilter filter1, IOFileFilter filter2) {
075: if (filter1 == null || filter2 == null) {
076: throw new IllegalArgumentException(
077: "The filters must not be null");
078: }
079: this .fileFilters = new ArrayList();
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 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 fileFilters) {
109: this .fileFilters = new ArrayList(fileFilters);
110: }
111:
112: /**
113: * {@inheritDoc}
114: */
115: public boolean accept(final File file) {
116: if (this .fileFilters.size() == 0) {
117: return false;
118: }
119: for (Iterator iter = this .fileFilters.iterator(); iter
120: .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
137: .hasNext();) {
138: IOFileFilter fileFilter = (IOFileFilter) iter.next();
139: if (!fileFilter.accept(file, name)) {
140: return false;
141: }
142: }
143: return true;
144: }
145:
146: }
|