| | 1 | | using System.Collections.ObjectModel; |
| | 2 | |
|
| | 3 | | namespace 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> |
| | 13 | | public 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) |
| 1592 | 21 | | : base(new List<T>(enumerable)) |
| 1592 | 22 | | { |
| 1592 | 23 | | } |
| | 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) => |
| 73 | 32 | | 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> |
| 75 | 40 | | 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() => |
| 154 | 49 | | 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() => |
| 1 | 58 | | $"[{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) => |
| 2 | 67 | | 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) => |
| 2 | 76 | | !Equals(left, right); |
| | 77 | | } |