Example usage for java.lang Object toString

List of usage examples for java.lang Object toString

Introduction

In this page you can find the example usage for java.lang Object toString.

Prototype

public String toString() 

Source Link

Document

Returns a string representation of the object.

Usage

From source file:Main.java

public static String getRequestParamValue(Object obj, String charset) {
    if (obj == null) {
        return "";
    }/*from  ww  w .ja  v  a 2  s .c  om*/
    String value;

    if (obj instanceof List) {
        StringBuilder sb = new StringBuilder();
        if (obj != null) {
            for (Object o : (List<?>) obj) {
                if (o != null) {
                    sb.append(o.toString());
                    sb.append(',');
                }
            }
        }
        if (sb.length() > 0) {
            sb.deleteCharAt(sb.length() - 1);
        }
        value = sb.toString();
    }

    else {
        value = obj.toString();
    }

    try {
        return URLEncoder.encode(value, charset);
    } catch (UnsupportedEncodingException e) {
        return value;
    }
}

From source file:com.qwazr.utils.ArrayUtils.java

public static String[] toStringArray(Collection<Object> collection) {
    if (collection == null)
        return null;
    String[] array = new String[collection.size()];
    int i = 0;/*  w ww  .  ja va2s .  co m*/
    for (Object val : collection)
        array[i++] = val == null ? null : val.toString();
    return array;
}

From source file:com.mseeworld.qzh.util.Debug.java

/**
 * ?/*from   www .  j  av  a2  s.  c  o  m*/
 * @param objects
 * @date    2013-5-8
 */
public static final void printf(String msg, Object... objects) {
    if (ArrayUtils.isEmpty(objects)) {
        System.out.println(msg);
        return;
    }
    for (int i = 0, len = objects.length; i < len; i++) {
        Object obj = objects[i];
        msg = msg.replaceFirst("\\{\\}", obj == null ? "" : obj.toString());
    }
    System.out.println(msg);
}

From source file:Main.java

public static <T> T castObjectOrThrow(Object o, Class<T> clazz) {
    try {/*from   ww w.  jav  a 2s  . co m*/
        return clazz.cast(o);
    } catch (ClassCastException e) {
        throw new ClassCastException(o.toString() + " must implement " + clazz.getSimpleName());
    }
}

From source file:Main.java

/**
 *  /*from   w  w w .j  ava  2 s .  co m*/
 * @param json
 * @param key
 * @param defaultString
 * @return
 */
public static String getJsonString(JSONObject json, String key, String defaultString) {
    Object value = getJsonValue(json, key, defaultString);
    String string = (value == null) ? ("") : (value.toString());
    return string;
}

From source file:Main.java

/**
 * Mocks the test coverage for enums that generate the values and valueOf methods. This
 * is needed to get 100% on code coverage with Jacoco on those methods that are created synthetically.
 *
 * @param enumClass The enum class to partially test cover.
 *//*  ww w . j  a  va 2 s . com*/
public static void shallowEnumCodeCoverage(Class<? extends Enum<?>> enumClass) {
    try {
        for (Object object : (Object[]) enumClass.getMethod("values").invoke(null)) {
            enumClass.getMethod("valueOf", String.class).invoke(null, object.toString());
        }
    } catch (Throwable e) {
        throw new RuntimeException(e);
    }
}

From source file:Main.java

/**
 * Returns the {@value #BLUEPRINT_HEADER} if present from the given dictionary.
 * //from  w  w  w  .  ja v a 2  s .c  o m
 * @param headers
 * @return
 */
public static String getBlueprintHeader(Dictionary headers) {
    Object header = null;
    if (headers != null)
        header = headers.get(BLUEPRINT_HEADER);
    return (header != null ? header.toString().trim() : null);
}

From source file:czlab.xlib.CU.java

public static String nsb(Object x) {
    return x == null ? "" : x.toString();
}

From source file:com.threewks.thundr.view.jsp.el.StringFunctions.java

public static List<String> split(Object arg, String regex) {
    return arg == null ? Collections.<String>emptyList()
            : Arrays.asList(arg.toString().split(regex == null ? "\\s+" : regex));
}

From source file:Main.java

/**
 * Returns a list of strings from al collection of arbitrary objects.
 *
 * Uses the <code>toString()</code> operation of every collection element.
 * Null elements set to null in the list.
 *
 * @param  coll collection/*ww w. j  av  a2 s. c o  m*/
 * @return      list of strings
 */
public static List<String> toStringList(Collection<?> coll) {
    if (coll == null) {
        throw new NullPointerException("coll == null");
    }

    List<String> list = new ArrayList<>(coll.size());

    for (Object o : coll) {
        if (o == null) {
            list.add(null);
        } else {
            list.add(o.toString());
        }
    }

    return list;
}