Implement algorithm to merge playlists
This commit is contained in:
parent
c34549a47d
commit
270a541a7c
3 changed files with 57 additions and 7 deletions
|
@ -11,18 +11,62 @@ import java.util.List;
|
||||||
public interface PlaylistLocalItem extends LocalItem {
|
public interface PlaylistLocalItem extends LocalItem {
|
||||||
String getOrderingName();
|
String getOrderingName();
|
||||||
|
|
||||||
|
long getDisplayIndex();
|
||||||
|
|
||||||
static List<PlaylistLocalItem> merge(
|
static List<PlaylistLocalItem> merge(
|
||||||
final List<PlaylistMetadataEntry> localPlaylists,
|
final List<PlaylistMetadataEntry> localPlaylists,
|
||||||
final List<PlaylistRemoteEntity> remotePlaylists) {
|
final List<PlaylistRemoteEntity> remotePlaylists) {
|
||||||
// todo: merge algorithm
|
|
||||||
final List<PlaylistLocalItem> items = new ArrayList<>(
|
// Merge localPlaylists and remotePlaylists by displayIndex.
|
||||||
|
// If two items have the same displayIndex, sort them in CASE_INSENSITIVE_ORDER.
|
||||||
|
// This algorithm is similar to the merge operation in merge sort.
|
||||||
|
|
||||||
|
final List<PlaylistLocalItem> result = new ArrayList<>(
|
||||||
localPlaylists.size() + remotePlaylists.size());
|
localPlaylists.size() + remotePlaylists.size());
|
||||||
items.addAll(localPlaylists);
|
final List<PlaylistLocalItem> itemsWithSameIndex = new ArrayList<>();
|
||||||
items.addAll(remotePlaylists);
|
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++;
|
||||||
|
}
|
||||||
|
addItemsWithSameIndex(result, itemsWithSameIndex);
|
||||||
|
|
||||||
Collections.sort(items, Comparator.comparing(PlaylistLocalItem::getOrderingName,
|
// If displayIndex does not match actual index, update displayIndex.
|
||||||
Comparator.nullsLast(String.CASE_INSENSITIVE_ORDER)));
|
// This may happen when a new list is created with default displayIndex = 0.
|
||||||
|
// todo: update displayIndex
|
||||||
|
|
||||||
return items;
|
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()) {
|
||||||
|
// The new item has a different displayIndex,
|
||||||
|
// add previous items with same index to the result.
|
||||||
|
addItemsWithSameIndex(result, itemsWithSameIndex);
|
||||||
|
itemsWithSameIndex.clear();
|
||||||
|
}
|
||||||
|
itemsWithSameIndex.add(item);
|
||||||
|
}
|
||||||
|
|
||||||
|
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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -39,4 +39,9 @@ public class PlaylistMetadataEntry implements PlaylistLocalItem {
|
||||||
public String getOrderingName() {
|
public String getOrderingName() {
|
||||||
return name;
|
return name;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public long getDisplayIndex() {
|
||||||
|
return displayIndex;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -153,6 +153,7 @@ public class PlaylistRemoteEntity implements PlaylistLocalItem {
|
||||||
this.uploader = uploader;
|
this.uploader = uploader;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
public long getDisplayIndex() {
|
public long getDisplayIndex() {
|
||||||
return displayIndex;
|
return displayIndex;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue