using System;
using System.Text;

namespace ExtensionMethods
{
	/// <summary>
	/// <para>Add extensions to <c><see cref="Byte"/></c></para>
	/// </summary>
	public static class ByteExtensions
	{
		/// <summary>
		/// Convert the given <see cref="Nullable"/> <see cref="byte"/> to a string, but return null if <paramref name="Byte"/> == null
		/// </summary>
		/// <param name="Byte"></param>
		/// <returns>null or <see cref="Byte.ToString()"/></returns>
		public static string ToNullableString(this byte? Byte)
		{
			if (Byte == null)
				return null;
			return Byte.ToString();
		}

		/// <summary>
		/// <para>Concatenates a speceified separator <see cref="string"/> between each element of a specified <see cref="sbyte"/> array, yielding a single concatenated string</para>
		/// </summary>
		/// <param name="array"></param>
		/// <param name="separator">A <see cref="string"/></param>
		/// <returns>The concatenated <see cref="string"/></returns>
		public static string Join(this sbyte[] array, string separator)
		{
			StringBuilder concat = new StringBuilder();
			bool notFirst = false;
			foreach (sbyte item in array)
			{
				if (notFirst)
					concat.Append(separator);
				else
					notFirst = true;
				concat.Append(item);
			}
			return concat.ToString();
		}
		/// <summary>
		/// <para>Concatenates a speceified separator <see cref="string"/> between each element of a specified <see cref="sbyte"/> array, yielding a single concatenated string</para>
		/// </summary>
		/// <param name="array"></param>
		/// <param name="separator">A <see cref="string"/></param>
		/// <typeparam name="T">T must be an enumerated type</typeparam>
		/// <returns>The concatenated <see cref="string"/></returns>
		public static string Join<T>(this sbyte[] array, string separator) where T : struct, IConvertible
		{
			if (!typeof(T).IsEnum)
				throw new ArgumentException("T must be an enumerated type");

			StringBuilder concat = new StringBuilder();
			bool notFirst = false;
			foreach (sbyte item in array)
			{
				if (notFirst)
					concat.Append(separator);
				else
					notFirst = true;
				concat.Append(((T)((object)item)).ToString());
			}
			return concat.ToString();
		}
	}
}