View Javadoc

1   // %3904049365:hoplugins.teamAnalyzer.util%
2   package hoplugins.teamAnalyzer.util;
3   
4   import hoplugins.teamAnalyzer.manager.NameManager;
5   
6   import java.util.StringTokenizer;
7   
8   
9   /***
10   * Team Player's name utility
11   *
12   * @author <a href=mailto:draghetto@users.sourceforge.net>Massimiliano Amato</a>
13   */
14  public class NameUtil {
15      //~ Methods ------------------------------------------------------------------------------------
16  
17      /***
18       * Returns only the first name initial(s) for a player name, separated by "."
19       *
20       * @param name The player fullname
21       *
22       * @return The first name initial(s)
23       */
24      public static String getInitials(String name) {
25          String firstName = getFirstName(name);
26          StringTokenizer st = new StringTokenizer(firstName, " ");
27          String initials = "";
28  
29          while (st.hasMoreTokens()) {
30              initials = initials + st.nextToken().charAt(0) + ".";
31          }
32  
33          return initials.trim();
34      }
35  
36      /***
37       * Returns the player last name
38       *
39       * @param name The player fullname
40       *
41       * @return the lastname
42       */
43      public static String getLastName(String name) {
44          String lastName = NameManager.getLastName(name);
45  
46          return lastName.trim();
47      }
48  
49      /***
50       * Returns the player description
51       *
52       * @param name player fullname
53       *
54       * @return
55       */
56      public static String getPlayerDesc(String name) {
57          return getPlayerName(name, true);
58      }
59  
60      /***
61       * Returns the player first name
62       *
63       * @param name The player fullname
64       *
65       * @return the firstname
66       */
67      private static String getFirstName(String name) {
68          String lastName = NameManager.getLastName(name);
69          int index = name.indexOf(lastName);
70  
71          if (index > 0) {
72              return name.substring(0, index);
73          }
74  
75          return "";
76      }
77  
78      /***
79       * Returns the Player name formatted as specified
80       *
81       * @param name the player full name
82       * @param detail true if you want initials, false if full
83       *
84       * @return the formatted player name
85       */
86      private static String getPlayerName(String name, boolean detail) {
87          String playerName = "";
88  
89          if (name.length() > 22) {
90              StringTokenizer st = new StringTokenizer(name, "/");
91  
92              while (st.hasMoreTokens()) {
93                  String nameStr = st.nextToken();
94  
95                  if (detail) {
96                      playerName = playerName + getInitials(nameStr) + " ";
97                  }
98  
99                  playerName = playerName + getLastName(nameStr) + "/";
100             }
101 
102             playerName = playerName.substring(0, playerName.length() - 1);
103         } else {
104             playerName = name;
105         }
106 
107         if ((playerName.length() > 22) && detail) {
108             playerName = getPlayerName(playerName, false);
109         }
110 
111         return playerName;
112     }
113 }