Java List Print printTable(List data)

Here you can find the source of printTable(List data)

Description

print the table

License

Open Source License

Parameter

Parameter Description
data a parameter

Declaration

public static void printTable(List<String> data) 

Method Source Code

//package com.java2s;
/*//  www . j  a v a2  s. c o  m
 * Copyright (c) 2009 The Regents of the University of California.
 * All rights reserved.
 *
 * Permission is hereby granted, without written agreement and without
 * license or royalty fees, to use, copy, modify, and distribute this
 * software and its documentation for any purpose, provided that the above
 * copyright notice and the following two paragraphs appear in all copies
 * of this software.
 *
 * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY
 * FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
 * THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
 * PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF
 * CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,
 * ENHANCEMENTS, OR MODIFICATIONS.
 *
 */

import java.util.List;

public class Main {
    /**
     * print the table
     *
     * @param data
     */
    public static void printTable(List<String> data) {
        if (isNotProperTableData(data)) {
            return;
        }

        int numColumns = data.get(0).split("\\s").length;

        int size = 0;
        for (int i = 0; i < numColumns; i++) {
            size += getColumnSize(data, i);
        }
        int adjustedSize = size + 6 + (numColumns - 1) * 3; //"|| ", " | ", " ||"

        System.out.println(doubleHorizontalLine(adjustedSize));
        for (int i = 0; i < data.size(); i++) {
            String line = data.get(i);
            System.out.print("||");
            String[] columnData = line.split("\\s");
            for (int j = 0; j < numColumns; j++) {
                System.out.print(" " + columnData[j]
                        + getCellSpaces(data, j, columnData[j]) + " |");
            }
            System.out.println("|");
            //         if( i != data.size() - 1)
            //            System.out.println( tableHorizontalLine(data,numColumns) );
        }
        System.out.println(doubleHorizontalLine(adjustedSize));
    }

    /**
     * return true if data is not proper table data
     *
     * @param data
     * @return
     */
    private static boolean isNotProperTableData(List<String> data) {
        if (data == null || data.size() == 0) {
            return true;
        }
        int numColumns = data.get(0).split("\\s").length;
        if (numColumns == 0) {
            return true;
        }
        for (String line : data) {
            if (line.split("\\s").length != numColumns) {
                return true;
            }
        }
        return false;
    }

    /**
     * get the column size
     *
     * @param data
     * @param columnIndex
     * @return
     */
    private static int getColumnSize(List<String> data, int columnIndex) {
        int size = 0;
        for (String line : data) {
            int lineLength = line.split("\\s")[columnIndex].length();
            if (size < lineLength) {
                size = lineLength;
            }
        }
        return size;
    }

    /**
     * create a double horiz line
     *
     * @param size
     * @return
     */
    public static String doubleHorizontalLine(int size) {
        return horizontalLine(size, '=');
    }

    /**
     * return spaces for a cell
     *
     * @param data
     * @param columnIndex
     * @param columnData
     * @return
     */
    private static String getCellSpaces(List<String> data, int columnIndex,
            String columnData) {
        return getSpaces(getColumnSize(data, columnIndex)
                - columnData.length());
    }

    /**
     * create a horiz line
     *
     * @param size
     * @param c
     * @return
     */
    private static String horizontalLine(int size, char c) {
        String line = "";
        for (int i = 0; i < size; i++) {
            line += c;
        }
        return line;
    }

    /**
     * return n spaces
     *
     * @param n
     * @return
     */
    private static String getSpaces(int n) {
        String spaces = "";
        for (int i = 0; i < n; i++) {
            spaces += " ";
        }
        return spaces;
    }
}

Related

  1. printRules(List rules, Class clazz)
  2. printStats(final List shocktimes)
  3. printStringList(List list, String name)
  4. printStringList(List listToPrint)
  5. printStringList(List strings)
  6. printTopNTokens(LinkedList> list, int n)
  7. printTuple(List tuple)
  8. printTwoColumns(List left, List right, int leftWidth, int rightWidth, int sep)
  9. printValues(List values)

  10. HOME | Copyright © www.java2s.com 2016