Skip to content

Commit 2fb4300

Browse files
committed
Solve new review comments
1 parent 279d0f0 commit 2fb4300

File tree

2 files changed

+19
-47
lines changed

2 files changed

+19
-47
lines changed

Common/Securities/UniverseManager.cs

Lines changed: 6 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -37,21 +37,6 @@ public class UniverseManager : BaseExtendedDictionary<Symbol, Universe, Concurre
3737
/// </summary>
3838
public event EventHandler<UniverseManagerChanged> CollectionChanged;
3939

40-
/// <summary>
41-
/// Gets the number of elements contained in the dictionary
42-
/// </summary>
43-
public override int Count => TypedDictionary.Skip(0).Count();
44-
45-
/// <summary>
46-
/// Gets the keys of the dictionary
47-
/// </summary>
48-
public override ICollection<Symbol> Keys => TypedDictionary.Select(x => x.Key).ToList();
49-
50-
/// <summary>
51-
/// Gets the values of the dictionary
52-
/// </summary>
53-
public override ICollection<Universe> Values => TypedDictionary.Select(x => x.Value).ToList();
54-
5540
/// <summary>
5641
/// Read-only dictionary containing all active securities. An active security is
5742
/// a security that is currently selected by the universe or has holdings or open orders.
@@ -73,7 +58,7 @@ public UniverseManager() : base(new ConcurrentDictionary<Symbol, Universe>())
7358
/// </summary>
7459
public override void Add(Symbol key, Universe value)
7560
{
76-
if (TypedDictionary.TryAdd(key, value))
61+
if (Dictionary.TryAdd(key, value))
7762
{
7863
lock (_pendingChanges)
7964
{
@@ -87,7 +72,7 @@ public override void Add(Symbol key, Universe value)
8772
/// </summary>
8873
public void Update(Symbol key, Universe value, NotifyCollectionChangedAction action)
8974
{
90-
if (TypedDictionary.ContainsKey(key) && !_pendingChanges.Any(x => x.Value == value))
75+
if (Dictionary.ContainsKey(key) && !_pendingChanges.Any(x => x.Value == value))
9176
{
9277
lock (_pendingChanges)
9378
{
@@ -122,8 +107,7 @@ public void ProcessChanges()
122107
/// </summary>
123108
public override bool Remove(Symbol key)
124109
{
125-
Universe universe;
126-
if (TypedDictionary.TryRemove(key, out universe))
110+
if (Dictionary.TryRemove(key, out var universe))
127111
{
128112
universe.Dispose();
129113
OnCollectionChanged(new UniverseManagerChanged(NotifyCollectionChangedAction.Remove, universe));
@@ -139,16 +123,16 @@ public override Universe this[Symbol symbol]
139123
{
140124
get
141125
{
142-
if (!TypedDictionary.ContainsKey(symbol))
126+
if (!Dictionary.ContainsKey(symbol))
143127
{
144128
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)}\")'");
145129
}
146-
return TypedDictionary[symbol];
130+
return Dictionary[symbol];
147131
}
148132
set
149133
{
150134
Universe existing;
151-
if (TypedDictionary.TryGetValue(symbol, out existing) && existing != value)
135+
if (Dictionary.TryGetValue(symbol, out existing) && existing != value)
152136
{
153137
throw new ArgumentException($"Unable to over write existing Universe: {symbol.Value}");
154138
}

Common/Util/BaseExtendedDictionary.cs

Lines changed: 13 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -22,28 +22,30 @@
2222
namespace Common.Util
2323
{
2424
/// <summary>
25-
/// Provides a default implementation of ExtendedDictionary that can be used with any key-value pair types
25+
/// Provides a generic implementation of ExtendedDictionary with specific dictionary type
2626
/// </summary>
2727
[PandasNonExpandable]
28-
public class BaseExtendedDictionary<TKey, TValue> : ExtendedDictionary<TKey, TValue>, IDictionary<TKey, TValue>
28+
public class BaseExtendedDictionary<TKey, TValue, TDictionary> : ExtendedDictionary<TKey, TValue>, IDictionary<TKey, TValue>
29+
where TDictionary : IDictionary<TKey, TValue>, new()
2930
{
3031
/// <summary>
3132
/// The dictionary instance
3233
/// </summary>
33-
protected IDictionary<TKey, TValue> Dictionary { get; }
34+
protected TDictionary Dictionary { get; }
3435

3536
/// <summary>
3637
/// Initializes a new instance of the BaseExtendedDictionary class that is empty
3738
/// </summary>
38-
public BaseExtendedDictionary() : this(new Dictionary<TKey, TValue>())
39+
public BaseExtendedDictionary()
3940
{
41+
Dictionary = new TDictionary();
4042
}
4143

4244
/// <summary>
4345
/// Initializes a new instance of the BaseExtendedDictionary class that contains elements copied from the specified dictionary
4446
/// </summary>
4547
/// <param name="dictionary">The dictionary whose elements are copied to the new dictionary</param>
46-
public BaseExtendedDictionary(IDictionary<TKey, TValue> dictionary)
48+
public BaseExtendedDictionary(TDictionary dictionary)
4749
{
4850
Dictionary = dictionary;
4951
}
@@ -55,7 +57,7 @@ public BaseExtendedDictionary(IDictionary<TKey, TValue> dictionary)
5557
/// <param name="data">The data source for this dictionary</param>
5658
/// <param name="keySelector">Delegate used to select a key from the value</param>
5759
public BaseExtendedDictionary(IEnumerable<TValue> data, Func<TValue, TKey> keySelector)
58-
: this(new Dictionary<TKey, TValue>())
60+
: this()
5961
{
6062
foreach (var datum in data)
6163
{
@@ -221,32 +223,24 @@ IEnumerator IEnumerable.GetEnumerator()
221223
}
222224

223225
/// <summary>
224-
/// Provides a generic implementation of ExtendedDictionary with specific dictionary type
226+
/// Provides a default implementation of ExtendedDictionary using Dictionary{TKey, TValue}
225227
/// </summary>
226228
[PandasNonExpandable]
227-
public class BaseExtendedDictionary<TKey, TValue, TDictionary> : BaseExtendedDictionary<TKey, TValue>
228-
where TDictionary : IDictionary<TKey, TValue>, new()
229+
public class BaseExtendedDictionary<TKey, TValue> : BaseExtendedDictionary<TKey, TValue, Dictionary<TKey, TValue>>
229230
{
230-
/// <summary>
231-
/// Gets the typed dictionary instance
232-
/// </summary>
233-
protected TDictionary TypedDictionary { get; }
234-
235231
/// <summary>
236232
/// Initializes a new instance of the BaseExtendedDictionary class that is empty
237233
/// </summary>
238-
public BaseExtendedDictionary() : base(new TDictionary())
234+
public BaseExtendedDictionary() : base()
239235
{
240-
TypedDictionary = (TDictionary)Dictionary;
241236
}
242237

243238
/// <summary>
244239
/// Initializes a new instance of the BaseExtendedDictionary class that contains elements copied from the specified dictionary
245240
/// </summary>
246241
/// <param name="dictionary">The dictionary whose elements are copied to the new dictionary</param>
247-
public BaseExtendedDictionary(TDictionary dictionary) : base(dictionary)
242+
public BaseExtendedDictionary(IDictionary<TKey, TValue> dictionary) : base(new Dictionary<TKey, TValue>(dictionary))
248243
{
249-
TypedDictionary = dictionary;
250244
}
251245

252246
/// <summary>
@@ -255,14 +249,8 @@ public BaseExtendedDictionary(TDictionary dictionary) : base(dictionary)
255249
/// </summary>
256250
/// <param name="data">The data source for this dictionary</param>
257251
/// <param name="keySelector">Delegate used to select a key from the value</param>
258-
public BaseExtendedDictionary(IEnumerable<TValue> data, Func<TValue, TKey> keySelector)
259-
: base(new TDictionary())
252+
public BaseExtendedDictionary(IEnumerable<TValue> data, Func<TValue, TKey> keySelector) : base(data, keySelector)
260253
{
261-
TypedDictionary = (TDictionary)Dictionary;
262-
foreach (var datum in data)
263-
{
264-
TypedDictionary[keySelector(datum)] = datum;
265-
}
266254
}
267255
}
268256
}

0 commit comments

Comments
 (0)