Coverage report

  %line %branch
org.apache.commons.jexl.util.Coercion
63% 
86% 

 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  
  *  @since 1.0
 22  
  *  @author <a href="mailto:geirm@apache.org">Geir Magnusson Jr.</a>
 23  
  */
 24  0
 public class Coercion {
 25  
 
 26  
     /**
 27  
      * Coerce to a Boolean.
 28  
      *
 29  
      * @param val Object to be coerced.
 30  
      * @return The Boolean coerced value, or null if none possible.
 31  
      */
 32  
     public static Boolean coerceBoolean(Object val) {
 33  92
         if (val == null) {
 34  0
             return Boolean.FALSE;
 35  92
         } else if (val instanceof Boolean) {
 36  88
             return (Boolean) val;
 37  4
         } else if (val instanceof String) {
 38  4
             return Boolean.valueOf((String) val);
 39  
         }
 40  0
         return null;
 41  
     }
 42  
 
 43  
     /**
 44  
      * Coerce to a Integer.
 45  
      *
 46  
      * @param val Object to be coerced.
 47  
      * @return The Integer coerced value.
 48  
      * @throws Exception If Integer coercion fails.
 49  
      */
 50  
     public static Integer coerceInteger(Object val)
 51  
     throws Exception {
 52  12
         if (val == null) {
 53  0
             return new Integer(0);
 54  12
         } else if (val instanceof String) {
 55  0
             if ("".equals(val)) {
 56  0
                 return new Integer(0);
 57  
             }
 58  0
             return Integer.valueOf((String) val);
 59  12
         } else if (val instanceof Character) {
 60  0
             return new Integer(((Character) val).charValue());
 61  12
         } else if (val instanceof Boolean) {
 62  0
             throw new Exception("Boolean->Integer coercion exception");
 63  12
         } else if (val instanceof Number) {
 64  12
             return new Integer(((Number) val).intValue());
 65  
         }
 66  
 
 67  0
         throw new Exception("Integer coercion exception");
 68  
     }
 69  
 
 70  
     /**
 71  
      * Coerce to a Long.
 72  
      *
 73  
      * @param val Object to be coerced.
 74  
      * @return The Long coerced value.
 75  
      * @throws Exception If Long coercion fails.
 76  
      */
 77  
     public static Long coerceLong(Object val)
 78  
     throws Exception {
 79  267
         if (val == null) {
 80  1
             return new Long(0);
 81  266
         } else if (val instanceof String) {
 82  20
             if ("".equals(val)) {
 83  0
                 return new Long(0);
 84  
             }
 85  20
             return Long.valueOf((String) val);
 86  246
         } else if (val instanceof Character) {
 87  2
             return new Long(((Character) val).charValue());
 88  244
         } else if (val instanceof Boolean) {
 89  0
             throw new Exception("Boolean->Long coercion exception");
 90  244
         } else if (val instanceof Number) {
 91  244
             return new Long(((Number) val).longValue());
 92  
         }
 93  
 
 94  0
         throw new Exception("Long coercion exception");
 95  
     }
 96  
 
 97  
     /**
 98  
      * Coerce to a Double.
 99  
      *
 100  
      * @param val Object to be coerced.
 101  
      * @return The Double coerced value.
 102  
      * @throws Exception If Double coercion fails.
 103  
      */
 104  
     public static Double coerceDouble(Object val)
 105  
     throws Exception {
 106  43
         if (val == null) {
 107  0
             return new Double(0);
 108  43
         } else if (val instanceof String) {
 109  6
             if ("".equals(val)) {
 110  0
                 return new Double(0);
 111  
             }
 112  
 
 113  
             /*
 114  
              * the spec seems to be iffy about this.  Going to give it a wack
 115  
              *  anyway
 116  
              */
 117  
 
 118  6
             return new Double((String) val);
 119  37
         } else if (val instanceof Character) {
 120  0
             int i = ((Character) val).charValue();
 121  
 
 122  0
             return new Double(Double.parseDouble(String.valueOf(i)));
 123  37
         } else if (val instanceof Boolean) {
 124  0
             throw new Exception("Boolean->Double coercion exception");
 125  37
         } else if (val instanceof Double) {
 126  6
             return (Double) val;
 127  31
         } else if (val instanceof Number) {
 128  
             //The below construct is used rather than ((Number)val).doubleValue() to ensure
 129  
             //equality between comparint new Double( 6.4 / 3 ) and the jexl expression of 6.4 / 3
 130  31
             return new Double(Double.parseDouble(String.valueOf(val)));
 131  
         }
 132  
 
 133  0
         throw new Exception("Double coercion exception");
 134  
     }
 135  
 
 136  
     /**
 137  
      * Is Object a floating point number.
 138  
      *
 139  
      * @param o Object to be analyzed.
 140  
      * @return true if it is a Float or a Double.
 141  
      */
 142  
     public static boolean isFloatingPoint(final Object o) {
 143  122
         return o instanceof Float || o instanceof Double;
 144  
     }
 145  
 
 146  
     /**
 147  
      * Is Object a whole number.
 148  
      *
 149  
      * @param o Object to be analyzed.
 150  
      * @return true if Integer, Long, Byte, Short or Character.
 151  
      */
 152  
     public static boolean isNumberable(final Object o) {
 153  70
         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.