Java Object Value Of valueOf(Object o)

Here you can find the source of valueOf(Object o)

Description

value Of

License

Open Source License

Parameter

Parameter Description
o Object.

Return

String representation of value.

Declaration

private static String valueOf(Object o) 

Method Source Code

//package com.java2s;
/* /*from   www.  java2s.  c om*/
 Copyright (C) GridGain Systems. All Rights Reserved.
    
 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 {
    /**
     *
     * @param o Object.
     * @return String representation of value.
     */
    private static String valueOf(Object o) {
        if (o == null)
            return "null";
        if (o instanceof byte[])
            return "size=" + ((byte[]) o).length;
        if (o instanceof Byte[])
            return "size=" + ((Byte[]) o).length;
        if (o instanceof Object[])
            return "size=" + ((Object[]) o).length + ", values=["
                    + mkString((Object[]) o, 120) + "]";
        return o.toString();
    }

    /**
     *
     * @param arr Object array.
     * @param maxSz Maximum string size.
     * @return Fixed size string.
     */
    private static String mkString(Object[] arr, int maxSz) {
        String sep = ", ";

        StringBuilder sb = new StringBuilder();

        Boolean first = true;

        for (Object v : arr) {
            if (first)
                first = false;
            else
                sb.append(sep);

            sb.append(v);

            if (sb.length() > maxSz)
                break;
        }

        if (sb.length() >= maxSz) {
            String end = "...";

            sb.setLength(maxSz - end.length());

            sb.append(end);
        }

        return sb.toString();
    }
}

Related

  1. valueOf(final Object o)
  2. valueOf(Object o)
  3. valueOf(Object o)
  4. valueOf(Object obj)
  5. valueOf(Object obj)
  6. valueOf(Object obj)