Version of FileIterator.

This commit is contained in:
Christian P. MOMON 2010-06-18 01:35:55 +02:00
parent 1c78c0462c
commit fa2c541f0a
2 changed files with 102 additions and 31 deletions

View file

@ -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<File>
protected FileIteratorStates states;
protected File previous;
protected String filter;
protected Pattern pattern;
/**
*
@ -26,8 +22,6 @@ public class FileIterator implements Iterator<File>
{
this.states = new FileIteratorStates(root);
this.previous = null;
this.filter = null;
this.pattern = null;
}
@ -36,11 +30,22 @@ public class FileIterator implements Iterator<File>
*/
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);
}

View file

@ -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<FileIteratorState>
{
private static final long serialVersionUID = 8790133455427427766L;
protected int currentDepth;;
protected int currentDepth;
protected Pattern pattern;
/**
@ -27,6 +30,48 @@ public class FileIteratorStates extends Vector<FileIteratorState>
{
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<FileIteratorState>
}
/**
* 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<FileIteratorState>
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);