View Javadoc

1   // %1126721046323:hoplugins.commons.utils%
2   package hoplugins.commons.utils;
3   
4   import plugins.IHOMiniModel;
5   
6   import java.util.List;
7   import java.util.StringTokenizer;
8   
9   /***
10   * Utility for Rating Formatting
11   *
12   * @author <a href=mailto:draghetto@users.sourceforge.net>Massimiliano Amato</a>
13   */
14  public final class RatingUtil {
15      /***
16       * Private default constuctor to prevent class instantiation.
17       */
18      private RatingUtil() {
19      }
20  
21      /***
22       * Utility Method that returns a string representation of the rating
23       *
24       * @param rating int value of the rating
25       * @param showNumber is the numberical representation being shown
26       * @param showText is the textual representation being shown
27       * @param model IHOMinimodel
28       *
29       * @return String with rating as configured
30       */
31      public static String getRating(int rating, boolean showNumber,
32          boolean showText, IHOMiniModel model) {
33          if (rating == 0) {
34              return "";
35          }
36  
37          final String value = model.getHelper().getNameForBewertung(rating,
38                  showNumber, true);
39          final StringTokenizer st = new StringTokenizer(value, "(");
40          String level = st.nextToken().trim();
41          StringTokenizer st2 = new StringTokenizer(st.nextToken(), ")");
42          final String subLevel = st2.nextToken();
43  
44          if (subLevel.indexOf(model.getResource().getProperty("veryhigh")) > -1) {
45              level = level + "++";
46          }
47          else if (subLevel.indexOf(model.getResource().getProperty("high")) > -1) {
48              level = level + "+";
49          }
50          else if (subLevel.indexOf(model.getResource().getProperty("verylow")) > -1) {
51              level = level + "--";
52          }
53          else if (subLevel.indexOf(model.getResource().getProperty("low")) > -1) {
54              level = level + "-";
55          }
56  
57          if (!showText) {
58              level = "";
59          }
60  
61          if (showNumber) {
62              st2 = new StringTokenizer(st.nextToken(), ")");
63  
64              final String number = st2.nextToken();
65  
66              if (level.length() > 0) {
67                  level = level + " (" + number + ")";
68              }
69              else {
70                  level = number;
71              }
72          }
73  
74          return level;
75      }
76  
77      /***
78       * Utility Method that returns a double representation of the rating
79       *
80       * @param desc String representation of the rating
81       * @param isNumeric Indicator if the string includes the numerical value
82       * @param isDescription Indicator if the string includes the description
83       * @param skills List of skills
84       *
85       * @return ouble with raing
86       */
87      public static double getRating(String desc, boolean isNumeric,
88          boolean isDescription, List skills) {
89          if (isNumeric && !isDescription) {
90              return Double.parseDouble(desc);
91          }
92  
93          if (isNumeric && isDescription) {
94              final StringTokenizer st = new StringTokenizer(desc, "(");
95  
96              st.nextToken();
97  
98              String s = st.nextToken();
99  
100             s = s.substring(0, s.length() - 1);
101 
102             return Double.parseDouble(s);
103         }
104 
105         desc.trim();
106 
107         double extra = 0.5;
108 
109         String valueStr = null;
110 
111         if (desc.indexOf("++") > -1) {
112             extra = 0.7;
113             valueStr = desc.substring(0, desc.indexOf("++"));
114         }
115         else if (desc.indexOf("+") > -1) {
116             extra = 0.6;
117             valueStr = desc.substring(0, desc.indexOf("+"));
118         }
119         else if (desc.indexOf("--") > -1) {
120             extra = 0.3;
121             valueStr = desc.substring(0, desc.indexOf("--"));
122         }
123         else if (desc.indexOf("-") > -1) {
124             extra = 0.4;
125             valueStr = desc.substring(0, desc.indexOf("-"));
126         }
127         else {
128             // Unable to determine value
129             return 0.0;
130         }
131 
132         final int value = skills.indexOf(valueStr);
133 
134         return extra + value;
135     }
136 }