Java List Print printBox(List data)

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

Description

print a box

License

Open Source License

Parameter

Parameter Description
data a parameter

Declaration

public static void printBox(List<String> data) 

Method Source Code

//package com.java2s;
/*/*from  www . j a v  a 2  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 a box
     *
     * @param data
     */
    public static void printBox(List<String> data) {
        if (data == null || data.size() <= 0) {
            return;
        }
        int size = 0;
        for (String line : data) {
            if (line.length() > size) {
                size = line.length();
            }
        }
        int adjustedSize = size + 4; //Add 4 to the size for "| " and " |" in the box.
        System.out.println(singleHorizontalLine(adjustedSize));
        for (String line : data) {
            System.out.println(boxLine(line, adjustedSize));
        }
        System.out.println(singleHorizontalLine(adjustedSize));
    }

    /**
     * create a single horiz line
     *
     * @param size
     * @return
     */
    public static String singleHorizontalLine(int size) {
        return horizontalLine(size, '-');
    }

    /**
     * create a box line
     *
     * @param line
     * @param size
     * @return
     */
    private static String boxLine(String line, int size) {
        return "| " + line + getSpaces(size - line.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. print(List list)
  2. print(Map> map)
  3. print_int(List l)
  4. printArgument(List parameters, StringBuilder sb)
  5. printBoard(List> board)
  6. printCollection(List s)
  7. printDataPoint(List dataPoint)
  8. printDelimited(final List list, final String delimiter)
  9. printEachLine(List list)

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