Altered ExtractedMetadata to avoid extra allocations.

This commit is contained in:
neviyn 2016-03-08 19:21:44 +00:00
parent eb02ca7665
commit 1eceab8227
4 changed files with 37 additions and 34 deletions

View File

@ -503,15 +503,16 @@ public class PlayerGUI implements PlayerCallbackInterface, LibraryCallbackInterf
} }
private class GlobalKeyboardShortcuts extends GlobalKeyAdapter { private class GlobalKeyboardShortcuts extends GlobalKeyAdapter {
final int modifierKey = 93; final int modifierKey = 164;
final int playPause = 116; final int playPause = 192;
final int stop = 117; final int stop = 222;
final int previous = 118; final int previous = 219;
final int next = 119; final int next = 221;
boolean modified = false; boolean modified = false;
@Override @Override
public void keyPressed(GlobalKeyEvent event) { public void keyPressed(GlobalKeyEvent event) {
System.out.println(event.getVirtualKeyCode());
if(event.getVirtualKeyCode() == modifierKey) if(event.getVirtualKeyCode() == modifierKey)
modified = true; modified = true;
else if(modified){ else if(modified){

View File

@ -48,7 +48,7 @@ public class Gateway {
public static Optional<Song> getOneSong(ExtractedMetadata metadata){ public static Optional<Song> getOneSong(ExtractedMetadata metadata){
try (Session session = DatabaseManager.getInstance().getSession()) { try (Session session = DatabaseManager.getInstance().getSession()) {
Song song = (Song)session.createCriteria(Song.class) Song song = (Song)session.createCriteria(Song.class)
.add(Restrictions.eq("songFile", metadata.songFile)).uniqueResult(); .add(Restrictions.eq("songFile", metadata.getSongFile())).uniqueResult();
return (song == null) ? Optional.empty() : Optional.of(song); return (song == null) ? Optional.empty() : Optional.of(song);
} }
} }
@ -94,23 +94,23 @@ public class Gateway {
public static void addSong(ExtractedMetadata metadata) { public static void addSong(ExtractedMetadata metadata) {
try (Session session = DatabaseManager.getInstance().getSession()) { try (Session session = DatabaseManager.getInstance().getSession()) {
session.beginTransaction(); session.beginTransaction();
Optional<Album> albumObj = getOneAlbum(metadata.album); Optional<Album> albumObj = getOneAlbum(metadata.getAlbum());
if (!albumObj.isPresent()) { if (!albumObj.isPresent()) {
Album album = new Album(metadata.album, AlbumArtExtractor.getImageData(new File(metadata.songFile))); Album album = new Album(metadata.getAlbum(), AlbumArtExtractor.getImageData(new File(metadata.getSongFile())));
albumObj = Optional.of(album); albumObj = Optional.of(album);
} }
Optional<Artist> artistObj = getOneArtist(metadata.artist); Optional<Artist> artistObj = getOneArtist(metadata.getArtist());
if (!artistObj.isPresent()) { if (!artistObj.isPresent()) {
Artist artist = new Artist(metadata.artist); Artist artist = new Artist(metadata.getArtist());
artistObj = Optional.of(artist); artistObj = Optional.of(artist);
} }
Optional<Song> songObj = getOneSong(metadata); Optional<Song> songObj = getOneSong(metadata);
if(!songObj.isPresent()) if(!songObj.isPresent())
session.save(new Song(metadata.trackNumber, metadata.discNumber, metadata.title, artistObj.get(), session.save(new Song(metadata.getTrackNumber(), metadata.getDiscNumber(), metadata.getTitle(), artistObj.get(),
albumObj.get(), metadata.genre, metadata.songFile)); albumObj.get(), metadata.getGenre(), metadata.getSongFile()));
else else
songObj.get().updateData(metadata.trackNumber, metadata.discNumber, metadata.title, artistObj.get(), songObj.get().updateData(metadata.getTrackNumber(), metadata.getDiscNumber(), metadata.getTitle(), artistObj.get(),
albumObj.get(), metadata.genre, metadata.songFile); albumObj.get(), metadata.getGenre(), metadata.getSongFile());
session.getTransaction().commit(); session.getTransaction().commit();
} }
} }

View File

@ -18,30 +18,32 @@ import java.util.Optional;
* Internal class representing metadata extracted from a music file. * Internal class representing metadata extracted from a music file.
*/ */
public class ExtractedMetadata { public class ExtractedMetadata {
public final String title; private final Tag audioTags;
public final String album; private String songFile;
public final String artist;
public final String genre;
public final String songFile;
public final String trackNumber;
public final String discNumber;
/** /**
* @param audioTags jaudiotagger tag data. * @param audioTags jaudiotagger tag data.
* @param songFile Location of the song file on the filesystem. * @param songFile Location of the song file on the filesystem.
*/ */
public ExtractedMetadata(Tag audioTags, File songFile) { public ExtractedMetadata(Tag audioTags, File songFile) {
this.trackNumber = audioTags.getFirst(FieldKey.TRACK); this.audioTags = audioTags;
this.title = audioTags.getFirst(FieldKey.TITLE);
this.album = audioTags.getFirst(FieldKey.ALBUM);
this.artist = audioTags.getFirst(FieldKey.ARTIST);
this.genre = audioTags.getFirst(FieldKey.GENRE);
this.discNumber = audioTags.getFirst(FieldKey.DISC_NO);
this.songFile = songFile.getAbsolutePath(); this.songFile = songFile.getAbsolutePath();
} }
public String getTitle(){ return audioTags.getFirst(FieldKey.TITLE); }
public String getArtist(){ return audioTags.getFirst(FieldKey.ARTIST); }
public String getAlbum(){ return audioTags.getFirst(FieldKey.ALBUM); }
public String getTrackNumber(){ return audioTags.getFirst(FieldKey.TRACK); }
public String getGenre(){ return audioTags.getFirst(FieldKey.GENRE); }
public String getDiscNumber(){ return audioTags.getFirst(FieldKey.DISC_NO); }
public String getSongFile(){ return songFile; }
@Override @Override
public String toString() { public String toString() { return getTitle()+ " - " + getArtist(); }
return title + " - " + artist;
}
} }

View File

@ -153,11 +153,11 @@ public class GatewayTest {
ExtractedMetadata metadata = new ExtractedMetadata(tags, new File("")); ExtractedMetadata metadata = new ExtractedMetadata(tags, new File(""));
Gateway.addSong(metadata); Gateway.addSong(metadata);
Song song = (Song)session.createCriteria(Song.class).uniqueResult(); Song song = (Song)session.createCriteria(Song.class).uniqueResult();
assertEquals(song.getAlbum().getName(), metadata.album); assertEquals(song.getAlbum().getName(), metadata.getAlbum());
assertEquals(song.getArtist().getName(), metadata.artist); assertEquals(song.getArtist().getName(), metadata.getArtist());
assertEquals(song.getTrackNumber(), Integer.valueOf(1)); assertEquals(song.getTrackNumber(), Integer.valueOf(1));
assertEquals(song.getTitle(), metadata.title); assertEquals(song.getTitle(), metadata.getTitle());
assertEquals(song.getGenre(), metadata.genre); assertEquals(song.getGenre(), metadata.getGenre());
assertEquals(song.getDiscNumber(), Integer.valueOf(100)); assertEquals(song.getDiscNumber(), Integer.valueOf(100));
} }
} }