View Javadoc

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