Java Type Coalesce coalesce(String... strings)

Here you can find the source of coalesce(String... strings)

Description

returns the first not empty string {Category} StringUtil {talendTypes} String {param} string("testMe1","testMe2") strings: String.

License

Apache License

Declaration

public static String coalesce(String... strings) 

Method Source Code

//package com.java2s;
/**//  w  ww .j a  v a2 s  . co  m
 * Copyright 2015 Jan Lolling jan.lolling@gmail.com
 * 
 * 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.
 */

public class Main {
    /**
     * returns the first not empty string
     * 
     * {Category} StringUtil
     * 
     * {talendTypes} String
     * 
     * {param} string("testMe1","testMe2") strings: String.
     * 
     * {example} coalesce(" test","hans"," TEST ","Tata") # "test"
     */
    public static String coalesce(String... strings) {
        for (String s : strings) {
            if (s != null) {
                s = s.trim();
                if (isEmpty(s) == false) {
                    return s;
                }
            }
        }
        return null;
    }

    /**
     * returns true if the string is not filled or contains "null"
     * 
     * {Category} StringUtil
     * 
     * {talendTypes} String
     * 
     * {param} object("value1") strings: String.
     * 
     * {example} isEmpty(aString) # 2323133_18
     */
    public static boolean isEmpty(String s) {
        if (s == null) {
            return true;
        }
        if (s.trim().isEmpty()) {
            return true;
        }
        if (s.trim().equalsIgnoreCase("null")) {
            return true;
        }
        return false;
    }
}

Related

  1. coalesce(float... p)
  2. coalesce(Object src, Object defaultValue)
  3. coalesce(Object... args)
  4. coalesce(Object... items)
  5. coalesce(String str1, String str2)
  6. coalesce(String... strings)
  7. coalesce(String... vals)
  8. coalesce(String[] values)
  9. coalesce(T a, T b)