From fa2c541f0a0f695379701856cb32c7efb61e0a5e Mon Sep 17 00:00:00 2001 From: "Christian P. MOMON" Date: Fri, 18 Jun 2010 01:35:55 +0200 Subject: [PATCH] Version of FileIterator. --- src/fr/devinsy/util/FileIterator.java | 25 +++-- src/fr/devinsy/util/FileIteratorStates.java | 108 ++++++++++++++++---- 2 files changed, 102 insertions(+), 31 deletions(-) diff --git a/src/fr/devinsy/util/FileIterator.java b/src/fr/devinsy/util/FileIterator.java index 2acb34d..5ad0a3a 100644 --- a/src/fr/devinsy/util/FileIterator.java +++ b/src/fr/devinsy/util/FileIterator.java @@ -2,8 +2,6 @@ package fr.devinsy.util; import java.io.File; import java.util.Iterator; -import java.util.regex.Matcher; -import java.util.regex.Pattern; @@ -16,8 +14,6 @@ public class FileIterator implements Iterator protected FileIteratorStates states; protected File previous; - protected String filter; - protected Pattern pattern; /** * @@ -26,8 +22,6 @@ public class FileIterator implements Iterator { this.states = new FileIteratorStates(root); this.previous = null; - this.filter = null; - this.pattern = null; } @@ -36,14 +30,25 @@ public class FileIterator implements Iterator */ public FileIterator (File root, String filter) { - this.states = new FileIteratorStates(root); + this.states = new FileIteratorStates(root, filter); this.previous = null; - - this.filter = filter; - this.pattern = Pattern.compile(filter); } + /** + * + */ + protected String filter() + { + String result; + + result = this.states.filter(); + + // + return(result); + } + + /** * */ diff --git a/src/fr/devinsy/util/FileIteratorStates.java b/src/fr/devinsy/util/FileIteratorStates.java index 1ca8f83..3750ae9 100644 --- a/src/fr/devinsy/util/FileIteratorStates.java +++ b/src/fr/devinsy/util/FileIteratorStates.java @@ -2,6 +2,7 @@ package fr.devinsy.util; import java.io.File; import java.util.Vector; +import java.util.regex.Pattern; @@ -12,7 +13,9 @@ public class FileIteratorStates extends Vector { private static final long serialVersionUID = 8790133455427427766L; - protected int currentDepth;; + protected int currentDepth; + protected Pattern pattern; + /** @@ -27,9 +30,51 @@ public class FileIteratorStates extends Vector { 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); + } + + /** * */ @@ -92,6 +137,46 @@ public class FileIteratorStates extends Vector } + /** + * 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); + } + } + } + } + } + + /** * */ @@ -106,26 +191,7 @@ public class FileIteratorStates extends Vector this.push(result); } - // Up states if needed. - boolean ended = false; - while(!ended) - { - if (this.hasNext()) - { - ended = true; - } - else - { - if (this.currentDepth == 0) - { - ended = true; - } - else - { - this.pop(); - } - } - } + shift(); // return(result);