Coverage report

  %line %branch
org.apache.commons.jexl.util.Coercion
62% 
87% 

 1  
 /*
 2  
  * Copyright 2002,2004 The Apache Software Foundation.
 3  
  * 
 4  
  * Licensed under the Apache License, Version 2.0 (the "License");
 5  
  * you may not use this file except in compliance with the License.
 6  
  * You may obtain a copy of the License at
 7  
  * 
 8  
  *      http://www.apache.org/licenses/LICENSE-2.0
 9  
  * 
 10  
  * Unless required by applicable law or agreed to in writing, software
 11  
  * distributed under the License is distributed on an "AS IS" BASIS,
 12  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13  
  * See the License for the specific language governing permissions and
 14  
  * limitations under the License.
 15  
  */
 16  
 package org.apache.commons.jexl.util;
 17  
 
 18  
 /**
 19  
  *  Coercion utilities for the JSTL EL-like coercion.
 20  
  *
 21  
  *  @author <a href="mailto:geirm@apache.org">Geir Magnusson Jr.</a>
 22  
  */
 23  0
 public class Coercion
 24  
 {
 25  
 
 26  
     public static Boolean coerceBoolean(Object val) {
 27  333
         if (val == null)
 28  
         {
 29  0
             return Boolean.FALSE;
 30  
         }
 31  333
         else if (val instanceof Boolean)
 32  
         {
 33  297
             return (Boolean) val;
 34  
         }
 35  36
         else if (val instanceof String)
 36  
         {
 37  36
             return Boolean.valueOf((String) val);
 38  
         }
 39  
 
 40  0
         return null;
 41  
     }
 42  
 
 43  
     public static Integer coerceInteger(Object val)
 44  
         throws Exception
 45  
     {
 46  108
         if (val == null)
 47  
         {
 48  0
             return new Integer(0);
 49  
         }
 50  108
         else if (val instanceof String)
 51  
         {
 52  0
             if("".equals((String) val))
 53  0
                 return new Integer(0);
 54  
 
 55  0
             return Integer.valueOf((String)val);
 56  
         }
 57  108
         else if(val instanceof Character)
 58  
         {
 59  0
             return new Integer((int)((Character)val).charValue());
 60  
         }
 61  108
         else if(val instanceof Boolean)
 62  
         {
 63  0
             throw new Exception("Boolean->Integer coercion exception");
 64  
         }
 65  108
         else if(val instanceof Number)
 66  
         {
 67  108
             return new Integer(((Number)val).intValue());
 68  
         }
 69  
 
 70  0
         throw new Exception("Integer coercion exception");
 71  
     }
 72  
 
 73  
     public static Long coerceLong(Object val)
 74  
         throws Exception
 75  
     {
 76  738
         if (val == null)
 77  
         {
 78  0
             return new Long(0);
 79  
         }
 80  738
         else if (val instanceof String)
 81  
         {
 82  117
             if("".equals((String) val))
 83  0
                 return new Long(0);
 84  
 
 85  117
             return Long.valueOf((String)val);
 86  
         }
 87  621
         else if(val instanceof Character)
 88  
         {
 89  18
             return new Long((long)((Character)val).charValue());
 90  
         }
 91  603
         else if(val instanceof Boolean)
 92  
         {
 93  0
             throw new Exception("Boolean->Integer coercion exception");
 94  
         }
 95  603
         else if(val instanceof Number)
 96  
         {
 97  603
             return new Long(((Number)val).longValue());
 98  
         }
 99  
 
 100  0
         throw new Exception("Long coercion exception");
 101  
     }
 102  
 
 103  
     public static Double coerceDouble(Object val)
 104  
         throws Exception
 105  
     {
 106  216
         if (val == null)
 107  
         {
 108  0
             return new Double(0);
 109  
         }
 110  216
         else if (val instanceof String)
 111  
         {
 112  27
             if("".equals((String) val))
 113  0
                 return new Double(0);
 114  
 
 115  
             /*
 116  
              * the spec seems to be iffy about this.  Going to give it a wack
 117  
              *  anyway
 118  
              */
 119  
 
 120  27
             return new Double((String) val);
 121  
         }
 122  189
         else if(val instanceof Character)
 123  
         {
 124  0
             int i = ((Character)val).charValue();
 125  
 
 126  0
             return new Double(Double.parseDouble(String.valueOf(i)));
 127  
         }
 128  189
         else if(val instanceof Boolean)
 129  
         {
 130  0
             throw new Exception("Boolean->Integer coercion exception");
 131  
         }
 132  189
         else if(val instanceof Double)
 133  
         {
 134  36
             return (Double) val;
 135  
         }
 136  153
         else if (val instanceof Number)
 137  
         {
 138  
             //The below construct is used rather than ((Number)val).doubleValue() to ensure
 139  
             //equality between comparint new Double( 6.4 / 3 ) and the jexl expression of 6.4 / 3
 140  153
             return new Double(Double.parseDouble(String.valueOf(val)));
 141  
         }
 142  
 
 143  0
         throw new Exception("Double coercion exception");
 144  
     }
 145  
 
 146  
     public static boolean isFloatingPoint( final Object o )
 147  
     {
 148  342
         return o instanceof Float || o instanceof Double;
 149  
     }
 150  
 
 151  
     public static boolean isNumberable( final Object o )
 152  
     {
 153  225
         return o instanceof Integer
 154  
             || o instanceof Long
 155  
             || o instanceof Byte
 156  
             || o instanceof Short
 157  
             || o instanceof Character;
 158  
     }
 159  
 
 160  
 }

This report is generated by jcoverage, Maven and Maven JCoverage Plugin.