/** * Change the item. * * @param old DOCUMENT_ME * @param sNewName DOCUMENT_ME * @return new album * @throws JajukException the jajuk exception */ public Album changeAlbumName(Album old, String sNewName) throws JajukException { // check there is actually a change if (old.getName2().equals(sNewName)) { return old; } // check up front as later the state of the track is already changed boolean bQueueUpdateRequired = false; if (QueueModel.getPlayingFile() != null && QueueModel.getPlayingFile().getTrack().getAlbum().equals(old)) { bQueueUpdateRequired = true; } Album newItem = registerAlbum(sNewName, old.getDiscID()); // re apply old properties from old item newItem.cloneProperties(old); // update tracks for (Track track : TrackManager.getInstance().getTracks()) { if (track.getAlbum().equals(old)) { TrackManager.getInstance().changeTrackAlbum(track, sNewName, null); } } // if current track album name is changed, notify it if (bQueueUpdateRequired) { ObservationManager.notify(new JajukEvent(JajukEvents.ALBUM_CHANGED)); } // remove old item removeItem(old); return newItem; }
/** * Return ordered rarely listen albums list. * * @param bHideUnmounted if true, unmounted albums are not chosen * @param iNb nb of items to return * @return top albums, can be less items than required according to nb of available albums */ public List<Album> getRarelyListenAlbums(boolean bHideUnmounted, int iNb) { // create a temporary table to remove unmounted albums // We consider an album as mounted if a least one track is mounted // This hashmap contains album-> album hits (each track hit average) final Map<Album, Float> cache = new HashMap<Album, Float>(AlbumManager.getInstance().getElementCount()); // This hashmap contains album-> nb of tracks already taken into account // for average Map<Album, Integer> cacheNb = new HashMap<Album, Integer>(AlbumManager.getInstance().getElementCount()); ReadOnlyIterator<Track> it = TrackManager.getInstance().getTracksIterator(); while (it.hasNext()) { Track track = it.next(); if (track.getPlayeableFile(bHideUnmounted) != null) { float newHits = 0f; Integer nb = cacheNb.get(track.getAlbum()); if (nb == null) { nb = 0; } Float previousRate = cache.get(track.getAlbum()); if (previousRate == null) { newHits = track.getHits(); } else { newHits = ((previousRate * nb) + track.getHits()) / (nb + 1); } cacheNb.put(track.getAlbum(), nb + 1); cache.put(track.getAlbum(), newHits); } } // Now sort albums by rating List<Album> sortedAlbums = new ArrayList<Album>(cache.keySet()); Collections.sort( sortedAlbums, new Comparator<Album>() { public int compare(Album o1, Album o2) { // We inverse comparison as we want lowest scores return (int) (cache.get(o2) - cache.get(o1)); } }); return getTopAlbums(sortedAlbums, iNb); }
/** * Return top albums based on the average of each album rating * * @param bHideUnmounted if true, unmounted albums are not chosen * @param iNbBestofAlbums nb of items to return * @return top albums, can be less items than required according to nb of available albums */ public List<Album> getBestOfAlbums(boolean bHideUnmounted, int iNbBestofAlbums) { // create a temporary table to remove unmounted albums // We consider an album as mounted if a least one track is mounted // This hashmap contains album-> album rates final HashMap<Album, Float> cacheRate = new HashMap<Album, Float>(AlbumManager.getInstance().getElementCount()); // This hashmap contains album-> nb of tracks already taken into account // for average HashMap<Album, Integer> cacheNb = new HashMap<Album, Integer>(AlbumManager.getInstance().getElementCount()); for (Track track : TrackManager.getInstance().getTracks()) { if (track.getPlayeableFile(bHideUnmounted) != null) { float newRate = 0f; Integer nb = cacheNb.get(track.getAlbum()); if (nb == null) { nb = 0; } Float previousRate = cacheRate.get(track.getAlbum()); if (previousRate == null) { newRate = track.getRate(); } else { newRate = ((previousRate * nb) + track.getRate()) / (nb + 1); } cacheNb.put(track.getAlbum(), nb + 1); cacheRate.put(track.getAlbum(), newRate); } } // Now sort albums by rating ArrayList<Album> sortedAlbums = new ArrayList<Album>(cacheRate.keySet()); Collections.sort( sortedAlbums, new Comparator<Album>() { public int compare(Album o1, Album o2) { // lowest first return (int) (cacheRate.get(o1) - cacheRate.get(o2)); } }); return getTopAlbums(sortedAlbums, iNbBestofAlbums); }
/** * Force to refresh the album max rating, it is not done soon as it is pretty CPU consumming and * we don't need a track by track rating precision */ public void refreshMaxRating() { // create a temporary table to remove unmounted albums // We consider an album as mounted if a least one track is mounted // This hashmap contains album-> album rates final HashMap<Album, Float> cacheRate = new HashMap<Album, Float>(AlbumManager.getInstance().getElementCount()); // This hashmap contains album-> nb of tracks already taken into account // for average HashMap<Album, Integer> cacheNb = new HashMap<Album, Integer>(AlbumManager.getInstance().getElementCount()); for (Track track : TrackManager.getInstance().getTracks()) { if (track.getPlayeableFile(ConfigurationManager.getBoolean(CONF_OPTIONS_HIDE_UNMOUNTED)) != null) { float newRate = 0f; Integer nb = cacheNb.get(track.getAlbum()); if (nb == null) { nb = 0; } Float previousRate = cacheRate.get(track.getAlbum()); if (previousRate == null) { newRate = track.getRate(); } else { newRate = ((previousRate * nb) + track.getRate()) / (nb + 1); } cacheNb.put(track.getAlbum(), nb + 1); cacheRate.put(track.getAlbum(), newRate); } } // OK, now keep only the highest score for (Album album : cacheRate.keySet()) { long value = Math.round(cacheRate.get(album)); if (value > maxRate) { maxRate = value; } } }
/** * Get albums associated with this item * * @param item * @return */ public Set<Album> getAssociatedAlbums(Item item) { synchronized (AlbumManager.getInstance().getLock()) { Set<Album> out = new TreeSet<Album>(); // If item is a track, return albums containing this track if (item instanceof Track) { // we can return as a track has only one album if (item != null) { out.add(((Track) item).getAlbum()); } } else { Set<Track> tracks = TrackManager.getInstance().getAssociatedTracks(item); for (Track track : tracks) { out.add(track.getAlbum()); } } return out; } }
/** * Change the item * * @param old * @param sNewName * @return new album */ public Album changeAlbumName(Album old, String sNewName) throws JajukException { // check there is actually a change if (old.getName2().equals(sNewName)) { return old; } Album newItem = registerAlbum(sNewName); // re apply old properties from old item newItem.cloneProperties(old); // update tracks for (Track track : TrackManager.getInstance().getTracks()) { if (track.getAlbum().equals(old)) { TrackManager.getInstance().changeTrackAlbum(track, sNewName, null); } } // if current track album name is changed, notify it if (FIFO.getInstance().getCurrentFile() != null && FIFO.getInstance().getCurrentFile().getTrack().getAlbum().equals(old)) { ObservationManager.notify(new Event(EventSubject.EVENT_ALBUM_CHANGED)); } return newItem; }
/** * Return newest albums * * @param bHideUnmounted if true, unmounted albums are not chosen * @param iNb nb of items to return * @return newest albums */ public List<Album> getNewestAlbums(boolean bHideUnmounted, int iNb) { // create a temporary table to remove unmounted albums // We consider an album as mounted if a least one track is mounted // This hashmap contains album-> discovery date final HashMap<Album, Date> cache = new HashMap<Album, Date>(AlbumManager.getInstance().getElementCount()); for (Track track : TrackManager.getInstance().getTracks()) { if (track.getPlayeableFile(bHideUnmounted) != null) { cache.put(track.getAlbum(), track.getAdditionDate()); } } // Now sort albums by discovery date ArrayList<Album> sortedAlbums = new ArrayList<Album>(cache.keySet()); Collections.sort( sortedAlbums, new Comparator<Album>() { public int compare(Album o1, Album o2) { return cache.get(o1).compareTo(cache.get(o2)); } }); return getTopAlbums(sortedAlbums, iNb); }