001: /*
002: * Copyright 2002,2004 The Apache Software Foundation.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.apache.commons.jexl.util;
017:
018: /**
019: * Coercion utilities for the JSTL EL-like coercion.
020: *
021: * @since 1.0
022: * @author <a href="mailto:geirm@apache.org">Geir Magnusson Jr.</a>
023: */
024: public class Coercion {
025:
026: /**
027: * Coerce to a Boolean.
028: *
029: * @param val Object to be coerced.
030: * @return The Boolean coerced value, or null if none possible.
031: */
032: public static Boolean coerceBoolean(Object val) {
033: if (val == null) {
034: return Boolean.FALSE;
035: } else if (val instanceof Boolean) {
036: return (Boolean) val;
037: } else if (val instanceof String) {
038: return Boolean.valueOf((String) val);
039: }
040: return null;
041: }
042:
043: /**
044: * Coerce to a Integer.
045: *
046: * @param val Object to be coerced.
047: * @return The Integer coerced value.
048: * @throws Exception If Integer coercion fails.
049: */
050: public static Integer coerceInteger(Object val) throws Exception {
051: if (val == null) {
052: return new Integer(0);
053: } else if (val instanceof String) {
054: if ("".equals(val)) {
055: return new Integer(0);
056: }
057: return Integer.valueOf((String) val);
058: } else if (val instanceof Character) {
059: return new Integer(((Character) val).charValue());
060: } else if (val instanceof Boolean) {
061: throw new Exception("Boolean->Integer coercion exception");
062: } else if (val instanceof Number) {
063: return new Integer(((Number) val).intValue());
064: }
065:
066: throw new Exception("Integer coercion exception");
067: }
068:
069: /**
070: * Coerce to a Long.
071: *
072: * @param val Object to be coerced.
073: * @return The Long coerced value.
074: * @throws Exception If Long coercion fails.
075: */
076: public static Long coerceLong(Object val) throws Exception {
077: if (val == null) {
078: return new Long(0);
079: } else if (val instanceof String) {
080: if ("".equals(val)) {
081: return new Long(0);
082: }
083: return Long.valueOf((String) val);
084: } else if (val instanceof Character) {
085: return new Long(((Character) val).charValue());
086: } else if (val instanceof Boolean) {
087: throw new Exception("Boolean->Long coercion exception");
088: } else if (val instanceof Number) {
089: return new Long(((Number) val).longValue());
090: }
091:
092: throw new Exception("Long coercion exception");
093: }
094:
095: /**
096: * Coerce to a Double.
097: *
098: * @param val Object to be coerced.
099: * @return The Double coerced value.
100: * @throws Exception If Double coercion fails.
101: */
102: public static Double coerceDouble(Object val) throws Exception {
103: if (val == null) {
104: return new Double(0);
105: } else if (val instanceof String) {
106: if ("".equals(val)) {
107: return new Double(0);
108: }
109:
110: /*
111: * the spec seems to be iffy about this. Going to give it a wack
112: * anyway
113: */
114:
115: return new Double((String) val);
116: } else if (val instanceof Character) {
117: int i = ((Character) val).charValue();
118:
119: return new Double(Double.parseDouble(String.valueOf(i)));
120: } else if (val instanceof Boolean) {
121: throw new Exception("Boolean->Double coercion exception");
122: } else if (val instanceof Double) {
123: return (Double) val;
124: } else if (val instanceof Number) {
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:
130: throw new Exception("Double coercion exception");
131: }
132:
133: /**
134: * Is Object a floating point number.
135: *
136: * @param o Object to be analyzed.
137: * @return true if it is a Float or a Double.
138: */
139: public static boolean isFloatingPoint(final Object o) {
140: return o instanceof Float || o instanceof Double;
141: }
142:
143: /**
144: * Is Object a whole number.
145: *
146: * @param o Object to be analyzed.
147: * @return true if Integer, Long, Byte, Short or Character.
148: */
149: public static boolean isNumberable(final Object o) {
150: return o instanceof Integer || o instanceof Long
151: || o instanceof Byte || o instanceof Short
152: || o instanceof Character;
153: }
154:
155: }
|