|
16 | 16 | import build.bazel.remote.execution.v2.Digest;
|
17 | 17 | import com.google.common.base.Preconditions;
|
18 | 18 | import com.google.common.collect.Iterables;
|
19 |
| -import com.google.common.collect.Sets; |
20 | 19 | import com.google.devtools.build.lib.vfs.Path;
|
21 | 20 | import com.google.devtools.build.lib.vfs.PathFragment;
|
| 21 | +import com.google.errorprone.annotations.CanIgnoreReturnValue; |
22 | 22 | import com.google.protobuf.ByteString;
|
23 |
| -import java.util.ArrayList; |
24 | 23 | import java.util.HashMap;
|
25 |
| -import java.util.List; |
26 | 24 | import java.util.Map;
|
27 | 25 | import java.util.Objects;
|
28 | 26 | import java.util.SortedSet;
|
| 27 | +import java.util.TreeSet; |
29 | 28 |
|
30 | 29 | /**
|
31 | 30 | * Intermediate tree representation of a list of lexicographically sorted list of files. Each node
|
|
34 | 33 | final class DirectoryTree {
|
35 | 34 |
|
36 | 35 | interface Visitor {
|
| 36 | + |
37 | 37 | void visitDirectory(
|
38 | 38 | PathFragment dirname,
|
39 |
| - List<FileNode> files, |
40 |
| - List<SymlinkNode> symlinks, |
41 |
| - List<DirectoryNode> dirs); |
| 39 | + SortedSet<FileNode> files, |
| 40 | + SortedSet<SymlinkNode> symlinks, |
| 41 | + SortedSet<DirectoryNode> dirs); |
42 | 42 | }
|
43 | 43 |
|
44 | 44 | abstract static class Node implements Comparable<Node> {
|
@@ -204,26 +204,44 @@ public String toString() {
|
204 | 204 | }
|
205 | 205 |
|
206 | 206 | static class DirectoryNode extends Node {
|
207 |
| - private final SortedSet<Node> children = Sets.newTreeSet(); |
| 207 | + |
| 208 | + private final SortedSet<FileNode> files = new TreeSet<>(); |
| 209 | + private final SortedSet<SymlinkNode> symlinks = new TreeSet<>(); |
| 210 | + private final SortedSet<DirectoryNode> subdirs = new TreeSet<>(); |
208 | 211 |
|
209 | 212 | DirectoryNode(String pathSegment) {
|
210 | 213 | super(pathSegment);
|
211 | 214 | }
|
212 | 215 |
|
213 |
| - boolean addChild(Node child) { |
214 |
| - return children.add(Preconditions.checkNotNull(child, "child")); |
| 216 | + @CanIgnoreReturnValue |
| 217 | + boolean addChild(FileNode file) { |
| 218 | + return files.add(Preconditions.checkNotNull(file, "file")); |
| 219 | + } |
| 220 | + |
| 221 | + @CanIgnoreReturnValue |
| 222 | + boolean addChild(SymlinkNode symlink) { |
| 223 | + return symlinks.add(Preconditions.checkNotNull(symlink, "symlink")); |
| 224 | + } |
| 225 | + |
| 226 | + @CanIgnoreReturnValue |
| 227 | + boolean addChild(DirectoryNode subdir) { |
| 228 | + return subdirs.add(Preconditions.checkNotNull(subdir, "subdir")); |
215 | 229 | }
|
216 | 230 |
|
217 | 231 | @Override
|
218 | 232 | public int hashCode() {
|
219 |
| - return Objects.hash(super.hashCode(), children.hashCode()); |
| 233 | + return Objects.hash( |
| 234 | + super.hashCode(), files.hashCode(), symlinks.hashCode(), subdirs.hashCode()); |
220 | 235 | }
|
221 | 236 |
|
222 | 237 | @Override
|
223 | 238 | public boolean equals(Object o) {
|
224 | 239 | if (o instanceof DirectoryNode) {
|
225 | 240 | DirectoryNode other = (DirectoryNode) o;
|
226 |
| - return super.equals(other) && Objects.equals(children, other.children); |
| 241 | + return super.equals(other) |
| 242 | + && Objects.equals(files, other.files) |
| 243 | + && Objects.equals(symlinks, other.symlinks) |
| 244 | + && Objects.equals(subdirs, other.subdirs); |
227 | 245 | }
|
228 | 246 | return false;
|
229 | 247 | }
|
@@ -265,23 +283,10 @@ private void visit(Visitor visitor, PathFragment dirname) {
|
265 | 283 | return;
|
266 | 284 | }
|
267 | 285 |
|
268 |
| - List<FileNode> files = new ArrayList<>(dir.children.size()); |
269 |
| - List<SymlinkNode> symlinks = new ArrayList<>(); |
270 |
| - List<DirectoryNode> dirs = new ArrayList<>(); |
271 |
| - for (Node child : dir.children) { |
272 |
| - if (child instanceof FileNode) { |
273 |
| - files.add((FileNode) child); |
274 |
| - } else if (child instanceof SymlinkNode) { |
275 |
| - symlinks.add((SymlinkNode) child); |
276 |
| - } else if (child instanceof DirectoryNode) { |
277 |
| - dirs.add((DirectoryNode) child); |
278 |
| - visit(visitor, dirname.getRelative(child.pathSegment)); |
279 |
| - } else { |
280 |
| - throw new IllegalStateException( |
281 |
| - String.format("Node type '%s' is not supported", child.getClass().getSimpleName())); |
282 |
| - } |
| 286 | + for (DirectoryNode subdir : dir.subdirs) { |
| 287 | + visit(visitor, dirname.getRelative(subdir.getPathSegment())); |
283 | 288 | }
|
284 |
| - visitor.visitDirectory(dirname, files, symlinks, dirs); |
| 289 | + visitor.visitDirectory(dirname, dir.files, dir.symlinks, dir.subdirs); |
285 | 290 | }
|
286 | 291 |
|
287 | 292 | @Override
|
|
0 commit comments