-
Notifications
You must be signed in to change notification settings - Fork 3
/
LazyReadOnlyCollection.cs
108 lines (78 loc) · 4.19 KB
/
LazyReadOnlyCollection.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
using Gsemac.Collections.Properties;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
#if ENABLE_POLYFILLS
using Gsemac.Polyfills.System.Collections.Generic;
#endif
namespace Gsemac.Collections {
public sealed class LazyReadOnlyCollection<T> :
IList<T>,
IReadOnlyList<T> {
/// <summary>
/// Gets the element at the specified index.
/// </summary>
/// <param name="index">The zero-based index of the element to get.</param>
/// <returns>The element at the specified index.</returns>
public T this[int index] {
get {
if (index < 0 || index >= Count)
throw new ArgumentOutOfRangeException(nameof(index), ExceptionMessages.IndexOutOfRange);
return items.ElementAt(index);
}
}
T IList<T>.this[int index] {
get => items.ElementAt(index);
set => throw new NotSupportedException(ExceptionMessages.CollectionIsReadOnly);
}
/// <summary>
/// Gets the number of elements contained in the <see cref="LazyReadOnlyCollection{T}"/> instance.
/// </summary>
public int Count => items.Count();
bool ICollection<T>.IsReadOnly => true;
/// <summary>
/// Initializes a new instance of the <see cref="LazyReadOnlyCollection{T}"/> class that is a read-only wrapper around the specified <see cref="IEnumerable{T}"/>.
/// </summary>
/// <param name="items">The list to wrap.</param>
public LazyReadOnlyCollection(IEnumerable<T> items) {
if (items is null)
throw new ArgumentNullException(nameof(items));
this.items = items;
}
/// <summary>
/// Determines whether an element is in the <see cref="LazyReadOnlyCollection{T}"/>.
/// </summary>
/// <param name="item">The object to locate in the <see cref="LazyReadOnlyCollection{T}"/>. The value can be <see langword="null"/> for reference types.</param>
/// <returns><see langword="true"/> if value is found in the <see cref="LazyReadOnlyCollection{T}"/> otherwise, <see langword="false"/>.</returns>
public bool Contains(T item) {
return items.Any(i => i.Equals(item));
}
/// <summary>
/// Copies the entire <see cref="LazyReadOnlyCollection{T}"/> to a compatible one-dimensional <see cref="Array"/>, starting at the specified index of the target array.
/// </summary>
/// <param name="array">The one-dimensional <see cref="Array"/> that is the destination of the elements copied from <see cref="LazyReadOnlyCollection{T}"/>. The <see cref="Array"/> must have zero-based indexing.</param>
/// <param name="arrayIndex">The zero-based index in <paramref name="array"/> at which copying begins.</param>
public void CopyTo(T[] array, int arrayIndex) {
items.ToArray().CopyTo(array, arrayIndex);
}
/// <summary>
/// Returns an enumerator that iterates through the <see cref="LazyReadOnlyCollection{T}"/>.
/// </summary>
/// <returns>An <see cref="IEnumerator{T}"/> for the <see cref="LazyReadOnlyCollection{T}"/>.</returns>
public IEnumerator<T> GetEnumerator() {
return items.GetEnumerator();
}
void ICollection<T>.Add(T item) => throw new NotSupportedException(ExceptionMessages.CollectionIsReadOnly);
void ICollection<T>.Clear() => throw new NotSupportedException(ExceptionMessages.CollectionIsReadOnly);
bool ICollection<T>.Remove(T item) => throw new NotSupportedException(ExceptionMessages.CollectionIsReadOnly);
int IList<T>.IndexOf(T item) => throw new NotSupportedException(ExceptionMessages.CollectionIsReadOnly);
void IList<T>.Insert(int index, T item) => throw new NotSupportedException(ExceptionMessages.CollectionIsReadOnly);
void IList<T>.RemoveAt(int index) => throw new NotSupportedException(ExceptionMessages.CollectionIsReadOnly);
IEnumerator IEnumerable.GetEnumerator() {
return GetEnumerator();
}
// Private members
private readonly IEnumerable<T> items;
}
}