Java String Substritute substituteWhenEmpty(Object value, String valueWhenEmpty)

Here you can find the source of substituteWhenEmpty(Object value, String valueWhenEmpty)

Description

Similar to #nvl(Object,String) but returns valueWhenEmpty not only if value is null, but as well when the String representation of value is empty or contains only whitespaces.

License

Open Source License

Parameter

Parameter Description
property a parameter
string a parameter

Declaration

public static String substituteWhenEmpty(Object value, String valueWhenEmpty) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2010-2015 BSI Business Systems Integration AG.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:/*from w  w  w  .  j av a  2s. co m*/
 *     BSI Business Systems Integration AG - initial API and implementation
 ******************************************************************************/

public class Main {
    /**
     * Similar to {@link #nvl(Object, String)} but returns <code>valueWhenEmpty</code> not only if value is null, but as
     * well when the String representation of <code>value</code> is empty or contains only whitespaces.
     *
     * @param property
     * @param string
     * @return
     */
    public static String substituteWhenEmpty(Object value, String valueWhenEmpty) {
        String stringValue = nvl(value, null);
        return hasText(stringValue) ? stringValue : valueWhenEmpty;
    }

    /**
     * Returns the string-representation of <code>value</code>, or <code>valueWhenNull</code> if value is null.
     *
     * @param value
     * @param valueWhenNull
     * @see #substituteWhenEmpty(Object, String)
     */
    public static String nvl(Object value, String valueWhenNull) {
        if (value != null) {
            return value.toString();
        } else {
            return valueWhenNull;
        }
    }

    /**
     * Checks whether the given {@link CharSequence} contains visible characters. <br>
     * More formally: Checks whether the given {@link CharSequence} contains at least one character bigger than the
     * whitespace character: '\u0020'.
     *
     * @param s
     *          The {@link CharSequence} to check.
     * @return <code>true</code> if the provided {@link CharSequence} contains visible characters. <code>false</code>
     *         otherwise.
     */
    public static boolean hasText(CharSequence s) {
        if (s == null) {
            return false;
        }
        for (int i = 0; i < s.length(); i++) {
            if (s.charAt(i) > '\u0020') {
                return true;
            }
        }
        return false;
    }

    public static int length(CharSequence s) {
        if (s == null) {
            return 0;
        } else {
            return s.length();
        }
    }
}

Related

  1. substituteTabsAndNewLinesWithSpaces(String str)
  2. substituteText(String text, String token, String substitute)
  3. substituteToken(StringBuffer buf, String token, String value)
  4. substituteUserId(final long aUserId, final String aTemplate)
  5. substituteVars(String text, char[] names, Object[] values)