/// <summary>Initializes a new instance of the <see cref="TenorTimeSpanSpread"/> struct. /// </summary> /// <param name="firstTenor">The first tenor.</param> /// <param name="secondTenor">The second tenor.</param> /// <param name="firstTenorDescription">The optional description of the <paramref name="firstTenor"/>.</param> /// <param name="secondTenorDescription">The optional description of the <paramref name="secondTenor"/>.</param> /// <param name="stringRepresentationUsage">The string representation usage, i.e. the delimiter used for the <see cref="TenorTimeSpanSpread.ToString()"/> method.</param> public TenorTimeSpanSpread(TenorTimeSpan firstTenor, TenorTimeSpan secondTenor, string firstTenorDescription = "", string secondTenorDescription = "", StringRepresentationUsage stringRepresentationUsage = StringRepresentationUsage.Versus) { FirstTenor = firstTenor; SecondTenor = secondTenor; FirstTenorDescription = (firstTenorDescription != null) ? firstTenorDescription : String.Empty; SecondTenorDescription = (secondTenorDescription != null) ? secondTenorDescription : String.Empty; m_StringRepresentationUsage = stringRepresentationUsage; }
/// <summary>Initializes a new instance of the <see cref="TenorTimeSpanSpread"/> struct. /// </summary> /// <param name="tenorTimeSpan">The tenor time span.</param> /// <param name="description">The optional description of <paramref name="tenorTimeSpan"/>.</param> /// <param name="stringRepresentationUsage">The string representation usage, i.e. the delimiter used for the <see cref="TenorTimeSpanSpread.ToString()"/> method.</param> public TenorTimeSpanSpread(TenorTimeSpan tenorTimeSpan, string description = "", StringRepresentationUsage stringRepresentationUsage = StringRepresentationUsage.Versus) { FirstTenor = tenorTimeSpan; FirstTenorDescription = (description != null) ? description : String.Empty; SecondTenor = TenorTimeSpan.Null; SecondTenorDescription = String.Empty; m_StringRepresentationUsage = stringRepresentationUsage; }
/// <summary>Returns a <see cref="System.String"/> that represents this instance. /// </summary> /// <param name="stringRepresentationUsage">The string representation usage.</param> /// <returns>A <see cref="System.String"/> that represents this instance. /// </returns> public string ToString(StringRepresentationUsage stringRepresentationUsage) { if ((TenorTimeSpan.IsNull(FirstTenor) == true) && (TenorTimeSpan.IsNull(SecondTenor) == true) && (FirstTenorDescription == null || FirstTenorDescription.Length == 0) && (SecondTenorDescription == null || SecondTenorDescription.Length == 0)) { return(sm_NullIdentifierStringRepresentation.String); } StringBuilder strBuilder = new StringBuilder(); strBuilder.Append(FirstTenor.ToString()); if ((FirstTenorDescription != null) && (FirstTenorDescription.Length > 0)) { if (strBuilder.Length > 0) { strBuilder.Append(" "); } strBuilder.Append(String.Format(" [{0}]", FirstTenorDescription)); } if ((TenorTimeSpan.IsNull(SecondTenor) == false) || (SecondTenorDescription != null && SecondTenorDescription.Length > 0)) { switch (stringRepresentationUsage) { case StringRepresentationUsage.Versus: strBuilder.Append(String.Format(" vs. {0}", SecondTenor.ToString())); break; case StringRepresentationUsage.Minus: strBuilder.Append(String.Format(" - {0}", SecondTenor.ToString())); break; default: throw new NotImplementedException(); } if ((SecondTenorDescription != null) && (SecondTenorDescription.Length > 0)) { strBuilder.Append(String.Format(" [{0}]", SecondTenorDescription)); } } return(strBuilder.ToString()); }
/// <summary>Converts a <see cref="System.String"/> object into two <see cref="TenorTimeSpan"/> objects and its descriptions. /// </summary> /// <param name="tenorSpreadString">The <see cref="System.String"/> representation of the tenor spread.</param> /// <param name="firstTenor">The first tenor (output).</param> /// <param name="secondTenor">The second tenor (output).</param> /// <param name="firstTenorDescription">The description of <paramref name="firstTenor"/>; perhaps <c>null</c> (output).</param> /// <param name="secondTenorDescription">The description of <paramref name="secondTenor"/>; perhaps <c>null</c> (output).</param> /// <param name="stringRepresentationUsage">The string representation usage, i.e. the delimiter used for the <see cref="TenorTimeSpanSpread.ToString()"/> method (output).</param> /// <returns>A value indicating whether <paramref name="firstTenor"/>, <paramref name="secondTenor"/>, <paramref name="firstTenorDescription"/> and <paramref name="secondTenorDescription"/> contains valid data.</returns> private static bool TryParse(string tenorSpreadString, out TenorTimeSpan firstTenor, out TenorTimeSpan secondTenor, out string firstTenorDescription, out string secondTenorDescription, out StringRepresentationUsage stringRepresentationUsage) { if (tenorSpreadString == null) { throw new ArgumentNullException("tenorSpreadString"); } string tenorSpreadIDString = tenorSpreadString.ToIDString(); firstTenor = secondTenor = TenorTimeSpan.Null; firstTenorDescription = secondTenorDescription = String.Empty; stringRepresentationUsage = StringRepresentationUsage.Minus; if (((tenorSpreadIDString.Length == 1) && (tenorSpreadIDString[0] == '0')) || (tenorSpreadIDString == sm_NullIdentifierStringRepresentation.IDString)) { return(true); // special: '0' and "<null>" will be interpreted as 'null' } int delimiterIndex = tenorSpreadString.IndexOf('-'); if (delimiterIndex >= 0) { if (TryParse(tenorSpreadString.Substring(0, delimiterIndex), out firstTenor, out firstTenorDescription) == false) { return(false); } return(TryParse(tenorSpreadString.Substring(delimiterIndex + 1, tenorSpreadString.Length - delimiterIndex - 1), out secondTenor, out secondTenorDescription)); } delimiterIndex = tenorSpreadString.IndexOf('v'); // first character of "vs." if (delimiterIndex < 0) { return(TryParse(tenorSpreadString, out firstTenor, out firstTenorDescription)); } else if (TryParse(tenorSpreadString.Substring(0, delimiterIndex), out firstTenor, out firstTenorDescription) == false) { return(false); } if ((delimiterIndex + 2 < tenorSpreadString.Length) && (tenorSpreadString[delimiterIndex + 1] == 's') && (tenorSpreadString[delimiterIndex + 2] == '.')) { stringRepresentationUsage = StringRepresentationUsage.Versus; return(TryParse(tenorSpreadString.Substring(delimiterIndex + 3, tenorSpreadString.Length - delimiterIndex - 3), out secondTenor, out secondTenorDescription)); } return(false); }