Coverage Report - org.apache.myfaces.convert.ConverterUtils
 
Classes in this File Line Coverage Branch Coverage Complexity
ConverterUtils
0%
0/29
0%
0/16
6.6
 
 1  
 /*
 2  
  * Licensed to the Apache Software Foundation (ASF) under one
 3  
  * or more contributor license agreements.  See the NOTICE file
 4  
  * distributed with this work for additional information
 5  
  * regarding copyright ownership.  The ASF licenses this file
 6  
  * to you under the Apache License, Version 2.0 (the
 7  
  * "License"); you may not use this file except in compliance
 8  
  * with the License.  You may obtain a copy of the License at
 9  
  *
 10  
  *   http://www.apache.org/licenses/LICENSE-2.0
 11  
  *
 12  
  * Unless required by applicable law or agreed to in writing,
 13  
  * software distributed under the License is distributed on an
 14  
  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 15  
  * KIND, either express or implied.  See the License for the
 16  
  * specific language governing permissions and limitations
 17  
  * under the License.
 18  
  */
 19  
 package org.apache.myfaces.convert;
 20  
 
 21  
 
 22  
 /**
 23  
  * TODO: Move to util package and rename to better name
 24  
  *
 25  
  * @author Manfred Geiler (latest modification by $Author$)
 26  
  * @version $Revision$ $Date$
 27  
  */
 28  
 public final class ConverterUtils
 29  
 {
 30  
     //private static final Log log = LogFactory.getLog(ConverterUtils.class);
 31  
 
 32  0
     private ConverterUtils() {}
 33  
 
 34  
 
 35  
     public static int convertToInt(Object value)
 36  
     {
 37  0
         if (value instanceof Number)
 38  
         {
 39  0
             return ((Number) value).intValue();
 40  
         }
 41  0
         else if (value instanceof String)
 42  
         {
 43  
             try
 44  
             {
 45  0
                 return Integer.parseInt((String) value);
 46  
             }
 47  0
             catch (NumberFormatException e)
 48  
             {
 49  0
                 throw new IllegalArgumentException("Cannot convert " + value.toString() + " to int");
 50  
             }
 51  
         }
 52  
         else
 53  
         {
 54  0
             throw new IllegalArgumentException("Cannot convert " + value.toString() + " to int");
 55  
         }
 56  
     }
 57  
 
 58  
     public static boolean convertToBoolean(Object value)
 59  
     {
 60  0
         if (value instanceof Boolean)
 61  
         {
 62  0
             return ((Boolean) value);
 63  
         }
 64  0
         else if (value instanceof String)
 65  
         {
 66  
             try
 67  
             {
 68  0
                 return Boolean.parseBoolean((String) value);
 69  
             }
 70  0
             catch (Exception e)
 71  
             {
 72  0
                 throw new IllegalArgumentException("Cannot convert " + value.toString() + " to boolean");
 73  
             }
 74  
         }
 75  
         else
 76  
         {
 77  0
             throw new IllegalArgumentException("Cannot convert " + value.toString() + " to boolean");
 78  
         }
 79  
     }    
 80  
 
 81  
     public static long convertToLong(Object value)
 82  
     {
 83  0
         if (value instanceof Number)
 84  
         {
 85  0
             return ((Number) value).longValue();
 86  
         }
 87  0
         else if (value instanceof String)
 88  
         {
 89  
             try
 90  
             {
 91  0
                 return Long.parseLong((String) value);
 92  
             }
 93  0
             catch (NumberFormatException e)
 94  
             {
 95  0
                 throw new IllegalArgumentException("Cannot convert " + value.toString() + " to long");
 96  
             }
 97  
         }
 98  
         else
 99  
         {
 100  0
             throw new IllegalArgumentException("Cannot convert " + value.toString() + " to long");
 101  
         }
 102  
     }
 103  
 
 104  
     public static double convertToDouble(Object value)
 105  
     {
 106  0
         if (value instanceof Number)
 107  
         {
 108  0
             return ((Number) value).doubleValue();
 109  
         }
 110  0
         else if (value instanceof String)
 111  
         {
 112  
             try
 113  
             {
 114  0
                 return Double.parseDouble((String) value);
 115  
             }
 116  0
             catch (NumberFormatException e)
 117  
             {
 118  0
                 throw new IllegalArgumentException("Cannot convert " + value.toString() + " to double");
 119  
             }
 120  
         }
 121  
         else
 122  
         {
 123  0
             throw new IllegalArgumentException("Cannot convert " + value.toString() + " to double");
 124  
         }
 125  
     }
 126  
 
 127  
 
 128  
 }