blob: 1873e30234c478df631f4ab9d47a7fa6bfe04ccd [file] [log] [blame]
Jon Skeet60c059b2008-10-23 21:17:56 +01001// 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:
Jon Skeet68036862008-10-22 13:30:34 +01005// http://code.google.com/p/protobuf/
6//
Jon Skeet60c059b2008-10-23 21:17:56 +01007// Redistribution and use in source and binary forms, with or without
8// modification, are permitted provided that the following conditions are
9// met:
Jon Skeet68036862008-10-22 13:30:34 +010010//
Jon Skeet60c059b2008-10-23 21:17:56 +010011// * 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.
Jon Skeet68036862008-10-22 13:30:34 +010020//
Jon Skeet60c059b2008-10-23 21:17:56 +010021// 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.
Jon Skeet68036862008-10-22 13:30:34 +010032using System.Collections.Generic;
33using System.Collections.ObjectModel;
34using Google.ProtocolBuffers.Collections;
35
36namespace Google.ProtocolBuffers {
37 /// <summary>
38 /// Represents a single field in an UnknownFieldSet.
39 ///
40 /// An UnknownField consists of five lists of values. The lists correspond
41 /// to the five "wire types" used in the protocol buffer binary format.
42 /// The wire type of each field can be determined from the encoded form alone,
43 /// without knowing the field's declared type. So, we are able to parse
44 /// unknown values at least this far and separate them. Normally, only one
45 /// of the five lists will contain any values, since it is impossible to
46 /// define a valid message type that declares two different types for the
47 /// same field number. However, the code is designed to allow for the case
48 /// where the same unknown field number is encountered using multiple different
49 /// wire types.
50 ///
51 /// UnknownField is an immutable class. To construct one, you must use an
52 /// UnknownField.Builder.
53 /// </summary>
54 public sealed class UnknownField {
55
56 private static readonly UnknownField defaultInstance = CreateBuilder().Build();
57 private readonly ReadOnlyCollection<ulong> varintList;
58 private readonly ReadOnlyCollection<uint> fixed32List;
59 private readonly ReadOnlyCollection<ulong> fixed64List;
60 private readonly ReadOnlyCollection<ByteString> lengthDelimitedList;
61 private readonly ReadOnlyCollection<UnknownFieldSet> groupList;
62
63 private UnknownField(ReadOnlyCollection<ulong> varintList,
64 ReadOnlyCollection<uint> fixed32List,
65 ReadOnlyCollection<ulong> fixed64List,
66 ReadOnlyCollection<ByteString> lengthDelimitedList,
67 ReadOnlyCollection<UnknownFieldSet> groupList) {
68 this.varintList = varintList;
69 this.fixed32List = fixed32List;
70 this.fixed64List = fixed64List;
71 this.lengthDelimitedList = lengthDelimitedList;
72 this.groupList = groupList;
73 }
74
75 public static UnknownField DefaultInstance {
76 get { return defaultInstance; }
77 }
78
79 /// <summary>
80 /// The list of varint values for this field.
81 /// </summary>
82 public IList<ulong> VarintList {
83 get { return varintList; }
84 }
85
86 /// <summary>
87 /// The list of fixed32 values for this field.
88 /// </summary>
89 public IList<uint> Fixed32List {
90 get { return fixed32List; }
91 }
92
93 /// <summary>
94 /// The list of fixed64 values for this field.
95 /// </summary>
96 public IList<ulong> Fixed64List {
97 get { return fixed64List; }
98 }
99
100 /// <summary>
101 /// The list of length-delimited values for this field.
102 /// </summary>
103 public IList<ByteString> LengthDelimitedList {
104 get { return lengthDelimitedList; }
105 }
106
107 /// <summary>
108 /// The list of embedded group values for this field. These
109 /// are represented using UnknownFieldSets rather than Messages
110 /// since the group's type is presumably unknown.
111 /// </summary>
112 public IList<UnknownFieldSet> GroupList {
113 get { return groupList; }
114 }
115
Jon Skeet43da7ae2009-05-28 21:45:43 +0100116 public override bool Equals(object other) {
117 if (ReferenceEquals(this, other)) {
118 return true;
119 }
120 UnknownField otherField = other as UnknownField;
121 return otherField != null
122 && Lists.Equals(varintList, otherField.varintList)
123 && Lists.Equals(fixed32List, otherField.fixed32List)
124 && Lists.Equals(fixed64List, otherField.fixed64List)
125 && Lists.Equals(lengthDelimitedList, otherField.lengthDelimitedList)
126 && Lists.Equals(groupList, otherField.groupList);
127 }
128
129 public override int GetHashCode() {
130 int hash = 43;
131 hash = hash * 47 + Lists.GetHashCode(varintList);
132 hash = hash * 47 + Lists.GetHashCode(fixed32List);
133 hash = hash * 47 + Lists.GetHashCode(fixed64List);
134 hash = hash * 47 + Lists.GetHashCode(lengthDelimitedList);
135 hash = hash * 47 + Lists.GetHashCode(groupList);
136 return hash;
137 }
138
Jon Skeet68036862008-10-22 13:30:34 +0100139 /// <summary>
140 /// Constructs a new Builder.
141 /// </summary>
142 public static Builder CreateBuilder() {
143 return new Builder();
144 }
145
146 /// <summary>
147 /// Constructs a new Builder and initializes it to a copy of <paramref name="copyFrom"/>.
148 /// </summary>
149 public static Builder CreateBuilder(UnknownField copyFrom) {
150 return new Builder().MergeFrom(copyFrom);
151 }
152
153 /// <summary>
154 /// Serializes the field, including the field number, and writes it to
155 /// <paramref name="output"/>.
156 /// </summary>
157 public void WriteTo(int fieldNumber, CodedOutputStream output) {
158 foreach (ulong value in varintList) {
159 output.WriteUInt64(fieldNumber, value);
160 }
161 foreach (uint value in fixed32List) {
162 output.WriteFixed32(fieldNumber, value);
163 }
164 foreach (ulong value in fixed64List) {
165 output.WriteFixed64(fieldNumber, value);
166 }
167 foreach (ByteString value in lengthDelimitedList) {
168 output.WriteBytes(fieldNumber, value);
169 }
170 foreach (UnknownFieldSet value in groupList) {
171 output.WriteUnknownGroup(fieldNumber, value);
172 }
173 }
174
175 /// <summary>
176 /// Computes the number of bytes required to encode this field, including field
177 /// number.
178 /// </summary>
179 public int GetSerializedSize(int fieldNumber) {
180 int result = 0;
181 foreach (ulong value in varintList) {
182 result += CodedOutputStream.ComputeUInt64Size(fieldNumber, value);
183 }
184 foreach (uint value in fixed32List) {
185 result += CodedOutputStream.ComputeFixed32Size(fieldNumber, value);
186 }
187 foreach (ulong value in fixed64List) {
188 result += CodedOutputStream.ComputeFixed64Size(fieldNumber, value);
189 }
190 foreach (ByteString value in lengthDelimitedList) {
191 result += CodedOutputStream.ComputeBytesSize(fieldNumber, value);
192 }
193 foreach (UnknownFieldSet value in groupList) {
194 result += CodedOutputStream.ComputeUnknownGroupSize(fieldNumber, value);
195 }
196 return result;
197 }
198
199 /// <summary>
200 /// Serializes the length-delimited values of the field, including field
201 /// number, and writes them to <paramref name="output"/> using the MessageSet wire format.
202 /// </summary>
203 /// <param name="fieldNumber"></param>
204 /// <param name="output"></param>
205 public void WriteAsMessageSetExtensionTo(int fieldNumber, CodedOutputStream output) {
206 foreach (ByteString value in lengthDelimitedList) {
207 output.WriteRawMessageSetExtension(fieldNumber, value);
208 }
209 }
210
211 /// <summary>
212 /// Get the number of bytes required to encode this field, incuding field number,
213 /// using the MessageSet wire format.
214 /// </summary>
215 public int GetSerializedSizeAsMessageSetExtension(int fieldNumber) {
216 int result = 0;
217 foreach (ByteString value in lengthDelimitedList) {
218 result += CodedOutputStream.ComputeRawMessageSetExtensionSize(fieldNumber, value);
219 }
220 return result;
221 }
222
223 /// <summary>
224 /// Used to build instances of UnknownField.
225 /// </summary>
226 public sealed class Builder {
227
228 private List<ulong> varintList;
229 private List<uint> fixed32List;
230 private List<ulong> fixed64List;
231 private List<ByteString> lengthDelimitedList;
232 private List<UnknownFieldSet> groupList;
233
234 /// <summary>
235 /// Builds the field. After building, the builder is reset to an empty
236 /// state. (This is actually easier than making it unusable.)
237 /// </summary>
238 public UnknownField Build() {
239 return new UnknownField(MakeReadOnly(ref varintList),
240 MakeReadOnly(ref fixed32List),
241 MakeReadOnly(ref fixed64List),
242 MakeReadOnly(ref lengthDelimitedList),
243 MakeReadOnly(ref groupList));
244 }
245
246 /// <summary>
247 /// Merge the values in <paramref name="other" /> into this field. For each list
248 /// of values, <paramref name="other"/>'s values are append to the ones in this
249 /// field.
250 /// </summary>
251 public Builder MergeFrom(UnknownField other) {
252 varintList = AddAll(varintList, other.VarintList);
253 fixed32List = AddAll(fixed32List, other.Fixed32List);
254 fixed64List = AddAll(fixed64List, other.Fixed64List);
255 lengthDelimitedList = AddAll(lengthDelimitedList, other.LengthDelimitedList);
256 groupList = AddAll(groupList, other.GroupList);
257 return this;
258 }
259
260 /// <summary>
261 /// Returns a new list containing all of the given specified values from
262 /// both the <paramref name="current"/> and <paramref name="extras"/> lists.
263 /// If <paramref name="current" /> is null and <paramref name="extras"/> is empty,
264 /// null is returned. Otherwise, either a new list is created (if <paramref name="current" />
265 /// is null) or the elements of <paramref name="extras"/> are added to <paramref name="current" />.
266 /// </summary>
267 private static List<T> AddAll<T>(List<T> current, IList<T> extras)
268 {
269 if (extras.Count == 0) {
270 return current;
271 }
272 if (current == null) {
273 current = new List<T>(extras);
274 } else {
275 current.AddRange(extras);
276 }
277 return current;
278 }
279
280 /// <summary>
281 /// Clears the contents of this builder.
282 /// </summary>
283 public Builder Clear() {
284 varintList = null;
285 fixed32List = null;
286 fixed64List = null;
287 lengthDelimitedList = null;
288 groupList = null;
289 return this;
290 }
291
292 /// <summary>
293 /// Adds a varint value.
294 /// </summary>
295 public Builder AddVarint(ulong value) {
296 varintList = Add(varintList, value);
297 return this;
298 }
299
300 /// <summary>
301 /// Adds a fixed32 value.
302 /// </summary>
303 public Builder AddFixed32(uint value) {
304 fixed32List = Add(fixed32List, value);
305 return this;
306 }
307
308 /// <summary>
309 /// Adds a fixed64 value.
310 /// </summary>
311 public Builder AddFixed64(ulong value) {
312 fixed64List = Add(fixed64List, value);
313 return this;
314 }
315
316 /// <summary>
317 /// Adds a length-delimited value.
318 /// </summary>
319 public Builder AddLengthDelimited(ByteString value) {
320 lengthDelimitedList = Add(lengthDelimitedList, value);
321 return this;
322 }
323
324 /// <summary>
325 /// Adds an embedded group.
326 /// </summary>
327 /// <param name="value"></param>
328 /// <returns></returns>
329 public Builder AddGroup(UnknownFieldSet value) {
330 groupList = Add(groupList, value);
331 return this;
332 }
333
334 /// <summary>
335 /// Adds <paramref name="value"/> to the <paramref name="list"/>, creating
336 /// a new list if <paramref name="list"/> is null. The list is returned - either
337 /// the original reference or the new list.
338 /// </summary>
339 private static List<T> Add<T>(List<T> list, T value) {
340 if (list == null) {
341 list = new List<T>();
342 }
343 list.Add(value);
344 return list;
345 }
346
347 /// <summary>
348 /// Returns a read-only version of the given IList, and clears
349 /// the field used for <paramref name="list"/>. If the value
350 /// is null, an empty list is produced using Lists.Empty.
351 /// </summary>
352 /// <returns></returns>
353 private static ReadOnlyCollection<T> MakeReadOnly<T>(ref List<T> list) {
354 ReadOnlyCollection<T> ret = list == null ? Lists<T>.Empty : new ReadOnlyCollection<T>(list);
355 list = null;
356 return ret;
357 }
358 }
359 }
360}