Fixed nullability warnings in virtualized collections

This commit is contained in:
Markus Ewald 2025-07-05 14:21:12 +02:00
parent 4d74fd6e99
commit 364b8919b5
3 changed files with 81 additions and 46 deletions

View File

@ -9,7 +9,7 @@
<RootNamespace>Nuclex.Avalonia</RootNamespace> <RootNamespace>Nuclex.Avalonia</RootNamespace>
<IntermediateOutputPath>obj\source</IntermediateOutputPath> <IntermediateOutputPath>obj\source</IntermediateOutputPath>
<Nullable>enable</Nullable> <Nullable>enable</Nullable>
<LangVersion>8.0</LangVersion> <LangVersion>9.0</LangVersion>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
@ -25,6 +25,10 @@
<ItemGroup> <ItemGroup>
<PackageReference Include="Avalonia" Version="11.3.1" /> <PackageReference Include="Avalonia" Version="11.3.1" />
<PackageReference Include="MessageBox.Avalonia" Version="3.2.0" /> <PackageReference Include="MessageBox.Avalonia" Version="3.2.0" />
<PackageReference Include="Nullable" Version="1.3.1">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>

View File

@ -20,12 +20,16 @@ limitations under the License.
using System; using System;
using System.Collections; using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics;
using System.Threading.Tasks;
using System.Diagnostics.CodeAnalysis;
#if !NO_SPECIALIZED_COLLECTIONS #if !NO_SPECIALIZED_COLLECTIONS
using System.Collections.Specialized; using System.Collections.Specialized;
using System.Diagnostics;
#endif #endif
using Nuclex.Support.Collections;
namespace Nuclex.Avalonia.Collections { namespace Nuclex.Avalonia.Collections {
/// <summary> /// <summary>
@ -59,7 +63,7 @@ namespace Nuclex.Avalonia.Collections {
/// <summary>Immediately releases all resources owned by the instance</summary> /// <summary>Immediately releases all resources owned by the instance</summary>
public void Dispose() { public void Dispose() {
this.virtualList = null; this.virtualList = null!; // Only to make life easier got the GC
} }
/// <summary>The item at the enumerator's current position</summary> /// <summary>The item at the enumerator's current position</summary>
@ -119,7 +123,7 @@ namespace Nuclex.Avalonia.Collections {
/// <summary>The item at the enumerator's current position</summary> /// <summary>The item at the enumerator's current position</summary>
object IEnumerator.Current { object IEnumerator.Current {
get { return Current; } get { return Current!; } // No idea what the compiler's issue is here
} }
#if DEBUG #if DEBUG
@ -145,24 +149,24 @@ namespace Nuclex.Avalonia.Collections {
#endregion // class Enumerator #endregion // class Enumerator
/// <summary>Raised when an item has been added to the collection</summary> /// <summary>Raised when an item has been added to the collection</summary>
public event EventHandler<ItemEventArgs<TItem>> ItemAdded; public event EventHandler<ItemEventArgs<TItem>>? ItemAdded;
/// <summary>Raised when an item is removed from the collection</summary> /// <summary>Raised when an item is removed from the collection</summary>
public event EventHandler<ItemEventArgs<TItem>> ItemRemoved; public event EventHandler<ItemEventArgs<TItem>>? ItemRemoved;
/// <summary>Raised when an item is replaced in the collection</summary> /// <summary>Raised when an item is replaced in the collection</summary>
public event EventHandler<ItemReplaceEventArgs<TItem>> ItemReplaced; public event EventHandler<ItemReplaceEventArgs<TItem>>? ItemReplaced;
/// <summary>Raised when the collection is about to be cleared</summary> /// <summary>Raised when the collection is about to be cleared</summary>
/// <remarks> /// <remarks>
/// This could be covered by calling ItemRemoved for each item currently /// This could be covered by calling ItemRemoved for each item currently
/// contained in the collection, but it is often simpler and more efficient /// contained in the collection, but it is often simpler and more efficient
/// to process the clearing of the entire collection as a special operation. /// to process the clearing of the entire collection as a special operation.
/// </remarks> /// </remarks>
public event EventHandler Clearing; public event EventHandler? Clearing;
/// <summary>Raised when the collection has been cleared</summary> /// <summary>Raised when the collection has been cleared</summary>
public event EventHandler Cleared; public event EventHandler? Cleared;
#if !NO_SPECIALIZED_COLLECTIONS #if !NO_SPECIALIZED_COLLECTIONS
/// <summary>Called when the collection has changed</summary> /// <summary>Called when the collection has changed</summary>
public event NotifyCollectionChangedEventHandler CollectionChanged; public event NotifyCollectionChangedEventHandler? CollectionChanged;
#endif #endif
/// <summary> /// <summary>
@ -178,9 +182,10 @@ namespace Nuclex.Avalonia.Collections {
/// performance from requesting multiple items at once. /// performance from requesting multiple items at once.
/// </remarks> /// </remarks>
public AsyncVirtualObservableReadOnlyList(int pageSize = 32) { public AsyncVirtualObservableReadOnlyList(int pageSize = 32) {
this.typedList = new List<TItem>(); this.typedList = new TItem[0];
this.objectList = this.typedList as IList; this.objectList = (IList)this.typedList;
this.pageSize = pageSize; this.pageSize = pageSize;
this.fetchedPages = new bool[0];
} }
/// <summary> /// <summary>
@ -201,7 +206,7 @@ namespace Nuclex.Avalonia.Collections {
if(purgeItems) { if(purgeItems) {
int itemCount = this.assumedCount.Value; int itemCount = this.assumedCount.Value;
for(int index = 0; index < itemCount; ++index) { for(int index = 0; index < itemCount; ++index) {
this.typedList[index] = default(TItem); this.typedList[index] = default(TItem)!; // not going to be exposed to users
} }
} }
} }
@ -234,7 +239,7 @@ namespace Nuclex.Avalonia.Collections {
this.pageSize this.pageSize
); );
for(int index = itemIndex / this.pageSize; index < count; ++index) { for(int index = itemIndex / this.pageSize; index < count; ++index) {
this.typedList[index] = default(TItem); this.typedList[index] = default(TItem)!; // not going to be exposed to users
} }
} }
} }
@ -244,7 +249,19 @@ namespace Nuclex.Avalonia.Collections {
/// <param name="item">Item whose index will be determined</param> /// <param name="item">Item whose index will be determined</param>
/// <returns>The index of the item in the list or -1 if not found</returns> /// <returns>The index of the item in the list or -1 if not found</returns>
public int IndexOf(TItem item) { public int IndexOf(TItem item) {
return this.typedList.IndexOf(item); requireCount();
requireAllPages();
// TODO: this won't work, it will compare the placeholder items :-/
IComparer<TItem> itemComparer = Comparer<TItem>.Default;
for(int index = 0; index < this.assumedCount.Value; ++index) {
if(itemComparer.Compare(this.typedList[index], item) == 0) {
return index;
}
}
return -1;
} }
/// <summary>Inserts an item into the list at the specified index</summary> /// <summary>Inserts an item into the list at the specified index</summary>
@ -299,10 +316,7 @@ namespace Nuclex.Avalonia.Collections {
/// <param name="item">Item the list will be checked for</param> /// <param name="item">Item the list will be checked for</param>
/// <returns>True if the list contains the specified items</returns> /// <returns>True if the list contains the specified items</returns>
public bool Contains(TItem item) { public bool Contains(TItem item) {
requireCount(); return (IndexOf(item) != -1);
requireAllPages();
return this.typedList.Contains(item);
} }
/// <summary>Copies the contents of the list into an array</summary> /// <summary>Copies the contents of the list into an array</summary>
@ -570,6 +584,7 @@ namespace Nuclex.Avalonia.Collections {
); );
/// <summary>Ensures that the total number of items is known</summary> /// <summary>Ensures that the total number of items is known</summary>
[MemberNotNull(nameof(assumedCount))]
private void requireCount() { private void requireCount() {
if(!this.assumedCount.HasValue) { if(!this.assumedCount.HasValue) {
int itemCount = CountItems(); int itemCount = CountItems();
@ -589,11 +604,12 @@ namespace Nuclex.Avalonia.Collections {
this.assumedCount.HasValue, this.assumedCount.HasValue,
"This method should only be called when item count is already known" "This method should only be called when item count is already known"
); );
int pageCount = this.fetchedPages.Length;
// We may find that the list is shorter than expected while requesting pages.
// But the results will come in asynchronously, so we can't wait for it.
int pageCount = (this.assumedCount!.Value + this.pageSize - 1) / this.pageSize;
for(int index = 0; index < pageCount; ++index) { for(int index = 0; index < pageCount; ++index) {
requirePage(index); requirePage(index);
// CHECK: Should we throttle this by constructing a clever chain of
// ContinueWith() tasks so we don't cause a flood of async requests?
} }
} }
@ -609,7 +625,7 @@ namespace Nuclex.Avalonia.Collections {
} }
int startIndex = pageIndex * this.pageSize; int startIndex = pageIndex * this.pageSize;
int count = Math.Min(this.assumedCount.Value - startIndex, this.pageSize); int count = Math.Min(this.assumedCount!.Value - startIndex, this.pageSize);
CreatePlaceholderItems(this.typedList, pageIndex * this.pageSize, count); CreatePlaceholderItems(this.typedList, pageIndex * this.pageSize, count);
this.fetchedPages[pageIndex] = true; // Prevent double-fetch this.fetchedPages[pageIndex] = true; // Prevent double-fetch
@ -619,7 +635,7 @@ namespace Nuclex.Avalonia.Collections {
var placeholderItems = new List<TItem>(count); var placeholderItems = new List<TItem>(count);
for(int index = startIndex; index < count; ++index) { for(int index = startIndex; index < count; ++index) {
placeholderItems[index - startIndex] = this.typedList[index]; placeholderItems[index - startIndex] = this.typedList[index];
OnReplaced(default(TItem), this.typedList[index], index); //OnReplaced(default(TItem), this.typedList[index], index);
} }
int fetchedItemCount = await FetchItemsAsync(this.typedList, startIndex, count); int fetchedItemCount = await FetchItemsAsync(this.typedList, startIndex, count);
@ -630,7 +646,7 @@ namespace Nuclex.Avalonia.Collections {
// The count may have been adjusted if this truncated the list, // The count may have been adjusted if this truncated the list,
// so recalculate the actual number of items. Then send out change // so recalculate the actual number of items. Then send out change
// notifications for the items that have now been fetched. // notifications for the items that have now been fetched.
count = Math.Min(this.assumedCount.Value - startIndex, this.pageSize); count = Math.Min(this.assumedCount!.Value - startIndex, this.pageSize);
for(int index = startIndex; index < count; ++index) { for(int index = startIndex; index < count; ++index) {
OnReplaced(placeholderItems[index - startIndex], this.typedList[index], index); OnReplaced(placeholderItems[index - startIndex], this.typedList[index], index);
} }
@ -643,7 +659,7 @@ namespace Nuclex.Avalonia.Collections {
/// <summary>Tracks which pages have been fetched so far</summary> /// <summary>Tracks which pages have been fetched so far</summary>
private bool[] fetchedPages; private bool[] fetchedPages;
/// <summary>The wrapped list under its type-safe interface</summary> /// <summary>The wrapped list under its type-safe interface</summary>
private IList<TItem> typedList; private TItem[] typedList;
/// <summary>The wrapped list under its object interface</summary> /// <summary>The wrapped list under its object interface</summary>
private IList objectList; private IList objectList;
#if DEBUG #if DEBUG

View File

@ -20,12 +20,16 @@ limitations under the License.
using System; using System;
using System.Collections; using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
#if !NO_SPECIALIZED_COLLECTIONS #if !NO_SPECIALIZED_COLLECTIONS
using System.Collections.Specialized; using System.Collections.Specialized;
using System.Diagnostics;
#endif #endif
using Nuclex.Support.Collections;
namespace Nuclex.Avalonia.Collections { namespace Nuclex.Avalonia.Collections {
/// <summary> /// <summary>
@ -59,7 +63,7 @@ namespace Nuclex.Avalonia.Collections {
/// <summary>Immediately releases all resources owned by the instance</summary> /// <summary>Immediately releases all resources owned by the instance</summary>
public void Dispose() { public void Dispose() {
this.virtualList = null; this.virtualList = null!; // Only to make life easier got the GC
} }
/// <summary>The item at the enumerator's current position</summary> /// <summary>The item at the enumerator's current position</summary>
@ -119,7 +123,7 @@ namespace Nuclex.Avalonia.Collections {
/// <summary>The item at the enumerator's current position</summary> /// <summary>The item at the enumerator's current position</summary>
object IEnumerator.Current { object IEnumerator.Current {
get { return Current; } get { return Current!; } // No idea what the compiler's issue is here
} }
#if DEBUG #if DEBUG
@ -145,24 +149,24 @@ namespace Nuclex.Avalonia.Collections {
#endregion // class Enumerator #endregion // class Enumerator
/// <summary>Raised when an item has been added to the collection</summary> /// <summary>Raised when an item has been added to the collection</summary>
public event EventHandler<ItemEventArgs<TItem>> ItemAdded; public event EventHandler<ItemEventArgs<TItem>>? ItemAdded;
/// <summary>Raised when an item is removed from the collection</summary> /// <summary>Raised when an item is removed from the collection</summary>
public event EventHandler<ItemEventArgs<TItem>> ItemRemoved; public event EventHandler<ItemEventArgs<TItem>>? ItemRemoved;
/// <summary>Raised when an item is replaced in the collection</summary> /// <summary>Raised when an item is replaced in the collection</summary>
public event EventHandler<ItemReplaceEventArgs<TItem>> ItemReplaced; public event EventHandler<ItemReplaceEventArgs<TItem>>? ItemReplaced;
/// <summary>Raised when the collection is about to be cleared</summary> /// <summary>Raised when the collection is about to be cleared</summary>
/// <remarks> /// <remarks>
/// This could be covered by calling ItemRemoved for each item currently /// This could be covered by calling ItemRemoved for each item currently
/// contained in the collection, but it is often simpler and more efficient /// contained in the collection, but it is often simpler and more efficient
/// to process the clearing of the entire collection as a special operation. /// to process the clearing of the entire collection as a special operation.
/// </remarks> /// </remarks>
public event EventHandler Clearing; public event EventHandler? Clearing;
/// <summary>Raised when the collection has been cleared</summary> /// <summary>Raised when the collection has been cleared</summary>
public event EventHandler Cleared; public event EventHandler? Cleared;
#if !NO_SPECIALIZED_COLLECTIONS #if !NO_SPECIALIZED_COLLECTIONS
/// <summary>Called when the collection has changed</summary> /// <summary>Called when the collection has changed</summary>
public event NotifyCollectionChangedEventHandler CollectionChanged; public event NotifyCollectionChangedEventHandler? CollectionChanged;
#endif #endif
/// <summary> /// <summary>
@ -178,9 +182,10 @@ namespace Nuclex.Avalonia.Collections {
/// performance from requesting multiple items at once. /// performance from requesting multiple items at once.
/// </remarks> /// </remarks>
public VirtualObservableReadOnlyList(int pageSize = 32) { public VirtualObservableReadOnlyList(int pageSize = 32) {
this.typedList = new List<TItem>(); this.typedList = new TItem[0];
this.objectList = this.typedList as IList; this.objectList = (IList)this.typedList;
this.pageSize = pageSize; this.pageSize = pageSize;
this.fetchedPages = new bool[0];
} }
/// <summary> /// <summary>
@ -201,7 +206,7 @@ namespace Nuclex.Avalonia.Collections {
if(purgeItems) { if(purgeItems) {
int itemCount = this.assumedCount.Value; int itemCount = this.assumedCount.Value;
for(int index = 0; index < itemCount; ++index) { for(int index = 0; index < itemCount; ++index) {
this.typedList[index] = default(TItem); this.typedList[index] = default(TItem)!; // not going to be exposed to users
} }
} }
} }
@ -234,7 +239,7 @@ namespace Nuclex.Avalonia.Collections {
this.pageSize this.pageSize
); );
for(int index = itemIndex / this.pageSize; index < count; ++index) { for(int index = itemIndex / this.pageSize; index < count; ++index) {
this.typedList[index] = default(TItem); this.typedList[index] = default(TItem)!; // not going to be exposed to users
} }
} }
} }
@ -244,7 +249,19 @@ namespace Nuclex.Avalonia.Collections {
/// <param name="item">Item whose index will be determined</param> /// <param name="item">Item whose index will be determined</param>
/// <returns>The index of the item in the list or -1 if not found</returns> /// <returns>The index of the item in the list or -1 if not found</returns>
public int IndexOf(TItem item) { public int IndexOf(TItem item) {
return this.typedList.IndexOf(item); requireCount();
requireAllPages();
// TODO: this won't work, it will compare the placeholder items :-/
IComparer<TItem> itemComparer = Comparer<TItem>.Default;
for(int index = 0; index < this.assumedCount.Value; ++index) {
if(itemComparer.Compare(this.typedList[index], item) == 0) {
return index;
}
}
return -1;
} }
/// <summary>Inserts an item into the list at the specified index</summary> /// <summary>Inserts an item into the list at the specified index</summary>
@ -299,10 +316,7 @@ namespace Nuclex.Avalonia.Collections {
/// <param name="item">Item the list will be checked for</param> /// <param name="item">Item the list will be checked for</param>
/// <returns>True if the list contains the specified items</returns> /// <returns>True if the list contains the specified items</returns>
public bool Contains(TItem item) { public bool Contains(TItem item) {
requireCount(); return (IndexOf(item) != -1);
requireAllPages();
return this.typedList.Contains(item);
} }
/// <summary>Copies the contents of the list into an array</summary> /// <summary>Copies the contents of the list into an array</summary>
@ -342,7 +356,7 @@ namespace Nuclex.Avalonia.Collections {
/// <summary>Returns an enumerator for the items in the list</summary> /// <summary>Returns an enumerator for the items in the list</summary>
/// <returns>An enumerator for the list's items</returns> /// <returns>An enumerator for the list's items</returns>
public IEnumerator<TItem> GetEnumerator() { public IEnumerator<TItem> GetEnumerator() {
return this.typedList.GetEnumerator(); // TODO return new Enumerator(this);
} }
#region IEnumerable implementation #region IEnumerable implementation
@ -550,6 +564,7 @@ namespace Nuclex.Avalonia.Collections {
protected abstract int FetchItems(IList<TItem> target, int startIndex, int count); protected abstract int FetchItems(IList<TItem> target, int startIndex, int count);
/// <summary>Ensures that the total number of items is known</summary> /// <summary>Ensures that the total number of items is known</summary>
[MemberNotNull(nameof(assumedCount))]
private void requireCount() { private void requireCount() {
if(!this.assumedCount.HasValue) { if(!this.assumedCount.HasValue) {
int itemCount = CountItems(); int itemCount = CountItems();
@ -604,7 +619,7 @@ namespace Nuclex.Avalonia.Collections {
/// <summary>Tracks which pages have been fetched so far</summary> /// <summary>Tracks which pages have been fetched so far</summary>
private bool[] fetchedPages; private bool[] fetchedPages;
/// <summary>The wrapped list under its type-safe interface</summary> /// <summary>The wrapped list under its type-safe interface</summary>
private IList<TItem> typedList; private TItem[] typedList;
/// <summary>The wrapped list under its object interface</summary> /// <summary>The wrapped list under its object interface</summary>
private IList objectList; private IList objectList;
#if DEBUG #if DEBUG