Java Collection Print prettyPrint(Collection input)

Here you can find the source of prettyPrint(Collection input)

Description

Pretty print of a collection.

License

Open Source License

Parameter

Parameter Description
input the collection that should be printed

Return

the String representation

Declaration

public static String prettyPrint(Collection<?> input) 

Method Source Code

//package com.java2s;
/*****************************************************************************
 * This file is part of the Prolog Development Tool (PDT)
 * /* ww w .ja  v  a2  s  . c o  m*/
 * Author: Lukas Degener (among others)
 * WWW: http://sewiki.iai.uni-bonn.de/research/pdt/start
 * Mail: pdt@lists.iai.uni-bonn.de
 * Copyright (C): 2004-2012, CS Dept. III, University of Bonn
 * 
 * All rights reserved. This program is  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
 * 
 ****************************************************************************/

import java.util.Collection;

import java.util.Iterator;
import java.util.Map;
import java.util.Set;

public class Main {
    /**
     * Pretty print of a Map.
     * 
     * @param input the map that should be printed
     * @return the String representation
     */
    public static String prettyPrint(Map<String, ?> input) {
        if (input != null) {
            boolean first = true;
            StringBuffer result = new StringBuffer();
            Set<String> keys = input.keySet();
            Iterator<String> it = keys.iterator();
            while (it.hasNext()) {
                if (!first) {
                    result.append(", ");
                }
                String key = it.next();
                Object value = input.get(key);
                String valueAsString = value.toString();
                result.append(key + "-->" + valueAsString);
                first = false;

            }
            return result.toString();
        }
        return "";
    }

    /**
     * Pretty print of an array.
     *  
     * @param a the array that should be printed
     * @return the String representation
     */
    public static String prettyPrint(Object[] a) {
        if (a == null) {
            return "";
        }
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < a.length; i++) {
            if (i > 0) {
                sb.append(", ");
            }
            sb.append(a[i].toString());
        }
        return sb.toString();
    }

    /**
     * Pretty print of a collection.
     *  
     * @param input the collection that should be printed
     * @return the String representation
     */
    public static String prettyPrint(Collection<?> input) {
        if (input != null && !input.isEmpty()) {
            Iterator<?> it = input.iterator();
            return concatinateElements(it);
        }
        return "";
    }

    private static String concatinateElements(Iterator<?> it) {
        StringBuffer sb = new StringBuffer();
        boolean first = true;
        while (it.hasNext()) {
            if (!first) {
                sb.append(", ");
            }
            Object next = it.next();
            String elm = next == null ? "<null>" : next.toString();
            sb.append(elm);
            first = false;
        }
        return sb.toString();
    }
}

Related

  1. prettyPrint(Collection c)
  2. prettyPrint(Collection allGroups)
  3. prettyPrintCollection(Collection collection)
  4. prettyPrintListOfClasses( Collection package1DependenciesOnPackage2)
  5. print(Collection c)