/*---------------------------------------------------------------------------*\
$Id: 14203b009aa3cebe86dd1f29a66f67a3fb78c1d4 $
---------------------------------------------------------------------------
This software is released under a BSD-style license:
Copyright (c) 2004-2007 Brian M. Clapper. All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:
1. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
2. The end-user documentation included with the redistribution, if any,
must include the following acknowlegement:
"This product includes software developed by Brian M. Clapper
(bmc@clapper.org, http://www.clapper.org/bmc/). That software is
copyright (c) 2004-2007 Brian M. Clapper."
Alternately, this acknowlegement may appear in the software itself,
if wherever such third-party acknowlegements normally appear.
3. Neither the names "clapper.org", "clapper.org Java Utility Library",
nor any of the names of the project contributors may be used to
endorse or promote products derived from this software without prior
written permission. For written permission, please contact
bmc@clapper.org.
4. Products derived from this software may not be called "clapper.org
Java Utility Library", nor may "clapper.org" appear in their names
without prior written permission of Brian M. Clapper.
THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
NO EVENT SHALL BRIAN M. CLAPPER BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
\*---------------------------------------------------------------------------*/
package org.clapper.util.io;
import java.util.LinkedList;
import java.util.List;
import java.io.FilenameFilter;
import java.io.File;
/**
* An OrFilenameFilter logically ORs other
* java.io.FilenameFilter objects. When its
* {@link #accept accept()} method is called, the OrFilenameFilter
* object passes the file through the contained filters. The file is
* accepted if it is accepted by any of the contained filters. This class
* conceptually provides a logical "OR" operator for file filters.
*
* The contained filters are applied in the order they were added to the
* OrFilenameFilter object. This class's {@link #accept accept()}
* method stops looping over the contained filters as soon as it encounters
* one whose accept() method returns true (implementing
* a "short-circuited OR" operation.)
*
* @see FilenameFilter
* @see AndFilenameFilter
* @see NotFilenameFilter
* @see OrFilenameFilter
* @see RegexFilenameFilter
*
* @version $Revision: 5812 $
*
* @author Copyright © 2004-2007 Brian M. Clapper
*/
public final class OrFilenameFilter implements FilenameFilter
{
/*----------------------------------------------------------------------*\
Private Data Items
\*----------------------------------------------------------------------*/
private List filters = new LinkedList();
/*----------------------------------------------------------------------*\
Constructor
\*----------------------------------------------------------------------*/
/**
* Construct a new OrFilenameFilter with no contained filters.
*/
public OrFilenameFilter()
{
// nothing to do
}
/**
* Construct a new OrFilenameFilter with two contained filters.
* Additional filters may be added later, via calls to the
* {@link #addFilter addFilter()} method.
*
* @param filters filters to add
*/
public OrFilenameFilter (FilenameFilter... filters)
{
for (FilenameFilter filter : filters)
addFilter (filter);
}
/*----------------------------------------------------------------------*\
Public Methods
\*----------------------------------------------------------------------*/
/**
* Add a filter to the set of contained filters.
*
* @param filter the FilenameFilter to add.
*
* @return this object, to permit chained calls.
*
* @see #removeFilter
*/
public OrFilenameFilter addFilter (FilenameFilter filter)
{
filters.add (filter);
return this;
}
/**
* Remove a filter from the set of contained filters.
*
* @param filter the FilenameFilter to remove.
*
* @see #addFilter
*/
public void removeFilter (FilenameFilter filter)
{
filters.remove (filter);
}
/**
* Determine whether a file is to be accepted or not, based on the
* contained filters. The file is accepted if any one of the contained
* filters accepts it. This method stops looping over the contained
* filters as soon as it encounters one whose accept() method
* returns true (implementing a "short-circuited OR"
* operation.)
*
* If the set of contained filters is empty, then this method
* returns true.
*
* @param dir The directory containing the file.
* @param name the file name
*
* @return true if the file matches, false if it doesn't
*/
public boolean accept (File dir, String name)
{
boolean accepted = false;
if (filters.size() == 0)
accepted = true;
else
{
for (FilenameFilter filter : filters)
{
accepted = filter.accept (dir, name);
if (accepted)
break;
}
}
return accepted;
}
}
Ads help maintain this website.