Moved PlaylistUtils to default implementations in IPlaylist.

This commit is contained in:
neviyn 2016-03-25 03:52:15 +00:00
parent 4ffca843b4
commit 43b7585cd1
4 changed files with 86 additions and 65 deletions

View File

@ -9,7 +9,6 @@ import musicplayer.player.IPlayer;
import musicplayer.playlist.IPlaylist; import musicplayer.playlist.IPlaylist;
import musicplayer.util.ConfigManager; import musicplayer.util.ConfigManager;
import musicplayer.util.Icons; import musicplayer.util.Icons;
import musicplayer.util.PlaylistUtils;
import org.jnativehook.GlobalScreen; import org.jnativehook.GlobalScreen;
import org.jnativehook.NativeHookException; import org.jnativehook.NativeHookException;
import org.jnativehook.keyboard.NativeKeyEvent; import org.jnativehook.keyboard.NativeKeyEvent;
@ -263,7 +262,7 @@ public class PlayerGUI extends JPanel implements PlayerCallbackInterface {
if (!filename.endsWith(".m3u")) if (!filename.endsWith(".m3u"))
filename += ".m3u"; filename += ".m3u";
try { try {
PlaylistUtils.writePlaylistToFile(playlist.getSongList(), new File(filename)); IPlaylist.writePlaylistToFile(playlist.getSongList(), new File(filename));
} catch (IOException e1) { } catch (IOException e1) {
JOptionPane.showMessageDialog(null, e1.getMessage() + "\n" + Arrays.toString(e1.getStackTrace()), "Error", JOptionPane.ERROR_MESSAGE); JOptionPane.showMessageDialog(null, e1.getMessage() + "\n" + Arrays.toString(e1.getStackTrace()), "Error", JOptionPane.ERROR_MESSAGE);
} }
@ -278,7 +277,7 @@ public class PlayerGUI extends JPanel implements PlayerCallbackInterface {
fileChooser.setDialogType(JFileChooser.OPEN_DIALOG); fileChooser.setDialogType(JFileChooser.OPEN_DIALOG);
fileChooser.setFileFilter(m3uExtensionFilter); fileChooser.setFileFilter(m3uExtensionFilter);
if(fileChooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION){ if(fileChooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION){
PlaylistUtils.readPlaylistFromFile(fileChooser.getSelectedFile()).stream().forEach(playlist::addSong); IPlaylist.readPlaylistFromFile(fileChooser.getSelectedFile()).stream().forEach(playlist::addSong);
} }
}); });
playlistTools.add(menuItem); playlistTools.add(menuItem);

View File

@ -1,9 +1,18 @@
package musicplayer.playlist; package musicplayer.playlist;
import musicplayer.library.ILibrary;
import musicplayer.model.Album;
import musicplayer.model.Artist;
import musicplayer.model.Song; import musicplayer.model.Song;
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Optional; import java.util.Optional;
import java.util.stream.Collectors;
public interface IPlaylist { public interface IPlaylist {
void addSong(Song song); void addSong(Song song);
@ -22,4 +31,48 @@ public interface IPlaylist {
void setPlayingSong(Song song); void setPlayingSong(Song song);
void setStopped(); void setStopped();
/**
* Write a list of songs to a file as an m3u format playlist file.
* @param playlist Playlist of songs to write.
* @param targetFile File to write playlist into.
* @throws IOException
*/
static void writePlaylistToFile(List<Song> playlist, File targetFile) throws IOException {
if(!targetFile.exists())
targetFile.createNewFile();
try (BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(targetFile), "utf-8"))) {
writer.write("#EXTM3U\n");
for(Song song : playlist){
writer.write(song.getM3UFormatString());
}
}
}
/**
* Read songs in from an m3u playlist.
* Songs in the playlist with files that don't actually exists will be dropped.
* @param targetFile File to read m3u playlist data from.
* @return List of songs contained in the playlist.
*/
static List<Song> readPlaylistFromFile(File targetFile){
List<Song> result = new ArrayList<>();
if(targetFile.exists()){
try {
List<String> playlistData = Files.lines(targetFile.toPath()).filter(x -> !x.isEmpty() && !x.contains("#EXTINF")).collect(Collectors.toList());
if(playlistData.get(0).equals("#EXTM3U")){
playlistData.remove(0);
List<Path> songPaths = playlistData.stream().map(Paths::get).collect(Collectors.toList());
for(int i = 0; i < songPaths.size(); i++){
if(!Files.exists(songPaths.get(i))){ // If song file doesn't exists, try treating it as relative
songPaths.set(i, Paths.get(targetFile.getParent(), songPaths.get(i).toString()));
}
}
songPaths.stream().map(ILibrary::autoParse).filter(Optional::isPresent).map(Optional::get)
.forEach(x -> result.add(new Song(x.getTrackNumber(), x.getDiscNumber(), x.getTitle(), new Artist(x.getArtist()), new Album(x.getAlbum()), x.getGenre(), x.getSongFile())));
}
} catch (IOException ignored) { }
}
return result;
}
} }

