Coverage Report - org.mule.module.launcher.SimpleLoggingTable
 
Classes in this File Line Coverage Branch Coverage Complexity
SimpleLoggingTable
0%
0/42
0%
0/10
0
SimpleLoggingTable$TableColumn
0%
0/4
N/A
0
 
 1  
 /*
 2  
  * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
 3  
  * The software in this package is published under the terms of the CPAL v1.0
 4  
  * license, a copy of which has been included with this distribution in the
 5  
  * LICENSE.txt file.
 6  
  */
 7  
 package org.mule.module.launcher;
 8  
 
 9  
 import org.mule.util.StringUtils;
 10  
 
 11  
 import java.util.LinkedList;
 12  
 import java.util.List;
 13  
 
 14  
 /**
 15  
  * Provides a simple table implementation useful to log information in a tabular form.
 16  
  */
 17  0
 public class SimpleLoggingTable
 18  
 {
 19  0
     protected static final String NEW_LINE = String.format("%n");
 20  
     private static final char SEPARATOR_CHAR = '*';
 21  
 
 22  0
     private List<TableColumn> columns = new LinkedList<TableColumn>();
 23  0
     private final List<String[]> data = new LinkedList<String[]>();
 24  
     private int width;
 25  
 
 26  
     /**
 27  
      * Adds a new column to the table.
 28  
      *
 29  
      * @param title column title that will be displayed in the table header
 30  
      * @param size  the size of the column
 31  
      */
 32  
     public void addColumn(String title, int size)
 33  
     {
 34  0
         columns.add(new TableColumn(title, size));
 35  0
         width = calculateTableWidth();
 36  0
     }
 37  
 
 38  
     /**
 39  
      * Adds a new row of data into the table.
 40  
      *
 41  
      * @param dataRow the data to be added. DataRow must contain a value for
 42  
      *                each declared column.
 43  
      */
 44  
     public void addDataRow(String[] dataRow)
 45  
     {
 46  0
         if (dataRow.length != columns.size())
 47  
         {
 48  0
             throw new IllegalArgumentException("Data does not contain enough elements");
 49  
         }
 50  
 
 51  0
         data.add(dataRow.clone());
 52  0
     }
 53  
 
 54  
     @Override
 55  
     public String toString()
 56  
     {
 57  0
         StringBuilder builder = new StringBuilder();
 58  
 
 59  0
         addHeaders(builder);
 60  
 
 61  0
         addRows(builder);
 62  
 
 63  0
         return builder.toString();
 64  
     }
 65  
 
 66  
     private void addHeaders(StringBuilder builder)
 67  
     {
 68  0
         addSeparatorLine(builder);
 69  
 
 70  0
         for (TableColumn column : columns)
 71  
         {
 72  0
             builder.append(String.format("%c %s ", SEPARATOR_CHAR, formatHeaderValue(column.name, column.width)));
 73  
         }
 74  0
         builder.append(SEPARATOR_CHAR).append(NEW_LINE);
 75  
 
 76  0
         addSeparatorLine(builder);
 77  0
     }
 78  
 
 79  
     private void addRows(StringBuilder builder)
 80  
     {
 81  0
         for (String[] row : data)
 82  
         {
 83  0
             for (int i = 0; i < row.length; i++)
 84  
             {
 85  0
                 builder.append(String.format("%c %s ", SEPARATOR_CHAR, formatValue(row[i], columns.get(i).width)));
 86  
             }
 87  0
             builder.append(SEPARATOR_CHAR).append(NEW_LINE);
 88  
         }
 89  
 
 90  0
         addSeparatorLine(builder);
 91  0
     }
 92  
 
 93  
     private void addSeparatorLine(StringBuilder builder)
 94  
     {
 95  0
         builder.append(StringUtils.repeat(SEPARATOR_CHAR, width));
 96  0
         builder.append(NEW_LINE);
 97  0
     }
 98  
 
 99  
     /**
 100  
      * Calculates the real table width based on the column sizes and the extra
 101  
      * characters used to separate them.
 102  
      *
 103  
      * @return the real table width.
 104  
      */
 105  
     private int calculateTableWidth()
 106  
     {
 107  0
         int result = 0;
 108  
 
 109  0
         for (TableColumn column : columns)
 110  
         {
 111  
             // Count three chars more per column: a separator char + 2 white spaces
 112  0
             result = result + column.width + 3;
 113  
         }
 114  
 
 115  
         // Counts another separator at the right end
 116  0
         result++;
 117  
 
 118  0
         return result;
 119  
     }
 120  
 
 121  
     private String formatValue(String value, int size)
 122  
     {
 123  0
         String result = StringUtils.substring(value, 0, size);
 124  0
         result = StringUtils.rightPad(result, size, ' ');
 125  
 
 126  0
         return result;
 127  
     }
 128  
 
 129  
     private String formatHeaderValue(String value, int size)
 130  
     {
 131  0
         String result = StringUtils.substring(value, 0, size);
 132  0
         result = StringUtils.center(String.format("- - + %s + - -", result), size, ' ');
 133  
 
 134  0
         return result;
 135  
     }
 136  
 
 137  0
     private class TableColumn
 138  
     {
 139  
 
 140  
         protected final String name;
 141  
         protected final int width;
 142  
 
 143  
         public TableColumn(String name, int width)
 144  0
         {
 145  0
             this.name = name;
 146  0
             this.width = width;
 147  0
         }
 148  
     }
 149  
 }