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