Gridarta Editor
RecursiveFileIterator.java
Go to the documentation of this file.
1 /*
2  * Gridarta MMORPG map editor for Crossfire, Daimonin and similar games.
3  * Copyright (C) 2000-2023 The Gridarta Developers.
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18  */
19 
20 package net.sf.gridarta.model.io;
21 
22 import java.io.File;
23 import java.util.EmptyStackException;
24 import java.util.Iterator;
25 import java.util.Stack;
26 import org.jetbrains.annotations.NotNull;
27 
33 public class RecursiveFileIterator implements Iterator<File> {
34 
38  @NotNull
39  private final Stack<Iterator<File>> iteratorStack = new Stack<>();
40 
44  @NotNull
45  private Iterator<File> current;
46 
51  public RecursiveFileIterator(@NotNull final File dir) {
52  current = new FlatFileIterator(dir);
53  }
54 
55  @Override
56  public boolean hasNext() {
57  return current.hasNext();
58  }
59 
60  @NotNull
61  @Override
62  public File next() {
63  final File result = current.next();
64  iteratorStack.push(current);
65  current = new FlatFileIterator(result);
66  try {
67  while (!current.hasNext()) {
68  current = iteratorStack.pop();
69  }
70  } catch (final EmptyStackException ignore) {
71  /* ignore. */
72  }
73 
74  return result;
75  }
76 
77  @Override
78  public void remove() {
79  current.remove();
80  }
81 
82 }
net.sf.gridarta.model.io.RecursiveFileIterator.current
Iterator< File > current
The current Iterator.
Definition: RecursiveFileIterator.java:45
net.sf.gridarta.model.io.RecursiveFileIterator.next
File next()
Definition: RecursiveFileIterator.java:62
net.sf.gridarta.model.io.RecursiveFileIterator.iteratorStack
final Stack< Iterator< File > > iteratorStack
The stack of active Iterators.
Definition: RecursiveFileIterator.java:39
net.sf.gridarta.model.io.RecursiveFileIterator.hasNext
boolean hasNext()
Definition: RecursiveFileIterator.java:56
net.sf.gridarta.model.io.RecursiveFileIterator.RecursiveFileIterator
RecursiveFileIterator(@NotNull final File dir)
Creates a new instance.
Definition: RecursiveFileIterator.java:51
net.sf.gridarta.model.io.RecursiveFileIterator
An Iterator that recursively returns all files from a directory.
Definition: RecursiveFileIterator.java:33
net.sf.gridarta.model.io.FlatFileIterator
An Iterator that iterates non-recursively over the contents of a directory.
Definition: FlatFileIterator.java:33