Merge FileIterator and FileIteraotrStates. Add String[] constructor.

This commit is contained in:
Christian P. MOMON 2010-06-18 02:22:03 +02:00
parent fa2c541f0a
commit 6af7dea6b2
3 changed files with 200 additions and 223 deletions

View file

@ -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<File>
public class FileIterator extends Vector<FileIteratorState> implements Iterator<File>
{
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<File>
{
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<File>
/**
*
*
*/
@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<File>
/**
*
*
*/
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<File>
}
}
/**
*
*/
@ -97,7 +248,7 @@ public class FileIterator implements Iterator<File>
{
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);

View file

@ -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);
}
}
/**
*
*/

View file

@ -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<FileIteratorState>
{
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);
}
}