Home | History | Annotate | Download | only in util
      1 /*
      2  * Copyright (C) 2011 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 package com.android.tradefed.util;
     17 
     18 import java.io.PrintWriter;
     19 import java.util.ArrayList;
     20 import java.util.List;
     21 
     22 /**
     23  * Helper class to display a matrix of String elements so each element column is lined up
     24  */
     25 public class TableFormatter {
     26 
     27     private int mColumnSpacing = 2;
     28 
     29     /**
     30      * Sets the number of whitespace characters between each column.
     31      *
     32      * @param spacing the number of whitespace characters
     33      * @return the {@link TableFormatter}
     34      */
     35     public TableFormatter setColumnSpacing(int spacing) {
     36         mColumnSpacing = spacing;
     37         return this;
     38     }
     39 
     40     /**
     41      * Display given String elements as an table with aligned columns.
     42      *
     43      * @param table a matrix of String elements. Rows can be different length
     44      * @param writer the {@link PrintWriter} to dump output to
     45      */
     46     public void displayTable(List<List<String>> table, PrintWriter writer) {
     47         if (table.size() <= 0) {
     48             throw new IllegalArgumentException();
     49         }
     50         // build index of max column sizes
     51         List<Integer> maxColumnSizes = getColumnSizes(table);
     52         for (List<String> rowData : table) {
     53             for (int col = 0; col < rowData.size(); col++) {
     54                 writer.append(rowData.get(col));
     55                 int numPaddingChars = maxColumnSizes.get(col) - rowData.get(col).length();
     56                 insertPadding(numPaddingChars, writer);
     57             }
     58             writer.println();
     59         }
     60     }
     61 
     62     private void insertPadding(int numChars, PrintWriter writer) {
     63         for (int i = 0; i < numChars; i++) {
     64             writer.append(' ');
     65         }
     66     }
     67 
     68     private List<Integer> getColumnSizes(List<List<String>> table) {
     69         List<Integer> maxColumnSizes = new ArrayList<Integer>();
     70         for (List<String> rowData : table) {
     71             for (int colIndex = 0; colIndex < rowData.size(); colIndex++) {
     72                 if (colIndex >= maxColumnSizes.size()) {
     73                     maxColumnSizes.add(colIndex, 0);
     74                 }
     75                 // add column spacing to string length so there is at least that amount of space
     76                 // between columns
     77                 int stringSize = rowData.get(colIndex).length() + mColumnSpacing;
     78                 if (maxColumnSizes.get(colIndex) < stringSize) {
     79                     maxColumnSizes.set(colIndex, stringSize);
     80                 }
     81             }
     82         }
     83 
     84         return maxColumnSizes;
     85     }
     86 }
     87