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 OR logic across a list of
027: * file filters. This filter returns <code>true</code> if any 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>true</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 OrFileFilter 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>OrFileFilter</code>.
045: *
046: * @since Commons IO 1.1
047: */
048: public OrFileFilter() {
049: this .fileFilters = new ArrayList();
050: }
051:
052: /**
053: * Constructs a new instance of <code>OrFileFilter</code>
054: * with the specified filters.
055: *
056: * @param fileFilters the file filters for this filter, copied, null ignored
057: * @since Commons IO 1.1
058: */
059: public OrFileFilter(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 ORs 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 OrFileFilter(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(IOFileFilter ioFileFilter) {
102: return this .fileFilters.remove(ioFileFilter);
103: }
104:
105: /**
106: * {@inheritDoc}
107: */
108: public void setFileFilters(final List fileFilters) {
109: this .fileFilters = fileFilters;
110: }
111:
112: /**
113: * {@inheritDoc}
114: */
115: public boolean accept(final File file) {
116: for (Iterator iter = this .fileFilters.iterator(); iter
117: .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
131: .hasNext();) {
132: IOFileFilter fileFilter = (IOFileFilter) iter.next();
133: if (fileFilter.accept(file, name)) {
134: return true;
135: }
136: }
137: return false;
138: }
139:
140: }
|