Java Collection to String collectionToString(Collection c)

Here you can find the source of collectionToString(Collection c)

Description

An implementation of Collection.toString() suitable for classes with locks.

License

Creative Commons License

Declaration

static String collectionToString(Collection<?> c) 

Method Source Code

//package com.java2s;
import java.util.Collection;

public class Main {
    /**//from w  w w . j a va  2  s . co  m
     * An implementation of Collection.toString() suitable for classes
     * with locks.  Instead of holding a lock for the entire duration of
     * toString(), or acquiring a lock for each call to Iterator.next(),
     * we hold the lock only during the call to toArray() (less
     * disruptive to other threads accessing the collection) and follows
     * the maxim "Never call foreign code while holding a lock".
     */
    static String collectionToString(Collection<?> c) {
        final Object[] a = c.toArray();
        final int size = a.length;
        if (size == 0)
            return "[]";
        int charLength = 0;

        // Replace every array element with its string representation
        for (int i = 0; i < size; i++) {
            Object e = a[i];
            // Extreme compatibility with AbstractCollection.toString()
            String s = (e == c) ? "(this Collection)" : objectToString(e);
            a[i] = s;
            charLength += s.length();
        }

        return toString(a, size, charLength);
    }

    private static String objectToString(Object x) {
        // Extreme compatibility with StringBuilder.append(null)
        String s;
        return (x == null || (s = x.toString()) == null) ? "null" : s;
    }

    /**
     * Like Arrays.toString(), but caller guarantees that size > 0,
     * each element with index 0 <= i < size is a non-null String,
     * and charLength is the sum of the lengths of the input Strings.
     */
    static String toString(Object[] a, int size, int charLength) {
        // assert a != null;
        // assert size > 0;

        // Copy each string into a perfectly sized char[]
        // Length of [ , , , ] == 2 * size
        final char[] chars = new char[charLength + 2 * size];
        chars[0] = '[';
        int j = 1;
        for (int i = 0; i < size; i++) {
            if (i > 0) {
                chars[j++] = ',';
                chars[j++] = ' ';
            }
            String s = (String) a[i];
            int len = s.length();
            s.getChars(0, len, chars, j);
            j += len;
        }
        chars[j] = ']';
        // assert j == chars.length - 1;
        return new String(chars);
    }
}

Related

  1. collectionToString(Collection collection, String delimiter)
  2. collectionToString(Collection collection, String seperator)
  3. collectionToString(Collection list, String delimiter)
  4. collectionToString(Collection c)
  5. collectionToString(Collection c)
  6. collectionToString(Collection c)
  7. collectionToString(Collection c, String separator)
  8. collectionToString(Collection coll, String connectSymbol)
  9. collectionToString(Collection collection, String separator)