Example usage for org.apache.commons.lang3 ObjectUtils defaultIfNull

List of usage examples for org.apache.commons.lang3 ObjectUtils defaultIfNull

Introduction

In this page you can find the example usage for org.apache.commons.lang3 ObjectUtils defaultIfNull.

Prototype

public static <T> T defaultIfNull(final T object, final T defaultValue) 

Source Link

Document

Returns a default value if the object passed is null .

 ObjectUtils.defaultIfNull(null, null)      = null ObjectUtils.defaultIfNull(null, "")        = "" ObjectUtils.defaultIfNull(null, "zz")      = "zz" ObjectUtils.defaultIfNull("abc", *)        = "abc" ObjectUtils.defaultIfNull(Boolean.TRUE, *) = Boolean.TRUE 

Usage

From source file:io.wcm.devops.conga.model.util.MapExpander.java

/**
 * Get object from map with "deep" access resolving dots in the key as nested map keys.
 * @param map Map//from   ww  w . j a v a  2s . c o  m
 * @param key Key with dots
 * @return Value or null
 */
@SuppressWarnings("unchecked")
public static Object getDeep(Map<String, Object> map, String key) {
    if (map.containsKey(key)) {
        return ObjectUtils.defaultIfNull(map.get(key), "");
    }
    if (StringUtils.contains(key, ".")) {
        String keyPart = StringUtils.substringBefore(key, ".");
        String keySuffix = StringUtils.substringAfter(key, ".");
        Object resultKeyPart = map.get(keyPart);
        if (resultKeyPart != null && resultKeyPart instanceof Map) {
            return getDeep((Map<String, Object>) resultKeyPart, keySuffix);
        }
    }
    return null;
}

From source file:com.link_intersystems.lang.Assert.java

/**
 * Assert that a non-null value is returned by either returning the
 * <code>value</code> that is checked to be not <code>null</code> or the
 * <code>defaultIfNull</code> value.
 *
 * @param value//from   w  ww .  j  a va2 s  .co m
 * @param defaultIfNull
 * @return
 * @since 1.2.0.0
 */
public static <T> T defaultIfNull(T value, T defaultIfNull) {
    notNull("defaultIfNull", defaultIfNull);
    T guaranteedNotNull = ObjectUtils.defaultIfNull(value, defaultIfNull);
    return guaranteedNotNull;
}

From source file:controllers.base.Application.java

public static String minifyInProd(String file, Boolean neverMinify) {
    if (Play.isProd() && !ObjectUtils.defaultIfNull(neverMinify, false)) {
        String path = FilenameUtils.getFullPath(file);
        String name = FilenameUtils.getBaseName(file);
        String ext = FilenameUtils.getExtension(file);

        if (!name.toLowerCase().endsWith(".min")) {
            return String.format("%s%s.min.%s", path, name, ext);
        }/*from ww  w .  j  a  v  a  2s .co  m*/
    }

    return file;
}

From source file:com.lyncode.jtwig.expressions.operations.binary.ConcatenationOperation.java

@Override
public Object apply(JtwigPosition position, Object left, Object right) throws CalculateException {
    return ObjectUtils.defaultIfNull(left, "").toString() + ObjectUtils.defaultIfNull(right, "").toString();
}

From source file:com.thoughtworks.go.util.CsvRow.java

public String get(String key) {
    return (String) ObjectUtils.defaultIfNull(rowData.get(key), "");
}

From source file:com.thoughtworks.go.util.ConfigUtil.java

public ConfigUtil(String configFile) {
    this.configFile = (String) ObjectUtils.defaultIfNull(configFile, "<no config file specified>");
}

From source file:com.feilong.core.lang.ObjectUtilTest.java

/**
 * Test default if null or empty.// w  w  w .  j av a  2  s .co  m
 */
@Test
public void testDefaultIfNullOrEmpty() {
    assertEquals(1, ObjectUtil.defaultIfNullOrEmpty(new ArrayList<>(), 1));

    assertEquals("feilong", ObjectUtil.defaultIfNullOrEmpty("  ", "feilong"));
    assertEquals("  ", ObjectUtils.defaultIfNull("  ", "feilong"));

    assertEquals("fl", ObjectUtil.defaultIfNullOrEmpty("fl", "feilong"));
}

From source file:com.ejisto.modules.factory.impl.LocaleFactory.java

@Override
public Locale create(MockedField m, Locale actualValue) {
    Locale locale;//from  w w  w  . j  a va 2s. c om
    try {
        locale = LocaleUtils.toLocale(m.getFieldValue());
    } catch (IllegalArgumentException e) {
        locale = null;
    }
    return ObjectUtils.defaultIfNull(locale, actualValue);
}

From source file:controllers.base.Application.java

public static ProgressObserverToken createProgressObserverToken(final byte[] id, final Double initialProgress) {
    return Ebean.execute(new TxCallable<ProgressObserverToken>() {
        @Override// w  w w  . j a  v  a  2 s.c o m
        public ProgressObserverToken call() {
            ProgressObserverToken token = new ProgressObserverToken().setSession(getWebSession())
                    .setProgress((double) ObjectUtils.defaultIfNull(initialProgress, 0.0));

            if (id != null) {
                token.setId(id);
            }

            token.save();
            return token;
        }
    });
}

From source file:com.qcadoo.mes.deviationCausesReporting.domain.DeviationWithOccurrencesCount.java

public DeviationWithOccurrencesCount(final String deviationCause, final Long totalNumberOfOccurrences) {
    Preconditions.checkArgument(StringUtils.isNotEmpty(deviationCause),
            "Deviation cause (deviation type) cannot be empty!");
    this.deviationCause = deviationCause;
    this.totalNumberOfOccurrences = (Long) ObjectUtils.defaultIfNull(totalNumberOfOccurrences, 0L);
}