Skip to content

Commit

Permalink
listPlaylistSongs() throws an IndexOutOfBoundsException if a track is (
Browse files Browse the repository at this point in the history
…#155)

not found.
This commit handles does cases.
  • Loading branch information
rain0r authored Mar 3, 2024
1 parent a964df1 commit 743ed68
Showing 1 changed file with 24 additions and 5 deletions.
29 changes: 24 additions & 5 deletions src/main/java/org/bff/javampd/playlist/MPDPlaylistDatabase.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,37 @@
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Optional;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import org.bff.javampd.command.CommandExecutor;
import org.bff.javampd.database.DatabaseProperties;
import org.bff.javampd.database.MusicDatabase;
import org.bff.javampd.database.TagLister;
import org.bff.javampd.server.MPD;
import org.bff.javampd.song.MPDSong;
import org.bff.javampd.song.SongConverter;
import org.bff.javampd.song.SongDatabase;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* MPDPlaylistDatabase represents a playlist database to a {@link org.bff.javampd.server.MPD}. To
* obtain an instance of the class you must use the {@link
* org.bff.javampd.database.MusicDatabase#getPlaylistDatabase()} method from the {@link
* org.bff.javampd.server.MPD} connection class.
* MPDPlaylistDatabase represents a playlist database to a {@link MPD}. To obtain an instance of the
* class you must use the {@link MusicDatabase#getPlaylistDatabase()} method from the {@link MPD}
* connection class.
*
* @author Bill
*/
public class MPDPlaylistDatabase implements PlaylistDatabase {

private final SongDatabase songDatabase;
private final CommandExecutor commandExecutor;
private final DatabaseProperties databaseProperties;
private final TagLister tagLister;
private final SongConverter songConverter;

private static final Logger LOGGER = LoggerFactory.getLogger(MPDPlaylistDatabase.class);

@Inject
public MPDPlaylistDatabase(
SongDatabase songDatabase,
Expand Down Expand Up @@ -69,7 +76,19 @@ public Collection<MPDSong> listPlaylistSongs(String playlistName) {

List<MPDSong> songList =
songConverter.getSongFileNameList(response).stream()
.map(song -> new ArrayList<>(songDatabase.searchFileName(song)).get(0))
.map(
song -> {
Optional<MPDSong> mpdSong = Optional.empty();
try {
mpdSong =
Optional.of(new ArrayList<>(songDatabase.searchFileName(song)).get(0));
} catch (IndexOutOfBoundsException e) {
LOGGER.error("Could not find file: {}", song);
}
return mpdSong;
})
.filter(Optional::isPresent)
.map(Optional::get)
.collect(Collectors.toList());

// Handle web radio streams
Expand Down

0 comments on commit 743ed68

Please sign in to comment.