< Summary

Information
Class: MoreStructures.Utilities.ValueReadOnlyCollection<T>
Assembly: MoreStructures
File(s): /home/runner/work/MoreStructures/MoreStructures/MoreStructures/Utilities/ValueReadOnlyCollection.cs
Line coverage
100%
Covered lines: 9
Uncovered lines: 0
Coverable lines: 9
Total lines: 77
Line coverage: 100%
Branch coverage
100%
Covered branches: 6
Total branches: 6
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity Line coverage
.ctor(...)100%1100%
Equals(...)100%2100%
Equals(...)100%2100%
GetHashCode()100%2100%
ToString()100%1100%
op_Equality(...)100%1100%
op_Inequality(...)100%1100%

File(s)

/home/runner/work/MoreStructures/MoreStructures/MoreStructures/Utilities/ValueReadOnlyCollection.cs

#LineLine coverage
 1using System.Collections.ObjectModel;
 2
 3namespace MoreStructures.Utilities;
 4
 5/// <summary>
 6/// A readonly immutable generic collection of non-null items which performs equality by value.
 7/// </summary>
 8/// <typeparam name="T">The type of items in the collection.</typeparam>
 9/// <remarks>
 10/// Immutability can be guaranteed by cloning the provided enumerable and exposing a readonly view of it, but only
 11/// if immutability of underlying <typeparamref name="T"/> is provided, for example, by using immutable records.
 12/// </remarks>
 13public class ValueReadOnlyCollection<T> : ReadOnlyCollection<T>, IEquatable<ValueReadOnlyCollection<T>>
 14    where T : notnull
 15{
 16    /// <summary>
 17    /// Creates value readonly collection out of the provided enumerable, and independent from it.
 18    /// </summary>
 19    /// <param name="enumerable">The enumerable to be used to build the readonly collection.</param>
 20    public ValueReadOnlyCollection(IEnumerable<T> enumerable)
 159221        : base(new List<T>(enumerable))
 159222    {
 159223    }
 24
 25    /// <inheritdoc path="//*[not(self::summary)]"/>
 26    /// <summary>
 27    ///     <inheritdoc/>
 28    ///     <br/>
 29    ///     Equality is calculated by value, i.e. on the collections items directly.
 30    /// </summary>
 31    public override bool Equals(object? obj) =>
 7332        obj is ValueReadOnlyCollection<T> other && Equals(other);
 33
 34    /// <inheritdoc path="//*[not(self::summary)]"/>
 35    /// <summary>
 36    ///     <inheritdoc/>
 37    ///     <br/>
 38    ///     Equality is calculated by value, i.e. on the collections items directly.
 39    /// </summary>
 7540    public virtual bool Equals(ValueReadOnlyCollection<T>? other) => other is not null && this.SequenceEqual(other);
 41
 42    /// <inheritdoc path="//*[not(self::summary)]"/>
 43    /// <summary>
 44    ///     <inheritdoc/>
 45    ///     <br/>
 46    ///     The hash code is calculated by value, as an aggregate of the hash codes of its items.
 47    /// </summary>
 48    public override int GetHashCode() =>
 15449        Items.Aggregate(0.GetHashCode(), (acc, item) => acc ^ item.GetHashCode());
 50
 51    /// <inheritdoc path="//*[not(self::summary)]"/>
 52    /// <summary>
 53    ///     <inheritdoc/>
 54    ///     <br/>
 55    ///     Format: "[v1, v2, ...]".
 56    /// </summary>
 57    public override string ToString() =>
 158        $"[{string.Join(", ", Items)}]";
 59
 60    /// <summary>
 61    /// Compare the two provided value read-only collections for equality by value.
 62    /// </summary>
 63    /// <param name="left">The first term of comparison.</param>
 64    /// <param name="right">The second term of comparison.</param>
 65    /// <returns>True if the two collections are equal by their items, false otherwise.</returns>
 66    public static bool operator ==(ValueReadOnlyCollection<T> left, ValueReadOnlyCollection<T> right) =>
 267        Equals(left, right);
 68
 69    /// <summary>
 70    /// Compare the two provided value read-only collections for inequality by value.
 71    /// </summary>
 72    /// <param name="left">The first term of comparison.</param>
 73    /// <param name="right">The second term of comparison.</param>
 74    /// <returns>True if the two collections are different by their items, false otherwise.</returns>
 75    public static bool operator !=(ValueReadOnlyCollection<T> left, ValueReadOnlyCollection<T> right) =>
 276        !Equals(left, right);
 77}