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 net.sourceforge.atunes.utils.Logger;
24 import net.sourceforge.atunes.utils.StringUtils;
25 
26 /**
27  * Converts from ID3 ratings to stars
28  *
29  * @author alex
30  *
31  */
32 public class RatingsToStars {
33 
34 	private int minRatingValue;
35 
36 	private int maxRatingValue;
37 
38 	private int numberOfStars;
39 
40 	/**
41 	 * @param numberOfStars
42 	 */
setNumberOfStars(final int numberOfStars)43 	public void setNumberOfStars(final int numberOfStars) {
44 		this.numberOfStars = numberOfStars;
45 	}
46 
47 	/**
48 	 * @param minRatingValue
49 	 */
setMinRatingValue(final int minRatingValue)50 	public void setMinRatingValue(final int minRatingValue) {
51 		this.minRatingValue = minRatingValue;
52 	}
53 
54 	/**
55 	 * @param maxRatingValue
56 	 */
setMaxRatingValue(final int maxRatingValue)57 	public void setMaxRatingValue(final int maxRatingValue) {
58 		this.maxRatingValue = maxRatingValue;
59 	}
60 
61 	/**
62 	 * @param rating
63 	 * @return stars
64 	 */
ratingToStars(final String rating)65 	int ratingToStars(final String rating) {
66 		if (!StringUtils.isEmpty(rating)) {
67 			try {
68 				Integer stars = Integer.parseInt(rating);
69 				if (stars >= this.minRatingValue
70 						&& stars <= this.maxRatingValue) {
71 					return stars / (this.maxRatingValue / this.numberOfStars);
72 				}
73 			} catch (NumberFormatException e) {
74 				Logger.error("Wrong rating: ", rating);
75 			}
76 		}
77 		return this.minRatingValue;
78 	}
79 
80 	/**
81 	 * @param value
82 	 * @return rating string to store in tag
83 	 */
starsToRating(final Integer value)84 	public String starsToRating(final Integer value) {
85 		return Integer.toString(value
86 				* (this.maxRatingValue / this.numberOfStars));
87 	}
88 
89 }
90