Example usage for java.lang Class equals

List of usage examples for java.lang Class equals

Introduction

In this page you can find the example usage for java.lang Class equals.

Prototype

public boolean equals(Object obj) 

Source Link

Document

Indicates whether some other object is "equal to" this one.

Usage

From source file:hu.javaforum.commons.ReflectionHelper.java

/**
 * Converts the string value to instance of BigDecimal or BigInteger.
 *
 * @param fieldClass The field class//  w ww  .j a va2  s.c  om
 * @param stringValue The string value
 * @return The instance of the field class
 * @throws ParseException Throws when the string isn't parseable
 */
private static Object convertToBigNumbers(final Class fieldClass, final String stringValue)
        throws ParseException {
    Object parameter = null;

    if (fieldClass.equals(BigDecimal.class)) {
        parameter = new BigDecimal(stringValue);
    } else if (fieldClass.equals(BigInteger.class)) {
        parameter = new BigInteger(stringValue);
    }

    return parameter;
}

From source file:Classes.java

/**
 * Check if the given class is a primitive wrapper class.
 * /* w w w .ja v  a 2 s.  c  o  m*/
 * @param type
 *          Class to check.
 * @return True if the class is a primitive wrapper.
 */
public static boolean isPrimitiveWrapper(final Class type) {
    for (int i = 0; i < PRIMITIVE_WRAPPER_MAP.length; i += 2) {
        if (type.equals(PRIMITIVE_WRAPPER_MAP[i + 1])) {
            return true;
        }
    }

    return false;
}

From source file:nz.co.senanque.validationengine.ConvertUtils.java

public static Comparable<?> convertTo(Class<?> clazz, Object obj) {
    if (obj == null) {
        return null;
    }/*w  w  w .  j a  v  a2 s. com*/
    if (clazz.isAssignableFrom(obj.getClass())) {
        return (Comparable<?>) obj;
    }
    if (clazz.isPrimitive()) {
        if (clazz.equals(Long.TYPE)) {
            clazz = Long.class;
        } else if (clazz.equals(Integer.TYPE)) {
            clazz = Integer.class;
        } else if (clazz.equals(Float.TYPE)) {
            clazz = Float.class;
        } else if (clazz.equals(Double.TYPE)) {
            clazz = Double.class;
        } else if (clazz.equals(Boolean.TYPE)) {
            clazz = Boolean.class;
        }
    }
    if (Number.class.isAssignableFrom(clazz)) {
        if (obj.getClass().equals(String.class)) {
            obj = new Double((String) obj);
        }
        if (!Number.class.isAssignableFrom(obj.getClass())) {
            throw new RuntimeException(
                    "Cannot convert from " + obj.getClass().getName() + " to " + clazz.getName());
        }
        Number number = (Number) obj;
        if (clazz.equals(Long.class)) {
            return new Long(number.longValue());
        }
        if (clazz.equals(Integer.class)) {
            return new Integer(number.intValue());
        }
        if (clazz.equals(Float.class)) {
            return new Float(number.floatValue());
        }
        if (clazz.equals(Double.class)) {
            return new Double(number.doubleValue());
        }
        if (clazz.equals(BigDecimal.class)) {
            return new BigDecimal(number.doubleValue());
        }
    }
    final String oStr = String.valueOf(obj);
    if (clazz.equals(String.class)) {
        return oStr;
    }
    if (clazz.equals(java.util.Date.class)) {
        return java.sql.Date.valueOf(oStr);
    }
    if (clazz.equals(java.sql.Date.class)) {
        return java.sql.Date.valueOf(oStr);
    }
    if (clazz.equals(Boolean.class)) {
        return new Boolean(oStr);
    }
    throw new RuntimeException("Cannot convert from " + obj.getClass().getName() + " to " + clazz.getName());
}

From source file:hu.javaforum.commons.ReflectionHelper.java

/**
 * Converts the string value to instance of primitive wrapper.
 *
 * @param fieldClass The field class//from w w  w .  j a v a  2 s.  c  om
 * @param stringValue The string value
 * @return The instance of the field class
 * @throws ParseException Throws when the string isn't parseable
 */
