Java List Print prettyPrint(List rows)

Here you can find the source of prettyPrint(List rows)

Description

Pretty prints a list of String arrays.

License

Apache License

Parameter

Parameter Description
rows the rows to print

Declaration

private static void prettyPrint(List<String[]> rows) 

Method Source Code

//package com.java2s;
/**//www .  jav a  2s .c o  m
 * Copyright 2009-2013 NetRadius, LLC
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.util.*;

public class Main {
    /**
     * Pretty prints a list of String arrays. This method assumes the first array in the list
     * is the header.
     *
     * @param rows the rows to print
     */
    private static void prettyPrint(List<String[]> rows) {
        if (!rows.isEmpty()) {
            final int numCol = rows.get(0).length;
            final int[] maxLength = new int[numCol];
            Arrays.fill(maxLength, 0);
            for (String[] row : rows) {
                for (int i = row.length - 1; i >= 0; i--) {
                    if (row[i] == null && maxLength[i] < 4)
                        maxLength[i] = 4;
                    else if (row[i] != null && row[i].length() > maxLength[i])
                        maxLength[i] = row[i].length();
                }
            }
            final StringBuilder sb = new StringBuilder();
            int totalLength = 0;
            for (int i = 0; i < maxLength.length; i++) {
                totalLength += maxLength[i];
                if (i == 0)
                    sb.append("| ");
                sb.append("%").append(i + 1).append("$-").append(maxLength[i]).append("s | ");
                if (i == maxLength.length - 1)
                    sb.append("\n");
            }
            totalLength += numCol * 3 + 1;
            final String pattern = sb.toString();
            final Formatter formatter = new Formatter(System.out);
            System.out.print(line('=', totalLength));
            for (int i = 0; i < rows.size(); i++) {
                formatter.format(pattern, (Object[]) rows.get(i));
                if (i == 0)
                    System.out.print(line('=', totalLength));
                else
                    System.out.print(line('-', totalLength));
            }
        }
    }

    private static String line(char c, int num) {
        final StringBuilder sb = new StringBuilder();
        for (; num > 0; num--) {
            sb.append(c);
        }
        return sb.append("\n").toString();
    }
}

Related

  1. getGenBlueprintName(String slavenodelist, List masterList)
  2. limitLengthOfPrintedList(Collection list)
  3. prettyList(final List list)
  4. prettyList(List list)
  5. prettyPrint(List userPermissions)
  6. prettyPrintArgv(List argv)
  7. prettyPrintList(Collection objects, String separator)
  8. prettyPrintList(List list)
  9. prettyPrintWarningMessage(final List results, final String message)