Extract NamedRevBlob to top level class

This commit is contained in:
Paul Campbell 2018-06-15 10:07:23 +01:00
parent d6f8bcbc0f
commit 2305c0ed1b
3 changed files with 62 additions and 29 deletions

View file

@ -22,12 +22,9 @@
package net.kemitix.gitdb.impl; package net.kemitix.gitdb.impl;
import lombok.AccessLevel; import lombok.AccessLevel;
import lombok.Getter;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import org.eclipse.jgit.lib.Constants;
import org.eclipse.jgit.lib.Ref; import org.eclipse.jgit.lib.Ref;
import org.eclipse.jgit.lib.Repository; import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.revwalk.RevBlob;
import org.eclipse.jgit.revwalk.RevWalk; import org.eclipse.jgit.revwalk.RevWalk;
import org.eclipse.jgit.treewalk.TreeWalk; import org.eclipse.jgit.treewalk.TreeWalk;
import org.eclipse.jgit.treewalk.filter.PathFilter; import org.eclipse.jgit.treewalk.filter.PathFilter;
@ -66,7 +63,8 @@ class GitTreeReader {
while (treeWalk.next()) { while (treeWalk.next()) {
builder.add(new NamedRevBlob( builder.add(new NamedRevBlob(
treeWalk.getNameString(), treeWalk.getNameString(),
new RevWalk(repository).lookupBlob(treeWalk.getObjectId(0)))); new RevWalk(repository).lookupBlob(treeWalk.getObjectId(0)),
repository));
} }
return builder.build(); return builder.build();
} }
@ -82,26 +80,4 @@ class GitTreeReader {
return this; return this;
} }
/**
* Represents the key/value pairs read from the tree.
*/
@Getter(AccessLevel.PACKAGE)
@RequiredArgsConstructor(access = AccessLevel.PACKAGE)
protected class NamedRevBlob {
private final String name;
private final RevBlob revBlob;
/**
* Converts the blob to a String.
*
* @return a string
* @throws IOException of there was an error reading the blob
*/
String blobAsString() throws IOException {
return new String(repository.open(revBlob.getId(), Constants.OBJ_BLOB).getBytes());
}
}
} }

View file

@ -50,7 +50,7 @@ class KeyRemover {
* @param removed the boolean to update * @param removed the boolean to update
* @return a Consumer * @return a Consumer
*/ */
private static Consumer<GitTreeReader.NamedRevBlob> flagIfFound(final String key, final AtomicBoolean removed) { private static Consumer<NamedRevBlob> flagIfFound(final String key, final AtomicBoolean removed) {
return nvb -> { return nvb -> {
if (nvb.getName().equals(key)) { if (nvb.getName().equals(key)) {
removed.set(true); removed.set(true);
@ -64,7 +64,7 @@ class KeyRemover {
* @param key the key to match * @param key the key to match
* @return a Predicate * @return a Predicate
*/ */
private static Predicate<GitTreeReader.NamedRevBlob> isNotKey(final String key) { private static Predicate<NamedRevBlob> isNotKey(final String key) {
return item -> !item.getName().equals(key); return item -> !item.getName().equals(key);
} }
@ -74,7 +74,7 @@ class KeyRemover {
* @param treeFormatter the tree formatter to add to * @param treeFormatter the tree formatter to add to
* @return a Consumer * @return a Consumer
*/ */
private static Consumer<GitTreeReader.NamedRevBlob> addToTree(final TreeFormatter treeFormatter) { private static Consumer<NamedRevBlob> addToTree(final TreeFormatter treeFormatter) {
return item -> treeFormatter.append(item.getName(), item.getRevBlob()); return item -> treeFormatter.append(item.getName(), item.getRevBlob());
} }

View file

@ -0,0 +1,57 @@
/*
The MIT License (MIT)
Copyright (c) 2018 Paul Campbell
Permission is hereby granted, free of charge, to any person obtaining a copy of this software
and associated documentation files (the "Software"), to deal in the Software without restriction,
including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies
or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package net.kemitix.gitdb.impl;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import org.eclipse.jgit.lib.Constants;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.revwalk.RevBlob;
import java.io.IOException;
/**
* Represents the key/value pairs read from the tree.
*
* @author Paul Campbell (pcampbell@kemitix.net)
*/
@RequiredArgsConstructor(access = AccessLevel.PACKAGE)
class NamedRevBlob {
@Getter
private final String name;
@Getter
private final RevBlob revBlob;
private final Repository repository;
/**
* Converts the blob to a String.
*
* @return a string
* @throws IOException of there was an error reading the blob
*/
String blobAsString() throws IOException {
return new String(repository.open(revBlob.getId(), Constants.OBJ_BLOB).getBytes());
}
}