Example usage for org.apache.commons.beanutils ConvertUtils lookup

List of usage examples for org.apache.commons.beanutils ConvertUtils lookup

Introduction

In this page you can find the example usage for org.apache.commons.beanutils ConvertUtils lookup.

Prototype

public static Converter lookup(Class clazz) 

Source Link

Document

Look up and return any registered Converter for the specified destination class; if there is no registered Converter, return null.

For more details see ConvertUtilsBean.

Usage

From source file:org.acmsl.commons.utils.ConversionUtils.java

/**
 * Converts given String to char, if given value is not null.
 * @param value the value to convert.// w ww. j  a va2  s .c om
 * @return the converted value.
 */
@Nullable
public Character toCharIfNotNull(@Nullable final String value) {
    Character result = null;

    @Nullable
    final Converter t_Converter = ConvertUtils.lookup(Character.TYPE);

    if (t_Converter != null) {
        @Nullable
        final Object t_Result = t_Converter.convert(Character.TYPE, value);

        if (t_Result instanceof Character) {
            result = (Character) t_Result;
        }
    }

    return result;
}

From source file:org.acmsl.commons.utils.ConversionUtils.java

/**
 * Converts given String to short, if given value is not null.
 * @param value the value to convert.//from   www  . j  a  va2s. c o  m
 * @return the converted value.
 */
@Nullable
public Short toShortIfNotNull(@Nullable final String value) {
    Short result = null;

    @Nullable
    final Converter t_Converter = ConvertUtils.lookup(Short.TYPE);

    if (t_Converter != null) {
        @Nullable
        final Object t_Result = t_Converter.convert(Short.TYPE, value);

        if (t_Result instanceof Short) {
            result = (Short) t_Result;
        }
    }

    return result;
}

From source file:org.acmsl.commons.utils.ConversionUtils.java

/**
 * Converts given String to byte, if given value is not null.
 * @param value the value to convert./*from   w  w w .  j  a  va  2 s.com*/
 * @return the converted value.
 */
@Nullable
public Byte toByteIfNotNull(@Nullable final String value) {
    Byte result = null;

    @Nullable
    final Converter t_Converter = ConvertUtils.lookup(Byte.TYPE);

    if (t_Converter != null) {
        @Nullable
        final Object t_Result = t_Converter.convert(Byte.TYPE, value);

        if (t_Result instanceof Byte) {
            result = (Byte) t_Result;
        }
    }

    return result;
}

From source file:org.acmsl.commons.utils.ConversionUtils.java

/**
 * Converts given String to BigDecimal, if given value is not null.
 * @param value the value to convert.//from  w  ww .  jav  a  2s .co  m
 * @return the converted value.
 */
@Nullable
public BigDecimal toBigDecimalIfNotNull(@Nullable final String value) {
    BigDecimal result = null;

    @Nullable
    final Converter t_Converter = ConvertUtils.lookup(BigDecimal.class);

    if (t_Converter != null) {
        @Nullable
        final Object t_Result = t_Converter.convert(BigDecimal.class, value);

        if (t_Result instanceof BigDecimal) {
            result = (BigDecimal) t_Result;
        }
    }

    return result;
}

From source file:org.acmsl.commons.utils.ConversionUtils.java

/**
 * Converts given String to date, if given value is not null.
 * @param value the value to convert./*from  w  w  w.  j a v  a  2 s .co m*/
 * @return the converted value.
 */
@Nullable
public Date toDateIfNotNull(@Nullable final String value) {
    Date result = null;

    @Nullable
    final Converter t_Converter = ConvertUtils.lookup(Date.class);

    if (t_Converter != null) {
        @Nullable
        final Object t_Result = t_Converter.convert(Date.class, value);

        if (t_Result instanceof Date) {
            result = (Date) t_Result;
        }
    }

    return result;
}

From source file:org.apache.bval.jsr.xml.ValidationMappingParser.java

