You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by sc...@apache.org on 2005/06/26 11:57:21 UTC
svn commit: r201758 -
/jakarta/commons/proper/io/branches/finder-reminder/java/org/apache/commons/io/find/FileFinder.java
Author: scolebourne
Date: Sat Jun 25 08:38:12 2005
New Revision: 201758
URL: http://svn.apache.org/viewcvs?rev=201758&view=rev
Log:
Rework to use lists not arrays
bug 34256, inspired by gaxzerow
Modified:
jakarta/commons/proper/io/branches/finder-reminder/java/org/apache/commons/io/find/FileFinder.java
Modified: jakarta/commons/proper/io/branches/finder-reminder/java/org/apache/commons/io/find/FileFinder.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/io/branches/finder-reminder/java/org/apache/commons/io/find/FileFinder.java?rev=201758&r1=201757&r2=201758&view=diff
==============================================================================
--- jakarta/commons/proper/io/branches/finder-reminder/java/org/apache/commons/io/find/FileFinder.java (original)
+++ jakarta/commons/proper/io/branches/finder-reminder/java/org/apache/commons/io/find/FileFinder.java Sat Jun 25 08:38:12 2005
@@ -1,5 +1,5 @@
/*
- * Copyright 2001-2004 The Apache Software Foundation.
+ * Copyright 2001-2005 The Apache Software Foundation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -16,190 +16,257 @@
package org.apache.commons.io.find;
import java.io.File;
-import java.util.List;
-import java.util.LinkedList;
+import java.util.ArrayList;
+import java.util.Collections;
import java.util.Iterator;
+import java.util.List;
import java.util.Map;
/**
- * Finds Files in a file system.
- *
- * Informs FindListeners whenever a Find is made, and returns the
- * finds to the user.
+ * Searches for files that match certain criteria in a directory, and
+ * typically also subdirectories.
+ * <p>
+ * This is a programmatic equivalent of the search functions present
+ * in many operating systems. You can be informed dynamically of
+ * events as they occur by adding a listener.
+ *
+ * @author Henri Yandell
+ * @version $Id$
+ * @since 1.1
*/
public class FileFinder implements Finder {
- // helper methods to handle options in String->whatever
- private static int toInt(Object obj) {
- if(obj == null) {
- return 0;
- } else
- if(obj instanceof Number) {
- return ((Number)obj).intValue();
- } else {
- String str = obj.toString();
- try {
- return Integer.parseInt(str.toString());
- } catch(NumberFormatException nfe) {
- throw new IllegalArgumentException("String argument "+str+" must be parseable as an integer. ");
- }
- }
- }
- private static boolean toBoolean(Object obj) {
- if(obj == null) {
- return false;
- } else
- if(obj instanceof Boolean) {
- return ((Boolean)obj).booleanValue();
- } else
- if(obj instanceof Number) {
- return ((Number)obj).intValue() != 0;
- } else {
- String str = obj.toString();
- return new Boolean(str).booleanValue();
- }
- }
-
+ /** The list of listeners. */
private List findListeners;
/**
+ * Constructor.
+ */
+ public FileFinder() {
+ super();
+ }
+
+ //-----------------------------------------------------------------------
+ /**
* Find all files in the specified directory.
+ *
+ * @param directory the directory to search in
*/
public File[] find(File directory) {
- return find(directory, new java.util.HashMap());
+ return find(directory, Collections.EMPTY_MAP);
}
/**
- * @todo add maxdepth and mindepth somehow
+ * Finds files in the specified directory that match the given options.
+ *
+ * @param directory the directory to search in
+ * @param options the options to use
*/
public File[] find(File directory, Map options) {
- notifyDirectoryStarted(directory);
+ List retlist = new ArrayList();
+ find(directory, options, retlist);
+ File[] files = (File[]) retlist.toArray(new File[retlist.size()]);
+ return files;
+ }
+ /**
+ * Finds files in the specified directory that match the given options.
+ * The results are added to the non-null list provided.
+ * <p>
+ * Note: To avoid large memory allocations, avoid adding any FileListeners,
+ * and try overwriding List.add(Object o) instead.
+ *
+ * @param directory the directory to search in
+ * @param options the options to use
+ * @param foundFiles a List to which all found files will be added
+ */
+ public void find(File directory, Map options, List foundFiles) {
+ if (foundFiles == null) {
+ throw new IllegalArgumentException("foundFiles list cannot be null");
+ }
+
+ notifyDirectoryStarted(directory);
boolean depthFirst = toBoolean(options.get(Finder.DEPTH));
-
int maxDepth = toInt(options.get(Finder.MAXDEPTH));
int minDepth = toInt(options.get(Finder.MINDEPTH));
boolean ignoreHiddenDirs = toBoolean(options.get(Finder.IGNORE_HIDDEN_DIRS));
-
+
FindingFilter filter = new FindingFilter(options);
- List list = find(directory, filter, depthFirst);
- if(filter.accept(directory)) {
- if(depthFirst) {
- list.add( directory );
+ int startIdx = foundFiles.size();
+ find(directory, filter, depthFirst, foundFiles);
+ if (filter.accept(directory)) {
+ if (depthFirst) {
+ foundFiles.add(directory);
} else {
- list.add( 0, directory );
+ foundFiles.add(0, directory);
}
}
- File[] files = (File[]) list.toArray(new File[0]);
- notifyDirectoryFinished(directory, files);
- return files;
+ int endIdx = foundFiles.size();
+ notifyDirectoryFinished(directory, foundFiles, startIdx, endIdx);
}
- private List find(File directory, FindingFilter filter, boolean depthFirst) {
-
+ private void find(File directory, FindingFilter filter, boolean depthFirst, List retlist) {
// we can't use listFiles(filter) here, directories don't work correctly
File[] list = directory.listFiles();
-
if (list == null) {
- return null;
+ return;
}
-
- List retlist = new LinkedList();
int sz = list.length;
-
for (int i = 0; i < sz; i++) {
File tmp = list[i];
- if(!depthFirst && filter.accept(tmp)) {
+ if (!depthFirst && filter.accept(tmp)) {
retlist.add(tmp);
- notifyFileFound(directory,tmp);
+ notifyFileFound(directory, tmp);
}
if (tmp.isDirectory()) {
notifyDirectoryStarted(tmp);
- List sublist = find(tmp, filter, depthFirst);
- int subsz = sublist.size();
- for (int j = 0; j < subsz; j++) {
- retlist.add(sublist.get(j));
- }
- notifyDirectoryFinished(tmp, (File[]) sublist.toArray(new File[0]));
+ int startIdx = retlist.size();
+ find(tmp, filter, depthFirst, retlist);
+ int endIdx = retlist.size();
+ notifyDirectoryFinished(tmp, retlist, startIdx, endIdx);
}
- if(depthFirst && filter.accept(tmp)) {
+ if (depthFirst && filter.accept(tmp)) {
retlist.add(tmp);
- notifyFileFound(directory,tmp);
+ notifyFileFound(directory, tmp);
}
}
-
- return retlist;
}
-
+
+ //-----------------------------------------------------------------------
/**
- * Add a FindListener.
+ * Adds a FindListener that is notified when a file is found.
+ *
+ * @param listener the listener
*/
- public void addFindListener(FindListener fl) {
- if(findListeners == null) {
- findListeners = new LinkedList();
+ public void addFindListener(FindListener listener) {
+ if (findListeners == null) {
+ findListeners = new ArrayList();
}
- findListeners.add(fl);
+ findListeners.add(listener);
}
/**
- * Remove a FindListener.
+ * Removes a FindListener.
+ *
+ * @param listener the listener
*/
- public void removeFindListener(FindListener fl) {
- if(findListeners != null) {
- findListeners.remove(fl);
+ public void removeFindListener(FindListener listener) {
+ if (findListeners != null) {
+ findListeners.remove(listener);
}
}
+ //-----------------------------------------------------------------------
/**
* Notify all FindListeners that a directory is being started.
+ *
+ * @param directory the directory
*/
- public void notifyDirectoryStarted(File directory) {
- if(!directory.isDirectory()) {
+ protected void notifyDirectoryStarted(File directory) {
+ if (!directory.isDirectory()) {
return;
}
- if(findListeners != null) {
- FindEvent fe = new FindEvent(this,"directoryStarted",directory);
- Iterator itr = findListeners.iterator();
- while(itr.hasNext()) {
- FindListener findListener = (FindListener)itr.next();
- findListener.directoryStarted( fe );
+ if (findListeners != null) {
+ FindEvent fe = new FindEvent(this, "directoryStarted", directory);
+ Iterator it = findListeners.iterator();
+ while (it.hasNext()) {
+ FindListener findListener = (FindListener) it.next();
+ findListener.directoryStarted(fe);
}
}
}
/**
* Notify all FindListeners that a directory has been finished.
- * Supplying the filenames that have been found.
+ *
+ * @param directory the directory
+ * @param files the files that were found in the directory
*/
- public void notifyDirectoryFinished(File directory, File[] files) {
- if(!directory.isDirectory()) {
+ protected void notifyDirectoryFinished(File directory, File[] files) {
+ if (!directory.isDirectory()) {
return;
}
- if(findListeners != null) {
- FindEvent fe = new FindEvent(this,"directoryFinished",directory,files);
- Iterator itr = findListeners.iterator();
- while(itr.hasNext()) {
- FindListener findListener = (FindListener)itr.next();
- findListener.directoryFinished( fe );
+ if (findListeners != null) {
+ FindEvent fe = new FindEvent(this, "directoryFinished", directory, files);
+ Iterator it = findListeners.iterator();
+ while (it.hasNext()) {
+ FindListener findListener = (FindListener) it.next();
+ findListener.directoryFinished(fe);
}
}
}
+ private void notifyDirectoryFinished(File directory, List files, int startIdx, int endIdx) {
+ // only allocate a File array if we really have to
+ if (!directory.isDirectory() || findListeners == null || findListeners.size() == 0) {
+ return;
+ }
+
+ // we should drop this when/if FindEvent stops dealing with File arrays
+ File[] filesa = (File[]) files.subList(startIdx, endIdx).toArray(new File[endIdx - startIdx]);
+ notifyDirectoryFinished(directory, filesa);
+
+ }
+
/**
- * Notify FindListeners that a file has been found.
+ * Notifies each FindListeners that a file has been found.
+ *
+ * @param directory the directory being processed
+ * @param file the file that was found
*/
- public void notifyFileFound(File directory, File file) {
- if(file.isDirectory()) {
+ protected void notifyFileFound(File directory, File file) {
+ if (file.isDirectory()) {
return;
}
- if(findListeners != null) {
- FindEvent fe = new FindEvent(this,"fileFound",directory,file);
- Iterator itr = findListeners.iterator();
- while(itr.hasNext()) {
- FindListener findListener = (FindListener)itr.next();
- findListener.fileFound( fe );
+ if (findListeners != null) {
+ FindEvent fe = new FindEvent(this, "fileFound", directory, file);
+ Iterator it = findListeners.iterator();
+ while (it.hasNext()) {
+ FindListener findListener = (FindListener) it.next();
+ findListener.fileFound(fe);
}
}
}
-
+
+ //-----------------------------------------------------------------------
+ /**
+ * Converts an object to an int.
+ *
+ * @param obj the object
+ * @return an int
+ */
+ private static int toInt(Object obj) {
+ if (obj == null) {
+ return 0;
+ } else if (obj instanceof Number) {
+ return ((Number) obj).intValue();
+ } else {
+ String str = obj.toString();
+ try {
+ return Integer.parseInt(str.toString());
+ } catch(NumberFormatException nfe) {
+ throw new IllegalArgumentException("String argument " + str + " must be parseable as an integer");
+ }
+ }
+ }
+
+ /**
+ * Converts an object to a boolean.
+ *
+ * @param obj the object
+ * @return a boolean
+ */
+ private static boolean toBoolean(Object obj) {
+ if (obj == null) {
+ return false;
+ } else if (obj instanceof Boolean) {
+ return ((Boolean) obj).booleanValue();
+ } else if (obj instanceof Number) {
+ return ((Number) obj).intValue() != 0;
+ } else {
+ String str = obj.toString();
+ return new Boolean(str).booleanValue();
+ }
+ }
+
}
---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org