Java String Empty coerceValueIfNullOrEmpty(String s, String valueIfNullOrEmpty)

Here you can find the source of coerceValueIfNullOrEmpty(String s, String valueIfNullOrEmpty)

Description

Use this when you want to normalize empty and null strings This way, Utility.areObjectsEqual can used for comparison, where a null string is to be treated the same as an empty string.

License

Apache License

Parameter

Parameter Description
s a parameter
valueIfNullOrEmpty a parameter

Declaration

public static String coerceValueIfNullOrEmpty(String s, String valueIfNullOrEmpty) 

Method Source Code

//package com.java2s;
/**/*from w w  w.j a  v a 2 s.  c  o m*/
 * Copyright 2010-present Facebook.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.util.*;

public class Main {
    /**
     * Use this when you want to normalize empty and null strings
     * This way, Utility.areObjectsEqual can used for comparison, where a null string is to be treated the same as
     * an empty string.
     *
     * @param s
     * @param valueIfNullOrEmpty
     * @return
     */
    public static String coerceValueIfNullOrEmpty(String s, String valueIfNullOrEmpty) {
        if (isNullOrEmpty(s)) {
            return valueIfNullOrEmpty;
        }

        return s;
    }

    public static <T> boolean isNullOrEmpty(Collection<T> c) {
        return (c == null) || (c.size() == 0);
    }

    public static boolean isNullOrEmpty(String s) {
        return (s == null) || (s.length() == 0);
    }
}

Related

  1. emptyString()
  2. emptyString(final String a, final String defaultString)
  3. emptyString(final String string)
  4. emptyString(Object s)