Add cardinal methods.

This commit is contained in:
Christian P. MOMON 2010-06-20 14:44:37 +02:00
parent cb0a16fe13
commit 1fe86f0b8a
2 changed files with 109 additions and 6 deletions

View file

@ -17,6 +17,7 @@ public class FileIterator extends Vector<FileIteratorState> implements Iterator<
protected int currentDepth; protected int currentDepth;
protected Pattern pattern; protected Pattern pattern;
protected File previous; protected File previous;
protected boolean followLink;
/** /**
@ -34,6 +35,7 @@ public class FileIterator extends Vector<FileIteratorState> implements Iterator<
this.pattern = null; this.pattern = null;
this.previous = null; this.previous = null;
this.followLink = false;
} }
@ -61,6 +63,7 @@ public class FileIterator extends Vector<FileIteratorState> implements Iterator<
} }
this.previous = null; this.previous = null;
this.followLink = false;
} }
@ -75,10 +78,28 @@ public class FileIterator extends Vector<FileIteratorState> implements Iterator<
this.add(new FileIteratorState(pathnames, prefix)); this.add(new FileIteratorState(pathnames, prefix));
this.pattern = null; this.pattern = null;
this.previous = null; this.previous = null;
this.followLink = false;
} }
/**
*
*/
public void reset ()
{
this.currentDepth = 0;
this.previous = null;
if (this.size() > 0)
{
this.get(0).reset();
FileIteratorState firstState = this.get(0);
this.removeAllElements();
this.add(firstState);
}
}
/** /**
* *
*/ */
@ -171,14 +192,15 @@ public class FileIterator extends Vector<FileIteratorState> implements Iterator<
} }
else else
{ {
if ((this.pattern == null) || (this.pattern.matcher(next.getPath()).matches())) if (((this.pattern == null) || (this.pattern.matcher(next.getPath()).matches())) && (follow(next)))
{ {
ended = true; ended = true;
} }
else else
{ {
this.currentState().next(); this.currentState().next();
if (next.isDirectory())
if (next.isDirectory() && (follow(next)))
{ {
this.push(next); this.push(next);
} }
@ -214,14 +236,16 @@ public class FileIterator extends Vector<FileIteratorState> implements Iterator<
result = this.currentState().next(); result = this.currentState().next();
this.previous = result; this.previous = result;
if (result != null)
if (result.isDirectory())
{ {
this.push(result); if (result.isDirectory())
{
this.push(result);
}
} }
shift(); shift();
// //
return(result); return(result);
} }
@ -241,6 +265,74 @@ public class FileIterator extends Vector<FileIteratorState> implements Iterator<
} }
/**
*
*/
static public boolean isLink(File file) throws Exception
{
boolean result;
if (file.getCanonicalPath().equals(file.getAbsolutePath()))
{
result = false;
}
else
{
result = true;
}
//
return(result);
}
/**
*
*/
public boolean follow(File file)
{
boolean result;
result = false;
try
{
if ((this.followLink) || (!isLink(file)))
{
result = true;
}
else
{
result = false;
}
}
catch (Exception exception)
{
System.err.println("ERROR with file [" + this.next() + "]: " + exception.getMessage());
result = false;
}
//
return(result);
}
/**
*
*/
public int finalCountdown()
{
int result;
result = 0;
while (this.next() != null)
{
result += 1;
}
//
return(result);
}
/** /**
* *
*/ */

View file

@ -17,6 +17,8 @@ public class FileIteratorState implements Iterator<File>
*/ */
public FileIteratorState (String[] pathnames, String prefix) public FileIteratorState (String[] pathnames, String prefix)
{ {
this.currentIndex = 0;
if (prefix == null) if (prefix == null)
{ {
prefix = ""; prefix = "";
@ -56,6 +58,15 @@ public class FileIteratorState implements Iterator<File>
} }
/**
*
*/
public void reset()
{
currentIndex = 0;
}
/** /**
* *
*/ */