View Javadoc
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  public class SimpleLoggingTable
18  {
19      protected static final String NEW_LINE = String.format("%n");
20      private static final char SEPARATOR_CHAR = '*';
21  
22      private List<TableColumn> columns = new LinkedList<TableColumn>();
23      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          columns.add(new TableColumn(title, size));
35          width = calculateTableWidth();
36      }
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          if (dataRow.length != columns.size())
47          {
48              throw new IllegalArgumentException("Data does not contain enough elements");
49          }
50  
51          data.add(dataRow.clone());
52      }
53  
54      @Override
55      public String toString()
56      {
57          StringBuilder builder = new StringBuilder();
58  
59          addHeaders(builder);
60  
61          addRows(builder);
62  
63          return builder.toString();
64      }
65  
66      private void addHeaders(StringBuilder builder)
67      {
68          addSeparatorLine(builder);
69  
70          for (TableColumn column : columns)
71          {
72              builder.append(String.format("%c %s ", SEPARATOR_CHAR, formatHeaderValue(column.name, column.width)));
73          }
74          builder.append(SEPARATOR_CHAR).append(NEW_LINE);
75  
76          addSeparatorLine(builder);
77      }
78  
79      private void addRows(StringBuilder builder)
80      {
81          for (String[] row : data)
82          {
83              for (int i = 0; i < row.length; i++)
84              {
85                  builder.append(String.format("%c %s ", SEPARATOR_CHAR, formatValue(row[i], columns.get(i).width)));
86              }
87              builder.append(SEPARATOR_CHAR).append(NEW_LINE);
88          }
89  
90          addSeparatorLine(builder);
91      }
92  
93      private void addSeparatorLine(StringBuilder builder)
94      {
95          builder.append(StringUtils.repeat(SEPARATOR_CHAR, width));
96          builder.append(NEW_LINE);
97      }
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         int result = 0;
108 
109         for (TableColumn column : columns)
110         {
111             // Count three chars more per column: a separator char + 2 white spaces
112             result = result + column.width + 3;
113         }
114 
115         // Counts another separator at the right end
116         result++;
117 
118         return result;
119     }
120 
121     private String formatValue(String value, int size)
122     {
123         String result = StringUtils.substring(value, 0, size);
124         result = StringUtils.rightPad(result, size, ' ');
125 
126         return result;
127     }
128 
129     private String formatHeaderValue(String value, int size)
130     {
131         String result = StringUtils.substring(value, 0, size);
132         result = StringUtils.center(String.format("- - + %s + - -", result), size, ' ');
133 
134         return result;
135     }
136 
137     private class TableColumn
138     {
139 
140         protected final String name;
141         protected final int width;
142 
143         public TableColumn(String name, int width)
144         {
145             this.name = name;
146             this.width = width;
147         }
148     }
149 }