All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.sksamuel.jqm4gwt.Empty Maven / Gradle / Ivy

The newest version!
package com.sksamuel.jqm4gwt;

import java.util.Collection;
import java.util.Map;

/**
 * Null safe empty check for different data types.
 */
public class Empty {

    private Empty() {} // static class

    public static boolean is(String s) {
        return s == null || s.length() == 0; // optimize like Guava: s.isEmpty() -> s.length() == 0
    }

    public static boolean is(Collection collect) {
        return collect == null || collect.isEmpty();
    }

    public static boolean is(Map map) {
        return map == null || map.isEmpty();
    }

    /**
     * @return - if value is null returns replaceWithIfNull, otherwise just value.
     */
    public static  T nvl(T value, T replaceWithIfNull) {
        return value != null ? value : replaceWithIfNull;
    }

    /** The same as {@link Empty#nvl(Object, Object)} */
    public static  T nonNull(T value, T replaceWithIfNull) {
        return value != null ? value : replaceWithIfNull;
    }

    /**
     * @return - similar to nvl(), but returns replaceWithIfEmpty in case of null and empty,
     *           otherwise just value.
     */
    public static String nonEmpty(String value, String replaceWithIfEmpty) {
        return Empty.is(value) ? replaceWithIfEmpty : value;
    }

    public static  Collection nonEmpty(Collection value, Collection replaceWithIfEmpty) {
        return Empty.is(value) ? replaceWithIfEmpty : value;
    }

    public static  Map nonEmpty(Map value, Map replaceWithIfEmpty) {
        return Empty.is(value) ? replaceWithIfEmpty : value;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy