From c04c9bbd44be3d259df39df9f552f990486f679e Mon Sep 17 00:00:00 2001 From: John Colvin Date: Fri, 2 Mar 2018 22:34:11 -0500 Subject: [PATCH] Synthetic Management description lists legs, not name --- .../spark_api/security/SecurityFormatter.cs | 49 ++++++++++++++----- 1 file changed, 38 insertions(+), 11 deletions(-) diff --git a/src/client/spark_api/security/SecurityFormatter.cs b/src/client/spark_api/security/SecurityFormatter.cs index dfd498ae5..a12ac2da2 100644 --- a/src/client/spark_api/security/SecurityFormatter.cs +++ b/src/client/spark_api/security/SecurityFormatter.cs @@ -7,9 +7,9 @@ using FutureLeg = System.Tuple _formatted_name_cache = new ConcurrentDictionary(); + static class SecurityFormatter { + static readonly ConcurrentDictionary _formatted_name_cache = + new ConcurrentDictionary(); public static IEnumerable Parse(string str) { return str.Split('/').Select(a => { @@ -20,8 +20,9 @@ namespace bts.api } public static string Format(SecurityImmutable s, IEnumerable parts) { + var key = new SecurityFormattingKey(s.Id, parts); string formatted_security_name = string.Empty; - if (_formatted_name_cache.TryGetValue(s.Id, out formatted_security_name)) return formatted_security_name; + if (_formatted_name_cache.TryGetValue(key, out formatted_security_name)) return formatted_security_name; else { var part_formatters = parts.Select(p => PartFormatterFactory.get_formatter(p)).ToList(); switch (s.ContractCase) { @@ -39,9 +40,6 @@ namespace bts.api } else { goto default; } - case Security.ContractOneofCase.Synthetic: - formatted_security_name = s.Synthetic.Name; - break; default: if (part_formatters.Count > 0 && part_formatters[0].separator == "!") { if (part_formatters[0].add_part(s, out formatted_security_name)) { break; } @@ -49,20 +47,49 @@ namespace bts.api formatted_security_name = string.Join("", SecurityHelper.get_flat_future_legs(s).Select(flt => _format_future_leg(flt, part_formatters))); break; } - _formatted_name_cache.TryAdd(s.Id, formatted_security_name); + _formatted_name_cache.TryAdd(key, formatted_security_name); return formatted_security_name; } } public enum ContractParts { Name, Exchange, Symbol, Quantity, MappedExp, RawExp } - public class PartFormattingTuple { + class SecurityFormattingKey : IEquatable { + readonly uint _security; + readonly PartFormattingTuple[] _parts; + + public SecurityFormattingKey(uint security, IEnumerable parts) { + _security = security; + _parts = parts.ToList().ToArray(); + } + + public bool Equals(SecurityFormattingKey other) => + other != null && other._security == _security && other._parts.SequenceEqual(_parts); + + public override bool Equals(object obj) => + Equals(obj as SecurityFormattingKey); + + public override int GetHashCode() => + _security.GetHashCode() ^ _parts.GetHashCode(); + } + + public class PartFormattingTuple : IEquatable { + public readonly ContractParts Part; + public readonly string Argument; + public PartFormattingTuple(ContractParts part, string argument = null) { Part = part; Argument = argument; } - public readonly ContractParts Part; - public readonly string Argument; + + public bool Equals(PartFormattingTuple other) => + other != null && other.Part == Part && other.Argument == Argument; + + public override bool Equals(object obj) => + Equals(obj as PartFormattingTuple); + + public override int GetHashCode() => + Argument == null ? Part.GetHashCode() : Part.GetHashCode() ^ Argument.GetHashCode(); } #region Private Members