Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reduce usages of Guava #766

Merged
merged 1 commit into from
Oct 17, 2021
Merged

Reduce usages of Guava #766

merged 1 commit into from
Oct 17, 2021

Conversation

basil
Copy link
Member

@basil basil commented Oct 16, 2021

This PR contains some reduction of Guava usages. In general, depending on fewer third-party libraries in favor of native Java Platform functionality eases maintenance.

@@ -3054,7 +3053,7 @@ private void sparseCheckout(@NonNull List<String> paths) throws GitException, In
return;
} else if(paths.isEmpty() && coreSparseCheckoutConfigEnable) { // deactivating sparse checkout needed
deactivatingSparseCheckout = true;
paths = Lists.newArrayList("/*");
paths = Collections.singletonList("/*");
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

paths is on the stack, is not returned (because this method returns void), and is only ever read from (not written to) in this method. Therefore, it is safe to switch from a ArrayList to an undefined List implementation.

@@ -1452,7 +1449,7 @@ else if (!referencePath.isDirectory())

StoredConfig config = repository.getConfig();
config.setString("remote", remote, "url", url);
config.setStringList("remote", remote, "fetch", Lists.newArrayList(Iterables.transform(refspecs, Functions.toStringFunction())));
config.setStringList("remote", remote, "fetch", refspecs.stream().map(Object::toString).collect(Collectors.toList()));
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

refspecs is not null because of the code on line 1438, so it is safe to call stream on it. The Guava documentation explicitly recommends Object::toString over Functions.toStringFunction. The setStringList() method we are calling does a defensive copy of its input, so it is safe to switch from ArrayList to an undefined List implementation.

@@ -2463,7 +2460,7 @@ private void markAllRefs(RevWalk walk) throws IOException {
private void markRefs(RevWalk walk, Predicate<Ref> filter) throws IOException {
try (Repository repo = getRepository()) {
for (Ref r : repo.getAllRefs().values()) {
if (filter.apply(r)) {
if (filter.test(r)) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A consequence of switching from Guava's Predicate type to Java's Predicate type. Since this method is private, serialization is not a concern.

@@ -2775,7 +2772,7 @@ public boolean isBareRepository(String GIT_DIR) throws GitException, Interrupted
public String getDefaultRemote(String _default_) throws GitException, InterruptedException {
Set<String> remotes = getConfig(null).getSubsections("remote");
if (remotes.contains(_default_)) return _default_;
else return com.google.common.collect.Iterables.getFirst(remotes, null);
else return remotes.stream().findFirst().orElse(null);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remotes is not null, or we we would have NPE'd on the previous line, so it is safe to call stream(). The Guava documentation explicitly recommends this paradigm.

@basil basil mentioned this pull request Oct 16, 2021
8 tasks
@MarkEWaite MarkEWaite added the chore Reduces maintenance effort by changes not directly visible to users label Oct 17, 2021
@MarkEWaite MarkEWaite merged commit 67d3533 into jenkinsci:master Oct 17, 2021
@basil basil deleted the guava branch October 18, 2021 16:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
chore Reduces maintenance effort by changes not directly visible to users
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants