Example usage for org.apache.commons.lang3 StringUtils defaultIfEmpty

List of usage examples for org.apache.commons.lang3 StringUtils defaultIfEmpty

Introduction

In this page you can find the example usage for org.apache.commons.lang3 StringUtils defaultIfEmpty.

Prototype

public static <T extends CharSequence> T defaultIfEmpty(final T str, final T defaultStr) 

Source Link

Document

Returns either the passed in CharSequence, or if the CharSequence is empty or null , the value of defaultStr .

 StringUtils.defaultIfEmpty(null, "NULL")  = "NULL" StringUtils.defaultIfEmpty("", "NULL")    = "NULL" StringUtils.defaultIfEmpty(" ", "NULL")   = " " StringUtils.defaultIfEmpty("bat", "NULL") = "bat" StringUtils.defaultIfEmpty("", null)      = null 

Usage

From source file:org.codice.ddf.opensearch.source.OpenSearchSource.java

@Override
public SourceResponse query(QueryRequest queryRequest) throws UnsupportedQueryException {
    String methodName = "query";
    LOGGER.trace(methodName);//from  www. java 2 s.c  o  m

    final SourceResponse response;

    Subject subject = null;
    if (queryRequest.hasProperties()) {
        Object subjectObj = queryRequest.getProperties().get(SecurityConstants.SECURITY_SUBJECT);
        subject = (Subject) subjectObj;
    }

    Query query = queryRequest.getQuery();
    if (LOGGER.isTraceEnabled()) {
        LOGGER.trace("Received query: {}", query);
        FilterTransformer transform = new FilterTransformer();
        transform.setIndentation(2);
        try {
            LOGGER.trace(transform.transform(query));
        } catch (TransformerException e) {
            LOGGER.debug("Error transforming query to XML", e);
        }
    }

    final OpenSearchFilterVisitorObject openSearchFilterVisitorObject = (OpenSearchFilterVisitorObject) query
            .accept(openSearchFilterVisitor, new OpenSearchFilterVisitorObject());

    final ContextualSearch contextualSearch = openSearchFilterVisitorObject.getContextualSearch();
    final SpatialSearch spatialSearch = createCombinedSpatialSearch(
            openSearchFilterVisitorObject.getPointRadiusSearches(),
            openSearchFilterVisitorObject.getGeometrySearches(), numMultiPointRadiusVertices,
            distanceTolerance);
    final TemporalFilter temporalSearch = openSearchFilterVisitorObject.getTemporalSearch();
    final String idSearch = StringUtils.defaultIfEmpty((String) queryRequest.getPropertyValue(Metacard.ID),
            openSearchFilterVisitorObject.getId());

    final Map<String, String> searchPhraseMap = contextualSearch == null ? new HashMap<>()
            : contextualSearch.getSearchPhraseMap();

    // OpenSearch endpoints only support certain keyword, temporal, and spatial searches. The
    // OpenSearchSource additionally supports an id search when no other search criteria is
    // specified.
    if (MapUtils.isNotEmpty(searchPhraseMap) || spatialSearch != null || temporalSearch != null) {
        if (StringUtils.isNotEmpty(idSearch)) {
            LOGGER.debug(
                    "Ignoring the id search {}. Querying the source with the keyword, temporal, and/or spatial OpenSearch parameters",
                    idSearch);
        }

        final WebClient restWebClient = factory.getWebClientForSubject(subject);
        if (restWebClient == null) {
            throw new UnsupportedQueryException("Unable to create restWebClient");
        }
        response = doOpenSearchQuery(queryRequest, subject, spatialSearch, temporalSearch, searchPhraseMap,
                restWebClient);
    } else if (StringUtils.isNotEmpty(idSearch)) {
        final WebClient restWebClient = newRestClient(query, idSearch, false, subject);
        if (restWebClient == null) {
            throw new UnsupportedQueryException("Unable to create restWebClient");
        }

        response = doQueryById(queryRequest, restWebClient);
    } else {
        LOGGER.debug(
                "The OpenSearch Source only supports id searches or searches with certain keyword, \"{}\" temporal, or \"{}\" spatial criteria, but the query was {}. See the documentation for more details about supported searches.",
                OpenSearchConstants.SUPPORTED_TEMPORAL_SEARCH_TERM,
                OpenSearchConstants.SUPPORTED_SPATIAL_SEARCH_TERM, query);
        throw new UnsupportedQueryException(
                "OpenSearch query parameters could not be created from the query criteria.");
    }

    setSourceId(response);

    LOGGER.trace(methodName);

    return response;
}

From source file:org.dederem.common.service.ConfigService.java

/**
 * Default constructor.//from  www  .ja v  a  2 s  .  c o  m
 *
 * @throws IOException
 *             I/O error.
 */
public ConfigService() {
    super();

    final Properties props = new Properties();
    try (final InputStream input = this.getClass().getResourceAsStream("/config/defaultConfig.properties")) {
        props.load(input);
    } catch (final IOException ex) {
        ConfigService.LOG.error(ex.getMessage(), ex);
    }
    this.configFile = new File(
            StringUtils.defaultIfEmpty(props.getProperty("config.dir"), "/etc/dederem.conf"));
    this.repoDir = new File(StringUtils.defaultIfEmpty(props.getProperty("repo.dir"), "/opt/dederem"));
}

From source file:org.eclipse.hawkbit.ui.common.UserDetailsFormatter.java

private static String formatUserName(final int expectedNameLength, final UserDetails userDetails) {
    if (!(userDetails instanceof UserPrincipal)) {
        return userDetails.getUsername();
    }//w  w w . j  ava2 s.c  om

    final UserPrincipal userPrincipal = (UserPrincipal) userDetails;

    String lastname = StringUtils.defaultIfEmpty(userPrincipal.getLastname(), "");

    if (!StringUtils.isEmpty(lastname)) {
        lastname += DETAIL_SEPERATOR;
    }

    final String firstAndLastname = lastname + StringUtils.defaultIfEmpty(userPrincipal.getFirstname(), "");

    final String trimmedUsername = trimAndFormatDetail(firstAndLastname, expectedNameLength);

    if (StringUtils.isEmpty(trimmedUsername)) {
        return trimAndFormatDetail(userPrincipal.getLoginname(), expectedNameLength);
    }
    return trimmedUsername;
}

From source file:org.eclipse.hawkbit.ui.common.UserDetailsFormatter.java

private static String trimAndFormatDetail(final String formatString, final int expectedDetailLength) {
    final String detail = StringUtils.defaultIfEmpty(formatString, "");
    final String trimmedDetail = StringUtils.substring(detail, 0, expectedDetailLength);
    if (StringUtils.length(detail) > expectedDetailLength) {
        return trimmedDetail + TRIM_APPENDIX;
    }/*  w  ww.  j  a va  2  s . c  om*/
    return trimmedDetail;
}

From source file:org.firebrandocm.dao.ClassMeta.java

/**
     * Constructor./*from w  ww .jav a2s  . c om*/
     * Extracts and caches metadata for persistent entity classes
     *
     * @param target             the target class
     * @param persistenceFactory the persistence factory managing the entity
     */
    public ClassMeta(Class<T> target, AbstractPersistenceFactory persistenceFactory)
            throws ClassNotFoundException, IntrospectionException, InstantiationException, IllegalAccessException {
        log.debug(String.format("Initializing class metadata for %s", target));
        this.target = target;
        //If this is a top level structure that holds a column family
        if (target.isAnnotationPresent(ColumnFamily.class)) {
            ColumnFamily columnFamilyAnnotation = target.getAnnotation(ColumnFamily.class);
            consistencyLevel = columnFamilyAnnotation.consistencyLevel();
            counterColumnFamily = columnFamilyAnnotation.defaultValidationClass() == CounterColumnType.class;
            keySpace = StringUtils.defaultIfEmpty(columnFamilyAnnotation.keySpace(),
                    persistenceFactory.getDefaultKeySpace());
            columnFamily = target.getSimpleName();
            initializeColumnFamilyDefinition();
            processFields(target, "");
            processMethods(target);
            addClassTypePropertyIfSupported();
            initializeProxyFactory(persistenceFactory);
            initializeNamedQueries(target);
        } else {
            throw new IllegalArgumentException(target + " is not annotated with " + ColumnFamily.class);
        }
    }

From source file:org.firebrandocm.dao.ClassMeta.java

/**
     * Private helper to initialize a column family definition
     *//* ww  w .  j a  v a  2  s .  co m*/
    private void initializeColumnFamilyDefinition() throws ClassNotFoundException {
        columnFamilyDefinition = new CfDef();
        columnFamilyDefinition.setName(columnFamily);
        columnFamilyDefinition.setKeyspace(getKeySpace());
        ColumnFamily cfAnnotation = target.getAnnotation(ColumnFamily.class);
        String comparatorType = cfAnnotation.compareWith().getName();
        if (cfAnnotation.reversed()) {
            comparatorType = String.format("%s(reversed=true)", comparatorType);
        }
        columnFamilyDefinition.setComparator_type(comparatorType);
        columnFamilyDefinition.setKey_cache_size(cfAnnotation.keysCached());
        columnFamilyDefinition.setRow_cache_size(cfAnnotation.rowsCached());
        columnFamilyDefinition.setComment(StringUtils.defaultIfEmpty(cfAnnotation.comment(), null));
        columnFamilyDefinition.setComparator_type(cfAnnotation.compareWith().getName());
        columnFamilyDefinition.setRead_repair_chance(cfAnnotation.readRepairChance());
        columnFamilyDefinition.setGc_grace_seconds(cfAnnotation.gcGraceSeconds());
        columnFamilyDefinition.setDefault_validation_class(
                StringUtils.defaultIfEmpty(cfAnnotation.defaultValidationClass().getName(), null));
        columnFamilyDefinition.setKey_validation_class(
                StringUtils.defaultIfEmpty(cfAnnotation.defaultKeyValidationClass().getName(), null));
        columnFamilyDefinition.setMin_compaction_threshold(cfAnnotation.minCompactionThreshold());
        columnFamilyDefinition.setMax_compaction_threshold(cfAnnotation.maxCompactionThreshold());
        columnFamilyDefinition.setReplicate_on_write(cfAnnotation.replicateOnWrite());
    }

