blob: 7aed8473dcd4449b0548af7817413bd3b7c90609 [file] [log] [blame]
csharptestd965c662011-05-20 13:57:07 -05001#region Copyright notice and license
2// Protocol Buffers - Google's data interchange format
3// Copyright 2008 Google Inc. All rights reserved.
4// http://github.com/jskeet/dotnet-protobufs/
5// Original C++/Java/Python code:
6// http://code.google.com/p/protobuf/
7//
8// Redistribution and use in source and binary forms, with or without
9// modification, are permitted provided that the following conditions are
10// met:
11//
12// * Redistributions of source code must retain the above copyright
13// notice, this list of conditions and the following disclaimer.
14// * Redistributions in binary form must reproduce the above
15// copyright notice, this list of conditions and the following disclaimer
16// in the documentation and/or other materials provided with the
17// distribution.
18// * Neither the name of Google Inc. nor the names of its
19// contributors may be used to endorse or promote products derived from
20// this software without specific prior written permission.
21//
22// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
25// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
26// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
28// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
32// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33#endregion
34
35using System;
36using System.Collections.Generic;
37using System.Collections.ObjectModel;
38using Google.ProtocolBuffers.Collections;
39
40namespace Google.ProtocolBuffers {
41 /// <summary>
42 /// Represents a single field in an UnknownFieldSet.
43 ///
44 /// An UnknownField consists of five lists of values. The lists correspond
45 /// to the five "wire types" used in the protocol buffer binary format.
46 /// The wire type of each field can be determined from the encoded form alone,
47 /// without knowing the field's declared type. So, we are able to parse
48 /// unknown values at least this far and separate them. Normally, only one
49 /// of the five lists will contain any values, since it is impossible to
50 /// define a valid message type that declares two different types for the
51 /// same field number. However, the code is designed to allow for the case
52 /// where the same unknown field number is encountered using multiple different
53 /// wire types.
54 ///
55 /// UnknownField is an immutable class. To construct one, you must use an
56 /// UnknownField.Builder.
57 /// </summary>
58 public sealed class UnknownField {
59
60 private static readonly UnknownField defaultInstance = CreateBuilder().Build();
61 private readonly ReadOnlyCollection<ulong> varintList;
62 private readonly ReadOnlyCollection<uint> fixed32List;
63 private readonly ReadOnlyCollection<ulong> fixed64List;
64 private readonly ReadOnlyCollection<ByteString> lengthDelimitedList;
65 private readonly ReadOnlyCollection<UnknownFieldSet> groupList;
66
67 private UnknownField(ReadOnlyCollection<ulong> varintList,
68 ReadOnlyCollection<uint> fixed32List,
69 ReadOnlyCollection<ulong> fixed64List,
70 ReadOnlyCollection<ByteString> lengthDelimitedList,
71 ReadOnlyCollection<UnknownFieldSet> groupList) {
72 this.varintList = varintList;
73 this.fixed32List = fixed32List;
74 this.fixed64List = fixed64List;
75 this.lengthDelimitedList = lengthDelimitedList;
76 this.groupList = groupList;
77 }
78
79 public static UnknownField DefaultInstance {
80 get { return defaultInstance; }
81 }
82
83 /// <summary>
84 /// The list of varint values for this field.
85 /// </summary>
86 public IList<ulong> VarintList {
87 get { return varintList; }
88 }
89
90 /// <summary>
91 /// The list of fixed32 values for this field.
92 /// </summary>
93 public IList<uint> Fixed32List {
94 get { return fixed32List; }
95 }
96
97 /// <summary>
98 /// The list of fixed64 values for this field.
99 /// </summary>
100 public IList<ulong> Fixed64List {
101 get { return fixed64List; }
102 }
103
104 /// <summary>
105 /// The list of length-delimited values for this field.
106 /// </summary>
107 public IList<ByteString> LengthDelimitedList {
108 get { return lengthDelimitedList; }
109 }
110
111 /// <summary>
112 /// The list of embedded group values for this field. These
113 /// are represented using UnknownFieldSets rather than Messages
114 /// since the group's type is presumably unknown.
115 /// </summary>
116 public IList<UnknownFieldSet> GroupList {
117 get { return groupList; }
118 }
119
120 public override bool Equals(object other) {
121 if (ReferenceEquals(this, other)) {
122 return true;
123 }
124 UnknownField otherField = other as UnknownField;
125 return otherField != null
126 && Lists.Equals(varintList, otherField.varintList)
127 && Lists.Equals(fixed32List, otherField.fixed32List)
128 && Lists.Equals(fixed64List, otherField.fixed64List)
129 && Lists.Equals(lengthDelimitedList, otherField.lengthDelimitedList)
130 && Lists.Equals(groupList, otherField.groupList);
131 }
132
133 public override int GetHashCode() {
134 int hash = 43;
135 hash = hash * 47 + Lists.GetHashCode(varintList);
136 hash = hash * 47 + Lists.GetHashCode(fixed32List);
137 hash = hash * 47 + Lists.GetHashCode(fixed64List);
138 hash = hash * 47 + Lists.GetHashCode(lengthDelimitedList);
139 hash = hash * 47 + Lists.GetHashCode(groupList);
140 return hash;
141 }
142
143 /// <summary>
144 /// Constructs a new Builder.
145 /// </summary>
146 public static Builder CreateBuilder() {
147 return new Builder();
148 }
149
150 /// <summary>
151 /// Constructs a new Builder and initializes it to a copy of <paramref name="copyFrom"/>.
152 /// </summary>
153 public static Builder CreateBuilder(UnknownField copyFrom) {
154 return new Builder().MergeFrom(copyFrom);
155 }
156
157 /// <summary>
158 /// Serializes the field, including the field number, and writes it to
159 /// <paramref name="output"/>.
160 /// </summary>
161 public void WriteTo(int fieldNumber, CodedOutputStream output) {
162 foreach (ulong value in varintList) {
163 output.WriteUInt64(fieldNumber, value);
164 }
165 foreach (uint value in fixed32List) {
166 output.WriteFixed32(fieldNumber, value);
167 }
168 foreach (ulong value in fixed64List) {
169 output.WriteFixed64(fieldNumber, value);
170 }
171 foreach (ByteString value in lengthDelimitedList) {
172 output.WriteBytes(fieldNumber, value);
173 }
174 foreach (UnknownFieldSet value in groupList) {
175#pragma warning disable 0612
176 output.WriteUnknownGroup(fieldNumber, value);
177#pragma warning restore 0612
178 }
179 }
180
181 /// <summary>
182 /// Computes the number of bytes required to encode this field, including field
183 /// number.
184 /// </summary>
185 public int GetSerializedSize(int fieldNumber) {
186 int result = 0;
187 foreach (ulong value in varintList) {
188 result += CodedOutputStream.ComputeUInt64Size(fieldNumber, value);
189 }
190 foreach (uint value in fixed32List) {
191 result += CodedOutputStream.ComputeFixed32Size(fieldNumber, value);
192 }
193 foreach (ulong value in fixed64List) {
194 result += CodedOutputStream.ComputeFixed64Size(fieldNumber, value);
195 }
196 foreach (ByteString value in lengthDelimitedList) {
197 result += CodedOutputStream.ComputeBytesSize(fieldNumber, value);
198 }
199 foreach (UnknownFieldSet value in groupList) {
200#pragma warning disable 0612
201 result += CodedOutputStream.ComputeUnknownGroupSize(fieldNumber, value);
202#pragma warning restore 0612
203 }
204 return result;
205 }
206
207 /// <summary>
208 /// Serializes the length-delimited values of the field, including field
209 /// number, and writes them to <paramref name="output"/> using the MessageSet wire format.
210 /// </summary>
211 /// <param name="fieldNumber"></param>
212 /// <param name="output"></param>
213 public void WriteAsMessageSetExtensionTo(int fieldNumber, CodedOutputStream output) {
214 foreach (ByteString value in lengthDelimitedList) {
215 output.WriteRawMessageSetExtension(fieldNumber, value);
216 }
217 }
218
219 /// <summary>
220 /// Get the number of bytes required to encode this field, incuding field number,
221 /// using the MessageSet wire format.
222 /// </summary>
223 public int GetSerializedSizeAsMessageSetExtension(int fieldNumber) {
224 int result = 0;
225 foreach (ByteString value in lengthDelimitedList) {
226 result += CodedOutputStream.ComputeRawMessageSetExtensionSize(fieldNumber, value);
227 }
228 return result;
229 }
230
231 /// <summary>
232 /// Used to build instances of UnknownField.
233 /// </summary>
234 public sealed class Builder {
235
236 private List<ulong> varintList;
237 private List<uint> fixed32List;
238 private List<ulong> fixed64List;
239 private List<ByteString> lengthDelimitedList;
240 private List<UnknownFieldSet> groupList;
241
242 /// <summary>
243 /// Builds the field. After building, the builder is reset to an empty
244 /// state. (This is actually easier than making it unusable.)
245 /// </summary>
246 public UnknownField Build() {
247 return new UnknownField(MakeReadOnly(ref varintList),
248 MakeReadOnly(ref fixed32List),
249 MakeReadOnly(ref fixed64List),
250 MakeReadOnly(ref lengthDelimitedList),
251 MakeReadOnly(ref groupList));
252 }
253
254 /// <summary>
255 /// Merge the values in <paramref name="other" /> into this field. For each list
256 /// of values, <paramref name="other"/>'s values are append to the ones in this
257 /// field.
258 /// </summary>
259 public Builder MergeFrom(UnknownField other) {
260 varintList = AddAll(varintList, other.VarintList);
261 fixed32List = AddAll(fixed32List, other.Fixed32List);
262 fixed64List = AddAll(fixed64List, other.Fixed64List);
263 lengthDelimitedList = AddAll(lengthDelimitedList, other.LengthDelimitedList);
264 groupList = AddAll(groupList, other.GroupList);
265 return this;
266 }
267
268 /// <summary>
269 /// Returns a new list containing all of the given specified values from
270 /// both the <paramref name="current"/> and <paramref name="extras"/> lists.
271 /// If <paramref name="current" /> is null and <paramref name="extras"/> is empty,
272 /// null is returned. Otherwise, either a new list is created (if <paramref name="current" />
273 /// is null) or the elements of <paramref name="extras"/> are added to <paramref name="current" />.
274 /// </summary>
275 private static List<T> AddAll<T>(List<T> current, IList<T> extras)
276 {
277 if (extras.Count == 0) {
278 return current;
279 }
280 if (current == null) {
281 current = new List<T>(extras);
282 } else {
283 current.AddRange(extras);
284 }
285 return current;
286 }
287
288 /// <summary>
289 /// Clears the contents of this builder.
290 /// </summary>
291 public Builder Clear() {
292 varintList = null;
293 fixed32List = null;
294 fixed64List = null;
295 lengthDelimitedList = null;
296 groupList = null;
297 return this;
298 }
299
300 /// <summary>
301 /// Adds a varint value.
302 /// </summary>
303 [CLSCompliant(false)]
304 public Builder AddVarint(ulong value) {
305 varintList = Add(varintList, value);
306 return this;
307 }
308
309 /// <summary>
310 /// Adds a fixed32 value.
311 /// </summary>
312 [CLSCompliant(false)]
313 public Builder AddFixed32(uint value) {
314 fixed32List = Add(fixed32List, value);
315 return this;
316 }
317
318 /// <summary>
319 /// Adds a fixed64 value.
320 /// </summary>
321 [CLSCompliant(false)]
322 public Builder AddFixed64(ulong value) {
323 fixed64List = Add(fixed64List, value);
324 return this;
325 }
326
327 /// <summary>
328 /// Adds a length-delimited value.
329 /// </summary>
330 public Builder AddLengthDelimited(ByteString value) {
331 lengthDelimitedList = Add(lengthDelimitedList, value);
332 return this;
333 }
334
335 /// <summary>
336 /// Adds an embedded group.
337 /// </summary>
338 /// <param name="value"></param>
339 /// <returns></returns>
340 public Builder AddGroup(UnknownFieldSet value) {
341 groupList = Add(groupList, value);
342 return this;
343 }
344
345 /// <summary>
346 /// Adds <paramref name="value"/> to the <paramref name="list"/>, creating
347 /// a new list if <paramref name="list"/> is null. The list is returned - either
348 /// the original reference or the new list.
349 /// </summary>
350 private static List<T> Add<T>(List<T> list, T value) {
351 if (list == null) {
352 list = new List<T>();
353 }
354 list.Add(value);
355 return list;
356 }
357
358 /// <summary>
359 /// Returns a read-only version of the given IList, and clears
360 /// the field used for <paramref name="list"/>. If the value
361 /// is null, an empty list is produced using Lists.Empty.
362 /// </summary>
363 /// <returns></returns>
364 private static ReadOnlyCollection<T> MakeReadOnly<T>(ref List<T> list) {
365 ReadOnlyCollection<T> ret = list == null ? Lists<T>.Empty : new ReadOnlyCollection<T>(list);
366 list = null;
367 return ret;
368 }
369 }
370 }
371}