Package com.alibaba.toolkit.util.typeconvert

Examples of com.alibaba.toolkit.util.typeconvert.ConvertFailedException


        } catch (ConvertFailedException e) {
            if (e.isDefaultValueSet()) {
                enumObj = Enum.getEnumByValue(targetType, e.getDefaultValue());

                if (enumObj != null) {
                    throw new ConvertFailedException(e).setDefaultValue(enumObj);
                }
            }

            return chain.convert(value);
        }

        if (enumObj == null) {
            throw new ConvertFailedException();
        }

        return enumObj;
    }
View Full Code Here


        FlagSet flagSet = null;

        try {
            flagSet = (FlagSet) targetType.newInstance();
        } catch (Exception e) {
            return new ConvertFailedException();
        }

        try {
            Object flagSetValue = chain.getConvertManager()
                                       .asTypeWithoutDefaultValue(flagSet.getUnderlyingClass(),
                                                                  value);

            flagSet.setValue(flagSetValue);
        } catch (ConvertFailedException e) {
            if (e.isDefaultValueSet()) {
                flagSet.setValue(e.getDefaultValue());
                throw new ConvertFailedException(e).setDefaultValue(flagSet);
            }

            return chain.convert(value);
        }
View Full Code Here

public class SqlDateConverter implements Converter {
    protected static final Date DEFAULT_VALUE = null;

    public Object convert(Object value, ConvertChain chain) {
        if (value == null) {
            throw new ConvertFailedException().setDefaultValue(DEFAULT_VALUE);
        }

        if (value instanceof Date) {
            return value;
        }

        if (value instanceof String) {
            String strValue = ((String) value).trim();

            try {
                return Date.valueOf(strValue);
            } catch (IllegalArgumentException e) {
                if (strValue.length() > 0) {
                    throw new ConvertFailedException(e);
                }

                throw new ConvertFailedException().setDefaultValue(DEFAULT_VALUE);
            }
        }

        return chain.convert(value);
    }
View Full Code Here

public class SqlTimeConverter implements Converter {
    protected static final Time DEFAULT_VALUE = null;

    public Object convert(Object value, ConvertChain chain) {
        if (value == null) {
            throw new ConvertFailedException().setDefaultValue(DEFAULT_VALUE);
        }

        if (value instanceof Time) {
            return value;
        }

        if (value instanceof String) {
            String strValue = ((String) value).trim();

            try {
                return Time.valueOf(strValue);
            } catch (IllegalArgumentException e) {
                if (strValue.length() > 0) {
                    throw new ConvertFailedException(e);
                }

                throw new ConvertFailedException().setDefaultValue(DEFAULT_VALUE);
            }
        }

        return chain.convert(value);
    }
View Full Code Here

public class BigIntegerConverter implements Converter {
    protected static final BigInteger DEFAULT_VALUE = BigInteger.ZERO;

    public Object convert(Object value, ConvertChain chain) {
        if (value == null) {
            throw new ConvertFailedException().setDefaultValue(DEFAULT_VALUE);
        }

        if (value instanceof BigInteger) {
            return value;
        }

        if (value instanceof BigDecimal) {
            return ((BigDecimal) value).toBigInteger();
        }

        if (value instanceof Number) {
            return new BigDecimal(value.toString()).toBigInteger();
        }

        if (value instanceof String) {
            String strValue = ((String) value).trim();

            try {
                return new BigInteger(strValue);
            } catch (NumberFormatException e) {
                if (strValue.length() > 0) {
                    throw new ConvertFailedException(e);
                }

                throw new ConvertFailedException().setDefaultValue(DEFAULT_VALUE);
            }
        }

        return chain.convert(value);
    }
View Full Code Here

public class ShortConverter implements Converter {
    protected static final Short DEFAULT_VALUE = new Short((short) 0);

