View Javadoc

1   package org.musicontroller.songselection;
2   
3   import java.util.ArrayList;
4   import java.util.Iterator;
5   import java.util.List;
6   
7   /**
8    * The LastPlayedContainer keeps a List of song-IDs that have been played and/or
9    * skipped in recent history. These song-ID's are used by the 
10   * SongSelector-interface to match to-be-played songs with the ones that have
11   * been previously played.
12   * @author Varienaja
13   */
14  public class LastPlayedContainer implements Cloneable {
15  	private int _maxHistory=8;
16  	
17  	/**
18  	 * The last songs played or skipped.
19  	 */
20  	private List<LastPlayedEntry> _lastsongs;
21  	
22  	public LastPlayedContainer() {
23  		_lastsongs = new ArrayList<LastPlayedEntry>(_maxHistory);
24  	}
25  	
26  	/**
27  	 * Add the played song to a list of recently played songs. Because this
28  	 * song could be in the list very long (The user might log out and come
29  	 * back later) we store the song-id and reload the song object when needed.
30  	 * This approach prevents Hibernate closed session exceptions.
31  	 * @param songid The id of the Song to add to the list.
32  	 */
33  	public void addToLastPlayed(long songid) {
34  		addToLastPlayed(songid, true);
35  	}
36  
37  	/**
38  	 * Add the played or skipped song to a list of recently played songs.
39  	 * @see org.musicontroller.songselection.LastPlayedContainer#addToLastPlayed(long)
40  	 * @param songid The id of the Song to add to the list.
41  	 * @param entirely whether or not the Song was played entirely..
42  	 */
43  	public void addToLastPlayed(long songid, boolean entirely) {
44  		if (_lastsongs.size()>_maxHistory+1) {
45  			_lastsongs.remove(0);
46  		}
47  		_lastsongs.add(new LastPlayedEntry(songid, entirely));
48  	}
49  	
50  	public int getSize() {
51  		return _lastsongs.size();
52  	}
53  	
54  	public Iterator<LastPlayedEntry> getIterator() {
55  		return _lastsongs.iterator();
56  	}
57  	
58  	/*
59  	 * (non-Javadoc)
60  	 * @see java.lang.Object#clone()
61  	 */
62  	@Override
63  	public LastPlayedContainer clone() throws CloneNotSupportedException {
64  		LastPlayedContainer clone = new LastPlayedContainer();
65  		
66  		for (LastPlayedEntry e : _lastsongs) {
67  			clone.addToLastPlayed(e.getSongid(), e.isPlayedEntirely());
68  		}
69  		
70  		return clone;
71  	}
72  	
73  	/*
74  	 * (non-Javadoc)
75  	 * @see java.lang.Object#toString()
76  	 */
77  	public String toString() {
78  		return _lastsongs.toString();
79  	}
80  
81  	/**
82  	 * Marks the last played Song in the list as 'skipped'.
83  	 */
84  	public void markLastSongAsSkipped() {
85  		int lastIndex = _lastsongs.size()-1;
86  		_lastsongs.get(lastIndex).markSkipped();
87  	}
88  }