Java Type Coerce coerceTypes(Class clazz, Object value)

Here you can find the source of coerceTypes(Class clazz, Object value)

Description

Coerce numeric types when mapping properties from nodes to entities.

License

Apache License

Parameter

Parameter Description
clazz the entity field type
value the property value

Return

converted value

Declaration

public static Object coerceTypes(Class clazz, Object value) 

Method Source Code

//package com.java2s;
/*/*from w ww .  j av a  2  s  .  c  o m*/
 * Copyright (c) 2002-2015 "Neo Technology,"
 * Network Engine for Objects in Lund AB [http://neotechnology.com]
 *
 * This product is licensed to you under the Apache License, Version 2.0 (the "License").
 * You may not use this product except in compliance with the License.
 *
 * This product may include a number of subcomponents with
 * separate copyright notices and license terms. Your use of the source
 * code for these subcomponents is subject to the terms and
 * conditions of the subcomponent's license, as noted in the LICENSE file.
 *
 */

public class Main {
    /**
     * Coerce numeric types when mapping properties from nodes to entities.
     * This deals with numeric types - Longs to ints, Doubles to floats, Integers to bytes.
     *
     * @param clazz the entity field type
     * @param value the property value
     * @return converted value
     */
    public static Object coerceTypes(Class clazz, Object value) {
        if (clazz.isPrimitive() && value == null) {
            return defaultForPrimitive(clazz, value);
        }
        if (value != null) {
            String className = clazz.getName();
            if ("int".equals(className) || Integer.class.equals(clazz)) {
                if (value.getClass().equals(Long.class)) {
                    Long longValue = (Long) value;
                    if (longValue < Integer.MIN_VALUE || longValue > Integer.MAX_VALUE) {
                        throw new IllegalArgumentException(
                                longValue + " cannot be cast to int without an overflow.");
                    }
                    return longValue.intValue();
                }
            }
            if ("float".equals(className) || (Float.class.equals(clazz))) {
                if (value.getClass().equals(Double.class)) {
                    Double dblValue = (Double) value;
                    if (dblValue < -(Float.MAX_VALUE) || dblValue > Float.MAX_VALUE) {
                        throw new IllegalArgumentException(
                                dblValue + " cannot be cast to float without an overflow.");
                    }
                    return dblValue.floatValue();
                }
                if (value.getClass().equals(Integer.class)) {
                    Integer intValue = (Integer) value;
                    if (intValue < -(Double.MAX_VALUE) || intValue > Double.MAX_VALUE) {
                        throw new IllegalArgumentException(
                                intValue + " cannot be cast to double without an overflow.");
                    }
                    return (float) intValue;
                }
            }
            if ("byte".equals(className) || Byte.class.equals(clazz)) {
                if (value.getClass().equals(Integer.class)) {
                    Integer intValue = (Integer) value;
                    if (intValue < Byte.MIN_VALUE || intValue > Byte.MAX_VALUE) {
                        throw new IllegalArgumentException(
                                intValue + " cannot be cast to byte without an overflow.");
                    }
                    return intValue.byteValue();
                }
            }
            if ("double".equals(className) || Double.class.equals(clazz)) {
                if (value.getClass().equals(Integer.class)) {
                    Integer intValue = (Integer) value;
                    if (intValue < -(Double.MAX_VALUE) || intValue > Double.MAX_VALUE) {
                        throw new IllegalArgumentException(
                                intValue + " cannot be cast to double without an overflow.");
                    }
                    return (double) intValue;
                }
                if (value.getClass().equals(Float.class)) {
                    Float floatValue = (Float) value;
                    return (double) floatValue;
                }
            }
            if ("long".equals(className) || Long.class.equals(clazz)) {
                if (value.getClass().equals(Integer.class)) {
                    Integer intValue = (Integer) value;
                    return (long) intValue;
                }
            }

            if ("short".equals(className) || Short.class.equals(clazz)) {
                if (value.getClass().equals(Long.class)) {
                    Long longValue = (Long) value;
                    if (longValue < Short.MIN_VALUE || longValue > Short.MAX_VALUE) {
                        throw new IllegalArgumentException(
                                longValue + " cannot be cast to short without an overflow.");
                    }
                    return longValue.shortValue();
                }
                if (value.getClass().equals(Integer.class)) {
                    Integer intValue = (Integer) value;
                    if (intValue < Short.MIN_VALUE || intValue > Short.MAX_VALUE) {
                        throw new IllegalArgumentException(
                                intValue + " cannot be cast to short without an overflow.");
                    }
                    return intValue.shortValue();
                }
            }
        }
        return value;
    }

    private static Object defaultForPrimitive(Class clazz, Object value) {
        String className = clazz.getName();
        if ("int".equals(className) || "byte".equals(className) || "short".equals(className)) {
            return 0;
        }
        if ("double".equals(className)) {
            return 0.0d;
        }
        if ("float".equals(className)) {
            return 0.0f;
        }
        if ("long".equals(className)) {
            return 0l;
        }
        return value;
    }
}

Related

  1. coerceTagValueInt(String tag, Object val)
  2. coerceToDouble(Object o)
  3. coerceToEntrySize(String s)
  4. coerceToString(Object object)
  5. coerceToType(Object objectIn, Class clazz)
  6. coerceTypes(Class clazz, Object value)