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

Clean up Optional-related code. #9285

Merged
merged 4 commits into from
Jan 11, 2023

Conversation

Isira-Seneviratne
Copy link
Member

@Isira-Seneviratne Isira-Seneviratne commented Nov 3, 2022

What is it?

  • Bugfix (user facing)
  • Feature (user facing)
  • Codebase improvement (dev facing)
  • Meta improvement to the project (dev facing)

Description of the changes in your PR

  • Remove SimplifyOptionalCallChains, as the Optional#isEmpty() method is now available in the Android SDK starting with version 33 and is desugared.
  • Make use of Optional method chaining.

Fixes the following issue(s)

  • Fixes #

APK testing

The APK can be found by going to the "Checks" tab below the title. On the left pane, click on "CI", scroll down to "artifacts" and click "app" to download the zip file which contains the debug APK of this PR.

Due diligence

@Stypox
Copy link
Member

Stypox commented Jan 2, 2023

Why do you think SimplifyOptionalCallChains is not needed anymore? It was not added by accident: it was added because the IDE was complaining. So why does Android Studio say it's not a valid method, but at the same time suggests it? Is it desugared properly?

image
image

@Isira-Seneviratne
Copy link
Member Author

Why do you think SimplifyOptionalCallChains is not needed anymore? It was not added by accident: it was added because the IDE was complaining. So why does Android Studio say it's not a valid method, but at the same time suggests it? Is it desugared properly?

image image

Yeah, the isEmpty() method was added to the Android SDK starting with version 33, and it gets desugared.

Copy link
Member

@Stypox Stypox left a comment

Choose a reason for hiding this comment

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

Other simplifications look good to me

Comment on lines 424 to 426
final var source = playbackListener.sourceOf(stream, streamInfo);

return Optional.ofNullable(source)
Copy link
Member

Choose a reason for hiding this comment

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

And also turn source1 into source below

Suggested change
final var source = playbackListener.sourceOf(stream, streamInfo);
return Optional.ofNullable(source)
return Optional.ofNullable(playbackListener.sourceOf(stream, streamInfo))

Comment on lines 2058 to 2062
}, () -> {
reloadPlayQueueManager();
setRecovery();
});
Copy link
Member

Choose a reason for hiding this comment

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

Maybe add a comment in this lambda to make it clear that it is called when we have no current stream info

@Stypox
Copy link
Member

Stypox commented Jan 2, 2023

Yeah, the isEmpty() method was added to the Android SDK starting with version 33, and it gets desugared.

Oh great! I had gradle out of sync with the ide

Copy link
Member

@Stypox Stypox left a comment

Choose a reason for hiding this comment

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

I made some small changes, in part described below, in the last commit, to fix some things and make further improvements. Thank you!

Comment on lines 65 to 66
.map(item -> item.localConfiguration)
.map(localConfiguration -> localConfiguration.tag)
Copy link
Member

Choose a reason for hiding this comment

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

map will crash if the return value of the lambda is null. I looked it up but there does not seem to be a mapNullable (just like ofNullable exists). So I used flatMap(... -> Optional.ofNullable(...))

jshell> Optional.of(new Object()).map(null)
|  Exception java.lang.NullPointerException
|        at Objects.requireNonNull (Objects.java:233)
|        at Optional.map (Optional.java:256)
|        at (#2:1)

jshell> Optional.of(new Object()).flatMap(o -> Optional.ofNullable(null))
$4 ==> Optional.empty

Copy link
Member Author

@Isira-Seneviratne Isira-Seneviratne Jan 11, 2023

Choose a reason for hiding this comment

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

null values returned in the lambda are converted to empty Optionals in the map method: https://developer.android.com/reference/java/util/Optional#map(java.util.function.Function%3C?%20super%20T,%20?%20extends%20U%3E)

You passed a null value for the lambda argument, not as the return type (the correct notation would be item -> null).

Copy link
Member

@Stypox Stypox Jan 11, 2023

Choose a reason for hiding this comment

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

You are right, I feel so stupid now... I will revert that part of my commit

Copy link
Member

Choose a reason for hiding this comment

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

I pushed ad605e2

Comment on lines 1893 to 1887
|| getRoot().map(View::getParent).orElse(null) == null) {
if (!isPlayerAndPlayerServiceAvailable() || player.UIs().get(MainPlayerUi.class).isEmpty()
|| getRoot().map(View::getParent).isEmpty()) {
Copy link
Member

Choose a reason for hiding this comment

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

Your code is perfectly equivalent, but I think the .map(View::getParent) was wrongly being used to allow getParent to return null.

@Stypox Stypox merged commit 8797669 into TeamNewPipe:dev Jan 11, 2023
@Isira-Seneviratne Isira-Seneviratne deleted the Optional_cleanup branch January 11, 2023 13:57
@sonarcloud
Copy link

sonarcloud bot commented Jan 11, 2023

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 5 Code Smells

0.0% 0.0% Coverage
0.0% 0.0% Duplication

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
codequality Improvements to the codebase to improve the code quality
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants