public static Maybe castPrimitiveMaybe()

in utils/common/src/main/java/org/apache/brooklyn/util/javalang/coerce/PrimitiveStringTypeCoercions.java [91:183]


    public static <T> Maybe<T> castPrimitiveMaybe(Object value, Class<T> targetType) {
        if (value==null) return null;
        assert isPrimitiveOrBoxer(targetType) : "targetType="+targetType;
        assert isPrimitiveOrBoxer(value.getClass()) : "value="+targetType+"; valueType="+value.getClass();

        Class<?> sourceWrapType = Primitives.wrap(value.getClass());
        Class<?> targetWrapType = Primitives.wrap(targetType);
        
        // optimization, for when already correct type
        if (sourceWrapType == targetWrapType) {
            return Maybe.of((T) value);
        }
        
        if (targetWrapType == Boolean.class) {
            // only char can be mapped to boolean
            // (we could say 0=false, nonzero=true, but there is no compelling use case so better
            // to encourage users to write as boolean)
            if (sourceWrapType == Character.class)
                return stringToPrimitiveMaybe(value.toString(), targetType);
            
            return Maybe.absent(new ClassCoercionException("Cannot cast "+sourceWrapType+" ("+value+") to "+targetType));
        } else if (sourceWrapType == Boolean.class) {
            // boolean can't cast to anything else
            
            return Maybe.absent(new ClassCoercionException("Cannot cast "+sourceWrapType+" ("+value+") to "+targetType));
        }

        try {
            // for whole-numbers (where casting to long won't lose anything)...
            long v = 0;
            boolean islong = true;
            if (sourceWrapType == Character.class) {
                v = ((Character) value).charValue();
            } else if (sourceWrapType == Byte.class) {
                v = ((Byte) value).byteValue();
            } else if (sourceWrapType == Short.class) {
                v = ((Short) value).shortValue();
            } else if (sourceWrapType == Integer.class) {
                v = ((Integer) value).intValue();
            } else if (sourceWrapType == Long.class) {
                v = ((Long) value).longValue();
            } else {
                islong = false;
            }
            if (islong) {
                if (targetWrapType == Character.class) return Maybe.of((T) Character.valueOf((char) v));
                if (targetWrapType == Byte.class) return Maybe.of((T) (Byte) Byte.parseByte("" + v));
                if (targetWrapType == Short.class) return Maybe.of((T) (Short) Short.parseShort("" + v));
                if (targetWrapType == Integer.class) return Maybe.of((T) (Integer) Integer.parseInt("" + v));
                if (targetWrapType == Long.class) return Maybe.of((T) Long.valueOf(v));
                if (targetWrapType == Float.class) return Maybe.of((T) Float.valueOf(v));
                if (targetWrapType == Double.class) return Maybe.of((T) Double.valueOf(v));
                return Maybe.absent(new IllegalStateException("Unexpected: sourceType=" + sourceWrapType + "; targetType=" + targetWrapType));
            }

            // for real-numbers (cast to double)...
            double d = 0;
            boolean isdouble = true;
            if (sourceWrapType == Float.class) {
                d = ((Float) value).floatValue();
            } else if (sourceWrapType == Double.class) {
                d = ((Double) value).doubleValue();
            } else {
                isdouble = false;
            }

            if (isdouble) {
                if (targetWrapType == Double.class) return Maybe.of((T) Double.valueOf(d));

                BigDecimal dd = BigDecimal.valueOf(d);
                if (targetWrapType == Float.class) {
                    float candidate = (float) d;
                    if (dd.subtract(BigDecimal.valueOf(candidate)).abs().compareTo(BigDecimal.valueOf(CommonAdaptorTypeCoercions.DELTA_FOR_COERCION)) > 0) {
                        throw new IllegalStateException("Decimal value out of range; cannot convert " + candidate + " to float");
                    }
                    return Maybe.of((T) (Float) candidate);
                }

                if (targetWrapType == Integer.class) return Maybe.of((T) Integer.valueOf((dd.intValueExact())));
                if (targetWrapType == Long.class) return Maybe.of((T) Long.valueOf(dd.longValueExact()));
                if (targetWrapType == Short.class) return Maybe.of((T) Short.valueOf(dd.shortValueExact()));
                if (targetWrapType == Byte.class) return Maybe.of((T) Byte.valueOf(dd.byteValueExact()));

                if (targetWrapType == Character.class)
                    return Maybe.of((T) Character.valueOf((char) dd.intValueExact()));
            }
        } catch (Exception e) {
            Exceptions.propagateIfFatal(e);
            return Maybe.absent(new IllegalStateException("Unexpected error: sourceType="+sourceWrapType+"; targetType="+targetWrapType+": "+Exceptions.collapseText(e), e));
        }

        return Maybe.absent(new IllegalStateException("Unexpected: sourceType="+sourceWrapType+"; targetType="+targetWrapType));
    }