From 2f68bd55d0b047a90061a2a0658c88d25ed77d21 Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Wed, 11 Jul 2018 07:10:49 +0100 Subject: [PATCH] GitTreeReader.stream: refactor for readability --- .../net/kemitix/gitdb/impl/GitTreeReader.java | 55 +++++++------------ 1 file changed, 21 insertions(+), 34 deletions(-) diff --git a/src/main/java/net/kemitix/gitdb/impl/GitTreeReader.java b/src/main/java/net/kemitix/gitdb/impl/GitTreeReader.java index a51dd21..b8cabd0 100644 --- a/src/main/java/net/kemitix/gitdb/impl/GitTreeReader.java +++ b/src/main/java/net/kemitix/gitdb/impl/GitTreeReader.java @@ -26,7 +26,6 @@ import lombok.RequiredArgsConstructor; import net.kemitix.mon.result.Result; import org.eclipse.jgit.lib.Ref; import org.eclipse.jgit.lib.Repository; -import org.eclipse.jgit.revwalk.RevCommit; import org.eclipse.jgit.revwalk.RevTree; import org.eclipse.jgit.revwalk.RevWalk; import org.eclipse.jgit.treewalk.TreeWalk; @@ -35,7 +34,6 @@ import org.eclipse.jgit.treewalk.filter.TreeFilter; import java.util.Optional; import java.util.concurrent.Callable; -import java.util.function.Consumer; import java.util.function.Function; import java.util.stream.Stream; @@ -58,27 +56,35 @@ class GitTreeReader { * @return a stream of key/value pairs as NamedRevBlobs */ Result> stream(final Ref branchRef) { - final TreeWalk treeWalk = new TreeWalk(repository); final RevWalk revWalk = new RevWalk(repository); - return Result.of(parseBranchCommit(branchRef, revWalk)) - .map(RevCommit::getTree) - .flatMap(addTreeTo(treeWalk)) - .peek(disableRecursion(treeWalk)) - .peek(setTreeFilter(treeWalk)) - .flatMap(streamMatching(treeWalk, revWalk)); + return Result.of(parseTree(branchRef, revWalk)) + .andThen(configureFilter()) + .andThen(buildStream(revWalk)); } - private Function>> streamMatching( - final TreeWalk treeWalk, - final RevWalk revWalk - ) { - return x -> Result.of(() -> { + private Callable parseTree(final Ref branchRef, final RevWalk revWalk) { + return () -> revWalk.parseCommit(branchRef.getObjectId()).getTree(); + } + + private Function> configureFilter() { + return tree -> () -> { + final TreeWalk treeWalk = new TreeWalk(repository); + treeWalk.addTree(tree); + treeWalk.setRecursive(false); + Optional.ofNullable(treeFilter) + .ifPresent(treeWalk::setFilter); + return treeWalk; + }; + } + + private Function>> buildStream(final RevWalk revWalk) { + return treeWalk -> () -> { final Stream.Builder builder = Stream.builder(); while (treeWalk.next()) { builder.add(namedRevBlob(treeWalk, revWalk)); } return builder.build(); - }); + }; } private NamedRevBlob namedRevBlob(final TreeWalk treeWalk, final RevWalk revWalk) { @@ -88,25 +94,6 @@ class GitTreeReader { repository); } - private Consumer setTreeFilter(final TreeWalk treeWalk) { - return x -> Optional.ofNullable(treeFilter).ifPresent(treeWalk::setFilter); - } - - private Consumer disableRecursion(final TreeWalk treeWalk) { - return x -> treeWalk.setRecursive(false); - } - - private Function> addTreeTo(final TreeWalk treeWalk) { - return tree -> Result.of(() -> { - treeWalk.addTree(tree); - return null; - }); - } - - private Callable parseBranchCommit(final Ref branchRef, final RevWalk revWalk) { - return () -> revWalk.parseCommit(branchRef.getObjectId()); - } - /** * Sets a path filter to limit the stream by. *