diff options
author | Jon Skeet <skeet@pobox.com> | 2008-08-21 21:57:58 +0100 |
---|---|---|
committer | Jon Skeet <skeet@pobox.com> | 2008-08-21 21:57:58 +0100 |
commit | 0bf2ad145daf4ed7eb0f72c111b496d55233e0b9 (patch) | |
tree | 6c611fe5fb2b26aed50bcc2c4197d7ecc378a6b7 /csharp/ProtocolBuffers/Collections | |
parent | ad6903fe33ca0a79e7618a25e83de769438d18ce (diff) | |
download | protobuf-0bf2ad145daf4ed7eb0f72c111b496d55233e0b9.tar.gz protobuf-0bf2ad145daf4ed7eb0f72c111b496d55233e0b9.tar.bz2 protobuf-0bf2ad145daf4ed7eb0f72c111b496d55233e0b9.zip |
Implemented popsicle immutability for lists. Modified MessageStreamIterator to be singly generic.
Diffstat (limited to 'csharp/ProtocolBuffers/Collections')
-rw-r--r-- | csharp/ProtocolBuffers/Collections/PopsicleList.cs | 95 |
1 files changed, 95 insertions, 0 deletions
diff --git a/csharp/ProtocolBuffers/Collections/PopsicleList.cs b/csharp/ProtocolBuffers/Collections/PopsicleList.cs new file mode 100644 index 00000000..4efe13d0 --- /dev/null +++ b/csharp/ProtocolBuffers/Collections/PopsicleList.cs @@ -0,0 +1,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"); + } + } + } +} |