Skip to content

Commit

Permalink
#35 Fixed a bunch of casts from IList<T> to IList that will fail if t…
Browse files Browse the repository at this point in the history
…he IList<T> is something that doesn't inherit from IList, which was happening in some unit tests under .NET 8.0
  • Loading branch information
stewienj committed Dec 18, 2024
1 parent 147acaf commit ca3a68a
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 45 deletions.
14 changes: 7 additions & 7 deletions Swordfish.NET.CollectionsV3/ConcurrentObservableCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ public virtual void AddRange(IList<T> items) =>
DoReadWriteNotify(
() => ImmutableList.Count,
(index) => ImmutableList.AddRange(items),
(index) => new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, (IList)items, index)
(index) => new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, items as IList ?? items.ToList(), index)
);

/// <summary>
Expand All @@ -124,7 +124,7 @@ public virtual void AddRange(IList<T> items) =>
public virtual void InsertRange(int index, IList<T> items) =>
DoWriteNotify(
() => ImmutableList.InsertRange(index, items),
() => new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, (IList)items, index)
() => new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, items as IList ?? items.ToList(), index)
);

/// <summary>
Expand All @@ -134,21 +134,21 @@ public void RemoveRange(int index, int count) =>
DoReadWriteNotify(
() => ImmutableList.GetRange(index, count),
(items) => ImmutableList.RemoveRange(index, count),
(items) => new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, (IList)items, index)
(items) => new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, items, index)
);

public void RemoveRange(IList<T> items) =>
DoWriteNotify(
() => ImmutableList.RemoveRange(items),
() => new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, (IList)items)
() => new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, items as IList ?? items.ToList())
);

public virtual void Reset(IList<T> items) =>
DoReadWriteNotify(
() => ImmutableList.ToArray(),
(oldItems) => ImmutableList<T>.Empty.AddRange(items),
(oldItems) => new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, (IList)oldItems, 0),
(oldItems) => new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, (IList)items, 0)
(oldItems) => new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, oldItems, 0),
(oldItems) => new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, items as IList ?? items.ToList(), 0)
);

public T[] ToArray() => ImmutableList.ToArray();
Expand Down Expand Up @@ -258,7 +258,7 @@ public void Clear() =>
DoReadWriteNotify(
() => ImmutableList.ToArray(),
(items) => ImmutableList.Clear(),
(items) => new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, (IList)items, 0)
(items) => new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, items, 0)
);

public bool Contains(T item) => ImmutableList.Contains(item);
Expand Down
16 changes: 5 additions & 11 deletions Swordfish.NET.CollectionsV3/ConcurrentObservableDictionary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,16 +68,10 @@ public virtual void Add(KeyValuePair<TKey, TValue> pair)
/// </summary>
public virtual void AddRange(IEnumerable<KeyValuePair<TKey, TValue>> pairs)
{
// Convert to a list off the bat, as this is used multiple times and is required to be
// an IList for NotifyCollectionChangedEventArgs
if (!(pairs is IList<KeyValuePair<TKey, TValue>> pairsList))
{
pairsList = pairs.ToList();
}
DoReadWriteNotify(
() => _internalCollection.Count,
(index) => _internalCollection.AddRange(pairsList),
(index) => new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, (IList)pairsList, index)
(index) => _internalCollection.AddRange(pairs),
(index) => new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, pairs as IList ?? pairs.ToList(), index)
);
}

Expand Down Expand Up @@ -250,7 +244,7 @@ public IList<KeyValuePair<TKey, TValue>> RemoveRange(int index, int count)
(items) =>
{
localRemovedItems = items;
return new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, (IList)items, index);
return new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, items as IList ?? items.ToList(), index);
}
);
return localRemovedItems;
Expand All @@ -271,7 +265,7 @@ public void RemoveRange(IEnumerable<TKey> keys)
// remove the keys from the dictionary, remove the range from the list
(items) => _internalCollection.RemoveRange(keysList),
// Notify which items were removed
(items) => new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, (IList)items)
(items) => new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, items as IList ?? items.ToList())
);
}

Expand Down Expand Up @@ -414,7 +408,7 @@ public void Clear()
// remove the keys from the dictionary, remove the range from the list
(items) => ImmutableDictionaryListPair<TKey, TValue>.Empty,
// Notify which items were removed
(items) => new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, (IList)items, 0)
(items) => new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, items, 0)
);
}

