Coverage report

| %line | %branch
--- | --- | ---
org.apache.commons.jexl.util.Coercion |
64% |
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 | 567 |
             if (val == null)
28 |  |
             {
29 | 0 |
                 return Boolean.FALSE;
30 |  |
             }
31 | 567 |
             else if (val instanceof Boolean)
32 |  |
             {
33 | 507 |
                 return (Boolean) val;
34 |  |
             }
35 | 60 |
             else if (val instanceof String)
36 |  |
             {
37 | 60 |
                 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 | 180 |
             if (val == null)
47 |  |
             {
48 | 0 |
                 return new Integer(0);
49 |  |
             }
50 | 180 |
             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 | 180 |
             else if(val instanceof Character)
58 |  |
             {
59 | 0 |
                 return new Integer((int)((Character)val).charValue());
60 |  |
             }
61 | 180 |
             else if(val instanceof Boolean)
62 |  |
             {
63 | 0 |
                 throw new Exception("Boolean->Integer coercion exception");
64 |  |
             }
65 | 180 |
             else if(val instanceof Number)
66 |  |
             {
67 | 180 |
                 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 | 1236 |
             if (val == null)
77 |  |
             {
78 | 0 |
                 return new Long(0);
79 |  |
             }
80 | 1236 |
             else if (val instanceof String)
81 |  |
             {
82 | 201 |
                 if("".equals((String) val))
83 | 0 |
                     return new Long(0);
84 |  |
85 | 201 |
                 return Long.valueOf((String)val);
86 |  |
             }
87 | 1035 |
             else if(val instanceof Character)
88 |  |
             {
89 | 30 |
                 return new Long((long)((Character)val).charValue());
90 |  |
             }
91 | 1005 |
             else if(val instanceof Boolean)
92 |  |
             {
93 | 0 |
                 throw new Exception("Boolean->Integer coercion exception");
94 |  |
             }
95 | 1005 |
             else if(val instanceof Number)
96 |  |
             {
97 | 1005 |
                 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 | 366 |
             if (val == null)
107 |  |
             {
108 | 0 |
                 return new Double(0);
109 |  |
             }
110 | 366 |
             else if (val instanceof String)
111 |  |
             {
112 | 51 |
                 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 | 51 |
                 return new Double((String) val);
121 |  |
             }
122 | 315 |
             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 | 315 |
             else if(val instanceof Boolean)
129 |  |
             {
130 | 0 |
                 throw new Exception("Boolean->Integer coercion exception");
131 |  |
             }
132 | 315 |
             else if(val instanceof Double)
133 |  |
             {
134 | 60 |
                 return (Double) val;
135 |  |
             }
136 | 255 |
             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 | 255 |
                 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 | 570 |
             return o instanceof Float || o instanceof Double;
149 |  |
         }
150 |  |
151 |  |
         public static boolean isNumberable( final Object o )
152 |  |
         {
153 | 394 |
             return o instanceof Integer
154 | 6 |
                 || o instanceof Long
155 | 6 |
                 || o instanceof Byte
156 | 6 |
                 || o instanceof Short
157 | 6 |
                 || o instanceof Character;
158 |  |
         }
159 |  |
160 |  |
     }

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