blob: 4efe13d0a7094b2f776d4ff4a542dfbeaa164d09 (
plain) (
blame)
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
|
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
namespace Google.ProtocolBuffers.Collections {
/// <summary>
/// Proxies calls to a <see cref="List{T}" />, but allows the list
/// to be made read-only (with the <see cref="MakeReadOnly" /> method),
/// after which any modifying methods throw <see cref="NotSupportedException" />.
/// </summary>
public sealed class PopsicleList<T> : IList<T> {
private readonly List<T> items = new List<T>();
private bool readOnly = false;
/// <summary>
/// Makes this list read-only ("freezes the popsicle"). From this
/// point on, mutating methods (Clear, Add etc) will throw a
/// NotSupportedException. There is no way of "defrosting" the list afterwards.
/// </summary>
public void MakeReadOnly() {
readOnly = true;
}
public int IndexOf(T item) {
return items.IndexOf(item);
}
public void Insert(int index, T item) {
ValidateModification();
items.Insert(index, item);
}
public void RemoveAt(int index) {
ValidateModification();
items.RemoveAt(index);
}
public T this[int index] {
get {
return items[index];
}
set {
ValidateModification();
items[index] = value;
}
}
public void Add(T item) {
ValidateModification();
items.Add(item);
}
public void Clear() {
ValidateModification();
items.Clear();
}
public bool Contains(T item) {
return items.Contains(item);
}
public void CopyTo(T[] array, int arrayIndex) {
items.CopyTo(array, arrayIndex);
}
public int Count {
get { return items.Count; }
}
public bool IsReadOnly {
get { return readOnly; }
}
public bool Remove(T item) {
ValidateModification();
return items.Remove(item);
}
public IEnumerator<T> GetEnumerator() {
return items.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator() {
return GetEnumerator();
}
private void ValidateModification() {
if (readOnly) {
throw new NotSupportedException("List is read-only");
}
}
}
}
|