View File

@ -1,62 +0,0 @@
package musicplayer.util;
import musicplayer.library.ILibrary;
import musicplayer.model.Album;
import musicplayer.model.Artist;
import musicplayer.model.Song;
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
public final class PlaylistUtils {
/**
* Write a list of songs to a file as an m3u format playlist file.
* @param playlist Playlist of songs to write.
* @param targetFile File to write playlist into.
* @throws IOException
*/
public static void writePlaylistToFile(List<Song> playlist, File targetFile) throws IOException {
if(!targetFile.exists())
targetFile.createNewFile();
try (BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(targetFile), "utf-8"))) {
writer.write("#EXTM3U\n");
for(Song song : playlist){
writer.write(song.getM3UFormatString());
}
}
}
/**
* Read songs in from an m3u playlist.
* Songs in the playlist with files that don't actually exists will be dropped.
* @param targetFile File to read m3u playlist data from.
* @return List of songs contained in the playlist.
*/
public static List<Song> readPlaylistFromFile(File targetFile){
List<Song> result = new ArrayList<>();
if(targetFile.exists()){
try {
List<String> playlistData = Files.lines(targetFile.toPath()).filter(x -> !x.isEmpty() && !x.contains("#EXTINF")).collect(Collectors.toList());
if(playlistData.get(0).equals("#EXTM3U")){
playlistData.remove(0);
List<Path> songPaths = playlistData.stream().map(Paths::get).collect(Collectors.toList());
for(int i = 0; i < songPaths.size(); i++){
if(!Files.exists(songPaths.get(i))){ // If song file doesn't exists, try treating it as relative
songPaths.set(i, Paths.get(targetFile.getParent(), songPaths.get(i).toString()));
}
}
songPaths.stream().map(ILibrary::autoParse).filter(Optional::isPresent).map(Optional::get)
.forEach(x -> result.add(new Song(x.getTrackNumber(), x.getDiscNumber(), x.getTitle(), new Artist(x.getArtist()), new Album(x.getAlbum()), x.getGenre(), x.getSongFile())));
}
} catch (IOException ignored) { }
}
return result;
}
}

View File

@ -0,0 +1,31 @@
package musicplayer.playlist;
import musicplayer.library.ILibrary;
import musicplayer.model.Song;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import static org.junit.Assert.*;
public class IPlaylistTest {
@Rule
public TemporaryFolder folder = new TemporaryFolder();
@Test
public void testWriteAndReadPlaylist() throws Exception {
Song sampleSong = new Song(ILibrary.autoParse(
new File(IPlaylistTest.class.getResource("/sample.mp3").getFile()).toPath()).get());
File playlistFile = folder.newFile();
List<Song> playlist = new ArrayList<>();
playlist.add(sampleSong);
IPlaylist.writePlaylistToFile(playlist, playlistFile);
List<Song> result = IPlaylist.readPlaylistFromFile(playlistFile);
assertEquals(sampleSong, result.get(0));
}
}