1 /*
2  * aTunes
3  * Copyright (C) Alex Aranda, Sylvain Gaudard and contributors
4  *
5  * See http://www.atunes.org/wiki/index.php?title=Contributing for information about contributors
6  *
7  * http://www.atunes.org
8  * http://sourceforge.net/projects/atunes
9  *
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License
12  * as published by the Free Software Foundation; either version 2
13  * of the License, or (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  */
20 
21 package net.sourceforge.atunes.kernel.modules.tags;
22 
23 import java.util.List;
24 
25 import net.sourceforge.atunes.model.ILocalAudioObject;
26 import net.sourceforge.atunes.model.IStateRepository;
27 import net.sourceforge.atunes.model.ITagAdapter;
28 import net.sourceforge.atunes.utils.StringUtils;
29 
30 /**
31  * Selects tag adapter for a specific file type
32  *
33  * @author alex
34  *
35  */
36 public class TagAdapterSelector {
37 
38 	private IStateRepository stateRepository;
39 
40 	private List<ITagAdapter> tagAdapters;
41 
42 	private ITagAdapter propertiesFileTagAdapter;
43 
44 	/**
45 	 * @param propertiesFileTagAdapter
46 	 */
setPropertiesFileTagAdapter( final ITagAdapter propertiesFileTagAdapter)47 	public void setPropertiesFileTagAdapter(
48 			final ITagAdapter propertiesFileTagAdapter) {
49 		this.propertiesFileTagAdapter = propertiesFileTagAdapter;
50 	}
51 
52 	/**
53 	 * @param stateRepository
54 	 */
setStateRepository(final IStateRepository stateRepository)55 	public void setStateRepository(final IStateRepository stateRepository) {
56 		this.stateRepository = stateRepository;
57 	}
58 
59 	/**
60 	 * @param tagAdapters
61 	 */
setTagAdapters(final List<ITagAdapter> tagAdapters)62 	public void setTagAdapters(final List<ITagAdapter> tagAdapters) {
63 		this.tagAdapters = tagAdapters;
64 	}
65 
66 	/**
67 	 * Selects adapter for given local audio object
68 	 *
69 	 * @param audioObject
70 	 * @return
71 	 */
selectAdapter(final ILocalAudioObject audioObject)72 	ITagAdapter selectAdapter(final ILocalAudioObject audioObject) {
73 		for (ITagAdapter tagAdapter : this.tagAdapters) {
74 			if (tagAdapter.isFormatSupported(audioObject)) {
75 				return tagAdapter;
76 			}
77 		}
78 		throw new IllegalStateException(StringUtils.getString(
79 				"No tag adapter for audio object: ", audioObject.getUrl()));
80 	}
81 
82 	/**
83 	 * Selects adapter for rating read or write
84 	 *
85 	 * @param audioObject
86 	 * @return
87 	 */
selectAdapterForRating(final ILocalAudioObject audioObject)88 	ITagAdapter selectAdapterForRating(final ILocalAudioObject audioObject) {
89 		for (ITagAdapter tagAdapter : this.tagAdapters) {
90 			if (tagAdapter.isFormatSupported(audioObject)
91 					&& isAdapterSuitableForRatingRead(tagAdapter)) {
92 				return tagAdapter;
93 			}
94 		}
95 		// Use properties file adapter as default
96 		// This will write rating in properties if format does not support
97 		// internal rating
98 		// and option to use internal rating is enabled
99 		return this.propertiesFileTagAdapter;
100 	}
101 
102 	/**
103 	 * @param adapter
104 	 * @return true if adapter supports rating read with current settings
105 	 */
isAdapterSuitableForRatingRead(final ITagAdapter adapter)106 	boolean isAdapterSuitableForRatingRead(final ITagAdapter adapter) {
107 		return adapter.isStoreRatingInFile() == this.stateRepository
108 				.isStoreRatingInFile();
109 	}
110 }
111