Example usage for org.apache.commons.lang.math NumberUtils stringToInt

List of usage examples for org.apache.commons.lang.math NumberUtils stringToInt

Introduction

In this page you can find the example usage for org.apache.commons.lang.math NumberUtils stringToInt.

Prototype

public static int stringToInt(String str, int defaultValue) 

Source Link

Document

Convert a String to an int, returning a default value if the conversion fails.

If the string is null, the default value is returned.

 NumberUtils.stringToInt(null, 1) = 1 NumberUtils.stringToInt("", 1)   = 1 NumberUtils.stringToInt("1", 0)  = 1 

Usage

From source file:org.objectstyle.cayenne.dataview.DataView.java

private void loadField(ObjEntityView entityView, Element element) {
    String name = element.getAttributeValue("name");
    ObjEntityViewField field = new ObjEntityViewField();
    field.setName(name);/*  w  w w  . ja  v  a 2 s  . c om*/
    String prefIndex = element.getAttributeValue("pref-index");
    field.setPreferredIndex(NumberUtils.stringToInt(prefIndex, -1));
    entityView.insertField(field);

    String calcType = element.getAttributeValue("calc-type");
    Validate.notNull(calcType);
    CalcTypeEnum fieldCalcType = CalcTypeEnum.getEnum(calcType);
    Validate.isTrue(
            CalcTypeEnum.NO_CALC_TYPE.equals(fieldCalcType) || CalcTypeEnum.LOOKUP_TYPE.equals(fieldCalcType),
            "Calc Type not supported yet: ", fieldCalcType);
    field.setCalcType(fieldCalcType);

    ObjEntity objEntity = entityView.getObjEntity();

    if (CalcTypeEnum.NO_CALC_TYPE.equals(fieldCalcType)) {
        String objAttributeName = element.getAttributeValue("obj-attribute-name");
        Validate.notNull(objAttributeName);
        ObjAttribute objAttribute = (ObjAttribute) objEntity.getAttribute(objAttributeName);
        field.setObjAttribute(objAttribute);
    } else if (CalcTypeEnum.LOOKUP_TYPE.equals(fieldCalcType)) {
        String objRelationshipName = element.getAttributeValue("obj-relationship-name");
        Validate.notNull(objRelationshipName);
        ObjRelationship objRelationship = (ObjRelationship) objEntity.getRelationship(objRelationshipName);
        field.setObjRelationship(objRelationship);
        Element lookupElement = element.getChild("lookup");
        Validate.notNull(lookupElement);
        String lookupEntityView = lookupElement.getAttributeValue("obj-entity-view-name");
        Validate.notNull(lookupEntityView);
        String lookupEntityField = lookupElement.getAttributeValue("field-name");
        Validate.notNull(lookupEntityField);
        String[] lookupDescriptor = new String[] { lookupEntityView, lookupEntityField };
        lookupReferenceTable.put(field, lookupDescriptor);
    }

    String dataType = element.getAttributeValue("data-type");
    Validate.notNull(dataType);
    field.setDataType(dataTypeSpec.getDataType(dataType));

    String editable = element.getAttributeValue("editable");
    field.setEditable(BooleanUtils.toBoolean(editable));

    String visible = element.getAttributeValue("visible");
    field.setVisible(BooleanUtils.toBoolean(visible));

    Element captionElement = element.getChild("caption");
    if (captionElement != null)
        field.setCaption(StringUtils.stripToEmpty(captionElement.getText()));

    Element editFormatElement = element.getChild("edit-format");
    if (editFormatElement != null) {
        String formatClassName = editFormatElement.getAttributeValue("class");
        Validate.notNull(formatClassName);
        Class formatClass;
        try {
            formatClass = Class.forName(formatClassName);
            Map parameters = DataView.childrenToMap(editFormatElement);
            Format format = formatFactory.createFormat(formatClass, locale, parameters);
            field.setEditFormat(format);
        } catch (ClassNotFoundException ex) {
        }
    }

    Element displayFormatElement = element.getChild("display-format");
    if (displayFormatElement != null) {
        String formatClassName = displayFormatElement.getAttributeValue("class");
        Validate.notNull(formatClassName);
        Class formatClass;
        try {
            formatClass = Class.forName(formatClassName);
            Map parameters = DataView.childrenToMap(displayFormatElement);
            Format format = formatFactory.createFormat(formatClass, locale, parameters);
            field.setDisplayFormat(format);
        } catch (ClassNotFoundException ex) {
        }
    }

    Element defaultValueElement = element.getChild("default-value");
    if (defaultValueElement != null) {
        String defaultValueStr = StringUtils.stripToEmpty(defaultValueElement.getText());
        Object defaultValue = dataTypeSpec.create(field.getDataType(), defaultValueStr);
        field.setDefaultValue(defaultValue);
    }
}