private Object convertToResultType(Class<?> returnType, String value, String defaultPackage) {
    /**/* ww  w.  j  av a  2  s  .  c o m*/
     * Class is represented by the fully qualified class name of the class.
     * spec: Note that if the raw string is unqualified,
     * default package is taken into account.
     */
    if (returnType.equals(Class.class)) {
        value = toQualifiedClassName(value, defaultPackage);
    }
    if (Byte.class.equals(returnType) || byte.class.equals(returnType)) { // spec mandates it
        return Byte.parseByte(value);
    }
    if (Short.class.equals(returnType) || short.class.equals(returnType)) {
        return Short.parseShort(value);
    }
    if (Integer.class.equals(returnType) || int.class.equals(returnType)) {
        return Integer.parseInt(value);
    }
    if (Long.class.equals(returnType) || long.class.equals(returnType)) {
        return Long.parseLong(value);
    }
    if (Float.class.equals(returnType) || float.class.equals(returnType)) {
        return Float.parseFloat(value);
    }
    if (Double.class.equals(returnType) || double.class.equals(returnType)) {
        return Double.parseDouble(value);
    }
    if (Boolean.class.equals(returnType) || boolean.class.equals(returnType)) {
        return Boolean.parseBoolean(value);
    }
    if (Character.class.equals(returnType) || char.class.equals(returnType)) {
        if (value.length() > 1) {
            throw new IllegalArgumentException("a char has a length of 1");
        }
        return value.charAt(0);
    }

    /* Converter lookup */
    Converter converter = ConvertUtils.lookup(returnType);
    if (converter == null && returnType.isEnum()) {
        converter = EnumerationConverter.getInstance();
    }

    if (converter == null) {
        return converter;
    }
    return converter.convert(returnType, value);
}

From source file:org.eiichiro.bootleg.json.ValueTypeJsonSerializer.java

/**
 * Serializes the specified user-defined value type object to 
 * <code>JsonPrimitive</code> representation.
 */// w ww.j  a  va 2s .  c  om
public JsonElement serialize(T src, Type typeOfSrc, JsonSerializationContext context) {
    Class<?> type = Types.getRawType(typeOfSrc);
    Converter converter = ConvertUtils.lookup(type);

    if ((converter != null && converter instanceof AbstractConverter)) {
        String string = (String) ConvertUtils.convert(src, String.class);

        if (string != null) {
            return new JsonPrimitive(string);
        }
    }

    return new JsonPrimitive(src.toString());
}

From source file:org.eiichiro.bootleg.xml.ValueTypeXmlAdapter.java

/**
 * Marshals the specified user-defined value type object to single XML value 
 * string representation./*from w w w. jav  a  2s  . co m*/
 * 
 * @throws If the specified object is not a user-defined value type.
 */
@Override
public String marshal(BoundType v) throws Exception {
    Class<? extends Object> type = v.getClass();

    if (!Types.isUserDefinedValueType(type)) {
        throw new IllegalArgumentException("Type [" + type + "] must be an user-defined value type; "
                + "@XmlJavaTypeAdapter(ValueTypeXmlAdapter.class) "
                + "can be annotated to user-defined value type and field only");
    }

    Converter converter = ConvertUtils.lookup(type);

    if ((converter != null && converter instanceof AbstractConverter)) {
        String string = (String) ConvertUtils.convert(v, String.class);

        if (string != null) {
            return string;
        }
    }

    return v.toString();
}

From source file:org.onebusaway.csv_entities.schema.DefaultFieldMapping.java

public DefaultFieldMapping(Class<?> entityType, String csvFieldName, String objFieldName, Class<?> objFieldType,
        boolean required) {
    super(entityType, csvFieldName, objFieldName, required);
    _objFieldType = objFieldType;//from ww  w  . j  ava  2 s  .c  o m
    _converter = ConvertUtils.lookup(objFieldType);
    if (_converter == null && objFieldType.equals(Object.class))
        _converter = new DefaultConverter();
}

From source file:org.onebusaway.gtfs_transformer.deferred.DeferredValueSupport.java

/**
 * Returns a {@link Converter} that can convert values to the target value
 * type. If the target entity type + property has a custom converter defined
 * in the GTFS entity schema, we will use that as instead.
 * /*from   w ww  .  j a  v  a 2s . co m*/
 * @param targetEntityType the target entity type whose property will be
 *          updated.
 * @param targetPropertyName the target property name for the property that
 *          will be updated on the target entity.
 * @param targetValueType the target value type we wish to convert to
 */
public Converter resolveConverter(Class<?> targetEntityType, String targetPropertyName,
        Class<?> targetValueType) {
    SingleFieldMapping mapping = _schemaCache.getFieldMappingForCsvFieldName(targetEntityType,
            targetPropertyName);
    if (mapping == null) {
        mapping = _schemaCache.getFieldMappingForObjectFieldName(targetEntityType, targetPropertyName);
    }
    if (mapping != null) {
        if (mapping instanceof ConverterFactory) {
            ConverterFactory factory = (ConverterFactory) mapping;
            return factory.create(_reader.getContext());
        }
        if (mapping instanceof Converter) {
            return (Converter) mapping;
        }
    }
    return ConvertUtils.lookup(targetValueType);
}