Skip to content

Commit ba394a7

Browse files
committed
Update test and Javadoc
1 parent d32490a commit ba394a7

2 files changed

Lines changed: 56 additions & 12 deletions

File tree

app/src/main/java/org/schabi/newpipe/database/playlist/PlaylistLocalItem.java

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,34 @@ public interface PlaylistLocalItem extends LocalItem {
1313

1414
long getDisplayIndex();
1515

16+
/**
17+
* Merge localPlaylists and remotePlaylists by the display index.
18+
* If two items have the same display index, sort them in {@code CASE_INSENSITIVE_ORDER}.
19+
*
20+
* @param localPlaylists local playlists in the display index order
21+
* @param remotePlaylists remote playlists in the display index order
22+
* @return merged playlists
23+
*/
1624
static List<PlaylistLocalItem> merge(
1725
final List<PlaylistMetadataEntry> localPlaylists,
1826
final List<PlaylistRemoteEntity> remotePlaylists) {
19-
// The playlists from the database must be in the display index order.
2027

21-
// Merge localPlaylists and remotePlaylists by display index.
22-
// If two items have the same display index, sort them in CASE_INSENSITIVE_ORDER.
28+
for (int i = 1; i < localPlaylists.size(); i++) {
29+
if (localPlaylists.get(i).getDisplayIndex()
30+
< localPlaylists.get(i - 1).getDisplayIndex()) {
31+
throw new IllegalArgumentException(
32+
"localPlaylists is not in the display index order");
33+
}
34+
}
35+
36+
for (int i = 1; i < remotePlaylists.size(); i++) {
37+
if (remotePlaylists.get(i).getDisplayIndex()
38+
< remotePlaylists.get(i - 1).getDisplayIndex()) {
39+
throw new IllegalArgumentException(
40+
"remotePlaylists is not in the display index order");
41+
}
42+
}
43+
2344
// This algorithm is similar to the merge operation in merge sort.
2445

2546
final List<PlaylistLocalItem> result = new ArrayList<>(

app/src/test/java/org/schabi/newpipe/database/playlist/PlaylistLocalItemTest.java

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,35 +24,58 @@ public void emptyPlaylists() {
2424
public void onlyLocalPlaylists() {
2525
final List<PlaylistMetadataEntry> localPlaylists = new ArrayList<>();
2626
final List<PlaylistRemoteEntity> remotePlaylists = new ArrayList<>();
27-
localPlaylists.add(new PlaylistMetadataEntry(1, "name1", "", 2, 1));
27+
localPlaylists.add(new PlaylistMetadataEntry(1, "name1", "", 0, 1));
2828
localPlaylists.add(new PlaylistMetadataEntry(2, "name2", "", 1, 1));
29-
localPlaylists.add(new PlaylistMetadataEntry(3, "name3", "", 0, 1));
29+
localPlaylists.add(new PlaylistMetadataEntry(3, "name3", "", 3, 1));
3030
final List<PlaylistLocalItem> mergedPlaylists =
3131
PlaylistLocalItem.merge(localPlaylists, remotePlaylists);
3232

3333
assertEquals(3, mergedPlaylists.size());
3434
assertEquals(0, mergedPlaylists.get(0).getDisplayIndex());
3535
assertEquals(1, mergedPlaylists.get(1).getDisplayIndex());
36-
assertEquals(2, mergedPlaylists.get(2).getDisplayIndex());
36+
assertEquals(3, mergedPlaylists.get(2).getDisplayIndex());
37+
}
38+
39+
@Test(expected = IllegalArgumentException.class)
40+
public void invalidLocalPlaylists() {
41+
final List<PlaylistMetadataEntry> localPlaylists = new ArrayList<>();
42+
final List<PlaylistRemoteEntity> remotePlaylists = new ArrayList<>();
43+
localPlaylists.add(new PlaylistMetadataEntry(1, "name1", "", 2, 1));
44+
localPlaylists.add(new PlaylistMetadataEntry(2, "name2", "", 1, 1));
45+
localPlaylists.add(new PlaylistMetadataEntry(3, "name3", "", 0, 1));
46+
PlaylistLocalItem.merge(localPlaylists, remotePlaylists);
3747
}
3848

3949
@Test
4050
public void onlyRemotePlaylists() {
4151
final List<PlaylistMetadataEntry> localPlaylists = new ArrayList<>();
4252
final List<PlaylistRemoteEntity> remotePlaylists = new ArrayList<>();
4353
remotePlaylists.add(new PlaylistRemoteEntity(
44-
1, "name1", "url1", "", "", 2, 1L));
54+
1, "name1", "url1", "", "", 1, 1L));
4555
remotePlaylists.add(new PlaylistRemoteEntity(
46-
2, "name2", "url2", "", "", 1, 1L));
56+
2, "name2", "url2", "", "", 2, 1L));
4757
remotePlaylists.add(new PlaylistRemoteEntity(
48-
3, "name3", "url3", "", "", 0, 1L));
58+
3, "name3", "url3", "", "", 4, 1L));
4959
final List<PlaylistLocalItem> mergedPlaylists =
5060
PlaylistLocalItem.merge(localPlaylists, remotePlaylists);
5161

5262
assertEquals(3, mergedPlaylists.size());
53-
assertEquals(0, mergedPlaylists.get(0).getDisplayIndex());
54-
assertEquals(1, mergedPlaylists.get(1).getDisplayIndex());
55-
assertEquals(2, mergedPlaylists.get(2).getDisplayIndex());
63+
assertEquals(1, mergedPlaylists.get(0).getDisplayIndex());
64+
assertEquals(2, mergedPlaylists.get(1).getDisplayIndex());
65+
assertEquals(4, mergedPlaylists.get(2).getDisplayIndex());
66+
}
67+
68+
@Test(expected = IllegalArgumentException.class)
69+
public void invalidRemotePlaylists() {
70+
final List<PlaylistMetadataEntry> localPlaylists = new ArrayList<>();
71+
final List<PlaylistRemoteEntity> remotePlaylists = new ArrayList<>();
72+
remotePlaylists.add(new PlaylistRemoteEntity(
73+
1, "name1", "url1", "", "", 1, 1L));
74+
remotePlaylists.add(new PlaylistRemoteEntity(
75+
2, "name2", "url2", "", "", 3, 1L));
76+
remotePlaylists.add(new PlaylistRemoteEntity(
77+
3, "name3", "url3", "", "", 0, 1L));
78+
PlaylistLocalItem.merge(localPlaylists, remotePlaylists);
5679
}
5780

5881
@Test

0 commit comments

Comments
 (0)