From ea73351d39874b1e889f11359ed4f626d0d6f030 Mon Sep 17 00:00:00 2001 From: Chris Cameron Date: Mon, 28 Jan 2019 16:58:21 -0500 Subject: [PATCH] refactor: Removing redundant critical section, support for chaining TableBuilder methods --- ICD.Common.Utils/TableBuilder.cs | 60 ++++++++++++++------------------ 1 file changed, 27 insertions(+), 33 deletions(-) diff --git a/ICD.Common.Utils/TableBuilder.cs b/ICD.Common.Utils/TableBuilder.cs index a10ad03..1ccee6a 100644 --- a/ICD.Common.Utils/TableBuilder.cs +++ b/ICD.Common.Utils/TableBuilder.cs @@ -17,7 +17,6 @@ namespace ICD.Common.Utils private const char INTERSECT = '+'; private readonly List m_Rows; - private readonly SafeCriticalSection m_RowsSection; private readonly string[] m_Columns; /// @@ -39,7 +38,6 @@ namespace ICD.Common.Utils public TableBuilder(params string[] columns) { m_Rows = new List(); - m_RowsSection = new SafeCriticalSection(); m_Columns = columns; } @@ -49,9 +47,10 @@ namespace ICD.Common.Utils /// Clears all of the rows. /// [PublicAPI] - public void ClearRows() + public TableBuilder ClearRows() { - m_RowsSection.Execute(() => m_Rows.Clear()); + m_Rows.Clear(); + return this; } /// @@ -59,11 +58,11 @@ namespace ICD.Common.Utils /// /// [PublicAPI] - public void AddRow(params object[] row) + public TableBuilder AddRow(params object[] row) { string[] stringRow = row.Select(o => string.Format("{0}", o)) .ToArray(); - AddRow(stringRow); + return AddRow(stringRow); } /// @@ -71,31 +70,33 @@ namespace ICD.Common.Utils /// /// [PublicAPI] - public void AddRow(params string[] row) + public TableBuilder AddRow(params string[] row) { if (row != null && row.Length != m_Columns.Length) throw new ArgumentException("Row must match columns length."); - m_RowsSection.Execute(() => m_Rows.Add(row)); + m_Rows.Add(row); + + return this; } /// /// Adds an empty row to the builder. /// [PublicAPI] - public void AddEmptyRow() + public TableBuilder AddEmptyRow() { - AddRow(new string[m_Columns.Length]); + return AddRow(new string[m_Columns.Length]); } [PublicAPI] - public void AddSeparator() + public TableBuilder AddSeparator() { - AddRow(null); + return AddRow(null); } [PublicAPI] - public void AddHeader(params string[] row) + public TableBuilder AddHeader(params string[] row) { if (row.Length != m_Columns.Length) throw new ArgumentException("Row must match columns length."); @@ -103,6 +104,8 @@ namespace ICD.Common.Utils AddSeparator(); AddRow(row); AddSeparator(); + + return this; } /// @@ -112,30 +115,21 @@ namespace ICD.Common.Utils { StringBuilder sb = new StringBuilder(); - m_RowsSection.Enter(); + int[] columnWidths = GetColumnWidths(); - try + AppendRow(sb, m_Columns, columnWidths); + AppendSeparator(sb, columnWidths); + + foreach (string[] row in m_Rows) { - int[] columnWidths = GetColumnWidths(); - - AppendRow(sb, m_Columns, columnWidths); - AppendSeparator(sb, columnWidths); - - foreach (string[] row in m_Rows) - { - if (row == null) - AppendSeparator(sb, columnWidths); - else - AppendRow(sb, row, columnWidths); - } - - AppendSeparator(sb, columnWidths); - } - finally - { - m_RowsSection.Leave(); + if (row == null) + AppendSeparator(sb, columnWidths); + else + AppendRow(sb, row, columnWidths); } + AppendSeparator(sb, columnWidths); + return sb.ToString(); }