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

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

Introduction

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

Prototype

public static Object convert(String values[], Class clazz) 

Source Link

Document

Convert an array of specified values to an array of objects of the specified class (if possible).

For more details see ConvertUtilsBean.

Usage

From source file:com.tdclighthouse.prototype.utils.Configuration.java

@SuppressWarnings("unchecked")
public <T> T getItem(String key, T defaultValue, Class<T> clazz) {
    T result = defaultValue;//  ww w.  j av  a 2 s .c o  m
    String stringValue = properties.getProperty(key);
    Object converted = ConvertUtils.convert(stringValue, clazz);
    if (clazz.isAssignableFrom(converted.getClass())) {
        result = (T) converted;
    } else {
        LOG.error(
                "the given value \"{}\" of the key \"{}\" is not convertable to {}. So we default back to the default value",
                stringValue, key, clazz.getSimpleName());
    }
    return result;
}

From source file:common.util.PropertyFilter.java

/**
 * @param filterName/*from  w  ww.j a  v  a2  s  .  c o  m*/
 * @param value
 */
public PropertyFilter(final String filterName, final String value) {

    String matchTypeStr;
    String matchPattenCode = LikeMatchPatten.ALL.toString();
    String matchTypeCode;
    String propertyTypeCode;

    if (filterName.contains("LIKE") && filterName.charAt(0) != 'L') {
        matchTypeStr = StringUtils.substringBefore(filterName, PARAM_PREFIX);
        matchPattenCode = StringUtils.substring(matchTypeStr, 0, 1);
        matchTypeCode = StringUtils.substring(matchTypeStr, 1, matchTypeStr.length() - 1);
        propertyTypeCode = StringUtils.substring(matchTypeStr, matchTypeStr.length() - 1,
                matchTypeStr.length());
    } else {
        matchTypeStr = StringUtils.substringBefore(filterName, PARAM_PREFIX);
        matchTypeCode = StringUtils.substring(matchTypeStr, 0, matchTypeStr.length() - 1);
        propertyTypeCode = StringUtils.substring(matchTypeStr, matchTypeStr.length() - 1,
                matchTypeStr.length());
    }

    try {
        matchType = Enum.valueOf(MatchType.class, matchTypeCode);
        likeMatchPatten = Enum.valueOf(LikeMatchPatten.class, matchPattenCode);
    } catch (RuntimeException e) {
        throw new IllegalArgumentException("filter name: " + filterName
                + "Not prepared in accordance with rules, not get more types of property.", e);
    }

    try {
        propertyType = Enum.valueOf(PropertyType.class, propertyTypeCode).getValue();
    } catch (RuntimeException e) {
        throw new IllegalArgumentException("filter name: " + filterName
                + "Not prepared in accordance with the rules, attribute value types can not be.", e);
    }

    String propertyNameStr = StringUtils.substringAfter(filterName, PARAM_PREFIX);
    propertyNames = StringUtils.split(propertyNameStr, PropertyFilter.OR_SEPARATOR);

    Validate.isTrue(propertyNames.length > 0, "filter name: " + filterName
            + "Not prepared in accordance with the rules, property names can not be.");

    this.propertyValue = ConvertUtils.convert(value, propertyType);
}

From source file:com.iorga.webappwatcher.util.BasicParameterHandler.java

@SuppressWarnings("unchecked")
protected V convertFromString(final String value) {
    return (V) ConvertUtils.convert(value, getFieldClass());
}

From source file:cn.guoyukun.spring.jpa.repository.hibernate.type.CollectionToStringUserType.java

/**
 * JDBC ResultSet??,??/*from w w w .j a va  2 s.  c  o m*/
 * (?null?)
 * names????
 *
 * @param names
 * @param owner
 * @return
 * @throws org.hibernate.HibernateException
 * @throws java.sql.SQLException
 */
@Override
public Object nullSafeGet(ResultSet rs, String[] names, SessionImplementor session, Object owner)
        throws HibernateException, SQLException {
    String valueStr = rs.getString(names[0]);
    if (StringUtils.isEmpty(valueStr)) {
        return newCollection();
    }

    String[] values = StringUtils.split(valueStr, separator);

    Collection result = newCollection();

    for (String value : values) {
        if (StringUtils.isNotEmpty(value)) {
            result.add(ConvertUtils.convert(value, elementType));
        }
    }
    return result;

}

From source file:com.sunlights.common.utils.PropertyFilter.java

/**
 * @param filterName/*from  w  ww  .j a va  2  s .c o  m*/
 * @param value
 */
