Java Collection to String toString(Collection collection, String separator, int fixedLength)

Here you can find the source of toString(Collection collection, String separator, int fixedLength)

Description

Convert array into string in table format.

License

Open Source License

Parameter

Parameter Description
collection a parameter
separator a parameter
fixedLength a parameter

Declaration

public static String toString(Collection collection, String separator, int fixedLength) 

Method Source Code

//package com.java2s;
/*//w  w  w .  ja va  2s  .co m
Open Auto Trading : A fully automatic equities trading platform with machine learning capabilities
Copyright (C) 2015 AnyObject Ltd.
    
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
    
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.
    
You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.util.*;

public class Main {
    public static final String LINE_SEPARATOR = System.getProperty("line.separator");

    /**
     * Convert array into string in table format.
     *
     * @param collection
     * @param separator
     * @param fixedLength
     * @return
     */
    public static String toString(Collection collection, String separator, int fixedLength) {
        Iterator iter;

        if (collection == null || (!(iter = collection.iterator()).hasNext())) {
            return "";
        }

        StringBuilder sb = new StringBuilder(matchLength(String.valueOf(iter.next()), "", fixedLength));

        while (iter.hasNext()) {
            sb.append(separator).append(matchLength(String.valueOf(iter.next()), "", fixedLength));
        }

        return sb.toString();
    }

    /**
     * Convert array into string in table format.
     *
     * @param objects
     * @param separator
     * @param fixedLength
     * @return
     */
    public static String toString(Object[] objects, String separator, int fixedLength) {
        if (objects == null) {
            return "";
        }

        return toString(Arrays.asList(objects), separator, fixedLength);
    }

    /**
     * Return a table from the provided column heads and table.
     *
     * @param columnHeads
     * @param table
     * @param cellSeparator
     * @param cellwidth
     * @return
     */
    public static String toString(String[] columnHeads, Object[][] table, String cellSeparator, int cellwidth) {
        return toString(columnHeads, Arrays.asList(table), cellSeparator, cellwidth);
    }

    /**
     * Return a table from the provided column heads and table.
     *
     * @param columnHeads column names or the first row of the table
     * @param table a 2-dimensional array that holds the data
     * @param cellSeparator separator between columns, recommended "|"
     * @param cellwidth the maximum width (number of character) of each cell
     * @return
     */
    public static String toString(String[] columnHeads, List<Object[]> table, String cellSeparator, int cellwidth) {
        StringBuilder sb = new StringBuilder();

        //        sb.append("\n");

        sb.append(toString(columnHeads, cellSeparator, cellwidth)).append(LINE_SEPARATOR);

        for (Object[] objects : table) {
            sb.append(toString(objects, cellSeparator, cellwidth)).append(LINE_SEPARATOR);
        }

        sb.append(table.size()).append(" row");
        if (table.size() > 1) {
            sb.append("s");
        }

        return sb.toString();
    }

    /**
     * Converts a collection of objects into a string using the provide
     * separator.
     *
     * @param collection
     * @param separator
     * @return
     */
    public static String toString(Collection collection, String separator) {
        Iterator iter;

        if (collection == null || (!(iter = collection.iterator()).hasNext())) {
            return "";
        }

        StringBuilder sb = new StringBuilder(String.valueOf(iter.next()));

        while (iter.hasNext()) {
            sb.append(separator).append(String.valueOf(iter.next()));
        }

        return sb.toString();
    }

    /**
     * Converts a collection of objects into a string using ", ".
     *
     * @param collection
     * @return
     */
    public static String toString(Collection collection) {
        return toString(collection, ", ");
    }

    /**
     * Converts an array of objects into a string using the provide separator.
     *
     * @param objects
     * @param separator
     * @return
     */
    public static String toString(Object[] objects, String separator) {
        if (objects == null) {
            return "";
        }

        return toString(Arrays.asList(objects), separator);
    }

    /**
     * Converts an array of objects into a string using ", ".
     *
     * @param objects
     * @return
     */
    public static String toString(Object[] objects) {
        return toString(objects, ", ");
    }

    /**
     * Converts a 2-D array of objects into a string using the provided line
     * separator lineSeparator and column separator colSeparator.
     *
     * @param objects
     * @param lineSeparator
     * @param colSeparator
     * @return
     */
    public static String toString(Object[][] objects, String lineSeparator, String colSeparator) {
        if (objects == null) {
            return "";
        }

        StringBuilder sb = new StringBuilder();

        for (int i = 0; i < objects.length; i++) {
            if (sb.length() > 0) {
                sb.append(lineSeparator);
            }

            sb.append(toString(objects[i], colSeparator));
        }

        return sb.toString();
    }

    /**
     * Converts a 2-D array of objects into a string using line separator "\n"
     * and column separator ": ".
     *
     * @param objects
     * @return
     */
    public static String toString(Object[][] objects) {
        return toString(objects, LINE_SEPARATOR, ": ");
    }

    /**
     * Converts a map into a string using separator ";" and column separator ":
     * ".
     *
     * @param <K> key
     * @param <V> value
     * @param map any map
     * @return
     */
    public static <K, V> String toString(Map<K, V> map) {
        if (map == null || map.isEmpty()) {
            return null;
        }

        List<String> entries = new ArrayList<String>();

        for (Map.Entry entry : map.entrySet()) {
            entries.add(entry.getKey() + ": " + entry.getValue());
        }

        return toString(entries, ";");
    }

    /**
     * Return a new string that match the provided length. String with shorter
     * length will be added space at behind. String with longer length will be
     * truncated to the specified length.
     *
     *
     * @param object
     * @param symbol
     * @param length
     * @return
     */
    public static String matchLength(Object object, String symbol, int length) {
        String newString = String.valueOf(object).trim();

        if (length <= 0) {
            return newString;
        }

        if (newString.length() > length) {
            newString = newString.substring(0, length - symbol.length()) + symbol;

        } else {
            while (newString.length() < length) {
                newString += " ";
            }
        }

        return newString;
    }
}

Related

  1. toStr(Collection collection)
  2. toStr(Collection elements)
  3. toString(Collection c)
  4. toString(Collection c)
  5. toString(Collection c, String start, String separator, String end)
  6. toString(Collection objects)
  7. toString(Collection setOfTests)
  8. toString(Collection softwareTags)
  9. toString(Collection collection)