19 using System.Collections.Generic;
34 private readonly IReadOnlySet<Security> _addedSecurities;
35 private readonly IReadOnlySet<Security> _removedSecurities;
36 private readonly IReadOnlySet<Security> _internalAddedSecurities;
37 private readonly IReadOnlySet<Security> _internalRemovedSecurities;
42 public int Count => _addedSecurities.Count + _removedSecurities.Count
43 + _internalAddedSecurities.Count + _internalRemovedSecurities.Count;
68 public IReadOnlyList<Security>
AddedSecurities => GetFilteredList(_addedSecurities,
90 private SecurityChanges(IEnumerable<Security> additions, IEnumerable<Security> removals,
91 IEnumerable<Security> internalAdditions, IEnumerable<Security> internalRemovals)
93 _addedSecurities = additions.ToHashSet();
94 _removedSecurities = removals.ToHashSet();
95 _internalAddedSecurities = internalAdditions.ToHashSet();
96 _internalRemovedSecurities = internalRemovals.ToHashSet();
106 _addedSecurities = changes._addedSecurities;
107 _removedSecurities = changes._removedSecurities;
108 _internalAddedSecurities = changes._internalAddedSecurities;
109 _internalRemovedSecurities = changes._internalRemovedSecurities;
121 if (left ==
None || left.
Count == 0)
return right;
122 if (right ==
None || right.
Count == 0)
return left;
124 var additions = Merge(left._addedSecurities, right._addedSecurities);
125 var internalAdditions = Merge(left._internalAddedSecurities, right._internalAddedSecurities);
127 var removals = Merge(left._removedSecurities, right._removedSecurities,
128 security => !additions.Contains(security) && !internalAdditions.Contains(security));
129 var internalRemovals = Merge(left._internalRemovedSecurities, right._internalRemovedSecurities,
130 security => !additions.Contains(security) && !internalAdditions.Contains(security));
132 return new SecurityChanges(additions, removals, internalAdditions, internalRemovals);
143 public static SecurityChanges Create(IReadOnlyCollection<Security> additions, IReadOnlyCollection<Security> removals,
144 IReadOnlyCollection<Security> internalAdditions, IReadOnlyCollection<Security> internalRemovals)
147 return additions?.
Count + removals?.Count + internalAdditions?.Count + internalRemovals?.Count > 0
148 ?
new SecurityChanges(additions, removals, internalAdditions, internalRemovals)
152 #region Overrides of Object
165 return "SecurityChanges: None";
168 var added =
string.Empty;
171 added = $
" Added: {string.Join(",
", AddedSecurities.Select(x => x.Symbol.ID))}";
173 var removed =
string.Empty;
176 removed = $
" Removed: {string.Join(",
", RemovedSecurities.Select(x => x.Symbol.ID))}";
179 return $
"SecurityChanges: {added}{removed}";
187 private IReadOnlyList<Security> GetFilteredList(IReadOnlySet<Security> source, IReadOnlySet<Security> secondSource =
null)
189 IEnumerable<Security> enumerable = source;
190 if (secondSource !=
null && secondSource.Count > 0)
192 enumerable = enumerable.Union(secondSource);
196 .OrderBy(security => security.Symbol.Value)
204 private static HashSet<Security> Merge(IReadOnlyCollection<Security> left, IReadOnlyCollection<Security> right, Func<Security, bool> filter =
null)
207 IEnumerable<Security> result = left;
208 if (right.Count != 0)
218 result = result.Concat(right);
224 result = result.Where(filter.Invoke);
227 return new HashSet<Security>(result);
236 private readonly List<Security> _internalAdditions =
new();
237 private readonly List<Security> _internalRemovals =
new();
238 private readonly List<Security> _additions =
new();
239 private readonly List<Security> _removals =
new();
248 _internalAdditions.Add(security);
252 _additions.Add(security);
263 _internalRemovals.Add(security);
267 _removals.Add(security);
278 _internalAdditions.Clear();
280 _internalRemovals.Clear();