Skip to content

Commit

Permalink
Actually there is no need to use flatMap
Browse files Browse the repository at this point in the history
`null` values returned in the lambda are converted to empty `Optional`s in the `map` method: https://developer.android.com/reference/java/util/Optional#map(java.util.function.Function%3C?%20super%20T,%20?%20extends%20U%3E)
  • Loading branch information
Stypox committed Jan 11, 2023
1 parent eed44b3 commit ad605e2
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 4 deletions.
3 changes: 2 additions & 1 deletion app/src/main/java/org/schabi/newpipe/RouterActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@
import org.schabi.newpipe.views.FocusOverlayView;

import java.io.Serializable;
import java.lang.ref.Reference;
import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.Arrays;
Expand Down Expand Up @@ -748,7 +749,7 @@ public void onDestroy() {
*/
private Optional<AppCompatActivity> getActivityContext() {
return Optional.ofNullable(weakContext)
.flatMap(context -> Optional.ofNullable(context.get()))
.map(Reference::get)
.filter(context -> !context.isFinishing());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1887,7 +1887,7 @@ public void onFullscreenStateChanged(final boolean fullscreen) {
setupBrightness();
if (!isPlayerAndPlayerServiceAvailable()
|| player.UIs().get(MainPlayerUi.class).isEmpty()
|| getRoot().flatMap(v -> Optional.ofNullable(v.getParent())).isEmpty()) {
|| getRoot().map(View::getParent).isEmpty()) {
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ default Optional<Quality> getMaybeQuality() {
@NonNull
static Optional<MediaItemTag> from(@Nullable final MediaItem mediaItem) {
return Optional.ofNullable(mediaItem)
.flatMap(item -> Optional.ofNullable(item.localConfiguration))
.flatMap(localConfiguration -> Optional.ofNullable(localConfiguration.tag))
.map(item -> item.localConfiguration)
.map(localConfiguration -> localConfiguration.tag)
.filter(MediaItemTag.class::isInstance)
.map(MediaItemTag.class::cast);
}
Expand Down

0 comments on commit ad605e2

Please sign in to comment.