2018-02-06 06:32:23 +01:00
|
|
|
package org.schabi.newpipe.database.playlist;
|
|
|
|
|
|
|
|
import org.schabi.newpipe.database.LocalItem;
|
2020-07-04 11:31:24 +02:00
|
|
|
import org.schabi.newpipe.database.playlist.model.PlaylistRemoteEntity;
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.Collections;
|
2020-06-25 13:37:13 +02:00
|
|
|
import java.util.Comparator;
|
2020-07-04 11:31:24 +02:00
|
|
|
import java.util.List;
|
2018-02-06 06:32:23 +01:00
|
|
|
|
|
|
|
public interface PlaylistLocalItem extends LocalItem {
|
|
|
|
String getOrderingName();
|
2020-07-04 11:31:24 +02:00
|
|
|
|
2022-04-13 16:46:24 +02:00
|
|
|
long getDisplayIndex();
|
|
|
|
|
2022-05-11 12:08:14 +02:00
|
|
|
/**
|
|
|
|
* Merge localPlaylists and remotePlaylists by the display index.
|
|
|
|
* If two items have the same display index, sort them in {@code CASE_INSENSITIVE_ORDER}.
|
|
|
|
*
|
|
|
|
* @param localPlaylists local playlists in the display index order
|
|
|
|
* @param remotePlaylists remote playlists in the display index order
|
|
|
|
* @return merged playlists
|
|
|
|
*/
|
2020-07-04 11:31:24 +02:00
|
|
|
static List<PlaylistLocalItem> merge(
|
|
|
|
final List<PlaylistMetadataEntry> localPlaylists,
|
|
|
|
final List<PlaylistRemoteEntity> remotePlaylists) {
|
2022-04-13 16:46:24 +02:00
|
|
|
|
2022-05-11 12:08:14 +02:00
|
|
|
for (int i = 1; i < localPlaylists.size(); i++) {
|
|
|
|
if (localPlaylists.get(i).getDisplayIndex()
|
|
|
|
< localPlaylists.get(i - 1).getDisplayIndex()) {
|
|
|
|
throw new IllegalArgumentException(
|
|
|
|
"localPlaylists is not in the display index order");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 1; i < remotePlaylists.size(); i++) {
|
|
|
|
if (remotePlaylists.get(i).getDisplayIndex()
|
|
|
|
< remotePlaylists.get(i - 1).getDisplayIndex()) {
|
|
|
|
throw new IllegalArgumentException(
|
|
|
|
"remotePlaylists is not in the display index order");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-13 16:46:24 +02:00
|
|
|
// This algorithm is similar to the merge operation in merge sort.
|
|
|
|
|
|
|
|
final List<PlaylistLocalItem> result = new ArrayList<>(
|
2020-07-04 11:31:24 +02:00
|
|
|
localPlaylists.size() + remotePlaylists.size());
|
2022-04-13 16:46:24 +02:00
|
|
|
final List<PlaylistLocalItem> itemsWithSameIndex = new ArrayList<>();
|
2022-04-14 06:13:42 +02:00
|
|
|
|
2022-04-13 16:46:24 +02:00
|
|
|
int i = 0;
|
|
|
|
int j = 0;
|
|
|
|
while (i < localPlaylists.size()) {
|
|
|
|
while (j < remotePlaylists.size()) {
|
|
|
|
if (remotePlaylists.get(j).getDisplayIndex()
|
|
|
|
<= localPlaylists.get(i).getDisplayIndex()) {
|
|
|
|
addItem(result, remotePlaylists.get(j), itemsWithSameIndex);
|
|
|
|
j++;
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
addItem(result, localPlaylists.get(i), itemsWithSameIndex);
|
|
|
|
i++;
|
|
|
|
}
|
2022-04-15 14:44:54 +02:00
|
|
|
while (j < remotePlaylists.size()) {
|
|
|
|
addItem(result, remotePlaylists.get(j), itemsWithSameIndex);
|
|
|
|
j++;
|
|
|
|
}
|
2022-04-13 16:46:24 +02:00
|
|
|
addItemsWithSameIndex(result, itemsWithSameIndex);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void addItem(final List<PlaylistLocalItem> result, final PlaylistLocalItem item,
|
|
|
|
final List<PlaylistLocalItem> itemsWithSameIndex) {
|
|
|
|
if (!itemsWithSameIndex.isEmpty()
|
|
|
|
&& itemsWithSameIndex.get(0).getDisplayIndex() != item.getDisplayIndex()) {
|
2022-04-17 08:53:02 +02:00
|
|
|
// The new item has a different display index, add previous items with same
|
2022-04-14 06:13:42 +02:00
|
|
|
// index to the result.
|
2022-04-13 16:46:24 +02:00
|
|
|
addItemsWithSameIndex(result, itemsWithSameIndex);
|
|
|
|
itemsWithSameIndex.clear();
|
|
|
|
}
|
|
|
|
itemsWithSameIndex.add(item);
|
|
|
|
}
|
2020-07-04 11:31:24 +02:00
|
|
|
|
2022-04-13 16:46:24 +02:00
|
|
|
static void addItemsWithSameIndex(final List<PlaylistLocalItem> result,
|
|
|
|
final List<PlaylistLocalItem> itemsWithSameIndex) {
|
|
|
|
if (itemsWithSameIndex.size() > 1) {
|
|
|
|
Collections.sort(itemsWithSameIndex,
|
|
|
|
Comparator.comparing(PlaylistLocalItem::getOrderingName,
|
|
|
|
Comparator.nullsLast(String.CASE_INSENSITIVE_ORDER)));
|
|
|
|
}
|
|
|
|
result.addAll(itemsWithSameIndex);
|
2020-07-04 11:31:24 +02:00
|
|
|
}
|
2018-02-06 06:32:23 +01:00
|
|
|
}
|