Java Collection to String collectionToString(final Collection c)

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

Description

Converts a Collection into a String

License

Open Source License

Parameter

Parameter Description
c the Collection

Return

the String

Declaration

public final static String collectionToString(final Collection<?> c) 

Method Source Code

//package com.java2s;
/**//  w  w w . j a  v  a2 s.c o m
 * The contents of this file are subject to the Regenstrief Public License
 * Version 1.0 (the "License"); you may not use this file except in compliance with the License.
 * Please contact Regenstrief Institute if you would like to obtain a copy of the license.
 *
 * Software distributed under the License is distributed on an "AS IS"
 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
 * License for the specific language governing rights and limitations
 * under the License.
 *
 * Copyright (C) Regenstrief Institute.  All Rights Reserved.
 */

import java.util.*;

public class Main {
    /**
     * Converts a Collection into a String
     * 
     * @param c the Collection
     * @return the String
     **/
    public final static String collectionToString(final Collection<?> c) {
        final Iterator<?> iter = c == null ? null : c.iterator();

        if (!hasNext(iter)) {
            return null;
        }

        final StringBuilder sb = new StringBuilder();
        for (; iter.hasNext();) {
            final String s = toString(iter.next());
            if (s != null) {
                sb.append(s);
            }
            if (iter.hasNext()) {
                sb.append('\n');
            }
        }

        return sb.toString();
    }

    public final static <E> Iterator<E> iterator(final Iterable<E> iterable) {
        return iterable == null ? null : iterable.iterator();
    }

    public final static boolean hasNext(final Iterator<?> i) {
        return (i != null) && i.hasNext();
    }

    /**
     * Converts an object to a string
     * 
     * @param o the Object to convert
     * @return the String
     **/
    public final static String toString(final Object o) {
        return o == null ? null : o.toString();
    }

    /**
     * Appends the given character to the given StringBuffer, allocating the StringBuffer if
     * necessary
     * 
     * @param sb the StringBuffer
     * @param c the char
     * @return the StringBuffer
     **/
    public final static StringBuffer append(final StringBuffer sb, final char c) {
        return (sb == null ? new StringBuffer() : sb).append(c);
    }

    /**
     * Appends the given String to the given StringBuffer, allocating the StringBuffer if necessary
     * 
     * @param sb the StringBuffer
     * @param s the String
     * @return the StringBuffer
     **/
    public final static StringBuffer append(final StringBuffer sb, final String s) {
        return s == null ? sb : (sb == null ? new StringBuffer() : sb).append(s);
    }

    /**
     * Appends part of the given CharSequence to the given StringBuffer, allocating the StringBuffer
     * if necessary
     * 
     * @param sb the StringBuffer
     * @param s the String
     * @param offset the offset of the CharSequence at which to start copying
     * @param len the number of characters to copy
     * @return the StringBuffer
     **/
    public final static StringBuffer append(StringBuffer sb, final CharSequence s, int offset, int len) {
        if (sb == null) {
            sb = new StringBuffer(len);
        }
        sb.ensureCapacity(sb.length() + len);
        for (len += offset; offset < len; offset++) {
            sb.append(s.charAt(offset));
        }

        return sb;
    }

    /**
     * Appends the given character to the given StringBuilder, allocating the StringBuilder if
     * necessary
     * 
     * @param sb the StringBuilder
     * @param c the char
     * @return the StringBuilder
     **/
    public final static StringBuilder append(final StringBuilder sb, final char c) {
        return ((sb == null) ? new StringBuilder() : sb).append(c);
    }

    /**
     * Appends the given String to the given StringBuilder, allocating the StringBuilder if necessary
     * 
     * @param sb the StringBuilder
     * @param s the String
     * @return the StringBuilder
     **/
    public final static StringBuilder append(final StringBuilder sb, final String s) {
        return (s == null) ? sb : ((sb == null) ? new StringBuilder() : sb).append(s);
    }

    public final static <E> void ensureCapacity(final Collection<E> col, final int minCapacity) {
        if (col instanceof ArrayList) {
            ((ArrayList<E>) col).ensureCapacity(minCapacity);
        }
    }

    /**
     * Returns the length of the given array
     * 
     * @param array the array for which to return the length
     * @return the length
     **/
    public final static int length(final Object array) {
        if (array == null) {
            return 0;
        } else if (array instanceof byte[]) {
            return ((byte[]) array).length;
        } else if (array instanceof short[]) {
            return ((short[]) array).length;
        } else if (array instanceof int[]) {
            return ((int[]) array).length;
        } else if (array instanceof long[]) {
            return ((long[]) array).length;
        } else if (array instanceof float[]) {
            return ((float[]) array).length;
        } else if (array instanceof double[]) {
            return ((double[]) array).length;
        } else if (array instanceof boolean[]) {
            return ((boolean[]) array).length;
        } else if (array instanceof char[]) {
            return ((char[]) array).length;
        }
        return ((Object[]) array).length;
    }

    /**
     * Returns the length of the given array
     * 
     * @param array the array for which to return the length
     * @return the length
     **/
    public final static int length(final Object[] array) {
        return array == null ? 0 : array.length;
    }

    /**
     * Returns the length of the given array
     * 
     * @param array the array for which to return the length
     * @return the length
     **/
    public final static int length(final byte[] array) {
        return array == null ? 0 : array.length;
    }

    /**
     * Returns the length of the given array
     * 
     * @param array the array for which to return the length
     * @return the length
     **/
    public final static int length(final int[] array) {
        return array == null ? 0 : array.length;
    }

    /**
     * Retrieves the length of the CharSequence
     * 
     * @param s the CharSequence
     * @return the length
     **/
    public final static int length(final CharSequence s) {
        return s == null ? 0 : s.length();
    }
}

Related

  1. collectionToString(Collection col, String sep)
  2. collectionToString(Collection collection)
  3. collectionToString(Collection list)
  4. collectionToString(final Collection collection, final String recordSeparator)
  5. collectionToString(final Collection aCol, final String aSepLines)
  6. collectionToString(final Collection collection, final String separator)
  7. collectionToString(final Collection elements, final String separator)
  8. collectionToString(final String separator, final Collection coll)
  9. collectionToString(Iterable c)