Example usage for org.apache.commons.lang StringUtils length

List of usage examples for org.apache.commons.lang StringUtils length

Introduction

In this page you can find the example usage for org.apache.commons.lang StringUtils length.

Prototype

public static int length(String str) 

Source Link

Document

Gets a String's length or 0 if the String is null.

Usage

From source file:org.projectforge.framework.persistence.attr.entities.DefaultBaseWithAttrDO.java

public void putAttrInternal(final String key, final Character type, final String encodedString) {

    final JpaTabAttrBaseDO<M, Integer> tabr = getAttributeRow(key);
    final Class<?> required = StringUtils.length(encodedString) > JpaTabAttrDataBaseDO.DATA_MAXLENGTH
            ? getAttrEntityWithDataClass()
            : getAttrEntityClass();/*ww w . j  ava  2  s  .  co m*/

    if (tabr != null && required == tabr.getClass()) {
        tabr.setStringData(encodedString);
        tabr.setType(type);
        return;
    }

    if (StringUtils.length(encodedString) > JpaTabAttrDataBaseDO.DATA_MAXLENGTH) {
        putAttributeRow(key, createAttrEntityWithData(key, type, encodedString));
    } else {
        putAttributeRow(key, createAttrEntity(key, type, encodedString));
    }
}

From source file:reconf.client.constructors.SimpleConstructor.java

public Object construct(MethodData data) throws Throwable {
    if (data.hasAdapter()) {
        return data.getAdapter().adapt(data.getValue());
    }/*w ww .  j ava2 s .  c o m*/

    Class<?> returnClass = (Class<?>) data.getReturnType();

    String trimmed = StringUtils.defaultString(StringUtils.trim(data.getValue()));
    if (!trimmed.startsWith("'") || !trimmed.endsWith("'")) {
        throw new RuntimeException(msg.format("error.invalid.string", data.getValue(), data.getMethod()));
    }

    String wholeValue = StringUtils.substring(trimmed, 1, trimmed.length() - 1);

    if (String.class.equals(returnClass)) {
        return wholeValue;
    }

    if (null == data.getValue()) {
        return null;
    }

    if (Object.class.equals(returnClass)) {
        returnClass = String.class;
    }

    if (char.class.equals(data.getReturnType()) || Character.class.equals(data.getReturnType())) {
        if (StringUtils.length(wholeValue) == 1) {
            return CharUtils.toChar(wholeValue);
        }
        return CharUtils.toChar(StringUtils.replace(wholeValue, " ", ""));
    }

    if (primitiveBoxing.containsKey(returnClass)) {
        if (StringUtils.isBlank(wholeValue)) {
            return null;
        }
        Method parser = primitiveBoxing.get(returnClass).getMethod(
                "parse" + StringUtils.capitalize(returnClass.getSimpleName()), new Class<?>[] { String.class });
        return parser.invoke(primitiveBoxing.get(returnClass), new Object[] { StringUtils.trim(wholeValue) });
    }

    Method valueOf = null;
    try {
        valueOf = returnClass.getMethod("valueOf", new Class<?>[] { String.class });
    } catch (NoSuchMethodException ignored) {
    }

    if (valueOf == null) {
        try {

            valueOf = returnClass.getMethod("valueOf", new Class<?>[] { Object.class });
        } catch (NoSuchMethodException ignored) {
        }
    }

    if (null != valueOf) {
        if (StringUtils.isEmpty(wholeValue)) {
            return null;
        }
        return valueOf.invoke(data.getReturnType(), new Object[] { StringUtils.trim(wholeValue) });
    }

    Constructor<?> constructor = null;

    try {
        constructor = returnClass.getConstructor(String.class);
        constructor.setAccessible(true);

    } catch (NoSuchMethodException ignored) {
        throw new IllegalStateException(
                msg.format("error.string.constructor", returnClass.getSimpleName(), data.getMethod()));
    }

    try {
        return constructor.newInstance(wholeValue);

    } catch (Exception e) {
        if (e.getCause() != null && e.getCause() instanceof NumberFormatException) {
            return constructor.newInstance(StringUtils.trim(wholeValue));
        }
        throw e;
    }
}