Table of Contents

Class CollectionExtensions

Namespace
LfrlAnvil.Extensions
Assembly
LfrlAnvil.Core.dll

Contains extension methods for materialized collections.

public static class CollectionExtensions
Inheritance
CollectionExtensions
Inherited Members

Methods

ContainsAtLeast<T>(IReadOnlyCollection<T>, int)

Checks if the provided source contains at least count number of elements.

[Pure]
public static bool ContainsAtLeast<T>(this IReadOnlyCollection<T> source, int count)

Parameters

source IReadOnlyCollection<T>

Source collection.

count int

Expected minimum number of elements.

Returns

bool

true when source contains correct number of elements, otherwise false.

Type Parameters

T

Collection element type.

ContainsAtMost<T>(IReadOnlyCollection<T>, int)

Checks if the provided source contains at most count number of elements.

[Pure]
public static bool ContainsAtMost<T>(this IReadOnlyCollection<T> source, int count)

Parameters

source IReadOnlyCollection<T>

Source collection.

count int

Expected maximum number of elements.

Returns

bool

true when source contains correct number of elements, otherwise false.

Type Parameters

T

Collection element type.

ContainsExactly<T>(IReadOnlyCollection<T>, int)

Checks if the provided source contains exactly count number of elements.

[Pure]
public static bool ContainsExactly<T>(this IReadOnlyCollection<T> source, int count)

Parameters

source IReadOnlyCollection<T>

Source collection.

count int

Expected exact number of elements.

Returns

bool

true when source contains correct number of elements, otherwise false.

Type Parameters

T

Collection element type.

ContainsInRange<T>(IReadOnlyCollection<T>, int, int)

Checks if the provided source contains between minCount and maxCount number of elements.

[Pure]
public static bool ContainsInRange<T>(this IReadOnlyCollection<T> source, int minCount, int maxCount)

Parameters

source IReadOnlyCollection<T>

Source collection.

minCount int

Expected minimum number of elements.

maxCount int

Expected maximum number of elements.

Returns

bool

true when source contains correct number of elements, otherwise false.

Type Parameters

T

Collection element type.

CopyTo<T>(IReadOnlyCollection<T>, RentedMemorySequenceSpan<T>)

Copies the provided source into the given span.

public static void CopyTo<T>(this IReadOnlyCollection<T> source, RentedMemorySequenceSpan<T> span)

Parameters

source IReadOnlyCollection<T>

Source collection.

span RentedMemorySequenceSpan<T>

Target RentedMemorySequenceSpan<T> to copy source elements to.

Type Parameters

T

Collection element type.

Remarks

Copying starts at the beginning of the span.

Exceptions

ArgumentException

When source contains more elements than the span can hold.

EmptyIfNull<T>(IReadOnlyCollection<T>?)

Returns the provided source, unless it is null, in which case returns an empty array instead.

[Pure]
public static IReadOnlyCollection<T> EmptyIfNull<T>(this IReadOnlyCollection<T>? source)

Parameters

source IReadOnlyCollection<T>

Source collection.

Returns

IReadOnlyCollection<T>

source if it is not null, otherwise an empty array.

Type Parameters

T

Collection element type.

GetOrAddAsync<TKey, TValue>(ICache<TKey, TValue>, TKey, Func<TKey, CancellationToken, Task<TValue>>, CancellationToken)

Reads a value associated with the specified key from the cache, if it exists. Otherwise adds a new entry to the cache with the value returned by the asynchronous provider invocation and returns that value.

public static ValueTask<TValue> GetOrAddAsync<TKey, TValue>(this ICache<TKey, TValue> cache, TKey key, Func<TKey, CancellationToken, Task<TValue>> provider, CancellationToken cancellationToken = default) where TKey : notnull

Parameters

cache ICache<TKey, TValue>

Source cache.

key TKey

Entry's key.

provider Func<TKey, CancellationToken, Task<TValue>>

Entry's asynchronous value provider. Gets invoked only when entry with the provided key does not exist in the cache.

cancellationToken CancellationToken

Optional CancellationToken forwarded to the provider.

Returns

ValueTask<TValue>

Value associated with the provided key, if it exists, otherwise value returned by provider.

Type Parameters

TKey

Cache key type.

TValue

Cache value type.

GetOrAddAsync<TKey, TValue>(ICache<TKey, TValue>, TKey, Func<TKey, CancellationToken, ValueTask<TValue>>, CancellationToken)

Reads a value associated with the specified key from the cache, if it exists. Otherwise adds a new entry to the cache with the value returned by the asynchronous provider invocation and returns that value.

public static ValueTask<TValue> GetOrAddAsync<TKey, TValue>(this ICache<TKey, TValue> cache, TKey key, Func<TKey, CancellationToken, ValueTask<TValue>> provider, CancellationToken cancellationToken = default) where TKey : notnull

Parameters

cache ICache<TKey, TValue>

Source cache.

key TKey

Entry's key.

provider Func<TKey, CancellationToken, ValueTask<TValue>>

Entry's asynchronous value provider. Gets invoked only when entry with the provided key does not exist in the cache.

cancellationToken CancellationToken

Optional CancellationToken forwarded to the provider.

Returns

ValueTask<TValue>

Value associated with the provided key, if it exists, otherwise value returned by provider.

Type Parameters

TKey

Cache key type.

TValue

Cache value type.

GetOrAdd<TKey, TValue>(ICache<TKey, TValue>, TKey, Func<TKey, TValue>)

Reads a value associated with the specified key from the cache, if it exists. Otherwise adds a new entry to the cache with the value returned by the provider invocation and returns that value.

public static TValue GetOrAdd<TKey, TValue>(this ICache<TKey, TValue> cache, TKey key, Func<TKey, TValue> provider) where TKey : notnull

Parameters

cache ICache<TKey, TValue>

Source cache.

key TKey

Entry's key.

provider Func<TKey, TValue>

Entry's value provider. Gets invoked only when entry with the provided key does not exist in the cache.

Returns

TValue

Value associated with the provided key, if it exists, otherwise value returned by provider.

Type Parameters

TKey

Cache key type.

TValue

Cache value type.

IsEmpty<T>(IReadOnlyCollection<T>)

Checks if the provided source is empty.

[Pure]
public static bool IsEmpty<T>(this IReadOnlyCollection<T> source)

Parameters

source IReadOnlyCollection<T>

Source collection.

Returns

bool

true when source is empty, otherwise false.

Type Parameters

T

Collection element type.

IsNullOrEmpty<T>(IReadOnlyCollection<T>?)

Checks if the provided source is null or empty.

[Pure]
public static bool IsNullOrEmpty<T>(this IReadOnlyCollection<T>? source)

Parameters

source IReadOnlyCollection<T>

Source collection.

Returns

bool

true when source is null or empty, otherwise false.

Type Parameters

T

Collection element type.