    public Object convert(Object value, ConvertChain chain) {
        if (value == null) {
            throw new ConvertFailedException().setDefaultValue(DEFAULT_VALUE);
        }

        if (value instanceof Short) {
            return value;
        }

        if (value instanceof Number) {
            return new Short(((Number) value).shortValue());
        }

        if (value instanceof String) {
            String strValue = ((String) value).trim();

            try {
                return Short.decode(strValue);
            } catch (NumberFormatException e) {
                if (strValue.length() > 0) {
                    throw new ConvertFailedException(e);
                }

                throw new ConvertFailedException().setDefaultValue(DEFAULT_VALUE);
            }
        }

        return chain.convert(value);
    }
View Full Code Here

public class SqlTimestampConverter implements Converter {
    protected static final Timestamp DEFAULT_VALUE = null;

    public Object convert(Object value, ConvertChain chain) {
        if (value == null) {
            throw new ConvertFailedException().setDefaultValue(DEFAULT_VALUE);
        }

        if (value instanceof Timestamp) {
            return value;
        }

        if (value instanceof String) {
            String strValue = ((String) value).trim();

            try {
                return Timestamp.valueOf(strValue);
            } catch (IllegalArgumentException e) {
                if (strValue.length() > 0) {
                    throw new ConvertFailedException(e);
                }

                throw new ConvertFailedException().setDefaultValue(DEFAULT_VALUE);
            }
        }

        return chain.convert(value);
    }
View Full Code Here

public class StringConverter implements Converter {
    protected static final String DEFAULT_VALUE = "";

    public Object convert(Object value, ConvertChain chain) {
        if (value == null) {
            throw new ConvertFailedException().setDefaultValue(DEFAULT_VALUE);
        }

        if (value instanceof String) {
            return value;
        }
View Full Code Here

public class BigDecimalConverter implements Converter {
    protected static final BigDecimal DEFAULT_VALUE = new BigDecimal(BigInteger.ZERO);

    public Object convert(Object value, ConvertChain chain) {
        if (value == null) {
            throw new ConvertFailedException().setDefaultValue(DEFAULT_VALUE);
        }

        if (value instanceof BigDecimal) {
            return value;
        }

        if (value instanceof BigInteger) {
            return new BigDecimal((BigInteger) value);
        }

        if (value instanceof Number) {
            return new BigDecimal(value.toString());
        }

        if (value instanceof String) {
            String strValue = ((String) value).trim();

            try {
                return new BigDecimal(strValue);
            } catch (NumberFormatException e) {
                if (strValue.length() > 0) {
                    throw new ConvertFailedException(e);
                }

                throw new ConvertFailedException().setDefaultValue(DEFAULT_VALUE);
            }
        }

        return chain.convert(value);
    }
View Full Code Here

        "n"
    }));

    public Object convert(Object value, ConvertChain chain) {
        if (value == null) {
            throw new ConvertFailedException().setDefaultValue(DEFAULT_VALUE);
        }

        if (value instanceof Boolean) {
            return value;
        }

        if (value instanceof Number) {
            return (Math.abs(((Number) value).doubleValue()) < Float.MIN_VALUE)
                       ? Boolean.FALSE
                       : Boolean.TRUE;
        }

        if (value instanceof String) {
            String strValue = ((String) value).trim();

            try {
                return (Integer.decode(strValue).intValue() == 0) ? Boolean.FALSE
                                                                  : Boolean.TRUE;
            } catch (NumberFormatException e) {
                if (strValue.length() == 0) {
                    throw new ConvertFailedException().setDefaultValue(DEFAULT_VALUE);
                }

                strValue = strValue.toLowerCase();

                if (TRUE_STRINGS.contains(strValue)) {
                    return Boolean.TRUE;
                }

                if (FALSE_STRINGS.contains(strValue)) {
                    return Boolean.FALSE;
                }

                throw new ConvertFailedException(e);
            }
        }

        return chain.convert(value);
    }
View Full Code Here

TOP

Related Classes of com.alibaba.toolkit.util.typeconvert.ConvertFailedException

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.