mirror of
https://github.com/ICDSystems/ICD.Common.Utils.git
synced 2026-02-16 21:24:58 +00:00
refactor: Removing redundant critical section, support for chaining TableBuilder methods
This commit is contained in:
@@ -17,7 +17,6 @@ namespace ICD.Common.Utils
|
|||||||
private const char INTERSECT = '+';
|
private const char INTERSECT = '+';
|
||||||
|
|
||||||
private readonly List<string[]> m_Rows;
|
private readonly List<string[]> m_Rows;
|
||||||
private readonly SafeCriticalSection m_RowsSection;
|
|
||||||
private readonly string[] m_Columns;
|
private readonly string[] m_Columns;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -39,7 +38,6 @@ namespace ICD.Common.Utils
|
|||||||
public TableBuilder(params string[] columns)
|
public TableBuilder(params string[] columns)
|
||||||
{
|
{
|
||||||
m_Rows = new List<string[]>();
|
m_Rows = new List<string[]>();
|
||||||
m_RowsSection = new SafeCriticalSection();
|
|
||||||
m_Columns = columns;
|
m_Columns = columns;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -49,9 +47,10 @@ namespace ICD.Common.Utils
|
|||||||
/// Clears all of the rows.
|
/// Clears all of the rows.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
[PublicAPI]
|
[PublicAPI]
|
||||||
public void ClearRows()
|
public TableBuilder ClearRows()
|
||||||
{
|
{
|
||||||
m_RowsSection.Execute(() => m_Rows.Clear());
|
m_Rows.Clear();
|
||||||
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -59,11 +58,11 @@ namespace ICD.Common.Utils
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="row"></param>
|
/// <param name="row"></param>
|
||||||
[PublicAPI]
|
[PublicAPI]
|
||||||
public void AddRow(params object[] row)
|
public TableBuilder AddRow(params object[] row)
|
||||||
{
|
{
|
||||||
string[] stringRow = row.Select(o => string.Format("{0}", o))
|
string[] stringRow = row.Select(o => string.Format("{0}", o))
|
||||||
.ToArray();
|
.ToArray();
|
||||||
AddRow(stringRow);
|
return AddRow(stringRow);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -71,31 +70,33 @@ namespace ICD.Common.Utils
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="row"></param>
|
/// <param name="row"></param>
|
||||||
[PublicAPI]
|
[PublicAPI]
|
||||||
public void AddRow(params string[] row)
|
public TableBuilder AddRow(params string[] row)
|
||||||
{
|
{
|
||||||
if (row != null && row.Length != m_Columns.Length)
|
if (row != null && row.Length != m_Columns.Length)
|
||||||
throw new ArgumentException("Row must match columns length.");
|
throw new ArgumentException("Row must match columns length.");
|
||||||
|
|
||||||
m_RowsSection.Execute(() => m_Rows.Add(row));
|
m_Rows.Add(row);
|
||||||
|
|
||||||
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Adds an empty row to the builder.
|
/// Adds an empty row to the builder.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
[PublicAPI]
|
[PublicAPI]
|
||||||
public void AddEmptyRow()
|
public TableBuilder AddEmptyRow()
|
||||||
{
|
{
|
||||||
AddRow(new string[m_Columns.Length]);
|
return AddRow(new string[m_Columns.Length]);
|
||||||
}
|
}
|
||||||
|
|
||||||
[PublicAPI]
|
[PublicAPI]
|
||||||
public void AddSeparator()
|
public TableBuilder AddSeparator()
|
||||||
{
|
{
|
||||||
AddRow(null);
|
return AddRow(null);
|
||||||
}
|
}
|
||||||
|
|
||||||
[PublicAPI]
|
[PublicAPI]
|
||||||
public void AddHeader(params string[] row)
|
public TableBuilder AddHeader(params string[] row)
|
||||||
{
|
{
|
||||||
if (row.Length != m_Columns.Length)
|
if (row.Length != m_Columns.Length)
|
||||||
throw new ArgumentException("Row must match columns length.");
|
throw new ArgumentException("Row must match columns length.");
|
||||||
@@ -103,6 +104,8 @@ namespace ICD.Common.Utils
|
|||||||
AddSeparator();
|
AddSeparator();
|
||||||
AddRow(row);
|
AddRow(row);
|
||||||
AddSeparator();
|
AddSeparator();
|
||||||
|
|
||||||
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -112,30 +115,21 @@ namespace ICD.Common.Utils
|
|||||||
{
|
{
|
||||||
StringBuilder sb = new StringBuilder();
|
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();
|
if (row == null)
|
||||||
|
AppendSeparator(sb, columnWidths);
|
||||||
AppendRow(sb, m_Columns, columnWidths);
|
else
|
||||||
AppendSeparator(sb, columnWidths);
|
AppendRow(sb, row, 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();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
AppendSeparator(sb, columnWidths);
|
||||||
|
|
||||||
return sb.ToString();
|
return sb.ToString();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user