Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.ExtLibs/HeuristicLab.ProtobufCS/2.4.1/ProtobufCS/src/ProtocolBuffers/Collections/PopsicleList.cs @ 9503

Last change on this file since 9503 was 8295, checked in by abeham, 12 years ago

#1897:

  • Removed protocol buffers 0.9.1
  • Added protocol buffers 2.4.1
  • Updated proto processing command
File size: 6.5 KB
Line 
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.
32using System;
33using System.Collections;
34using System.Collections.Generic;
35
36namespace Google.ProtocolBuffers.Collections
37{
38    /// <summary>
39    /// Proxies calls to a <see cref="List{T}" />, but allows the list
40    /// to be made read-only (with the <see cref="MakeReadOnly" /> method),
41    /// after which any modifying methods throw <see cref="NotSupportedException" />.
42    /// </summary>
43    public sealed class PopsicleList<T> : IPopsicleList<T>, ICastArray
44    {
45        private static readonly bool CheckForNull = default(T) == null;
46        private static readonly T[] EmptySet = new T[0];
47
48        private List<T> items;
49        private bool readOnly;
50
51        /// <summary>
52        /// Makes this list read-only ("freezes the popsicle"). From this
53        /// point on, mutating methods (Clear, Add etc) will throw a
54        /// NotSupportedException. There is no way of "defrosting" the list afterwards.
55        /// </summary>
56        public void MakeReadOnly()
57        {
58            readOnly = true;
59        }
60
61        public int IndexOf(T item)
62        {
63            return items == null ? -1 : items.IndexOf(item);
64        }
65
66        public void Insert(int index, T item)
67        {
68            ValidateModification();
69            if (CheckForNull)
70            {
71                ThrowHelper.ThrowIfNull(item);
72            }
73            items.Insert(index, item);
74        }
75
76        public void RemoveAt(int index)
77        {
78            ValidateModification();
79            items.RemoveAt(index);
80        }
81
82        public T this[int index]
83        {
84            get
85            {
86                if (items == null)
87                {
88                    throw new ArgumentOutOfRangeException();
89                }
90                return items[index];
91            }
92            set
93            {
94                ValidateModification();
95                if (CheckForNull)
96                {
97                    ThrowHelper.ThrowIfNull(value);
98                }
99                items[index] = value;
100            }
101        }
102
103        public void Add(T item)
104        {
105            ValidateModification();
106            if (CheckForNull)
107            {
108                ThrowHelper.ThrowIfNull(item);
109            }
110            items.Add(item);
111        }
112
113        public void Clear()
114        {
115            ValidateModification();
116            items.Clear();
117        }
118
119        public bool Contains(T item)
120        {
121            return items == null ? false : items.Contains(item);
122        }
123
124        public void CopyTo(T[] array, int arrayIndex)
125        {
126            if (items != null)
127            {
128                items.CopyTo(array, arrayIndex);
129            }
130        }
131
132        public int Count
133        {
134            get { return items == null ? 0 : items.Count; }
135        }
136
137        public bool IsReadOnly
138        {
139            get { return readOnly; }
140        }
141
142        public bool Remove(T item)
143        {
144            ValidateModification();
145            return items.Remove(item);
146        }
147
148        public IEnumerator<T> GetEnumerator()
149        {
150            IEnumerable<T> tenum = (IEnumerable<T>)items ?? EmptySet;
151            return tenum.GetEnumerator();
152        }
153
154        IEnumerator IEnumerable.GetEnumerator()
155        {
156            return GetEnumerator();
157        }
158
159        public void Add(IEnumerable<T> collection)
160        {
161            ValidateModification();
162            ThrowHelper.ThrowIfNull(collection);
163
164            if (!CheckForNull || collection is PopsicleList<T>)
165            {
166                items.AddRange(collection);
167            }
168            else
169            {
170                // Assumption, it's ok to enumerate collections more than once.
171                if (collection is ICollection<T>)
172                {
173                    ThrowHelper.ThrowIfAnyNull(collection);
174                    items.AddRange(collection);
175                }
176                else
177                {
178                    foreach (T item in collection)
179                    {
180                        ThrowHelper.ThrowIfNull(item);
181                        items.Add(item);
182                    }
183                }
184            }
185        }
186
187        private void ValidateModification()
188        {
189            if (readOnly)
190            {
191                throw new NotSupportedException("List is read-only");
192            }
193            if (items == null)
194            {
195                items = new List<T>();
196            }
197        }
198
199        IEnumerable<TItemType> ICastArray.CastArray<TItemType>()
200        {
201            if (items == null)
202            {
203                return PopsicleList<TItemType>.EmptySet;
204            }
205            return (TItemType[]) (object) items.ToArray();
206        }
207    }
208}
Note: See TracBrowser for help on using the repository browser.