Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Licensed to the Apache Software Foundation (ASF) under one or more
   * contributor license agreements.  See the NOTICE file distributed with
   * this work for additional information regarding copyright ownership.
   * The ASF licenses this file to You under the Apache License, Version 2.0
   * (the "License"); you may not use this file except in compliance with
   * the License.  You may obtain a copy of the License at
   *
   *      http://www.apache.org/licenses/LICENSE-2.0
  *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
 
 package org.apache.el.lang;
 
 import static org.jboss.web.ELMessages.MESSAGES;
 
 
A helper class that implements the EL Specification

Author(s):
Jacob Hookom [jacob@hookom.net]
Version:
$Id: ELSupport.java 1967 2012-02-16 15:39:39Z remy.maucherat@jboss.com $
 
 public class ELSupport {
 
     private static final Long ZERO = Long.valueOf(0L);

    
Compare two objects, after coercing to the same type if appropriate. If the objects are identical, or they are equal according to equals(java.lang.Object,java.lang.Object) then return 0. If either object is a BigDecimal, then coerce both to BigDecimal first. Similarly for Double(Float), BigInteger, and Long(Integer, Char, Short, Byte). Otherwise, check that the first object is an instance of Comparable, and compare against the second object. If that is null, return 1, otherwise return the result of comparing against the second object. Similarly, if the second object is Comparable, if the first is null, return -1, else return the result of comparing against the first object. A null object is considered as:
  • ZERO when compared with Numbers
  • the empty string for String compares
  • Otherwise null is considered to be lower than anything else.

Parameters:
obj0 first object
obj1 second object
Returns:
-1, 0, or 1 if this object is less than, equal to, or greater than val.
Throws:
javax.el.ELException if neither object is Comparable
java.lang.ClassCastException if the objects are not mutually comparable
 
     public static final int compare(final Object obj0final Object obj1)
             throws ELException {
         if (obj0 == obj1 || equals(obj0obj1)) {
             return 0;
         }
         if (isBigDecimalOp(obj0obj1)) {
             BigDecimal bd0 = (BigDecimalcoerceToNumber(obj0BigDecimal.class);
             BigDecimal bd1 = (BigDecimalcoerceToNumber(obj1BigDecimal.class);
             return bd0.compareTo(bd1);
         }
         if (isDoubleOp(obj0obj1)) {
             Double d0 = (DoublecoerceToNumber(obj0Double.class);
             Double d1 = (DoublecoerceToNumber(obj1Double.class);
             return d0.compareTo(d1);
         }
         if (isBigIntegerOp(obj0obj1)) {
             BigInteger bi0 = (BigIntegercoerceToNumber(obj0BigInteger.class);
             BigInteger bi1 = (BigIntegercoerceToNumber(obj1BigInteger.class);
             return bi0.compareTo(bi1);
         }
         if (isLongOp(obj0obj1)) {
             Long l0 = (LongcoerceToNumber(obj0Long.class);
             Long l1 = (LongcoerceToNumber(obj1Long.class);
             return l0.compareTo(l1);
         }
         if (obj0 instanceof String || obj1 instanceof String) {
             return coerceToString(obj0).compareTo(coerceToString(obj1));
         }
         if (obj0 instanceof Comparable<?>) {
             @SuppressWarnings("unchecked"// checked above
             final Comparable<Objectcomparable = (Comparable<Object>) obj0;
            return (obj1 != null) ? comparable.compareTo(obj1) : 1;
        }
        if (obj1 instanceof Comparable<?>) {
            @SuppressWarnings("unchecked"// checked above
            final Comparable<Objectcomparable = (Comparable<Object>) obj1;
            return (obj0 != null) ? -comparable.compareTo(obj0) : -1;
        }
        throw new ELException(.errorComparing(obj0obj1));
    }

    
Compare two objects for equality, after coercing to the same type if appropriate. If the objects are identical (including both null) return true. If either object is null, return false. If either object is Boolean, coerce both to Boolean and check equality. Similarly for Enum, String, BigDecimal, Double(Float), Long(Integer, Short, Byte, Character) Otherwise default to using Object.equals().

Parameters:
obj0 the first object
obj1 the second object
Returns:
true if the objects are equal
Throws:
javax.el.ELException
    public static final boolean equals(final Object obj0final Object obj1)
    throws ELException {
        if (obj0 == obj1) {
            return true;
        } else if (obj0 == null || obj1 == null) {
            return false;
        } else if (isBigDecimalOp(obj0obj1)) {
            BigDecimal bd0 = (BigDecimalcoerceToNumber(obj0BigDecimal.class);
            BigDecimal bd1 = (BigDecimalcoerceToNumber(obj1BigDecimal.class);
            return bd0.equals(bd1);
        } else if (isDoubleOp(obj0obj1)) {
            Double d0 = (DoublecoerceToNumber(obj0Double.class);
            Double d1 = (DoublecoerceToNumber(obj1Double.class);
            return d0.equals(d1);
        } else if (isBigIntegerOp(obj0obj1)) {
            BigInteger bi0 = (BigIntegercoerceToNumber(obj0BigInteger.class);
            BigInteger bi1 = (BigIntegercoerceToNumber(obj1BigInteger.class);
            return bi0.equals(bi1);
        } else if (isLongOp(obj0obj1)) {
            Long l0 = (LongcoerceToNumber(obj0Long.class);
            Long l1 = (LongcoerceToNumber(obj1Long.class);
            return l0.equals(l1);
        } else if (obj0 instanceof Boolean || obj1 instanceof Boolean) {
            return coerceToBoolean(obj0).equals(coerceToBoolean(obj1));
        } else if (obj0.getClass().isEnum()) {
            return obj0.equals(coerceToEnum(obj1obj0.getClass()));
        } else if (obj1.getClass().isEnum()) {
            return obj1.equals(coerceToEnum(obj0obj1.getClass()));
        } else if (obj0 instanceof String || obj1 instanceof String) {
            int lexCompare = coerceToString(obj0).compareTo(coerceToString(obj1));
            return (lexCompare == 0) ? true : false;
        } else {
            return obj0.equals(obj1);
        }
    }
    // Going to have to have some casts /raw types somewhere so doing it here
    // keeps them all in one place. There might be a neater / better solution
    // but I couldn't find it
    @SuppressWarnings("unchecked")
    public static final Enum<?> coerceToEnum(final Object obj,
            @SuppressWarnings("rawtypes"Class type) {
        if (obj == null || "".equals(obj)) {
            return null;
        }
        if (type.isAssignableFrom(obj.getClass())) {
            return (Enum<?>) obj;
        }
        if (!(obj instanceof String)) {
            throw new ELException(.errorConverting(objobj.getClass(), type));
        }
        Enum<?> result;
        try {
             result = Enum.valueOf(type, (Stringobj);
        } catch (IllegalArgumentException iae) {
            throw new ELException(.errorConverting(objobj.getClass(), type));
        }
        return result;
    }

    
Convert an object to Boolean. Null and empty string are false.

Parameters:
obj the object to convert
Returns:
the Boolean value of the object
Throws:
javax.el.ELException if object is not Boolean or String
    public static final Boolean coerceToBoolean(final Object obj)
            throws ELException {
        if (obj == null || "".equals(obj)) {
            return .;
        }
        if (obj instanceof Boolean) {
            return (Booleanobj;
        }
        if (obj instanceof String) {
            return Boolean.valueOf((Stringobj);
        }
        throw new ELException(.errorConverting(objobj.getClass(), Boolean.class));
    }
    public static final Character coerceToCharacter(final Object obj)
            throws ELException {
        if (obj == null || "".equals(obj)) {
            return Character.valueOf((char) 0);
        }
        if (obj instanceof String) {
            return Character.valueOf(((Stringobj).charAt(0));
        }
        if (ELArithmetic.isNumber(obj)) {
            return Character.valueOf((char) ((Numberobj).shortValue());
        }
        Class<?> objType = obj.getClass();
        if (obj instanceof Character) {
            return (Characterobj;
        }
        throw new ELException(.errorConverting(objobjTypeCharacter.class));
    }
    protected static final Number coerceToNumber(final Number number,
            final Class<?> typethrows ELException {
        if (. == type || Long.class.equals(type)) {
            return Long.valueOf(number.longValue());
        }
        if (. == type || Double.class.equals(type)) {
            return new Double(number.doubleValue());
        }
        if (. == type || Integer.class.equals(type)) {
            return Integer.valueOf(number.intValue());
        }
        if (BigInteger.class.equals(type)) {
            if (number instanceof BigDecimal) {
                return ((BigDecimalnumber).toBigInteger();
            }
            if (number instanceof BigInteger) {
                return number;
            }
            return BigInteger.valueOf(number.longValue());
        }
        if (BigDecimal.class.equals(type)) {
            if (number instanceof BigDecimal) {
                return number;
            }
            if (number instanceof BigInteger) {
                return new BigDecimal((BigIntegernumber);
            }
            return new BigDecimal(number.doubleValue());
        }
        if (. == type || Byte.class.equals(type)) {
            return Byte.valueOf(number.byteValue());
        }
        if (. == type || Short.class.equals(type)) {
            return Short.valueOf(number.shortValue());
        }
        if (. == type || Float.class.equals(type)) {
            return new Float(number.floatValue());
        }
        if (Number.class.equals(type)) {
            return number;
        }
        throw new ELException(.errorConverting(numbernumber.getClass(), type));
    }
    public static final Number coerceToNumber(final Object obj,
            final Class<?> typethrows ELException {
        if (obj == null || "".equals(obj)) {
            return coerceToNumber(type);
        }
        if (obj instanceof String) {
            return coerceToNumber((Stringobjtype);
        }
        if (ELArithmetic.isNumber(obj)) {
            return coerceToNumber((Numberobjtype);
        }
        if (obj instanceof Character) {
            return coerceToNumber(Short.valueOf((short) ((Characterobj)
                    .charValue()), type);
        }
        throw new ELException(.errorConverting(objobj.getClass(), type));
    }
    protected static final Number coerceToNumber(final String val,
            final Class<?> typethrows ELException {
        if (. == type || Long.class.equals(type)) {
            try {
                return Long.valueOf(val);
            } catch (NumberFormatException nfe) {
                throw new ELException(.errorConverting(valString.classtype));
            }
        }
        if (. == type || Integer.class.equals(type)) {
            try {
                return Integer.valueOf(val);
            } catch (NumberFormatException nfe) {
                throw new ELException(.errorConverting(valString.classtype));
            }
        }
        if (. == type || Double.class.equals(type)) {
            try {
                return Double.valueOf(val);
            } catch (NumberFormatException nfe) {
                throw new ELException(.errorConverting(valString.classtype));
            }
        }
        if (BigInteger.class.equals(type)) {
            try {
                return new BigInteger(val);
            } catch (NumberFormatException nfe) {
                throw new ELException(.errorConverting(valString.classtype));
            }
        }
        if (BigDecimal.class.equals(type)) {
            try {
                return new BigDecimal(val);
            } catch (NumberFormatException nfe) {
                throw new ELException(.errorConverting(valString.classtype));
            }
        }
        if (. == type || Byte.class.equals(type)) {
            try {
                return Byte.valueOf(val);
            } catch (NumberFormatException nfe) {
                throw new ELException(.errorConverting(valString.classtype));
            }
        }
        if (. == type || Short.class.equals(type)) {
            try {
                return Short.valueOf(val);
            } catch (NumberFormatException nfe) {
                throw new ELException(.errorConverting(valString.classtype));
            }
        }
        if (. == type || Float.class.equals(type)) {
            try {
                return Float.valueOf(val);
            } catch (NumberFormatException nfe) {
                throw new ELException(.errorConverting(valString.classtype));
            }
        }
        throw new ELException(.errorConverting(valString.classtype));
    }

    
Coerce an object to a string

Parameters:
obj
Returns:
the String value of the object
    public static final String coerceToString(final Object obj) {
        if (obj == null) {
            return "";
        } else if (obj instanceof String) {
            return (Stringobj;
        } else if (obj instanceof Enum<?>) {
            return ((Enum<?>) obj).name();
        } else {
            return obj.toString();
        }
    }
    public static final Object coerceToType(final Object obj,
            final Class<?> typethrows ELException {
        if (type == null || Object.class.equals(type) ||
                (obj != null && type.isAssignableFrom(obj.getClass()))) {
            return obj;
        }
        if (String.class.equals(type)) {
            return coerceToString(obj);
        }
        if (ELArithmetic.isNumberType(type)) {
            return coerceToNumber(objtype);
        }
        if (Character.class.equals(type) || . == type) {
            return coerceToCharacter(obj);
        }
        if (Boolean.class.equals(type) || . == type) {
            return coerceToBoolean(obj);
        }
        if (type.isEnum()) {
            return coerceToEnum(objtype);
        }
        // new to spec
        if (obj == null)
            return null;
        if (obj instanceof String) {
            if ("".equals(obj))
                return null;
            PropertyEditor editor = PropertyEditorManager.findEditor(type);
            if (editor != null) {
                editor.setAsText((Stringobj);
                return editor.getValue();
            }
        }
        throw new ELException(.errorConverting(objobj.getClass(), type));
    }
    public static final boolean isBigDecimalOp(final Object obj0,
            final Object obj1) {
        return (obj0 instanceof BigDecimal || obj1 instanceof BigDecimal);
    }
    public static final boolean isBigIntegerOp(final Object obj0,
            final Object obj1) {
        return (obj0 instanceof BigInteger || obj1 instanceof BigInteger);
    }
    public static final boolean isDoubleOp(final Object obj0final Object obj1) {
        return (obj0 instanceof Double
                || obj1 instanceof Double
                || obj0 instanceof Float
                || obj1 instanceof Float);
    }
    public static final boolean isLongOp(final Object obj0final Object obj1) {
        return (obj0 instanceof Long
                || obj1 instanceof Long
                || obj0 instanceof Integer
                || obj1 instanceof Integer
                || obj0 instanceof Character
                || obj1 instanceof Character
                || obj0 instanceof Short
                || obj1 instanceof Short
                || obj0 instanceof Byte
                || obj1 instanceof Byte);
    }
    public static final boolean isStringFloat(final String str) {
        int len = str.length();
        if (len > 1) {
            for (int i = 0; i < leni++) {
                switch (str.charAt(i)) {
                case 'E':
                    return true;
                case 'e':
                    return true;
                case '.':
                    return true;
                }
            }
        }
        return false;
    }

    
    public ELSupport() {
        super();
    }
New to GrepCode? Check out our FAQ X