001         package com.croftsoft.core.text;
002    
003         /*********************************************************************
004         * A collection of static methods to parse primitive types.
005         *
006        * @version
007         *   $Id: ParseLib.java,v 1.2 2006/10/01 07:36:51 croft Exp $
008         * @since
009         *   1999-08-15
010         * @author
011         *   <a href="https://www.croftsoft.com/">David Wallace Croft</a>
012         *********************************************************************/
013    
014         public final class  ParseLib
015         //////////////////////////////////////////////////////////////////////
016         //////////////////////////////////////////////////////////////////////
017         {
018    
019         public static void  main ( String [ ]  args )
020         //////////////////////////////////////////////////////////////////////
021         {
022           System.out.println ( parseDouble ( null    , 1968.0 ) );
023           System.out.println ( parseDouble ( "1968.0", 1974.0 ) );
024           System.out.println ( parseDouble ( "abcdef", 1968.0 ) );
025         }
026    
027         //////////////////////////////////////////////////////////////////////
028         //////////////////////////////////////////////////////////////////////
029    
030         /*********************************************************************
031         * Parses a boolean value from a String.
032         *
033         * <p>
034         * All leading and trailing whitespace is trimmed and all characters
035         * are converted to lower case before the comparison.
036         * </p>
037         *
038         * @param  backup
039         *
040         *   Default value to be returned is s is null or cannot be parsed.
041         *
042         * @return
043         *
044         *   Returns false if s is "0", "f", "false", "n", "no" , or "off".
045         *   Returns true  if s is "1", "t", "true" , "y", "yes", or "on" .
046         *   Otherwise returns backup value.
047         *********************************************************************/
048         public static boolean  parseBoolean (
049           String   s,
050           boolean  backup )
051         //////////////////////////////////////////////////////////////////////
052         {
053           if ( s == null )
054           {
055             return backup;
056           }
057    
058           s = s.trim ( ).toLowerCase ( );
059    
060           if ( s.equals ( "0"     )
061             || s.equals ( "f"     )
062             || s.equals ( "false" )
063             || s.equals ( "n"     )
064             || s.equals ( "no"    )
065             || s.equals ( "off"   ) )
066           {
067             return false;
068           }
069             
070           if ( s.equals ( "1"    )
071             || s.equals ( "t"    )
072             || s.equals ( "true" )
073             || s.equals ( "y"    )
074             || s.equals ( "yes"  )
075             || s.equals ( "on"   ) )
076           {
077             return true;
078           }
079    
080           return backup;
081         }
082    
083         /*********************************************************************
084         * Parses a double out of a String.
085         *
086         * @param  backup
087         *   Default value returned if unable to parse the String.
088         *********************************************************************/
089         public static double  parseDouble ( String  s, double  backup )
090         //////////////////////////////////////////////////////////////////////
091         {
092           if ( s == null )
093           {
094             return backup;
095           }
096    
097           try
098           {
099             return Double.valueOf ( s ).doubleValue ( );
100           }
101           catch ( NumberFormatException  ex )
102           {
103             return backup;
104           }
105         }
106    
107         /*********************************************************************
108         * Returns backup if s is null or a NumberFormatException occurs.
109         *********************************************************************/
110         public static int  parseInt (
111           String  s,
112           int     backup )
113         //////////////////////////////////////////////////////////////////////
114         {
115           if ( s == null )
116           {
117             return backup;
118           }
119           
120           s = s.trim ( );
121           
122           if ( "".equals ( s ) )
123           {
124             return backup;
125           }
126    
127           try
128           {
129             return Integer.parseInt ( s );
130           }
131           catch ( NumberFormatException  e )
132           {
133             return backup;
134           }
135         }
136    
137         /*********************************************************************
138         * Returns backup if s is null or a NumberFormatException occurs.
139         *********************************************************************/
140         public static long  parseLong (
141           String  s,
142           long    backup )
143         //////////////////////////////////////////////////////////////////////
144         {
145           if ( s == null )
146           {
147             return backup;
148           }
149    
150           try
151           {
152             return Long.parseLong ( s );
153           }
154           catch ( NumberFormatException  e )
155           {
156             return backup;
157           }
158         }
159    
160         /*********************************************************************
161         * Strips non-number characters out of the String.
162         *
163         * <p>
164         * Example:  "$1,999.99 or best offer" ==> "1999.99"
165         * </p>
166         *********************************************************************/
167         public static String  stripNonNumbers ( String  s )
168         //////////////////////////////////////////////////////////////////////
169         {
170           if ( s == null )
171           {
172             return null;
173           }
174    
175           StringBuffer  stringBuffer = new StringBuffer ( );
176    
177           int  sLength = s.length ( );
178    
179           for ( int  i = 0; i < sLength; i++ )
180           {
181             char  c = s.charAt ( i );
182    
183             if ( c == '.'
184               || Character.isDigit ( c ) )
185             {
186               stringBuffer.append ( c );
187             }
188           }
189    
190           String  stripped = stringBuffer.toString ( );
191    
192           if ( stripped.length ( ) < 1 )
193           {
194             return null;
195           }
196    
197           return stripped;       
198         }
199    
200         //////////////////////////////////////////////////////////////////////
201         //////////////////////////////////////////////////////////////////////
202    
203         private  ParseLib ( ) { }
204    
205         //////////////////////////////////////////////////////////////////////
206         //////////////////////////////////////////////////////////////////////
207         }