Java Array to String toString(String[] paramNames, Object[] params)

Here you can find the source of toString(String[] paramNames, Object[] params)

Description

Converts the parameters to string.

License

Apache License

Parameter

Parameter Description
paramNames the names of parameters.
params the values of parameters.

Return

the string

Declaration

static String toString(String[] paramNames, Object[] params) 

Method Source Code

//package com.java2s;
/*/*from  w w  w  .ja  v  a  2  s. c  o  m*/
 Copyright 2014 OPM.gov

 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.List;

public class Main {
    /**
     * Converts the parameters to string.
     * 
     * @param paramNames
     *            the names of parameters.
     * @param params
     *            the values of parameters.
     * 
     * @return the string
     */
    static String toString(String[] paramNames, Object[] params) {
        StringBuffer sb = new StringBuffer(" Input parameters: {");
        if (params != null) {
            for (int i = 0; i < params.length; i++) {
                if (i > 0) {
                    sb.append(", ");
                }
                sb.append(paramNames[i]).append(":")
                        .append(toString(params[i]));
            }
        }
        sb.append("}.");

        return sb.toString();
    }

    /**
     * Converts the object to string.
     * 
     * @param obj
     *            the object
     * 
     * @return the string representation of the object.
     */
    static String toString(Object obj) {
        if (obj instanceof List<?>) {
            return toString((List<?>) obj);
        }
        return String.valueOf(obj);
    }

    /**
     * Converts the List to a string.
     * 
     * @param obj
     *            the List.
     * 
     * @return the string.
     */
    private static String toString(List<?> obj) {
        StringBuilder sb = new StringBuilder();
        sb.append("[");
        boolean first = true;
        for (Object element : obj) {
            if (first) {
                first = false;
            } else {
                sb.append(", ");
            }

            sb.append(toString(element));
        }
        sb.append("]");
        return sb.toString();
    }
}

Related

  1. toString(String arrayName, T[] targets)
  2. toString(String[] args, char color1, char color2)
  3. toString(String[] arguments)
  4. toString(String[] array, String delimiter)
  5. toString(String[] line)
  6. toString(T[] array)
  7. toString(T[] array)
  8. toString(T[] array, String prefix, String sep, String suffix)
  9. toStringArray(Collection strings)