17 using System.Collections;
18 using System.Collections.Concurrent;
19 using System.Collections.Generic;
20 using System.Collections.Specialized;
30 public class UniverseManager : IDictionary<Symbol, Universe>, INotifyCollectionChanged
32 private readonly Queue<NotifyCollectionChangedEventArgs> _pendingChanges =
new();
33 private readonly ConcurrentDictionary<Symbol, Universe> _universes;
45 .SelectMany(ukvp => ukvp.Value.Members.Select(mkvp => mkvp.Value))
46 .DistinctBy(s => s.Symbol).ToDictionary(s => s.Symbol);
53 _universes =
new ConcurrentDictionary<Symbol, Universe>();
56 #region IDictionary implementation
67 return _universes.GetEnumerator();
77 IEnumerator IEnumerable.GetEnumerator()
79 return ((IEnumerable)_universes).GetEnumerator();
86 public void Add(KeyValuePair<Symbol, Universe> item)
88 Add(item.Key, item.Value);
107 public bool Contains(KeyValuePair<Symbol, Universe> item)
116 public void CopyTo(KeyValuePair<Symbol, Universe>[] array,
int arrayIndex)
118 ((IDictionary<Symbol, Universe>)_universes).CopyTo(array, arrayIndex);
128 public bool Remove(KeyValuePair<Symbol, Universe> item)
139 public int Count => _universes.Skip(0).Count();
149 get {
return false; }
161 return _universes.ContainsKey(key);
174 if (_universes.TryAdd(key, value))
176 lock(_pendingChanges)
178 _pendingChanges.Enqueue(
new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, value));
188 NotifyCollectionChangedEventArgs universeChange;
191 lock (_pendingChanges)
193 _pendingChanges.TryDequeue(out universeChange);
196 if (universeChange !=
null)
201 while (universeChange !=
null);
214 if (_universes.TryRemove(key, out universe))
217 OnCollectionChanged(
new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, universe));
232 return _universes.TryGetValue(key, out value);
246 if (!_universes.ContainsKey(symbol))
248 throw new KeyNotFoundException($
"This universe symbol ({symbol}) was not found in your universe list. Please add this security or check it exists before using it with 'Universes.ContainsKey(\"{SymbolCache.GetTicker(symbol)}\")'");
250 return _universes[symbol];
255 if (_universes.TryGetValue(symbol, out existing) && existing != value)
257 throw new ArgumentException($
"Unable to over write existing Universe: {symbol.Value}");
261 if (existing ==
null)
274 public ICollection<Symbol>
Keys => _universes.Select(x => x.Key).ToList();
282 public ICollection<Universe>
Values => _universes.Select(x => x.Value).ToList();