Example usage for org.springframework.beans.propertyeditors CustomBooleanEditor setAsText

List of usage examples for org.springframework.beans.propertyeditors CustomBooleanEditor setAsText

Introduction

In this page you can find the example usage for org.springframework.beans.propertyeditors CustomBooleanEditor setAsText.

Prototype

@Override
    public void setAsText(@Nullable String text) throws IllegalArgumentException 

Source Link

Usage

From source file:org.finra.dm.core.ArgumentParser.java

/**
 * Retrieves the argument value, if any, as a String object and converts it to a boolean value.
 *
 * @param option the option that we want to query for
 * @param defaultValue the default value to return if option is not set or missing an argument value
 *
 * @return the value of the argument converted to a boolean value or default value when the option is not set or missing an argument value
 * @throws ParseException if the value of the argument is an invalid boolean value
 *
 *///w w w .  ja  va  2 s.c o  m
@SuppressFBWarnings(value = "UWF_FIELD_NOT_INITIALIZED_IN_CONSTRUCTOR", justification = "This is a false positive. A null check is present.")
public Boolean getStringValueAsBoolean(Option option, Boolean defaultValue) throws ParseException {
    Boolean result;

    ensureCommandLineNotNull();
    String stringValue = getStringValue(option);

    if (StringUtils.isNotBlank(stringValue)) {
        // Use custom boolean editor without allowed empty strings to convert the value of the argument to a boolean value.
        CustomBooleanEditor customBooleanEditor = new CustomBooleanEditor(false);
        try {
            customBooleanEditor.setAsText(stringValue);
        } catch (IllegalArgumentException e) {
            ParseException parseException = new ParseException(e.getMessage());
            parseException.initCause(e);
            throw parseException;
        }
        result = (Boolean) customBooleanEditor.getValue();
    } else {
        result = defaultValue;
    }

    return result;
}

From source file:org.finra.herd.core.helper.ConfigurationHelper.java

/**
 * Gets a property value as a boolean./*from www  .  j av a  2  s . co  m*/
 *
 * @param configurationValue the boolean configuration value
 * @param environment the environment containing the property
 *
 * @return the boolean property value
 */
public Boolean getBooleanProperty(ConfigurationValue configurationValue, Environment environment) {
    String booleanStringValue = getProperty(configurationValue, environment);

    // Use custom boolean editor without allowed empty strings to convert the value of the argument to a boolean value.
    CustomBooleanEditor customBooleanEditor = new CustomBooleanEditor(false);
    try {
        customBooleanEditor.setAsText(booleanStringValue);
    } catch (IllegalArgumentException e) {
        logErrorAndThrowIllegalStateException(configurationValue, "boolean", booleanStringValue, e);
    }

    // Return the boolean value.
    return (Boolean) customBooleanEditor.getValue();
}

From source file:org.finra.herd.service.helper.StorageDaoHelper.java

/**
 * Gets attribute value by name from the storage entity and returns it as a boolean. Most types of boolean strings are supported (e.g. true/false, on/off,
 * yes/no, etc.)./* ww w .  j  a v a2s  .c  o m*/
 *
 * @param attributeName the attribute name (case insensitive)
 * @param storageEntity the storage entity
 * @param attributeRequired specifies whether the attribute is mandatory (i.e. whether it has a value or not).
 * @param attributeValueRequiredIfExists specifies whether the attribute value is mandatory (i.e. the attribute must exist and its value must also contain a
 * value).
 *
 * @return the attribute value from the attribute with the attribute name as a boolean. If no value is configured and the attribute isn't required, then
 *         false is returned.
 * @throws IllegalStateException if an invalid storage attribute boolean value was configured.
 */
public boolean getBooleanStorageAttributeValueByName(String attributeName, StorageEntity storageEntity,
        boolean attributeRequired, boolean attributeValueRequiredIfExists) throws IllegalStateException {
    // Get the boolean string value.
    // The required flag is being passed so an exception will be thrown if it is required and isn't present.
    String booleanStringValue = getStorageAttributeValueByName(attributeName, storageEntity, attributeRequired,
            attributeValueRequiredIfExists);

    // If it isn't required, then treat a blank value as "false".
    if (StringUtils.isBlank(booleanStringValue)) {
        return false;
    }

    // Use custom boolean editor without allowed empty strings to convert the value of the argument to a boolean value.
    CustomBooleanEditor customBooleanEditor = new CustomBooleanEditor(attributeRequired);
    try {
        customBooleanEditor.setAsText(booleanStringValue);
    } catch (IllegalArgumentException e) {
        // This will produce a 500 HTTP status code error. If storage attributes are able to be updated by a REST invocation in the future,
        // we might want to consider making this a 400 instead since the user has the ability to fix the issue on their own.
        throw new IllegalStateException(
                String.format("Attribute \"%s\" for \"%s\" storage has an invalid boolean value: \"%s\".",
                        attributeName, storageEntity.getName(), booleanStringValue),
                e);
    }

    // Return the boolean value.
    return (Boolean) customBooleanEditor.getValue();
}