Java Collection Convert toCollectionString(Collection collection_p)

Here you can find the source of toCollectionString(Collection collection_p)

Description

Return a string representation of the given collection which is typically suitable as a match ID representation

License

Open Source License

Parameter

Parameter Description
collection_p a potentially null collection

Return

a non-null string

Declaration

public static String toCollectionString(Collection<?> collection_p) 

Method Source Code

//package com.java2s;
/**/*  w ww  .  j  a  v a  2s.  c om*/
 * <copyright>
 * 
 * Copyright (c) 2010-2012 Thales Global Services S.A.S.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    Thales Global Services S.A.S. - initial API and implementation
 * 
 * </copyright>
 */

import java.util.Collection;
import java.util.Iterator;

public class Main {
    /** A representation of the null value */
    public static final String NULL = "null";

    /**
     * Return a string representation of the given collection which is typically suitable
     * as a match ID representation
     * @param collection_p a potentially null collection
     * @return a non-null string
     */
    public static String toCollectionString(Collection<?> collection_p) {
        if (collection_p == null)
            return NULL;
        StringBuilder builder = new StringBuilder();
        builder.append('[');
        Iterator<?> it = collection_p.iterator();
        while (it.hasNext()) {
            Object element = it.next();
            String segment;
            if (element == collection_p)
                segment = "(this collection)"; //$NON-NLS-1$
            else if (element == null)
                segment = NULL;
            else
                segment = element.toString();
            builder.append(segment);
            if (it.hasNext())
                builder.append('.');
        }
        builder.append(']');
        return builder.toString();
    }
}

Related

  1. to2DIntArray(Collection coll)
  2. to2DStringArray(Collection coll)
  3. toAlphaCollection(Collection collection)
  4. toArray(Collection items, Object array[], boolean convert_to_primitive)
  5. toCollectionString(Collection c, char sep)
  6. toCommaDelimitedString(Collection c)
  7. toCommaDelimitedStringInQuotes(Collection c)
  8. toCommaSep(Collection strings)
  9. toCommaSeparatedString(final Collection items)