+ Added StatementBuilder for use in places where we'll be doing a lot more conditional output construction.

This commit is contained in:
Michael L Smith 2016-02-22 19:22:04 -08:00
parent 4527a3cd33
commit 0e47d85f42
2 changed files with 65 additions and 0 deletions

View File

@ -43,6 +43,7 @@
</ItemGroup>
<ItemGroup>
<Compile Include="Ids.cs" />
<Compile Include="Scripting\StatementBuilder.cs" />
<Compile Include="Sizes.cs" />
<Compile Include="Spatial\Bounds.cs" />
<Compile Include="Spatial\Matrix.cs" />

View File

@ -0,0 +1,64 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace OSCADSharp.Scripting
{
/// <summary>
/// Extends the capabilities of StringBuilder with domain-specific behavior
/// </summary>
internal class StatementBuilder
{
private StringBuilder SB { get; set; } = new StringBuilder();
/// <summary>
/// Special append method for conditionally adding value-pairs
/// </summary>
/// <param name="name">The Name of the value-pair</param>
/// <param name="value">The value - if null this method does nothing</param>
/// <param name="prefixWithComma">(optional) Flag indicating whether a comma should be added before the value-pair</param>
public void AppendValuePairIfExists(string name, object value, bool prefixWithComma = false)
{
if (!String.IsNullOrEmpty(value?.ToString()))
{
if (prefixWithComma)
{
SB.Append(", ");
}
SB.Append(name);
SB.Append("=");
SB.Append(value);
}
}
/// <summary>
/// Pass-through for StringBuilder.Append
/// </summary>
/// <param name="text"></param>
public void Append(string text)
{
SB.Append(text);
}
/// <summary>
/// Pass-through for StringBuilder.AppendLine
/// </summary>
/// <param name="text"></param>
public void AppendLine(string text)
{
SB.AppendLine(text);
}
/// <summary>
/// Gets this builder's full string
/// </summary>
/// <returns></returns>
public override string ToString()
{
return SB.ToString();
}
}
}