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

Various internal code simplifications and modernizations #2989

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

HannesWell
Copy link
Member

This PR contains the internal code simplifications and modernizations that happened as side effect of the following PRs, in order to ease their review:

try (ZipFile zipFile = new ZipFile(file);) {
return zipFile.stream().map(entry -> getUri(file, entry)) //
.filter(Objects::nonNull).filter(isValidPredicate) //
.collect(Collectors.toSet());
Copy link
Contributor

Choose a reason for hiding this comment

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

Another subtle change in behavior. Collectors.toSet doesn't make any API guarantees about mutability of the returned set. Regardless of my personal preference of iteration vs stream pipelines, I'd go for collect(Collectors.toCollection(HashSet::new))

Copy link
Member Author

Choose a reason for hiding this comment

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

That's right, but since PathTraverser.traverseArchive() respectively findAllResourceUris() does not make any guarantees as well I assumed it's fine to change this implementation detail. However, I applied your suggestion.

@@ -84,7 +84,7 @@ public String toString() {
result.append("(empty)");
else {
result.append("{\n ");
result.append(Joiner.on("\n ").join(uris));
result.append(uris.stream().map(URI::toString).collect(Collectors.joining("\n ")));
Copy link
Contributor

Choose a reason for hiding this comment

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

Quite a mouthful compared to Joiner.on. Both in terms of characters, cognitive load and object allocations. I'd keep this as a Joiner.

Copy link
Member Author

Choose a reason for hiding this comment

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

Quite a mouthful compared to Joiner.on. Both in terms of characters, cognitive load and object allocations.

Yes it has more characters, but personally I would say it is simple since you can read the pipeline from left to right, but I'm more used to Streams than Guava's Joiner so it is a matter of taste.
I haven't counted all object creations, but Joiner also creates objects and the JVM+GCs are usually good in handling short living objects respectively even avoiding their creation on the heap.
But the biggest impact is probably due to the creation of the String result of the join operation and if you are really concerned about object creation here, I would try to avoid that by using something like the following instead:

for (Iterator<String> containers = invisibleContainers.iterator(); containers.hasNext();) {
	result.append(containers.next());
	if(containers.hasNext()) {
		result.append("\n    ");
	}
}

That being said, I reverted that change for now.

@HannesWell
Copy link
Member Author

Just rebased this on the latest master, is there anything else I should adjust?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants