Coverage Report - org.apache.commons.xmlio.in.ConversionHelpers
 
Classes in this File Line Coverage Branch Coverage Complexity
ConversionHelpers
0%
0/23
0%
0/12
3.667
 
 1  
 /*
 2  
  * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons-sandbox//xmlio/src/java/org/apache/commons/xmlio/in/ConversionHelpers.java,v 1.1 2004/10/08 11:56:20 ozeigermann Exp $
 3  
  * $Revision: 155476 $
 4  
  * $Date: 2005-02-26 13:31:24 +0000 (Sat, 26 Feb 2005) $
 5  
  *
 6  
  * ====================================================================
 7  
  *
 8  
  * Copyright 2004 The Apache Software Foundation 
 9  
  *
 10  
  * Licensed under the Apache License, Version 2.0 (the "License");
 11  
  * you may not use this file except in compliance with the License.
 12  
  * You may obtain a copy of the License at
 13  
  *
 14  
  *     http://www.apache.org/licenses/LICENSE-2.0
 15  
  *
 16  
  * Unless required by applicable law or agreed to in writing, software
 17  
  * distributed under the License is distributed on an "AS IS" BASIS,
 18  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 19  
  * See the License for the specific language governing permissions and
 20  
  * limitations under the License.
 21  
  *
 22  
  */
 23  
 
 24  
 package org.apache.commons.xmlio.in;
 25  
 
 26  
 /**
 27  
  * Collection of some simple conversion and fallback methods for convenience.
 28  
  *
 29  
  */
 30  0
 public class ConversionHelpers {
 31  
 
 32  
     /** Returns <code>value</code> if not null, otherwise <code>fallBack</code>.
 33  
      */
 34  
     public static String getString(String value, String fallBack) {
 35  0
         if (value == null)
 36  0
             return fallBack;
 37  0
         return value;
 38  
     }
 39  
 
 40  
     /** Gets int value from a string value. 
 41  
      * @param value string value to get int from
 42  
      * @return int representation of value or <code>-1</code> 
 43  
      * if it can not be converted to an int
 44  
      */
 45  
     public static int getInt(String value) {
 46  0
         return getInt(value, -1);
 47  
     }
 48  
 
 49  
     /** Gets int value from a string value. 
 50  
      * @param value string value to get int from
 51  
      * @param fallBack fall back value
 52  
      * @return int representation of value or <code>fallBack</code> 
 53  
      * if it can not be converted to an int
 54  
      */
 55  
     public static int getInt(String value, int fallBack) {
 56  0
         if (value == null)
 57  0
             return fallBack;
 58  
         try {
 59  0
             return Integer.valueOf(value).intValue();
 60  0
         } catch (NumberFormatException nfe) {
 61  0
             return fallBack;
 62  
         }
 63  
     }
 64  
 
 65  
     /** Gets long value from a string value. 
 66  
      * @param value string value to get long from
 67  
      * @return long representation of value or <code>-1L</code> 
 68  
      * if it can not be converted to a long
 69  
      */
 70  
     public static long getLong(String value) {
 71  0
         return getLong(value, -1L);
 72  
     }
 73  
 
 74  
     /** Gets long value from a string value. 
 75  
      * @param value string value to get long from
 76  
      * @param fallBack fall back value
 77  
      * @return long representation of value or <code>fallBack</code> 
 78  
      * if it can not be converted to a long
 79  
      */
 80  
     public static long getLong(String value, long fallBack) {
 81  0
         if (value == null)
 82  0
             return fallBack;
 83  
         try {
 84  0
             return Long.valueOf(value).longValue();
 85  0
         } catch (NumberFormatException nfe) {
 86  0
             return fallBack;
 87  
         }
 88  
     }
 89  
 
 90  
     /** Gets boolean value from a string value.
 91  
      * @param value string value to get boolean from
 92  
      * @param fallBack fall back value
 93  
      * @return boolean representation of value <code>fallBack</code> 
 94  
      * if it can not <em>properly</em> be converted to a boolean
 95  
      */
 96  
     public static boolean getBoolean(String value, boolean fallBack) {
 97  0
         if (value == null)
 98  0
             return fallBack;
 99  
         // do not use "Boolean.valueOf(" as this returns false on everything
 100  
         // but "true"
 101  0
         if ("true".equalsIgnoreCase(value))
 102  0
             return true;
 103  0
         if ("false".equalsIgnoreCase(value))
 104  0
             return false;
 105  0
         return fallBack;
 106  
     }
 107  
 }