zoukankan      html  css  js  c++  java
  • java toString方法

        //---------------------------------------------------------------------
        // Convenience methods for toString output
        //---------------------------------------------------------------------
    
        /**
         * Return a String representation of an object's overall identity.
         * @param obj the object (may be {@code null})
         * @return the object's identity as String representation,
         * or an empty String if the object was {@code null}
         */
        public static String identityToString(Object obj) {
            if (obj == null) {
                return EMPTY_STRING;
            }
            return obj.getClass().getName() + "@" + getIdentityHexString(obj);
        }
    
        /**
         * Return a hex String form of an object's identity hash code.
         * @param obj the object
         * @return the object's identity code in hex notation
         */
        public static String getIdentityHexString(Object obj) {
            return Integer.toHexString(System.identityHashCode(obj));
        }
    
        /**
         * Return a content-based String representation if {@code obj} is
         * not {@code null}; otherwise returns an empty String.
         * <p>Differs from {@link #nullSafeToString(Object)} in that it returns
         * an empty String rather than "null" for a {@code null} value.
         * @param obj the object to build a display String for
         * @return a display String representation of {@code obj}
         * @see #nullSafeToString(Object)
         */
        public static String getDisplayString(Object obj) {
            if (obj == null) {
                return EMPTY_STRING;
            }
            return nullSafeToString(obj);
        }
    
        /**
         * Determine the class name for the given object.
         * <p>Returns {@code "null"} if {@code obj} is {@code null}.
         * @param obj the object to introspect (may be {@code null})
         * @return the corresponding class name
         */
        public static String nullSafeClassName(Object obj) {
            return (obj != null ? obj.getClass().getName() : NULL_STRING);
        }
    
        /**
         * Return a String representation of the specified Object.
         * <p>Builds a String representation of the contents in case of an array.
         * Returns {@code "null"} if {@code obj} is {@code null}.
         * @param obj the object to build a String representation for
         * @return a String representation of {@code obj}
         */
        public static String nullSafeToString(Object obj) {
            if (obj == null) {
                return NULL_STRING;
            }
            if (obj instanceof String) {
                return (String) obj;
            }
            if (obj instanceof Object[]) {
                return nullSafeToString((Object[]) obj);
            }
            if (obj instanceof boolean[]) {
                return nullSafeToString((boolean[]) obj);
            }
            if (obj instanceof byte[]) {
                return nullSafeToString((byte[]) obj);
            }
            if (obj instanceof char[]) {
                return nullSafeToString((char[]) obj);
            }
            if (obj instanceof double[]) {
                return nullSafeToString((double[]) obj);
            }
            if (obj instanceof float[]) {
                return nullSafeToString((float[]) obj);
            }
            if (obj instanceof int[]) {
                return nullSafeToString((int[]) obj);
            }
            if (obj instanceof long[]) {
                return nullSafeToString((long[]) obj);
            }
            if (obj instanceof short[]) {
                return nullSafeToString((short[]) obj);
            }
            String str = obj.toString();
            return (str != null ? str : EMPTY_STRING);
        }
    
        /**
         * Return a String representation of the contents of the specified array.
         * <p>The String representation consists of a list of the array's elements,
         * enclosed in curly braces ({@code "{}"}). Adjacent elements are separated
         * by the characters {@code ", "} (a comma followed by a space). Returns
         * {@code "null"} if {@code array} is {@code null}.
         * @param array the array to build a String representation for
         * @return a String representation of {@code array}
         */
        public static String nullSafeToString(Object[] array) {
            if (array == null) {
                return NULL_STRING;
            }
            int length = array.length;
            if (length == 0) {
                return EMPTY_ARRAY;
            }
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < length; i++) {
                if (i == 0) {
                    sb.append(ARRAY_START);
                }
                else {
                    sb.append(ARRAY_ELEMENT_SEPARATOR);
                }
                sb.append(String.valueOf(array[i]));
            }
            sb.append(ARRAY_END);
            return sb.toString();
        }
    
        /**
         * Return a String representation of the contents of the specified array.
         * <p>The String representation consists of a list of the array's elements,
         * enclosed in curly braces ({@code "{}"}). Adjacent elements are separated
         * by the characters {@code ", "} (a comma followed by a space). Returns
         * {@code "null"} if {@code array} is {@code null}.
         * @param array the array to build a String representation for
         * @return a String representation of {@code array}
         */
        public static String nullSafeToString(boolean[] array) {
            if (array == null) {
                return NULL_STRING;
            }
            int length = array.length;
            if (length == 0) {
                return EMPTY_ARRAY;
            }
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < length; i++) {
                if (i == 0) {
                    sb.append(ARRAY_START);
                }
                else {
                    sb.append(ARRAY_ELEMENT_SEPARATOR);
                }
    
                sb.append(array[i]);
            }
            sb.append(ARRAY_END);
            return sb.toString();
        }
    
        /**
         * Return a String representation of the contents of the specified array.
         * <p>The String representation consists of a list of the array's elements,
         * enclosed in curly braces ({@code "{}"}). Adjacent elements are separated
         * by the characters {@code ", "} (a comma followed by a space). Returns
         * {@code "null"} if {@code array} is {@code null}.
         * @param array the array to build a String representation for
         * @return a String representation of {@code array}
         */
        public static String nullSafeToString(byte[] array) {
            if (array == null) {
                return NULL_STRING;
            }
            int length = array.length;
            if (length == 0) {
                return EMPTY_ARRAY;
            }
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < length; i++) {
                if (i == 0) {
                    sb.append(ARRAY_START);
                }
                else {
                    sb.append(ARRAY_ELEMENT_SEPARATOR);
                }
                sb.append(array[i]);
            }
            sb.append(ARRAY_END);
            return sb.toString();
        }
    
        /**
         * Return a String representation of the contents of the specified array.
         * <p>The String representation consists of a list of the array's elements,
         * enclosed in curly braces ({@code "{}"}). Adjacent elements are separated
         * by the characters {@code ", "} (a comma followed by a space). Returns
         * {@code "null"} if {@code array} is {@code null}.
         * @param array the array to build a String representation for
         * @return a String representation of {@code array}
         */
        public static String nullSafeToString(char[] array) {
            if (array == null) {
                return NULL_STRING;
            }
            int length = array.length;
            if (length == 0) {
                return EMPTY_ARRAY;
            }
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < length; i++) {
                if (i == 0) {
                    sb.append(ARRAY_START);
                }
                else {
                    sb.append(ARRAY_ELEMENT_SEPARATOR);
                }
                sb.append("'").append(array[i]).append("'");
            }
            sb.append(ARRAY_END);
            return sb.toString();
        }
    
        /**
         * Return a String representation of the contents of the specified array.
         * <p>The String representation consists of a list of the array's elements,
         * enclosed in curly braces ({@code "{}"}). Adjacent elements are separated
         * by the characters {@code ", "} (a comma followed by a space). Returns
         * {@code "null"} if {@code array} is {@code null}.
         * @param array the array to build a String representation for
         * @return a String representation of {@code array}
         */
        public static String nullSafeToString(double[] array) {
            if (array == null) {
                return NULL_STRING;
            }
            int length = array.length;
            if (length == 0) {
                return EMPTY_ARRAY;
            }
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < length; i++) {
                if (i == 0) {
                    sb.append(ARRAY_START);
                }
                else {
                    sb.append(ARRAY_ELEMENT_SEPARATOR);
                }
    
                sb.append(array[i]);
            }
            sb.append(ARRAY_END);
            return sb.toString();
        }
    
        /**
         * Return a String representation of the contents of the specified array.
         * <p>The String representation consists of a list of the array's elements,
         * enclosed in curly braces ({@code "{}"}). Adjacent elements are separated
         * by the characters {@code ", "} (a comma followed by a space). Returns
         * {@code "null"} if {@code array} is {@code null}.
         * @param array the array to build a String representation for
         * @return a String representation of {@code array}
         */
        public static String nullSafeToString(float[] array) {
            if (array == null) {
                return NULL_STRING;
            }
            int length = array.length;
            if (length == 0) {
                return EMPTY_ARRAY;
            }
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < length; i++) {
                if (i == 0) {
                    sb.append(ARRAY_START);
                }
                else {
                    sb.append(ARRAY_ELEMENT_SEPARATOR);
                }
    
                sb.append(array[i]);
            }
            sb.append(ARRAY_END);
            return sb.toString();
        }
    
        /**
         * Return a String representation of the contents of the specified array.
         * <p>The String representation consists of a list of the array's elements,
         * enclosed in curly braces ({@code "{}"}). Adjacent elements are separated
         * by the characters {@code ", "} (a comma followed by a space). Returns
         * {@code "null"} if {@code array} is {@code null}.
         * @param array the array to build a String representation for
         * @return a String representation of {@code array}
         */
        public static String nullSafeToString(int[] array) {
            if (array == null) {
                return NULL_STRING;
            }
            int length = array.length;
            if (length == 0) {
                return EMPTY_ARRAY;
            }
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < length; i++) {
                if (i == 0) {
                    sb.append(ARRAY_START);
                }
                else {
                    sb.append(ARRAY_ELEMENT_SEPARATOR);
                }
                sb.append(array[i]);
            }
            sb.append(ARRAY_END);
            return sb.toString();
        }
    
        /**
         * Return a String representation of the contents of the specified array.
         * <p>The String representation consists of a list of the array's elements,
         * enclosed in curly braces ({@code "{}"}). Adjacent elements are separated
         * by the characters {@code ", "} (a comma followed by a space). Returns
         * {@code "null"} if {@code array} is {@code null}.
         * @param array the array to build a String representation for
         * @return a String representation of {@code array}
         */
        public static String nullSafeToString(long[] array) {
            if (array == null) {
                return NULL_STRING;
            }
            int length = array.length;
            if (length == 0) {
                return EMPTY_ARRAY;
            }
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < length; i++) {
                if (i == 0) {
                    sb.append(ARRAY_START);
                }
                else {
                    sb.append(ARRAY_ELEMENT_SEPARATOR);
                }
                sb.append(array[i]);
            }
            sb.append(ARRAY_END);
            return sb.toString();
        }
    
        /**
         * Return a String representation of the contents of the specified array.
         * <p>The String representation consists of a list of the array's elements,
         * enclosed in curly braces ({@code "{}"}). Adjacent elements are separated
         * by the characters {@code ", "} (a comma followed by a space). Returns
         * {@code "null"} if {@code array} is {@code null}.
         * @param array the array to build a String representation for
         * @return a String representation of {@code array}
         */
        public static String nullSafeToString(short[] array) {
            if (array == null) {
                return NULL_STRING;
            }
            int length = array.length;
            if (length == 0) {
                return EMPTY_ARRAY;
            }
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < length; i++) {
                if (i == 0) {
                    sb.append(ARRAY_START);
                }
                else {
                    sb.append(ARRAY_ELEMENT_SEPARATOR);
                }
                sb.append(array[i]);
            }
            sb.append(ARRAY_END);
            return sb.toString();
        }
  • 相关阅读:
    商贸通帐套隐藏方法
    固定资产打开提示:上年度数据未结转!
    ZOJ 2432 Greatest Common Increasing Subsequence
    POJ 1080 Human Gene Functions
    POJ 1088 滑雪
    POJ 1141 Brackets Sequence
    POJ 1050 To the Max
    HDOJ 1029 Ignatius and the Princess IV
    POJ 2247 Humble Numbers
    HDOJ 1181 变形课
  • 原文地址:https://www.cnblogs.com/ghgyj/p/4027907.html
Copyright © 2011-2022 走看看