private static Object convertToPrimitive(final Class fieldClass, final String stringValue)
        throws ParseException {
    Object parameter = null;

    if (fieldClass.equals(boolean.class)) {
        parameter = Boolean.parseBoolean(stringValue);
    } else if (fieldClass.equals(byte.class)) {
        parameter = Byte.parseByte(stringValue);
    } else if (fieldClass.equals(double.class)) {
        parameter = Double.parseDouble(stringValue);
    } else if (fieldClass.equals(float.class)) {
        parameter = Float.parseFloat(stringValue);
    } else if (fieldClass.equals(int.class)) {
        parameter = Integer.parseInt(stringValue);
    } else if (fieldClass.equals(long.class)) {
        parameter = Long.parseLong(stringValue);
    } else if (fieldClass.equals(short.class)) {
        parameter = Short.parseShort(stringValue);
    }

    return parameter;
}

From source file:hu.javaforum.commons.ReflectionHelper.java

/**
 * Converts the string value to instance of primitive wrapper.
 *
 * @param fieldClass The field class//from   www  . j  av  a2s .co  m
 * @param stringValue The string value
 * @return The instance of the field class
 * @throws ParseException Throws when the string isn't parseable
 */
private static Object convertToPrimitiveWrapper(final Class fieldClass, final String stringValue)
        throws ParseException {
    Object parameter = null;

    if (fieldClass.equals(Boolean.class)) {
        parameter = Boolean.parseBoolean(stringValue);
    } else if (fieldClass.equals(Byte.class)) {
        parameter = Byte.parseByte(stringValue);
    } else if (fieldClass.equals(Double.class)) {
        parameter = Double.parseDouble(stringValue);
    } else if (fieldClass.equals(Float.class)) {
        parameter = Float.parseFloat(stringValue);
    } else if (fieldClass.equals(Integer.class)) {
        parameter = Integer.parseInt(stringValue);
    } else if (fieldClass.equals(Long.class)) {
        parameter = Long.parseLong(stringValue);
    } else if (fieldClass.equals(Short.class)) {
        parameter = Short.parseShort(stringValue);
    }

    return parameter;
}

From source file:NumberUtils.java

/**
 * Convert the given number into an instance of the given target class.
 * @param number the number to convert//from  w  ww. j  a v a2 s. c om
 * @param targetClass the target class to convert to
 * @return the converted number
 * @throws IllegalArgumentException if the target class is not supported
 * (i.e. not a standard Number subclass as included in the JDK)
 * @see java.lang.Byte
 * @see java.lang.Short
 * @see java.lang.Integer
 * @see java.lang.Long
 * @see java.math.BigInteger
 * @see java.lang.Float
 * @see java.lang.Double
 * @see java.math.BigDecimal
 */
public static Number convertNumberToTargetClass(Number number, Class<?> targetClass)
        throws IllegalArgumentException {

    //   Assert.notNull(number, "Number must not be null");
    //   Assert.notNull(targetClass, "Target class must not be null");

    if (targetClass.isInstance(number)) {
        return number;
    } else if (targetClass.equals(Byte.class)) {
        long value = number.longValue();
        if (value < Byte.MIN_VALUE || value > Byte.MAX_VALUE) {
            raiseOverflowException(number, targetClass);
        }
        return new Byte(number.byteValue());
    } else if (targetClass.equals(Short.class)) {
        long value = number.longValue();
        if (value < Short.MIN_VALUE || value > Short.MAX_VALUE) {
            raiseOverflowException(number, targetClass);
        }
        return new Short(number.shortValue());
    } else if (targetClass.equals(Integer.class)) {
        long value = number.longValue();
        if (value < Integer.MIN_VALUE || value > Integer.MAX_VALUE) {
            raiseOverflowException(number, targetClass);
        }
        return new Integer(number.intValue());
    } else if (targetClass.equals(Long.class)) {
        return new Long(number.longValue());
    } else if (targetClass.equals(Float.class)) {
        return new Float(number.floatValue());
    } else if (targetClass.equals(Double.class)) {
        return new Double(number.doubleValue());
    } else if (targetClass.equals(BigInteger.class)) {
        return BigInteger.valueOf(number.longValue());
    } else if (targetClass.equals(BigDecimal.class)) {
        // using BigDecimal(String) here, to avoid unpredictability of BigDecimal(double)
        // (see BigDecimal javadoc for details)
        return new BigDecimal(number.toString());
    } else {
        throw new IllegalArgumentException("Could not convert number [" + number + "] of type ["
                + number.getClass().getName() + "] to unknown target class [" + targetClass.getName() + "]");
    }
}

