1 package org.musicontroller.songselection;
2
3 import java.util.ArrayList;
4 import java.util.HashSet;
5 import java.util.List;
6 import java.util.Set;
7
8 import org.apache.log4j.Logger;
9 import org.musicontroller.core.Song;
10 import org.musicontroller.dao.Dao;
11 import org.varienaja.util.DenseSet;
12 import org.varienaja.util.RandomNumberGenerator;
13
14
15
16
17
18
19
20 public class RandomCandidateSelector implements CandidateSelector {
21 private static final Logger LOG = Logger.getLogger(RandomCandidateSelector.class);
22
23
24
25
26 private int _candidatecount;
27
28
29
30
31 private Dao _dao;
32
33
34
35
36 private DenseSet _songIDs;
37
38
39
40
41 private boolean _playRequestInOrder;
42
43
44
45
46 public RandomCandidateSelector() {
47 _playRequestInOrder = false;
48 _candidatecount = 20;
49 }
50
51
52
53
54
55 public void setDao(Dao dao) {
56 _dao = dao;
57 _songIDs = new DenseSet();
58 initializeSongSet();
59 }
60
61 public Dao getDao() {
62 return _dao;
63 }
64
65
66
67
68
69 public void playRequestsInOrder() {
70 this._playRequestInOrder = true;
71 }
72
73
74
75
76
77 public void playRequestsAtRandom() {
78 this._playRequestInOrder = false;
79 }
80
81
82
83
84
85 public List<Song> selectCandidates(List<Long> requests) {
86 List<Song> candidates = new ArrayList<Song>();
87
88
89 if (requests!=null && requests.size()!=0) {
90
91
92 if (requestsPlayedInOrder()) {
93 long nextRequest = requests.remove(0);
94 Song song = getDao().getSongById(nextRequest);
95 if (song!=null) candidates.add(song);
96 } else {
97
98
99 candidates.addAll(getDao().getSongsById(requests));
100 }
101 return candidates;
102 }
103
104 candidates.addAll(selectCandidates(_candidatecount-candidates.size()));
105 return candidates;
106 }
107
108
109
110
111
112 public List<Song> selectCandidates(int count) {
113
114
115
116 if(_songIDs.size()<1) {
117 initializeSongSet();
118 }
119
120
121 Set<Long> ids = new HashSet<Long>();
122 if (_songIDs.size()>count) {
123 while (ids.size() != count) {
124 int index = RandomNumberGenerator.nextInt(_songIDs.size());
125 ids.add(_songIDs.get(index));
126 }
127 } else {
128 for (int i=0; i<_songIDs.size(); i++) {
129 ids.add(_songIDs.get(i));
130 }
131 }
132 return getDao().getSongsById(ids);
133 }
134
135
136
137
138
139
140 public void removeFromCandidates(Song song) {
141 if(song==null) {
142 return;
143 }
144 _songIDs.remove(song.getId());
145 }
146
147
148
149
150
151 private void initializeSongSet() {
152 LOG.debug("Reading songids from database...");
153 for (Long l:_dao.getSongIds()) {
154 _songIDs.add(l);
155 }
156 LOG.debug("Done reading songids, "+_songIDs.size()+" songs added.");
157 }
158
159
160
161
162
163 public boolean requestsPlayedAtRandom() {
164 return !_playRequestInOrder;
165 }
166
167
168
169
170
171 public boolean requestsPlayedInOrder() {
172 return _playRequestInOrder;
173 }
174
175
176
177
178
179 public int getCandidateCount() {
180 return _candidatecount;
181 }
182
183
184
185
186
187 public void setCandidateCount(int candidatecount) {
188 _candidatecount = candidatecount;
189 }
190
191
192
193
194
195 public void addToCandidates(Song song) {
196 _songIDs.add(song.getId());
197 }
198
199 }