View Javadoc

1   /*
2   @See License.txt@
3    */
4   
5   package spellcast.util;
6   
7   /***
8    * Miscellaneous text utilities for Spellcast strings.
9    *
10   * @author Barrie Treloar
11   */
12  public abstract class TextUtil 
13  {
14      private TextUtil()
15      {
16      }
17  
18      /***
19       * From a line from Profiles.txt return the Gender string.
20       * 
21       * @param line a lone of the format <gender><space><wizard name>
22       * @return the gender or null if the line is malformed.
23       */
24      public static String getGender( String line )
25      {
26          String result = null;
27          
28          if ( line == null )
29          {
30              return result;
31          }
32          
33          int spaceIndex = line.indexOf( " " );
34          if ( spaceIndex != -1 )
35          {
36              result = line.substring( 0, spaceIndex );
37          }
38          
39          return result;
40      }
41  
42      /***
43       * From a line from Profiles.txt return the Wizard Name.
44       * 
45       * @param line a lone of the format <gender><space><wizard name>
46       * @return the wizard name or null if the line is malformed.
47       */
48      public static String getWizardName( String line )
49      {
50          String result = null;
51          
52          if ( line == null )
53          {
54              return result;
55          }
56          
57          int spaceIndex = line.indexOf( " " );
58          if ( spaceIndex != -1 && line.length() > spaceIndex + 1 )
59          {
60              result = line.substring( spaceIndex + 1, line.length() );
61          }
62          
63          return result;
64      }
65  
66      /***
67       * From a line from GameNames.txt return the numberOfUses or -1 if the line
68       * is malformed.
69       *
70       * @param line a line of the format <number><space><game name>
71       * @return the <number> from the line or -1 if the line is malformed.
72       */
73      public static int getNumberOfUses( String line )
74      {
75          int result = -1;
76          
77          if ( line == null )
78          {
79              return result;
80          }
81  
82          int spaceIndex = line.indexOf( " " );
83          if ( spaceIndex != -1 )
84          {
85              try
86              {
87                  result = Integer.parseInt( line.substring( 0, spaceIndex ) );
88                  // Zero or Negative numbers are not allowed
89                  if ( result <= 0 )
90                  {
91                      result = -1;
92                  }
93              }
94              catch ( NumberFormatException e )
95              {
96                  // ignore this as we already return -1 by default
97              }
98          }
99          
100         return result;
101     }
102 
103     /***
104      * From a line from GameNames.txt return the game name or null if the line
105      * is malformed.
106      *
107      * @param line a line of the format <number><space><game name>
108      * @return the <game name> from the line or null if the line is malformed.
109      */
110     public static String getGameName( String line )
111     {
112         String result = null;
113         
114         if ( line == null )
115         {
116             return result;
117         }
118         
119         int spaceIndex = line.indexOf( " " );
120         if ( spaceIndex != -1 && line.length() > spaceIndex + 1 )
121         {
122             result = line.substring( spaceIndex + 1, line.length() );
123         }
124         
125         return result;
126     }
127 
128     /***
129      * From a string of the form <host>[:<port>] return the host.
130      *
131      * @param serverAddress a string of the form <host>[:<port>]
132      * @return the host part from the serverAddress string
133      */
134     public static String getIPAddress( String serverAddressString )
135     {
136         String result = null;
137         
138         if ( serverAddressString != null )
139         {
140             String hostString = null;
141             int indexOfColon = serverAddressString.indexOf( ":" );
142             if ( indexOfColon != -1 )
143             {
144                 result = serverAddressString.substring( 0, indexOfColon );
145             }
146             else
147             {
148                 result = serverAddressString;
149             }
150         }
151 
152         return result;
153     }
154     
155     /***
156      * From a string of the form <host>[:<port>] return the port.
157      * If there is no port or an error during conversion then -1 is returned.
158      *
159      * @param serverAddressString a string of the form <host>[:<port>]
160      * @return the port of the serverAddressString or -1 if not present, or malformed
161      */
162     public static int getPort( String serverAddressString )
163     {
164         int result = -1;
165 
166         if ( serverAddressString == null )
167         {
168             return result;
169         }
170         
171         String portString = null;
172         int indexOfColon = serverAddressString.indexOf( ":" );
173         if ( indexOfColon != -1 && serverAddressString.length() > indexOfColon + 1 )
174         {
175             portString = serverAddressString.substring( indexOfColon + 1 );
176             try
177             {
178                 result = Integer.parseInt( portString );
179             }
180             catch ( NumberFormatException e )
181             {
182                 // Ignore this as we return -1 by default.
183             }
184         }
185 
186         return result;
187     }
188     
189 }
190