View Javadoc

1   package org.musicontroller.importer;
2   
3   import java.io.Serializable;
4   import java.util.Date;
5   import java.util.HashSet;
6   import java.util.Set;
7   
8   import org.varienaja.util.StringUtil;
9   
10  /**
11   * This class holds information about a music archive entry (usually a song), 
12   * including song title, band name, playlist name, length and tags. 
13   * <p>During the import process this bean also contains the location of the extracted
14   * MP3 file.
15   * 
16   * @author Hans Drexler
17   * @version $Id: MusicArchiveEntryBean.java,v 1.1 2010/03/16 18:55:43 varienaja Exp $
18   */
19  public class MusicArchiveEntryBean implements Serializable, Comparable<MusicArchiveEntryBean> {
20  
21  	private static final long serialVersionUID = 20071205181556L;;
22  
23  	/**
24  	 * Default constructor. Initializes the keyword set (empty).
25  	 * Sets the included indicator to true.
26  	 */
27  	public MusicArchiveEntryBean() {
28  		setKeywords(new HashSet<String>());
29  		setInclude(true);
30  	}
31  	
32  	/**
33  	 * The name of the plasylist (album) contained
34  	 * in this music archive.
35  	 */
36  	private String _playlistName;
37  	
38  	/**
39  	 * The band name of the band performing in
40  	 * this music archive.
41  	 */
42  	private String _bandName;
43  
44  	/**
45  	 * The name of the song.
46  	 */
47  	private String _songName;
48  	
49  	/**
50  	 * The length of the song in milliseconds.
51  	 */
52  	private int _songLength;
53  	
54  	/**
55  	 * The set of keywords associated with this song.
56  	 */
57  	private Set<String> _keywords;
58  
59  	/**
60  	 * The song index of this song in its playlist.
61  	 */
62  	private int _songIndex;
63  
64  	/**
65  	 * Indicates if this entry should be imported or not.
66  	 */
67  	private boolean _include;
68  
69  	/**
70  	 * This string is the location of the MP3 file in the <tt>inspected</tt> directory.
71  	 */
72  	private String _entryName;
73  	
74  	/**
75  	 * The releasedate of the playlist where this song is part of.
76  	 */
77  	private Date _releaseDate;
78  	
79  	/**
80  	 * Getter for the bandname.
81  	 * @return The bandname.
82  	 */
83  	public String getBandName() {
84  		return _bandName;
85  	}
86  
87  	/**
88  	 * Setter for the bandname.
89  	 * @param name The bandname.
90  	 */
91  	public void setBandName(String name) {
92  		_bandName = name;
93  	}
94  
95  	/**
96  	 * Getter for the playlist name.
97  	 * @return The playlist name.
98  	 */
99  	public String getPlaylistName() {
100 		return _playlistName;
101 	}
102 
103 	/**
104 	 * Setter for the playlist-name.
105 	 * @param name The new playlist name.
106 	 */
107 	public void setPlaylistName(String name) {
108 		_playlistName = name;
109 	}
110 
111 	/**
112 	 * Getter for the set of keywords.
113 	 * @return The keyword set. 
114 	 */
115 	public Set<String> getKeywords() {
116 		return _keywords;
117 	}
118 
119 	/**
120 	 * Setter for the keyword set.
121 	 * @param keywords The keyword set.
122 	 */
123 	public void setKeywords(Set<String> keywords) {
124 		this._keywords = keywords;
125 	}
126 
127 	/**
128 	 * Getter for the song length.
129 	 * @return The length of the song in milliseconds.
130 	 */
131 	public int getSongLength() {
132 		return _songLength;
133 	}
134 
135 	/**
136 	 * Setter for the song length.
137 	 * @param seconds The song length in milliseconds.
138 	 */
139 	public void setSongLength(int seconds) {
140 		_songLength = seconds;
141 	}
142 
143 	/**
144 	 * Getter for the name of the song.
145 	 * @return The song name.
146 	 */
147 	public String getSongName() {
148 		return _songName;
149 	}
150 
151 	/**
152 	 * Setter for the name of the song.
153 	 * @param name The song name.
154 	 */
155 	public void setSongName(String name) {
156 		_songName = name;
157 	}
158 
159 	/**
160 	 * Getter for the song index.
161 	 * @return The song index.
162 	 */
163 	public int getSongIndex() {
164 		return _songIndex;
165 	}
166 	
167 	/**
168 	 * Setter for the song index.
169 	 * @param index The index 
170 	 */
171 	public void setSongIndex(int index) {
172 		_songIndex = index;
173 	}
174 
175 	/**
176 	 * Setter for the included indicator.
177 	 * @param include The included indicator.
178 	 */
179 	public void setInclude(boolean include) {
180 		this._include = include;
181 	}
182 	
183 	/**
184 	 * Getter for the included indicator.
185 	 * @return The included indicator
186 	 */
187 	public boolean getInclude() {
188 		return this._include;
189 	}
190 
191 	/**
192 	 * Getter for the file name of the MP3 file in the <tt>inspected</tt> directrory. 
193 	 * @return The music entry file name.
194 	 */
195 	public String getEntryName() {
196 		return this._entryName;
197 	}
198 	
199 	/**
200 	 * Setter for the file name of the MP3 file in the <tt>inspected</tt> directrory.
201 	 * @param entryName The music entry file name.
202 	 */
203 	public void setEntryName(String entryName) {
204 		this._entryName = entryName;
205 	}
206 	
207 	/**
208 	 * Getter for the release date of the playlist.
209 	 * @return The release date.
210 	 */
211 	public Date getReleaseDate() {
212 		return _releaseDate;
213 	}
214 	
215 	public void setReleaseDate(Date releaseDate) {
216 		this._releaseDate = releaseDate;
217 	}
218 	
219 	/**
220 	 * Returns a String repesentation of the keywords in the
221 	 * bean as a comma-separated String of keywords.
222 	 * @return String representation of the Keywords
223 	 */
224 	public String getKeywordString() {
225 		StringBuilder result = new StringBuilder();
226 		boolean putComma = false;
227 		for(String kw: getKeywords()) {
228 			if(putComma) {
229 				result.append(",");
230 			} else {
231 				putComma = true;
232 			}
233 			result.append(kw);
234 		}
235 		return result.toString();
236 	}
237 	
238 	/**
239 	 * Splits the argument into comma-separated keywords and stores
240 	 * them in the keywords property of the bean. The first letter
241 	 * of each keyword will be capitalized. Empty keywords are
242 	 * ignored.
243 	 * @param keywords The comma-separated list of keywords.
244 	 */
245 	public void setKeywordString(String keywords) {
246 		getKeywords().clear();
247 		getKeywords().addAll(StringUtil.getIndividualWords(keywords));
248 	}
249 
250 	/**
251 	 * Two MusicArchiveEntryBean objects are the same if they have the same song index greater than 0.
252 	 * If the song index on both objects is zero, then they are equal if they have the same band name
253 	 * and song name.
254 	 */
255 	public boolean equals(Object o) {
256 		if (o==null) return false;
257 		
258 		if (o instanceof MusicArchiveEntryBean) {
259 			MusicArchiveEntryBean b= (MusicArchiveEntryBean) o;
260 			if(getSongIndex()==0) {
261 				StringBuilder sb1 = new StringBuilder();
262 				StringBuilder sb2 = new StringBuilder();
263 				sb1.append(getBandName());
264 				sb1.append(getSongName());
265 				sb2.append(b.getBandName());
266 				sb2.append(b.getSongName());
267 				return b.getSongIndex()==0 && sb1.toString().equals(sb2.toString());
268 			} else {
269 				return getSongIndex() == b.getSongIndex();
270 			}
271 		} else {
272 			return false;
273 		}
274 
275 	}
276 	
277 	/**
278 	 * Returns a hash code based on the song index. If the song index is zero,
279 	 * the hash code is computed from the band name and song name.
280 	 */
281 	public int hashCode() {
282 		if (getSongIndex()==0) {
283 			StringBuilder sb = new StringBuilder();
284 			sb.append(getBandName());
285 			sb.append(getSongName());
286 			return sb.toString().hashCode();
287 		} else {
288 			return getSongIndex();
289 		}
290 	}
291 	
292 	/**
293 	 * Define a natural ordening on MusicArchiveEntryBeans based on track number.
294 	 * If both track numbers are 0 then the Band name decides. If Band names are
295 	 * equal then song name decides.
296 	 */
297 	public int compareTo(MusicArchiveEntryBean o) {
298 		if(getSongIndex()==0) {
299 			int bandCompare = 0;
300 			if(getBandName()==null) {
301 				if(o.getBandName()==null) {
302 					// Go on to compare song names.
303 				} else {
304 					bandCompare = -1;
305 				}
306 			} else {
307 				if(o.getBandName()==null) {
308 					bandCompare = 1;
309 				} else {
310 					bandCompare = getBandName().compareTo(o.getBandName());
311 				}
312 			}
313 			if(bandCompare!=0) {
314 				return bandCompare;
315 			}
316 			// Band names are equal. Compare song names.
317 			int songCompare = 0;
318 			if(getSongName()==null) {
319 				if(o.getSongName()==null) {
320 					// Treat both song names null as being equal.
321 				} else {
322 					songCompare = -1;
323 				}
324 			} else {
325 				if(o.getSongName()==null) {
326 					songCompare = 1;
327 				} else {
328 					songCompare = getSongName().compareTo(o.getSongName());
329 				}
330 			}
331 			return songCompare;
332 		} else {
333 			return Integer.valueOf(getSongIndex()).compareTo(Integer.valueOf(o.getSongIndex()));
334 		}
335 	}
336 }