Here you can find the source of valueOf(final Object obj, final String defaultValue)
Assertions : assertNull(StringHelper.valueOf(null, null)); assertEquals("", StringHelper.valueOf(null, "")); assertEquals("a", StringHelper.valueOf(null, "a")); assertEquals("foo", StringHelper.valueOf("foo", "a"));
Parameter | Description |
---|---|
obj | a parameter |
defaultValue | the default value to return if object is null |
public static String valueOf(final Object obj, final String defaultValue)
//package com.java2s; /*/* ww w.jav a 2 s. com*/ * Copyright 2008-2010 the original author or authors * * 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 { /** * <p> * * <pre> * <b>Assertions :</b> * assertNotNull(StringHelper.valueOf("")); * assertEquals("", StringHelper.valueOf("")); * assertEquals("foo", StringHelper.valueOf("foo")); * </pre> * * </p> * * @param obj * @return the String representation of the object instance. <br/> * It uses {@link String#valueOf(Object)}, but if arg is null, it will return empty String "" */ public static String valueOf(final Object obj) { return valueOf(obj, ""); } /** * <p> * * <pre> * <b>Assertions :</b> * assertNull(StringHelper.valueOf(null, null)); * assertEquals("", StringHelper.valueOf(null, "")); * assertEquals("a", StringHelper.valueOf(null, "a")); * assertEquals("foo", StringHelper.valueOf("foo", "a")); * </pre> * * </p> * * @param obj * @param defaultValue the default value to return if object is null * @return the String representation of the object instance. <br/> * It uses {@link String#valueOf(Object)}, but if arg is null, it will return defaultValue */ public static String valueOf(final Object obj, final String defaultValue) { if (obj == null) { return defaultValue; } else { return String.valueOf(obj); } } }