From 6af7dea6b246eb942693a19a564b64369128d62b Mon Sep 17 00:00:00 2001 From: "Christian P. MOMON" Date: Fri, 18 Jun 2010 02:22:03 +0200 Subject: [PATCH] Merge FileIterator and FileIteraotrStates. Add String[] constructor. --- src/fr/devinsy/util/FileIterator.java | 199 +++++++++++++++++--- src/fr/devinsy/util/FileIteratorState.java | 25 +++ src/fr/devinsy/util/FileIteratorStates.java | 199 -------------------- 3 files changed, 200 insertions(+), 223 deletions(-) delete mode 100644 src/fr/devinsy/util/FileIteratorStates.java diff --git a/src/fr/devinsy/util/FileIterator.java b/src/fr/devinsy/util/FileIterator.java index 5ad0a3a..e79e2cf 100644 --- a/src/fr/devinsy/util/FileIterator.java +++ b/src/fr/devinsy/util/FileIterator.java @@ -2,39 +2,83 @@ package fr.devinsy.util; import java.io.File; import java.util.Iterator; +import java.util.Vector; +import java.util.regex.Pattern; /** * */ -public class FileIterator implements Iterator +public class FileIterator extends Vector implements Iterator { - static org.apache.log4j.Logger logger = org.apache.log4j.Logger.getLogger (FileIterator.class); + private static final long serialVersionUID = 8790133455427427766L; - protected FileIteratorStates states; + protected int currentDepth; + protected Pattern pattern; protected File previous; - + + /** * */ public FileIterator (File root) { - this.states = new FileIteratorStates(root); + super(); + + this.currentDepth = -1; // Note: push method increments this value. + if (root != null) + { + this.push(root); + } + + this.pattern = null; this.previous = null; } - - + + /** - * TODO + * */ public FileIterator (File root, String filter) { - this.states = new FileIteratorStates(root, filter); + super(); + + this.currentDepth = -1; // Note: push method increments this value. + if (root != null) + { + this.push(root); + } + + if (filter == null) + { + this.pattern = null; + } + else + { + this.pattern = Pattern.compile(filter); + shift(); + } + this.previous = null; } - - + + + /** + * + */ + public FileIterator (String[] pathnames, String prefix) + { + super(); + + this.currentDepth = 0; + this.add(new FileIteratorState(pathnames, prefix)); + this.pattern = null; + this.previous = null; + + } + + /** * */ @@ -42,7 +86,14 @@ public class FileIterator implements Iterator { String result; - result = this.states.filter(); + if (pattern == null) + { + result = ".*"; + } + else + { + result = this.pattern.toString(); + } // return(result); @@ -50,14 +101,13 @@ public class FileIterator implements Iterator /** - * + * */ - @Override - public boolean hasNext() + protected FileIteratorState currentState() { - boolean result; - - result = this.states.hasNext(); + FileIteratorState result; + + result = this.get(this.currentDepth); // return(result); @@ -65,23 +115,123 @@ public class FileIterator implements Iterator /** - * + * + */ + protected File currentFile() + { + File result; + + result = this.currentState().currentFile(); + + // + return(result); + } + + + /** + * + */ + public void push(File directory) + { + this.add(new FileIteratorState(directory)); + this.currentDepth += 1; + } + + + /** + * + */ + public void pop() + { + this.removeElementAt(this.currentDepth); + this.currentDepth -= 1; + } + + + /** + * Set indexes to the good next item. + */ + public void shift() + { + boolean ended = false; + while(!ended) + { + File next = this.currentFile(); + + if (next == null) + { + if (this.currentDepth == 0) + { + ended = true; + } + else + { + this.pop(); + } + } + else + { + if ((this.pattern == null) || (this.pattern.matcher(next.getPath()).matches())) + { + ended = true; + } + else + { + this.currentState().next(); + if (next.isDirectory()) + { + this.push(next); + } + } + } + } + } + + + /** + * + */ + @Override + public boolean hasNext() + { + boolean result; + + result = this.currentState().hasNext(); + + // + return(result); + } + + + /** + * */ @Override public File next() { File result; - result = this.states.next(); + result = this.currentState().next(); this.previous = result; + + + if (result.isDirectory()) + { + this.push(result); + } + + shift(); // return(result); } - - + + + /** + * + */ @Override - public void remove() + public void remove() { if (this.previous != null) { @@ -90,6 +240,7 @@ public class FileIterator implements Iterator } } + /** * */ @@ -97,7 +248,7 @@ public class FileIterator implements Iterator { String result; - result = "[depth=" + this.states.currentDepth + "][index=" + this.states.get(this.states.currentDepth).currentIndex() + "/" + this.states.get(this.states.currentDepth).files.length + "]"; + result = "[depth=" + this.currentDepth + "][index=" + this.get(this.currentDepth).currentIndex() + "/" + this.get(this.currentDepth).files.length + "]"; // return(result); diff --git a/src/fr/devinsy/util/FileIteratorState.java b/src/fr/devinsy/util/FileIteratorState.java index bb6ae82..b198863 100644 --- a/src/fr/devinsy/util/FileIteratorState.java +++ b/src/fr/devinsy/util/FileIteratorState.java @@ -1,6 +1,7 @@ package fr.devinsy.util; import java.io.File; +import java.util.Vector; @@ -12,6 +13,30 @@ public class FileIteratorState protected File[] files; protected int currentIndex; + /** + * + */ + public FileIteratorState (String[] pathnames, String prefix) + { + if (prefix == null) + { + prefix = ""; + } + else if (!prefix.endsWith(File.separator)) + { + prefix += File.separator; + } + + this.files = new File[pathnames.length]; + for (int pathnameIndex = 0; pathnameIndex < pathnames.length; pathnameIndex++) + { + String pathname = pathnames[pathnameIndex]; + + this.files[pathnameIndex] = new File(prefix + pathname); + } + } + + /** * */ diff --git a/src/fr/devinsy/util/FileIteratorStates.java b/src/fr/devinsy/util/FileIteratorStates.java deleted file mode 100644 index 3750ae9..0000000 --- a/src/fr/devinsy/util/FileIteratorStates.java +++ /dev/null @@ -1,199 +0,0 @@ -package fr.devinsy.util; - -import java.io.File; -import java.util.Vector; -import java.util.regex.Pattern; - - - -/** - * - */ -public class FileIteratorStates extends Vector -{ - private static final long serialVersionUID = 8790133455427427766L; - - protected int currentDepth; - protected Pattern pattern; - - - - /** - * - */ - public FileIteratorStates (File root) - { - super(); - - this.currentDepth = -1; // Note: push method increments this value. - if (root != null) - { - this.push(root); - } - - this.pattern = null; - } - - - /** - * - */ - public FileIteratorStates (File root, String filter) - { - super(); - - this.currentDepth = -1; // Note: push method increments this value. - if (root != null) - { - this.push(root); - } - - if (filter == null) - { - this.pattern = null; - } - else - { - this.pattern = Pattern.compile(filter); - } - - shift(); - } - - - /** - * - */ - protected String filter() - { - String result; - - result = this.pattern.toString(); - - // - return(result); - } - - - /** - * - */ - protected FileIteratorState currentState() - { - FileIteratorState result; - - result = this.get(this.currentDepth); - - // - return(result); - } - - - /** - * - */ - protected File currentFile() - { - File result; - - result = this.currentState().currentFile(); - - // - return(result); - } - - - /** - * - */ - public void push(File directory) - { - this.add(new FileIteratorState(directory)); - this.currentDepth += 1; - } - - - /** - * - */ - public void pop() - { - this.removeElementAt(this.currentDepth); - this.currentDepth -= 1; - } - - - /** - * - */ - public boolean hasNext() - { - boolean result; - - result = this.currentState().hasNext(); - - // - return(result); - } - - - /** - * Set indexes to the good next item. - */ - public void shift() - { - boolean ended = false; - while(!ended) - { - File next = this.currentFile(); - - if (next == null) - { - if (this.currentDepth == 0) - { - ended = true; - } - else - { - this.pop(); - } - } - else - { - if ((this.pattern == null) || (this.pattern.matcher(next.getPath()).matches())) - { - ended = true; - } - else - { - this.currentState().next(); - if (next.isDirectory()) - { - this.push(next); - } - } - } - } - } - - - /** - * - */ - public File next() - { - File result; - - result = this.currentState().next(); - - if (result.isDirectory()) - { - this.push(result); - } - - shift(); - - // - return(result); - } -}