|
33 | 33 | package org.scijava.io.location;
|
34 | 34 |
|
35 | 35 | import java.io.File;
|
| 36 | +import java.io.IOException; |
36 | 37 | import java.net.URI;
|
| 38 | +import java.util.Collections; |
| 39 | +import java.util.HashSet; |
| 40 | +import java.util.Set; |
37 | 41 |
|
38 | 42 | /**
|
39 | 43 | * {@link Location} backed by a {@link File} on disk.
|
40 | 44 | *
|
41 | 45 | * @author Curtis Rueden
|
| 46 | + * @author Gabriel Einsdorf |
42 | 47 | */
|
43 |
| -public class FileLocation extends AbstractLocation { |
| 48 | +public class FileLocation extends AbstractLocation implements |
| 49 | + BrowsableLocation |
| 50 | +{ |
44 | 51 |
|
45 | 52 | private final File file;
|
46 | 53 |
|
@@ -75,4 +82,47 @@ public String getName() {
|
75 | 82 | return file.getName();
|
76 | 83 | }
|
77 | 84 |
|
| 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 | + } |
78 | 128 | }
|
0 commit comments