Expand Down
26 changes: 6 additions & 20 deletions Swordfish.NET.CollectionsV3/ConcurrentObservableHashSet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,17 +63,10 @@ public bool Add(T value)
/// </summary>
public void AddRange(IEnumerable<T> values)
{
// Convert to a list off the bat, as this is used multiple times and is required to be
// an IList for NotifyCollectionChangedEventArgs
if (!(values is IList<T> valuesList))
{
valuesList = values.ToList();
}

DoReadWriteNotify(
() => _internalCollection.Count,
(index) => ((ImmutableHashSet<T>)_internalCollection).AddRange(valuesList),
(index) => new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, (IList)valuesList, index)
(index) => ((ImmutableHashSet<T>)_internalCollection).AddRange(values),
(index) => new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, values as IList ?? values.ToList(), index)
);
}

Expand All @@ -96,23 +89,16 @@ public bool Remove(T value)

public void RemoveRange(IEnumerable<T> values)
{
// Convert to a list off the bat, as this is used multiple times and is required to be
// an IList for NotifyCollectionChangedEventArgs
if (!(values is IList<T> valuesList))
{
valuesList = values.ToList();
}

DoWriteNotify(
() => ((ImmutableHashSet<T>)_internalCollection).RemoveRange(valuesList),
() => new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, (IList)valuesList)
() => ((ImmutableHashSet<T>)_internalCollection).RemoveRange(values),
() => new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, values as IList ?? values.ToList())
);
}

/// <summary>
/// This is the view of the colleciton that you should be binding to with your ListView/GridView control.
/// </summary>
public override IList<T> CollectionView => ((IEnumerable<T>)_internalCollection).ToList();
public override IList<T> CollectionView => _internalCollection.ToList();

public override int Count => _internalCollection.Count;

Expand Down Expand Up @@ -148,7 +134,7 @@ public void Clear()
// remove the keys from the dictionary, remove the range from the list
(items) => ImmutableHashSet<T>.Empty,
// Notify which items were removed
(items) => new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, (IList)items, 0)
(items) => new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, items, 0)
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public override void AddRange(IList<T> items)
DoReadWriteNotify(
() => 0,
getIndicesAndInsert,
(nothing) => new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, (IList)items.ToList())
(nothing) => new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, items as IList ?? items.ToList())
);
}

Expand All @@ -65,8 +65,8 @@ public override void Reset(IList<T> items) =>
// Should be quicker than sorting 1 by 1 on insert.
() => ImmutableList.ToArray(),
(oldItems) => ImmutableList<T>.Empty.AddRange(items.OrderBy(x => x, _sorter).ToList()),
(oldItems) => new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, (IList)oldItems, 0),
(oldItems) => new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, (IList)items, 0)
(oldItems) => new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, oldItems, 0),
(oldItems) => new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, items as IList ?? items.ToList(), 0)
);

public override void Insert(int index, T item)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public override void AddRange(IEnumerable<KeyValuePair<TKey, TValue>> pairs)
DoReadWriteNotify(
() => 0,
getIndicesAndInsert,
(nothing) => new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, (IList)pairsList)
(nothing) => new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, pairsList as IList ?? pairsList.ToList())
);
}

Expand Down
4 changes: 2 additions & 2 deletions Swordfish.NET.CollectionsV3/ConcurrentObservableSortedSet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public void RemoveRange(IEnumerable<T> values)
DoReadWriteNotify(
() => _internalCollection.Count,
(index) => ((ImmutableSortedSet<T>)_internalCollection).RemoveRange(values),
(index) => new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, (IList)values, index)
(index) => new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, values as IList ?? values.ToList(), index)
);
}
*/
Expand Down Expand Up @@ -139,7 +139,7 @@ public void Clear()
// remove the keys from the dictionary, remove the range from the list
(items) => ImmutableSortedSet<T>.Empty,
// Notify which items were removed
(items) => new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, (IList)items, 0)
(items) => new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, items, 0)
);
}

Expand Down
4 changes: 3 additions & 1 deletion Swordfish.NET.CollectionsV3/ImmutableDictionaryListPair.cs
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,9 @@ public ImmutableDictionaryListPair<TKey, TValue> RemoveRange(IList<TKey> keys)
var nodesToRemove = keys.Where(key => Dictionary.ContainsKey(key)).Select(key => Dictionary[key]);
if (nodesToRemove.Any())
{
return new ImmutableDictionaryListPair<TKey, TValue>(Dictionary.RemoveRange(keys), List.RemoveRange(nodesToRemove));
var newDictionary = Dictionary.RemoveRange(keys);
var newList = List.RemoveRange(nodesToRemove);
return new ImmutableDictionaryListPair<TKey, TValue>(newDictionary, newList);
}

else
Expand Down

0 comments on commit ca3a68a

Please sign in to comment.