Gridarta Editor
FlatFileIterator.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.Arrays;
24 import java.util.Iterator;
25 import java.util.NoSuchElementException;
26 import org.jetbrains.annotations.NotNull;
27 
33 public class FlatFileIterator implements Iterator<File> {
34 
38  @NotNull
39  private static final File @NotNull [] EMPTY_FILE_ARRAY = new File[0];
40 
44  @NotNull
45  private final File @NotNull [] files;
46 
50  private int pos;
51 
56  public FlatFileIterator(@NotNull final File dir) {
57  final File[] tmp = dir.listFiles();
58  files = tmp == null ? EMPTY_FILE_ARRAY : tmp;
59  Arrays.sort(files);
61  }
62 
63  /* {@inheritDoc} */
64 
65  @Override
66  public boolean hasNext() {
67  return pos < files.length;
68  }
69 
70  @Override
71  public File next() {
72  if (pos >= files.length) {
73  throw new NoSuchElementException("no more elements");
74  }
75 
76  final File result = files[pos++];
78  return result;
79  }
80 
81  @Override
82  public void remove() {
83  throw new UnsupportedOperationException("unsupported operation");
84  }
85 
89  private void skipSpecialNames() {
90  while (pos < files.length && files[pos].getName().equalsIgnoreCase(".svn")) {
91  pos++;
92  }
93  }
94 
95 }
net.sf.gridarta.model.io.FlatFileIterator.FlatFileIterator
FlatFileIterator(@NotNull final File dir)
Creates a new instance.
Definition: FlatFileIterator.java:56
net.sf.gridarta.model.io.FlatFileIterator.hasNext
boolean hasNext()
Definition: FlatFileIterator.java:66
net.sf.gridarta.model.io.FlatFileIterator.skipSpecialNames
void skipSpecialNames()
Skips special files that should be always ignored.
Definition: FlatFileIterator.java:89
net.sf.gridarta.model.io.FlatFileIterator.files
final File[] files
The files to return.
Definition: FlatFileIterator.java:45
net.sf.gridarta.model.io.FlatFileIterator.EMPTY_FILE_ARRAY
static final File[] EMPTY_FILE_ARRAY
An empty array of Files.
Definition: FlatFileIterator.java:39
net.sf.gridarta.model.io.FlatFileIterator.pos
int pos
The current index into files.
Definition: FlatFileIterator.java:50
net.sf.gridarta.model.io.FlatFileIterator.next
File next()
Definition: FlatFileIterator.java:71
net.sf.gridarta.model.io.FlatFileIterator
An Iterator that iterates non-recursively over the contents of a directory.
Definition: FlatFileIterator.java:33