Skip to content

Commit

Permalink
Use Collection.removeIf() instead of using Iterator.remove() to remov…
Browse files Browse the repository at this point in the history
…e elements conditionally.
  • Loading branch information
Isira-Seneviratne committed Nov 1, 2020
1 parent abcacf8 commit b0b0a75
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@
import org.schabi.newpipe.util.ThemeHelper;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.TimeUnit;

Expand Down Expand Up @@ -495,13 +494,12 @@ public void handleResult(@NonNull final ChannelInfo result) {

// handling ContentNotSupportedException not to show the error but an appropriate string
// so that crashes won't be sent uselessly and the user will understand what happened
for (Iterator<Throwable> it = errors.iterator(); it.hasNext();) {
final Throwable throwable = it.next();
errors.removeIf(throwable -> {
if (throwable instanceof ContentNotSupportedException) {
showContentNotSupported();
it.remove();
}
}
return throwable instanceof ContentNotSupportedException;
});

if (!errors.isEmpty()) {
showSnackBarError(errors, UserAction.REQUESTED_CHANNEL,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Queue;
Expand Down Expand Up @@ -758,16 +757,9 @@ private void initSuggestionObserver() {
}

// Remove duplicates
final Iterator<SuggestionItem> iterator = networkResult.iterator();
while (iterator.hasNext() && localResult.size() > 0) {
final SuggestionItem next = iterator.next();
for (final SuggestionItem item : localResult) {
if (item.query.equals(next.query)) {
iterator.remove();
break;
}
}
}
networkResult.removeIf(networkItem ->
localResult.stream().anyMatch(localItem ->
localItem.query.equals(networkItem.query)));

if (networkResult.size() > 0) {
result.addAll(networkResult);
Expand Down
10 changes: 3 additions & 7 deletions app/src/main/java/us/shandian/giga/service/DownloadManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;

import us.shandian.giga.get.DownloadMission;
import us.shandian.giga.get.FinishedMission;
Expand Down Expand Up @@ -564,14 +564,10 @@ private ArrayList<Object> getSpecialItems() {
synchronized (DownloadManager.this) {
ArrayList<Mission> pending = new ArrayList<>(mMissionsPending);
ArrayList<Mission> finished = new ArrayList<>(mMissionsFinished);
ArrayList<Mission> remove = new ArrayList<>(hidden);
List<Mission> remove = new ArrayList<>(hidden);

// hide missions (if required)
Iterator<Mission> iterator = remove.iterator();
while (iterator.hasNext()) {
Mission mission = iterator.next();
if (pending.remove(mission) || finished.remove(mission)) iterator.remove();
}
remove.removeIf(mission -> pending.remove(mission) || finished.remove(mission));

int fakeTotal = pending.size();
if (fakeTotal > 0) fakeTotal++;
Expand Down

0 comments on commit b0b0a75

Please sign in to comment.