001// Copyright (c) 2001 Hursh Jain (http://www.mollypages.org) 
002// The Molly framework is freely distributable under the terms of an
003// MIT-style license. For details, see the molly pages web site at:
004// http://www.mollypages.org/. Use, modify, have fun !
005
006package fc.io.fileselectors;
007
008import java.io.*;
009import java.util.*;
010
011/** 
012Allows for chaining FileFilters, used in java.io.File directory
013listing methods. Chained filters allow all filters in the chain
014to take turns accepting/rejecting a particular file name. The file
015name is only accepted if all filters in the chain accept that
016file. Example usage:
017<blockquote>
018<pre>
019java.io.File startdir = new java.io.File("\some\path\here");
020String filepattern = "gif,jpg";
021ChainedFileFilter filter = new SuffixFilter(filepattern, new NormalFileFilter()); 
022File[] files = startdir.listFiles(filter);
023</pre>
024</blockquote>
025The above will only list files (not directories) ending with 
026<tt>gif</tt> or <tt>jpg</tt>. 
027**/
028abstract public class ChainedFileSelector implements FileFilter
029{
030ChainedFileSelector next;
031
032/** 
033@param  cf  the next selector in the chain. Pass in <tt>null</tt> if there 
034      is no following selector in the chain.
035*/
036public ChainedFileSelector(ChainedFileSelector cf) { 
037  next = cf; 
038  }
039  
040public boolean accept(File name) {
041  boolean val = thisfilter(name);
042  if (val) 
043    val = nextfilter(name);
044  return val;
045  }
046
047protected boolean nextfilter(File name) {
048  if (next != null) 
049    return next.accept(name);
050  return true;
051  }
052
053/** 
054Should be implemented by subclasses to accept or reject a filename
055based on criteria appropriate for that subclass. 
056**/
057protected abstract boolean thisfilter(File name);
058}