public PropertyFilter(final String filterName, final String value) {

    String matchTypeStr;
    String matchPattenCode = LikeMatchPatten.ALL.toString();
    String matchTypeCode;
    String propertyTypeCode;

    if (filterName.contains("LIKE") && filterName.charAt(0) != 'L') {
        matchTypeStr = StringUtils.substringBefore(filterName, PARAM_PREFIX);
        matchPattenCode = StringUtils.substring(matchTypeStr, 0, 1);
        matchTypeCode = StringUtils.substring(matchTypeStr, 1, matchTypeStr.length() - 1);
        propertyTypeCode = StringUtils.substring(matchTypeStr, matchTypeStr.length() - 1,
                matchTypeStr.length());
    } else {
        matchTypeStr = StringUtils.substringBefore(filterName, PARAM_PREFIX);
        matchTypeCode = StringUtils.substring(matchTypeStr, 0, matchTypeStr.length() - 1);
        propertyTypeCode = StringUtils.substring(matchTypeStr, matchTypeStr.length() - 1,
                matchTypeStr.length());
    }

    try {
        matchType = Enum.valueOf(MatchType.class, matchTypeCode);
        likeMatchPatten = Enum.valueOf(LikeMatchPatten.class, matchPattenCode);
    } catch (RuntimeException e) {
        throw new IllegalArgumentException("filter name: " + filterName
                + "Not prepared in accordance with rules, not get more types of property.", e);
    }

    try {
        propertyType = Enum.valueOf(PropertyType.class, propertyTypeCode).getValue();
    } catch (RuntimeException e) {
        throw new IllegalArgumentException("filter name: " + filterName
                + "Not prepared in accordance with the rules, attribute value types can not be.", e);
    }

    String propertyNameStr = StringUtils.substringAfter(filterName, PARAM_PREFIX);
    propertyNames = StringUtils.split(propertyNameStr, PropertyFilter.OR_SEPARATOR);

    Validate.isTrue(propertyNames.length > 0, "filter name: " + filterName
            + "Not prepared in accordance with the rules, property names can not be.");
    this.propertyValue = ConvertUtils.convert(value, propertyType);

}

From source file:io.github.benas.easyproperties.PropertyInjector.java

private <A extends Annotation> void doInjectProperty(Field field, Object object, A annotation,
        AnnotationProcessor<A> annotationProcessor) throws PropertyInjectionException {
    try {/*  w  w w.  j  a  v  a  2 s.  co m*/
        Object value = annotationProcessor.processAnnotation(annotation, field);
        if (value != null) {
            Object typedValue = ConvertUtils.convert(value, field.getType());
            PropertyUtils.setProperty(object, field.getName(), typedValue);
        }
    } catch (Exception e) {
        throw new PropertyInjectionException(
                format("Unable to inject value from annotation '%s' in field '%s' of object '%s'", annotation,
                        field.getName(), object),
                e);
    }
}

From source file:com.aistor.modules.cms.service.LinkService.java

/**
 * ??/* w w w.j  a  v  a2s  .  c o  m*/
 */
public List<Object[]> findByIds(String ids) {
    List<Object[]> list = Lists.newArrayList();
    Long[] idss = (Long[]) ConvertUtils.convert(StringUtils.split(ids, ","), Long.class);
    if (idss.length > 0) {
        List<Link> l = linkDao.findByIdIn(idss);
        for (Link e : l) {
            list.add(new Object[] { e.getId(), StringUtils.abbr(e.getTitle(), 50) });
        }
    }
    return list;
}

From source file:com.siberhus.tdfl.transform.DefaultFieldSet.java

@SuppressWarnings("unchecked")
@Override//from   w w  w.ja va  2s.com
public <D> D read(Class<D> clazz, int index) {
    String value = readString(index);
    if (value == null) {
        return null;
    }
    return (D) ConvertUtils.convert(value, clazz);
}

From source file:bard.pubchem.model.PCAssayResult.java

public Object getValue(PCAssayColumn column) {
    Object obj = ConvertUtils.convert(getAllValues().get(column.getTID() - 1), column.getTypeClass());
    return obj;
}

From source file:cn.fql.utility.ClassUtility.java

/**
 * Import value to object according specified  <code>org.xml.sax.Attributes</code>
 *
 * @param obj  specified object instance
 * @param atts <code>org.xml.sax.Attributes</code>
 */// ww  w  . jav  a2s.co  m
public static void importValueFromAttribute(Object obj, org.xml.sax.Attributes atts) {
    if (atts != null) {
        PropertyDescriptor[] pds;
        try {
            pds = exportPropertyDesc(obj.getClass());
            if (pds != null && pds.length > 0) {
                for (int i = 0; i < pds.length; i++) {
                    PropertyDescriptor pd = pds[i];
                    String strValue = atts.getValue(pd.getName());
                    if (strValue != null) {
                        Method setter = pd.getWriteMethod();
                        if (setter != null) {
                            Object value = ConvertUtils.convert(strValue, pd.getPropertyType());
                            Object[] params = { value };
                            setter.invoke(obj, params);
                        }
                    }
                }
            }
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }
}