Use Comparator's comparing(), nullsLast() and reversed() methods.

This commit is contained in:
Isira Seneviratne 2020-06-25 17:07:13 +05:30
parent 290428b981
commit abcacf8c74
7 changed files with 23 additions and 22 deletions

View File

@ -19,6 +19,7 @@ import org.schabi.newpipe.util.ShareUtils;
import java.io.Serializable; import java.io.Serializable;
import java.util.Arrays; import java.util.Arrays;
import java.util.Comparator;
/** /**
* Fragment containing the software licenses. * Fragment containing the software licenses.
@ -64,7 +65,7 @@ public class LicenseFragment extends Fragment {
} }
} }
// Sort components by name // Sort components by name
Arrays.sort(softwareComponents, (o1, o2) -> o1.getName().compareTo(o2.getName())); Arrays.sort(softwareComponents, Comparator.comparing(SoftwareComponent::getName));
} }
@Nullable @Nullable

View File

@ -5,6 +5,7 @@ import org.schabi.newpipe.database.playlist.model.PlaylistRemoteEntity;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.Comparator;
import java.util.List; import java.util.List;
public interface PlaylistLocalItem extends LocalItem { public interface PlaylistLocalItem extends LocalItem {
@ -18,15 +19,8 @@ public interface PlaylistLocalItem extends LocalItem {
items.addAll(localPlaylists); items.addAll(localPlaylists);
items.addAll(remotePlaylists); items.addAll(remotePlaylists);
Collections.sort(items, (left, right) -> { Collections.sort(items, Comparator.comparing(PlaylistLocalItem::getOrderingName,
final String on1 = left.getOrderingName(); Comparator.nullsLast(String.CASE_INSENSITIVE_ORDER)));
final String on2 = right.getOrderingName();
if (on1 == null) {
return on2 == null ? 0 : 1;
} else {
return on2 == null ? -1 : on1.compareToIgnoreCase(on2);
}
});
return items; return items;
} }

View File

@ -43,6 +43,7 @@ import org.schabi.newpipe.util.ThemeHelper;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
import java.util.Comparator;
import java.util.List; import java.util.List;
import icepick.State; import icepick.State;
@ -68,18 +69,19 @@ public class StatisticsPlaylistFragment
private HistoryRecordManager recordManager; private HistoryRecordManager recordManager;
private List<StreamStatisticsEntry> processResult(final List<StreamStatisticsEntry> results) { private List<StreamStatisticsEntry> processResult(final List<StreamStatisticsEntry> results) {
final Comparator<StreamStatisticsEntry> comparator;
switch (sortMode) { switch (sortMode) {
case LAST_PLAYED: case LAST_PLAYED:
Collections.sort(results, (left, right) -> comparator = Comparator.comparing(StreamStatisticsEntry::getLatestAccessDate);
right.getLatestAccessDate().compareTo(left.getLatestAccessDate())); break;
return results;
case MOST_PLAYED: case MOST_PLAYED:
Collections.sort(results, (left, right) -> comparator = Comparator.comparingLong(StreamStatisticsEntry::getWatchCount);
Long.compare(right.getWatchCount(), left.getWatchCount())); break;
return results;
default: default:
return null; return null;
} }
Collections.sort(results, comparator.reversed());
return results;
} }
/////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////

View File

@ -18,6 +18,7 @@ import org.schabi.newpipe.extractor.stream.VideoStream;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
@ -265,10 +266,8 @@ public final class ListHelper {
*/ */
private static void sortStreamList(final List<VideoStream> videoStreams, private static void sortStreamList(final List<VideoStream> videoStreams,
final boolean ascendingOrder) { final boolean ascendingOrder) {
Collections.sort(videoStreams, (o1, o2) -> { final Comparator<VideoStream> comparator = ListHelper::compareVideoStreamResolution;
final int result = compareVideoStreamResolution(o1, o2); Collections.sort(videoStreams, ascendingOrder ? comparator : comparator.reversed());
return result == 0 ? 0 : (ascendingOrder ? result : -result);
});
} }
/** /**

View File

@ -35,6 +35,10 @@ public abstract class Mission implements Serializable {
*/ */
public StoredFileHelper storage; public StoredFileHelper storage;
public long getTimestamp() {
return timestamp;
}
/** /**
* Delete the downloaded file * Delete the downloaded file
* *

View File

@ -12,6 +12,7 @@ import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator; import java.util.Iterator;
import us.shandian.giga.get.DownloadMission; import us.shandian.giga.get.DownloadMission;
@ -198,7 +199,7 @@ public class DownloadManager {
} }
if (mMissionsPending.size() > 1) if (mMissionsPending.size() > 1)
Collections.sort(mMissionsPending, (mission1, mission2) -> Long.compare(mission1.timestamp, mission2.timestamp)); Collections.sort(mMissionsPending, Comparator.comparingLong(Mission::getTimestamp));
} }
/** /**

View File

@ -13,7 +13,7 @@
<suppress checks="FinalParameters" <suppress checks="FinalParameters"
files="ListHelper.java" files="ListHelper.java"
lines="282,314"/> lines="281,313"/>
<!-- org.schabi.newpipe.streams --> <!-- org.schabi.newpipe.streams -->
<suppress checks="LineLength" <suppress checks="LineLength"