From source file:org.firebrandocm.dao.ClassMetadata.java

/**
 * Constructor./*from   ww w . j a  v a  2  s .  c o m*/
 * Extracts and caches metadata for persistent entity classes
 *
 * @param target             the target class
 * @param persistenceFactory the persistence factory managing the entity
 */
public ClassMetadata(Class<T> target, AbstractPersistenceFactory persistenceFactory)
        throws ClassNotFoundException, IntrospectionException, InstantiationException, IllegalAccessException {
    log.debug(String.format("Initializing class metadata for %s", target));
    this.target = target;
    //If this is a top level structure that holds a column family
    if (target.isAnnotationPresent(ColumnFamily.class)) {
        ColumnFamily columnFamilyAnnotation = target.getAnnotation(ColumnFamily.class);
        consistencyLevel = columnFamilyAnnotation.consistencyLevel();
        counterColumnFamily = columnFamilyAnnotation.defaultValidationClass() == CounterColumnType.class;
        keySpace = StringUtils.defaultIfEmpty(columnFamilyAnnotation.keySpace(),
                persistenceFactory.getDefaultKeySpace());
        columnFamily = target.getSimpleName();
        initializeColumnFamilyDefinition();
        processFields(target, "");
        processMethods(target);
        addClassTypePropertyIfSupported();
        initializeProxyFactory(persistenceFactory);
        initializeNamedQueries(target);
    } else {
        throw new IllegalArgumentException(target + " is not annotated with " + ColumnFamily.class);
    }
}

From source file:org.firebrandocm.dao.ClassMetadata.java

/**
 * Private helper to initialize a column family definition
 *///ww w. ja  v  a2 s.  c om
private void initializeColumnFamilyDefinition() throws ClassNotFoundException {
    columnFamilyDefinition = new CfDef();
    columnFamilyDefinition.setName(columnFamily);
    columnFamilyDefinition.setKeyspace(getKeySpace());
    ColumnFamily cfAnnotation = target.getAnnotation(ColumnFamily.class);
    String comparatorType = cfAnnotation.compareWith().getName();
    if (cfAnnotation.reversed()) {
        comparatorType = String.format("%s(reversed=true)", comparatorType);
    }
    columnFamilyDefinition.setComparator_type(comparatorType);
    columnFamilyDefinition.setKey_cache_size(cfAnnotation.keysCached());
    columnFamilyDefinition.setRow_cache_size(cfAnnotation.rowsCached());
    columnFamilyDefinition.setComment(StringUtils.defaultIfEmpty(cfAnnotation.comment(), null));
    columnFamilyDefinition.setComparator_type(cfAnnotation.compareWith().getName());
    columnFamilyDefinition.setRead_repair_chance(cfAnnotation.readRepairChance());
    columnFamilyDefinition.setGc_grace_seconds(cfAnnotation.gcGraceSeconds());
    columnFamilyDefinition.setDefault_validation_class(
            StringUtils.defaultIfEmpty(cfAnnotation.defaultValidationClass().getName(), null));
    columnFamilyDefinition.setKey_validation_class(
            StringUtils.defaultIfEmpty(cfAnnotation.defaultKeyValidationClass().getName(), null));
    columnFamilyDefinition.setMin_compaction_threshold(cfAnnotation.minCompactionThreshold());
    columnFamilyDefinition.setMax_compaction_threshold(cfAnnotation.maxCompactionThreshold());
    columnFamilyDefinition.setReplicate_on_write(cfAnnotation.replicateOnWrite());
}

From source file:org.gbif.dwc.text.ArchiveFile.java

public void setFieldsTerminatedBy(String fieldsTerminatedBy) {
    this.fieldsTerminatedBy = StringUtils.defaultIfEmpty(fieldsTerminatedBy, null);
}

From source file:org.gbif.dwc.text.ArchiveFile.java

public void setLinesTerminatedBy(String linesTerminatedBy) {
    this.linesTerminatedBy = StringUtils.defaultIfEmpty(linesTerminatedBy, null);
}