Skip to content

Commit 62bf4a4

Browse files
gab1onectrueden
authored andcommitted
FileLocation: implement BrowsableLocation iface
Signed-off-by: Curtis Rueden <[email protected]>
1 parent 66781e9 commit 62bf4a4

File tree

1 file changed

+51
-1
lines changed

1 file changed

+51
-1
lines changed

src/main/java/org/scijava/io/location/FileLocation.java

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,21 @@
3333
package org.scijava.io.location;
3434

3535
import java.io.File;
36+
import java.io.IOException;
3637
import java.net.URI;
38+
import java.util.Collections;
39+
import java.util.HashSet;
40+
import java.util.Set;
3741

3842
/**
3943
* {@link Location} backed by a {@link File} on disk.
4044
*
4145
* @author Curtis Rueden
46+
* @author Gabriel Einsdorf
4247
*/
43-
public class FileLocation extends AbstractLocation {
48+
public class FileLocation extends AbstractLocation implements
49+
BrowsableLocation
50+
{
4451

4552
private final File file;
4653

@@ -75,4 +82,47 @@ public String getName() {
7582
return file.getName();
7683
}
7784

85+
// -- BrowsableLocation methods --
86+
87+
@Override
88+
public FileLocation parent() throws IOException {
89+
return new FileLocation(file.getParentFile());
90+
}
91+
92+
@Override
93+
public Set<BrowsableLocation> children() throws IOException {
94+
validateDirectory();
95+
final File[] files = file.listFiles();
96+
if (files == null) return Collections.emptySet();
97+
98+
final Set<BrowsableLocation> out = new HashSet<>(files.length);
99+
for (final File child : files) {
100+
out.add(new FileLocation(child));
101+
}
102+
return out;
103+
}
104+
105+
@Override
106+
public FileLocation sibling(final String path) {
107+
return new FileLocation(new File(file.getParentFile(), path));
108+
}
109+
110+
@Override
111+
public FileLocation child(final String name) {
112+
validateDirectory();
113+
return new FileLocation(new File(file, name));
114+
}
115+
116+
@Override
117+
public boolean isDirectory() {
118+
return file.isDirectory();
119+
}
120+
121+
// -- Helper methods --
122+
123+
private void validateDirectory() {
124+
if (isDirectory()) return;
125+
throw new IllegalArgumentException(
126+
"This location does not point to a directory!");
127+
}
78128
}

0 commit comments

Comments
 (0)