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