From source file:hu.bme.mit.sette.tools.spf.SpfParser.java

static ParameterType getParameterType(Class<?> javaClass) {
    if (javaClass.isPrimitive()) {
        javaClass = ClassUtils.primitiveToWrapper(javaClass);
    }/*from w  w  w  .  j  ava  2s  .  c  om*/

    if (javaClass.equals(Byte.class)) {
        return ParameterType.BYTE;
    } else if (javaClass.equals(Short.class)) {
        return ParameterType.SHORT;
    } else if (javaClass.equals(Integer.class)) {
        return ParameterType.INT;
    } else if (javaClass.equals(Long.class)) {
        return ParameterType.LONG;
    } else if (javaClass.equals(Float.class)) {
        return ParameterType.FLOAT;
    } else if (javaClass.equals(Double.class)) {
        return ParameterType.DOUBLE;
    } else if (javaClass.equals(Boolean.class)) {
        return ParameterType.BOOLEAN;
    } else if (javaClass.equals(Character.class)) {
        return ParameterType.CHAR;
    } else {
        // string or null
        return ParameterType.EXPRESSION;
    }
}

From source file:hu.bme.mit.sette.tools.spf.SpfParser.java

static String getDefaultParameterString(Class<?> javaClass) {
    if (javaClass.isPrimitive()) {
        javaClass = ClassUtils.primitiveToWrapper(javaClass);
    }/*from  w w  w.  j  ava  2 s  .  c  om*/

    if (javaClass.equals(Byte.class)) {
        return "1";
    } else if (javaClass.equals(Short.class)) {
        return "1";
    } else if (javaClass.equals(Integer.class)) {
        return "1";
    } else if (javaClass.equals(Long.class)) {
        return "1";
    } else if (javaClass.equals(Float.class)) {
        return "1.0";
    } else if (javaClass.equals(Double.class)) {
        return "1.0";
    } else if (javaClass.equals(Boolean.class)) {
        return "false";
    } else if (javaClass.equals(Character.class)) {
        return " ";
    } else {
        // string or null
        return "null";
    }
}

From source file:com.qpark.maven.plugin.flowmapper.TabularMappingTypeGenerator.java

private static String getValueInstance(final Class<?> valueClass, final String valueDefinition) {
    final StringBuffer sb = new StringBuffer(256);
    if (valueClass.equals(String.class)) {
        sb.append(valueDefinition);//from w  w  w.  ja  v  a  2 s  .c o  m
    } else if (valueClass.equals(BigDecimal.class)) {
        sb.append("new java.math.BigDecimal(");
        sb.append(valueDefinition);
        sb.append(".trim())");
    } else if (valueClass.equals(BigInteger.class)) {
        sb.append("new java.math.BigInteger(");
        sb.append(valueDefinition);
        sb.append(".trim())");
    } else {
        sb.append(valueClass.getName());
        sb.append(".valueOf(");
        sb.append(valueDefinition);
        sb.append(".trim())");
    }
    return sb.toString();
}

From source file:org.openlmis.fulfillment.testutils.DtoGenerator.java

private static Object generateValue(Class<?> type, Class<?> propertyType) {
    Object value = generateBaseValue(Primitives.wrap(propertyType));
    value = null == value ? generateCollectionValue(propertyType) : value;
    value = null == value ? generateCustomValue(propertyType) : value;

    if (null != value) {
        return value;
    }// ww  w  . ja  va2s.c  o  m

    if (type.equals(propertyType)) {
        // if types are equals it means that the given DTO contains a element which is represent as
        // a child or parent. For now we return null.
        return null;
    }

    return of(propertyType);
}