1 | // Protocol Buffers - Google's data interchange format
|
---|
2 | // Copyright 2008 Google Inc. All rights reserved.
|
---|
3 | // http://github.com/jskeet/dotnet-protobufs/
|
---|
4 | // Original C++/Java/Python code:
|
---|
5 | // http://code.google.com/p/protobuf/
|
---|
6 | //
|
---|
7 | // Redistribution and use in source and binary forms, with or without
|
---|
8 | // modification, are permitted provided that the following conditions are
|
---|
9 | // met:
|
---|
10 | //
|
---|
11 | // * Redistributions of source code must retain the above copyright
|
---|
12 | // notice, this list of conditions and the following disclaimer.
|
---|
13 | // * Redistributions in binary form must reproduce the above
|
---|
14 | // copyright notice, this list of conditions and the following disclaimer
|
---|
15 | // in the documentation and/or other materials provided with the
|
---|
16 | // distribution.
|
---|
17 | // * Neither the name of Google Inc. nor the names of its
|
---|
18 | // contributors may be used to endorse or promote products derived from
|
---|
19 | // this software without specific prior written permission.
|
---|
20 | //
|
---|
21 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
---|
22 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
---|
23 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
---|
24 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
---|
25 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
---|
26 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
---|
27 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
28 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
29 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
30 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
---|
31 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
32 | using System;
|
---|
33 | using System.Collections;
|
---|
34 | using System.Collections.Generic;
|
---|
35 |
|
---|
36 | namespace Google.ProtocolBuffers.Collections {
|
---|
37 | /// <summary>
|
---|
38 | /// Proxies calls to a <see cref="List{T}" />, but allows the list
|
---|
39 | /// to be made read-only (with the <see cref="MakeReadOnly" /> method),
|
---|
40 | /// after which any modifying methods throw <see cref="NotSupportedException" />.
|
---|
41 | /// </summary>
|
---|
42 | public sealed class PopsicleList<T> : IPopsicleList<T> {
|
---|
43 |
|
---|
44 | private readonly List<T> items = new List<T>();
|
---|
45 | private bool readOnly = false;
|
---|
46 |
|
---|
47 | /// <summary>
|
---|
48 | /// Makes this list read-only ("freezes the popsicle"). From this
|
---|
49 | /// point on, mutating methods (Clear, Add etc) will throw a
|
---|
50 | /// NotSupportedException. There is no way of "defrosting" the list afterwards.
|
---|
51 | /// </summary>
|
---|
52 | public void MakeReadOnly() {
|
---|
53 | readOnly = true;
|
---|
54 | }
|
---|
55 |
|
---|
56 | public int IndexOf(T item) {
|
---|
57 | return items.IndexOf(item);
|
---|
58 | }
|
---|
59 |
|
---|
60 | public void Insert(int index, T item) {
|
---|
61 | ValidateModification();
|
---|
62 | items.Insert(index, item);
|
---|
63 | }
|
---|
64 |
|
---|
65 | public void RemoveAt(int index) {
|
---|
66 | ValidateModification();
|
---|
67 | items.RemoveAt(index);
|
---|
68 | }
|
---|
69 |
|
---|
70 | public T this[int index] {
|
---|
71 | get {
|
---|
72 | return items[index];
|
---|
73 | }
|
---|
74 | set {
|
---|
75 | ValidateModification();
|
---|
76 | items[index] = value;
|
---|
77 | }
|
---|
78 | }
|
---|
79 |
|
---|
80 | public void Add(T item) {
|
---|
81 | ValidateModification();
|
---|
82 | items.Add(item);
|
---|
83 | }
|
---|
84 |
|
---|
85 | public void Clear() {
|
---|
86 | ValidateModification();
|
---|
87 | items.Clear();
|
---|
88 | }
|
---|
89 |
|
---|
90 | public bool Contains(T item) {
|
---|
91 | return items.Contains(item);
|
---|
92 | }
|
---|
93 |
|
---|
94 | public void CopyTo(T[] array, int arrayIndex) {
|
---|
95 | items.CopyTo(array, arrayIndex);
|
---|
96 | }
|
---|
97 |
|
---|
98 | public int Count {
|
---|
99 | get { return items.Count; }
|
---|
100 | }
|
---|
101 |
|
---|
102 | public bool IsReadOnly {
|
---|
103 | get { return readOnly; }
|
---|
104 | }
|
---|
105 |
|
---|
106 | public bool Remove(T item) {
|
---|
107 | ValidateModification();
|
---|
108 | return items.Remove(item);
|
---|
109 | }
|
---|
110 |
|
---|
111 | public void Add(IEnumerable<T> collection) {
|
---|
112 | if (readOnly) {
|
---|
113 | throw new NotSupportedException("List is read-only");
|
---|
114 | }
|
---|
115 | items.AddRange(collection);
|
---|
116 | }
|
---|
117 |
|
---|
118 | public IEnumerator<T> GetEnumerator() {
|
---|
119 | return items.GetEnumerator();
|
---|
120 | }
|
---|
121 |
|
---|
122 | IEnumerator IEnumerable.GetEnumerator() {
|
---|
123 | return GetEnumerator();
|
---|
124 | }
|
---|
125 |
|
---|
126 | private void ValidateModification() {
|
---|
127 | if (readOnly) {
|
---|
128 | throw new NotSupportedException("List is read-only");
|
---|
129 | }
|
---|
130 | }
|
---|
131 | }
|
---|
132 | }
|
---|