From 3789afb8248eb12202c0ee823fa13f8a9efaa7f3 Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Sat, 21 Jul 2018 07:57:17 +0100 Subject: [PATCH] KeyRemover.remove: refactor - rename GitTreeReader.stream as entries --- .../net/kemitix/gitdb/impl/GitDBRepo.java | 2 +- .../net/kemitix/gitdb/impl/GitTreeReader.java | 2 +- .../net/kemitix/gitdb/impl/KeyRemover.java | 24 ++++++++++++++----- .../net/kemitix/gitdb/impl/KeyWriter.java | 2 +- 4 files changed, 21 insertions(+), 9 deletions(-) diff --git a/src/main/java/net/kemitix/gitdb/impl/GitDBRepo.java b/src/main/java/net/kemitix/gitdb/impl/GitDBRepo.java index 321c526..20e759e 100644 --- a/src/main/java/net/kemitix/gitdb/impl/GitDBRepo.java +++ b/src/main/java/net/kemitix/gitdb/impl/GitDBRepo.java @@ -92,7 +92,7 @@ class GitDBRepo { } private Result> streamTree(final Ref branchRef, final GitTreeReader treeFilter) { - return treeFilter.stream(branchRef); + return treeFilter.entries(branchRef); } /** diff --git a/src/main/java/net/kemitix/gitdb/impl/GitTreeReader.java b/src/main/java/net/kemitix/gitdb/impl/GitTreeReader.java index b8cabd0..9651601 100644 --- a/src/main/java/net/kemitix/gitdb/impl/GitTreeReader.java +++ b/src/main/java/net/kemitix/gitdb/impl/GitTreeReader.java @@ -55,7 +55,7 @@ class GitTreeReader { * @param branchRef the branch to read * @return a stream of key/value pairs as NamedRevBlobs */ - Result> stream(final Ref branchRef) { + Result> entries(final Ref branchRef) { final RevWalk revWalk = new RevWalk(repository); return Result.of(parseTree(branchRef, revWalk)) .andThen(configureFilter()) diff --git a/src/main/java/net/kemitix/gitdb/impl/KeyRemover.java b/src/main/java/net/kemitix/gitdb/impl/KeyRemover.java index b1e28ca..0ae11c6 100644 --- a/src/main/java/net/kemitix/gitdb/impl/KeyRemover.java +++ b/src/main/java/net/kemitix/gitdb/impl/KeyRemover.java @@ -24,11 +24,13 @@ package net.kemitix.gitdb.impl; import lombok.RequiredArgsConstructor; import net.kemitix.mon.maybe.Maybe; import net.kemitix.mon.result.Result; +import net.kemitix.mon.result.WithResultContinuation; import org.eclipse.jgit.lib.*; import java.util.concurrent.atomic.AtomicBoolean; import java.util.function.Consumer; import java.util.function.Predicate; +import java.util.stream.Stream; import static net.kemitix.conditional.Condition.where; @@ -52,12 +54,22 @@ class KeyRemover { Result> remove(final Ref branchRef, final String key) { final TreeFormatter treeFormatter = new TreeFormatter(); final AtomicBoolean removed = new AtomicBoolean(false); - new GitTreeReader(repository) - .stream(branchRef) - .peek(s -> s.peek(flagIfFound(key, removed)) - .filter(isNotKey(key)) - .forEach(addToTree(treeFormatter))); - return insertTree(treeFormatter).maybe(oi -> removed.get()); + return new GitTreeReader(repository) + .entries(branchRef) + .thenWith(s -> addOthersToTree(key, treeFormatter, removed, s)) + .flatMap(s -> insertTree(treeFormatter)) + .maybe(oi -> removed.get()); + } + + private static WithResultContinuation> addOthersToTree( + final String key, + final TreeFormatter treeFormatter, + final AtomicBoolean removed, + final Stream s) { + return () -> s + .peek(flagIfFound(key, removed)) + .filter(isNotKey(key)) + .forEach(addToTree(treeFormatter)); } /** diff --git a/src/main/java/net/kemitix/gitdb/impl/KeyWriter.java b/src/main/java/net/kemitix/gitdb/impl/KeyWriter.java index 0c7364b..30f20fc 100644 --- a/src/main/java/net/kemitix/gitdb/impl/KeyWriter.java +++ b/src/main/java/net/kemitix/gitdb/impl/KeyWriter.java @@ -71,7 +71,7 @@ class KeyWriter { private Result getTreeFormatter(final Ref branchRef) { final TreeFormatter treeFormatter = new TreeFormatter(); final GitTreeReader gitTreeReader = new GitTreeReader(repository); - return gitTreeReader.stream(branchRef) + return gitTreeReader.entries(branchRef) .peek(s -> s.forEach(item -> treeFormatter.append(item.getName(), item.getRevBlob()))) .map(x -> treeFormatter); }