Update test and Javadoc

This commit is contained in:
GGAutomaton 2022-05-11 18:08:14 +08:00
parent d32490a4be
commit ba394a7ab4
2 changed files with 56 additions and 12 deletions

View file

@ -13,13 +13,34 @@ public interface PlaylistLocalItem extends LocalItem {
long getDisplayIndex(); long getDisplayIndex();
/**
* 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
*/
static List<PlaylistLocalItem> merge( static List<PlaylistLocalItem> merge(
final List<PlaylistMetadataEntry> localPlaylists, final List<PlaylistMetadataEntry> localPlaylists,
final List<PlaylistRemoteEntity> remotePlaylists) { final List<PlaylistRemoteEntity> remotePlaylists) {
// The playlists from the database must be in the display index order.
// Merge localPlaylists and remotePlaylists by display index. for (int i = 1; i < localPlaylists.size(); i++) {
// If two items have the same display index, sort them in CASE_INSENSITIVE_ORDER. 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");
}
}
// This algorithm is similar to the merge operation in merge sort. // This algorithm is similar to the merge operation in merge sort.
final List<PlaylistLocalItem> result = new ArrayList<>( final List<PlaylistLocalItem> result = new ArrayList<>(

View file

@ -24,16 +24,26 @@ public class PlaylistLocalItemTest {
public void onlyLocalPlaylists() { public void onlyLocalPlaylists() {
final List<PlaylistMetadataEntry> localPlaylists = new ArrayList<>(); final List<PlaylistMetadataEntry> localPlaylists = new ArrayList<>();
final List<PlaylistRemoteEntity> remotePlaylists = new ArrayList<>(); final List<PlaylistRemoteEntity> remotePlaylists = new ArrayList<>();
localPlaylists.add(new PlaylistMetadataEntry(1, "name1", "", 2, 1)); localPlaylists.add(new PlaylistMetadataEntry(1, "name1", "", 0, 1));
localPlaylists.add(new PlaylistMetadataEntry(2, "name2", "", 1, 1)); localPlaylists.add(new PlaylistMetadataEntry(2, "name2", "", 1, 1));
localPlaylists.add(new PlaylistMetadataEntry(3, "name3", "", 0, 1)); localPlaylists.add(new PlaylistMetadataEntry(3, "name3", "", 3, 1));
final List<PlaylistLocalItem> mergedPlaylists = final List<PlaylistLocalItem> mergedPlaylists =
PlaylistLocalItem.merge(localPlaylists, remotePlaylists); PlaylistLocalItem.merge(localPlaylists, remotePlaylists);
assertEquals(3, mergedPlaylists.size()); assertEquals(3, mergedPlaylists.size());
assertEquals(0, mergedPlaylists.get(0).getDisplayIndex()); assertEquals(0, mergedPlaylists.get(0).getDisplayIndex());
assertEquals(1, mergedPlaylists.get(1).getDisplayIndex()); assertEquals(1, mergedPlaylists.get(1).getDisplayIndex());
assertEquals(2, mergedPlaylists.get(2).getDisplayIndex()); assertEquals(3, mergedPlaylists.get(2).getDisplayIndex());
}
@Test(expected = IllegalArgumentException.class)
public void invalidLocalPlaylists() {
final List<PlaylistMetadataEntry> localPlaylists = new ArrayList<>();
final List<PlaylistRemoteEntity> remotePlaylists = new ArrayList<>();
localPlaylists.add(new PlaylistMetadataEntry(1, "name1", "", 2, 1));
localPlaylists.add(new PlaylistMetadataEntry(2, "name2", "", 1, 1));
localPlaylists.add(new PlaylistMetadataEntry(3, "name3", "", 0, 1));
PlaylistLocalItem.merge(localPlaylists, remotePlaylists);
} }
@Test @Test
@ -41,18 +51,31 @@ public class PlaylistLocalItemTest {
final List<PlaylistMetadataEntry> localPlaylists = new ArrayList<>(); final List<PlaylistMetadataEntry> localPlaylists = new ArrayList<>();
final List<PlaylistRemoteEntity> remotePlaylists = new ArrayList<>(); final List<PlaylistRemoteEntity> remotePlaylists = new ArrayList<>();
remotePlaylists.add(new PlaylistRemoteEntity( remotePlaylists.add(new PlaylistRemoteEntity(
1, "name1", "url1", "", "", 2, 1L)); 1, "name1", "url1", "", "", 1, 1L));
remotePlaylists.add(new PlaylistRemoteEntity( remotePlaylists.add(new PlaylistRemoteEntity(
2, "name2", "url2", "", "", 1, 1L)); 2, "name2", "url2", "", "", 2, 1L));
remotePlaylists.add(new PlaylistRemoteEntity( remotePlaylists.add(new PlaylistRemoteEntity(
3, "name3", "url3", "", "", 0, 1L)); 3, "name3", "url3", "", "", 4, 1L));
final List<PlaylistLocalItem> mergedPlaylists = final List<PlaylistLocalItem> mergedPlaylists =
PlaylistLocalItem.merge(localPlaylists, remotePlaylists); PlaylistLocalItem.merge(localPlaylists, remotePlaylists);
assertEquals(3, mergedPlaylists.size()); assertEquals(3, mergedPlaylists.size());
assertEquals(0, mergedPlaylists.get(0).getDisplayIndex()); assertEquals(1, mergedPlaylists.get(0).getDisplayIndex());
assertEquals(1, mergedPlaylists.get(1).getDisplayIndex()); assertEquals(2, mergedPlaylists.get(1).getDisplayIndex());
assertEquals(2, mergedPlaylists.get(2).getDisplayIndex()); assertEquals(4, mergedPlaylists.get(2).getDisplayIndex());
}
@Test(expected = IllegalArgumentException.class)
public void invalidRemotePlaylists() {
final List<PlaylistMetadataEntry> localPlaylists = new ArrayList<>();
final List<PlaylistRemoteEntity> remotePlaylists = new ArrayList<>();
remotePlaylists.add(new PlaylistRemoteEntity(
1, "name1", "url1", "", "", 1, 1L));
remotePlaylists.add(new PlaylistRemoteEntity(
2, "name2", "url2", "", "", 3, 1L));
remotePlaylists.add(new PlaylistRemoteEntity(
3, "name3", "url3", "", "", 0, 1L));
PlaylistLocalItem.merge(localPlaylists, remotePlaylists);
} }
@Test @Test