de.hunsicker.io
Class DirectoryScanner

java.lang.Object
  |
  +--de.hunsicker.io.DirectoryScanner
All Implemented Interfaces:
java.lang.Runnable

public class DirectoryScanner
extends java.lang.Object
implements java.lang.Runnable

Class for scanning a directory for files/directories that match a certain filter.

Version:
$Revision: 1.2.2.1 $
Author:
Marco Hunsicker
See Also:
FilenameFilter, org.apache.oro.io.RegexFilenameFilter

Constructor Summary
DirectoryScanner()
          Creates a new DirectoryScanner object.
DirectoryScanner(java.io.File directory)
          Creates a new DirectoryScanner object.
DirectoryScanner(java.io.File directory, int levels)
          Creates a new DirectoryScanner object.
DirectoryScanner(java.util.Set directories)
          Creates a new DirectoryScanner object.
DirectoryScanner(java.util.Set directories, int levels)
          Creates a new DirectoryScanner object.
DirectoryScanner(java.lang.String directory)
          Creates a new DirectoryScanner object.
DirectoryScanner(java.lang.String directory, int levels)
          Creates a new DirectoryScanner object.
 
Method Summary
 void addFilter(java.io.FilenameFilter filter)
          Adds the given file filter.
 java.io.File[] getFiles()
          Returns an array with the found files.
 int getMaxLevels()
          Returns the directory depth (the number of levels) to search.
 boolean isEmpty()
          Indicates whether the queue with the found files is empty.
 boolean isFinished()
          Indicates whether the scan has finished or is still running.
 java.util.Iterator iterator()
          Returns an iterator over the found files.
 java.io.File pop()
          Returns and removes the file at the top of the internal file queue.
 void removeFilter(java.io.FilenameFilter filter)
          Removes the given file filter.
 void reset()
          Resets the scanner so it can be reused.
 void run()
          Starts the scanning process.
 void setFilterPolicy(int policy)
          Sets the filter policy to use.
 void setMaxLevels(int level)
          Sets the directory depth (number of levels) to search.
 void setTargets(java.util.Collection targets)
          Sets the targets to search (both single files and directories are valid)
 void setTargets(java.util.Set targets)
          Sets the targets to search.
 java.io.File take()
          Returns and removes the file at the top of the internal file queue.
 void waitUntilFinished()
          Blocks until the scanner has finished processing.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

DirectoryScanner

public DirectoryScanner()
Creates a new DirectoryScanner object. Scans the current user directory.


DirectoryScanner

public DirectoryScanner(java.lang.String directory)
Creates a new DirectoryScanner object.

Parameters:
directory - directory to scan.

DirectoryScanner

public DirectoryScanner(java.lang.String directory,
                        int levels)
Creates a new DirectoryScanner object.

Parameters:
directory - directory to scan.
levels - number of levels to scan.

DirectoryScanner

public DirectoryScanner(java.io.File directory)
Creates a new DirectoryScanner object.

Parameters:
directory - directory to scan.

DirectoryScanner

public DirectoryScanner(java.io.File directory,
                        int levels)
Creates a new DirectoryScanner object.

Parameters:
directory - directory to scan.
levels - number of levels to scan.

DirectoryScanner

public DirectoryScanner(java.util.Set directories)
Creates a new DirectoryScanner object.

Parameters:
directories - directories to scan.

DirectoryScanner

public DirectoryScanner(java.util.Set directories,
                        int levels)
Creates a new DirectoryScanner object.

Parameters:
directories - directories to scan.
levels - number of levels to scan.
Method Detail

isEmpty

public boolean isEmpty()
Indicates whether the queue with the found files is empty.

Returns:
true if the queue is empty.

getFiles

public java.io.File[] getFiles()
Returns an array with the found files.

Returns:
the found files.

setFilterPolicy

public void setFilterPolicy(int policy)
Sets the filter policy to use.

Parameters:
policy - valid filter policy.

isFinished

public boolean isFinished()
Indicates whether the scan has finished or is still running.

Returns:
true if the scan has finished.

setMaxLevels

public void setMaxLevels(int level)
Sets the directory depth (number of levels) to search.

Parameters:
level - directory depth to search.
Throws:
java.lang.IllegalStateException - if the scanner hasn't finished processing yet (isFinished() == true)
See Also:
isFinished()

getMaxLevels

public int getMaxLevels()
Returns the directory depth (the number of levels) to search.

Returns:
directory depth to search.

setTargets

public void setTargets(java.util.Collection targets)
Sets the targets to search (both single files and directories are valid)

Parameters:
targets - collection with the targets to search.

setTargets

public void setTargets(java.util.Set targets)
Sets the targets to search.

Parameters:
targets - set with the targets to search.
Throws:
java.lang.NullPointerException - if targets == null
java.lang.IllegalStateException - if the scanner hasn't finished processing yet (isFinished() == true)

addFilter

public void addFilter(java.io.FilenameFilter filter)
Adds the given file filter.

Parameters:
filter - file filter to add.
Throws:
java.lang.IllegalStateException - if the scanner hasn't finished processing yet (isFinished() == true)

iterator

public java.util.Iterator iterator()
Returns an iterator over the found files.

Returns:
iterator over the found files.

pop

public java.io.File pop()
Returns and removes the file at the top of the internal file queue.

Returns:
the file at the top of the queue.
See Also:
isEmpty(), take()

removeFilter

public void removeFilter(java.io.FilenameFilter filter)
Removes the given file filter.

Parameters:
filter - file filter to remove.
Throws:
java.lang.IllegalStateException - if the scanner hasn't finished processing yet (isFinished() == true)

reset

public void reset()
Resets the scanner so it can be reused.


run

public void run()
Starts the scanning process. All files matching the given filters will be seamlessly added to the internal queue where they can be accessed via pop() or take() calls.

Specified by:
run in interface java.lang.Runnable

take

public java.io.File take()
                  throws java.lang.InterruptedException
Returns and removes the file at the top of the internal file queue. If the queue is empty, waits until one element is available, so this is in fact a blocking pop().

Returns:
the file at the top of the queue.
Throws:
java.lang.InterruptedException - if the queue contains no elements.
See Also:
pop()

waitUntilFinished

public void waitUntilFinished()
Blocks until the scanner has finished processing. Returns immediately if the scanner hasn't started processing already.



Copyright © 1997-2005 Jalopy. All Rights Reserved.