public static Tree treeOf(InventoryStructure<?> inventory) { @SuppressWarnings("unchecked") Tree.AbstractBuilder<?>[] tbld = new Tree.AbstractBuilder[1]; Consumer<ComputeHash.IntermediateHashContext> startChild = context -> { if (tbld[0] == null) { tbld[0] = Tree.builder(); } else { tbld[0] = tbld[0].startChild(); } }; BiConsumer<ComputeHash.IntermediateHashContext, ComputeHash.IntermediateHashResult> endChild = (ctx, result) -> { if (tbld[0] instanceof Tree.ChildBuilder) { tbld[0].withHash(result.identityHash).withPath(result.path); @SuppressWarnings("unchecked") Tree.AbstractBuilder<?> parent = ((Tree.ChildBuilder<?>) tbld[0]).getParent(); parent.addChild(((Tree.ChildBuilder<?>) tbld[0]).build()); tbld[0] = parent; } }; //identity hash is not computed using any relative paths, so we can pass null to the root path of the //computation. ComputeHash.IntermediateHashResult res = ComputeHash.treeOf(inventory, null, true, false, false, startChild, endChild, p -> null); tbld[0].withPath(res.path).withHash(res.identityHash); return ((Tree.Builder)tbld[0]).build(); }
.treeOf(structure, rootPath, true, true, true, startChild, endChild, hashLoader);
public static Tree treeOf(InventoryStructure<?> root, CanonicalPath rootPath, Function<RelativePath, Hashes> hashLoader) { Tree.AbstractBuilder<?>[] tbld = new Tree.AbstractBuilder[1]; Consumer<IntermediateHashContext> startChild = context -> { if (tbld[0] == null) { tbld[0] = Tree.builder(); } else { tbld[0] = tbld[0].startChild(); } }; BiConsumer<IntermediateHashContext, IntermediateHashResult> endChild = (ctx, result) -> { if (tbld[0] instanceof Tree.ChildBuilder) { tbld[0].withHash(new Hashes(result)).withPath(result.path); @SuppressWarnings("unchecked") Tree.AbstractBuilder<?> parent = ((Tree.ChildBuilder<?>) tbld[0]).getParent(); parent.addChild(((Tree.ChildBuilder<?>) tbld[0]).build()); tbld[0] = parent; } }; IntermediateHashResult res = ComputeHash.treeOf(root, rootPath, true, true, true, startChild, endChild, hashLoader); tbld[0].withPath(res.path).withHash(new Hashes(res)); return ((Tree.Builder)tbld[0]).build(); }