AbstractBuilder.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. #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. using System;
  35. using System.Collections;
  36. using System.Collections.Generic;
  37. using Google.ProtocolBuffers.Descriptors;
  38. namespace Google.ProtocolBuffers
  39. {
  40. /// <summary>
  41. /// Implementation of the non-generic IMessage interface as far as possible.
  42. /// </summary>
  43. public abstract partial class AbstractBuilder<TMessage, TBuilder> : AbstractBuilderLite<TMessage, TBuilder>,
  44. IBuilder<TMessage, TBuilder>
  45. where TMessage : AbstractMessage<TMessage, TBuilder>
  46. where TBuilder : AbstractBuilder<TMessage, TBuilder>
  47. {
  48. #region Unimplemented members of IBuilder
  49. public abstract UnknownFieldSet UnknownFields { get; set; }
  50. public abstract IDictionary<FieldDescriptor, object> AllFields { get; }
  51. public abstract object this[FieldDescriptor field] { get; set; }
  52. public abstract MessageDescriptor DescriptorForType { get; }
  53. public abstract int GetRepeatedFieldCount(FieldDescriptor field);
  54. public abstract object this[FieldDescriptor field, int index] { get; set; }
  55. public abstract bool HasField(FieldDescriptor field);
  56. public abstract bool HasOneof(OneofDescriptor oneof);
  57. public abstract FieldDescriptor OneofFieldDescriptor(OneofDescriptor oneof);
  58. public abstract IBuilder CreateBuilderForField(FieldDescriptor field);
  59. public abstract TBuilder ClearField(FieldDescriptor field);
  60. public abstract TBuilder ClearOneof(OneofDescriptor oneof);
  61. public abstract TBuilder AddRepeatedField(FieldDescriptor field, object value);
  62. #endregion
  63. public TBuilder SetUnknownFields(UnknownFieldSet fields)
  64. {
  65. UnknownFields = fields;
  66. return ThisBuilder;
  67. }
  68. public override TBuilder Clear()
  69. {
  70. foreach (FieldDescriptor field in AllFields.Keys)
  71. {
  72. ClearField(field);
  73. }
  74. return ThisBuilder;
  75. }
  76. public override sealed TBuilder MergeFrom(IMessageLite other)
  77. {
  78. if (other is IMessage)
  79. {
  80. return MergeFrom((IMessage) other);
  81. }
  82. throw new ArgumentException("MergeFrom(Message) can only merge messages of the same type.");
  83. }
  84. /// <summary>
  85. /// Merge the specified other message into the message being
  86. /// built. Merging occurs as follows. For each field:
  87. /// For singular primitive fields, if the field is set in <paramref name="other"/>,
  88. /// then <paramref name="other"/>'s value overwrites the value in this message.
  89. /// For singular message fields, if the field is set in <paramref name="other"/>,
  90. /// it is merged into the corresponding sub-message of this message using the same
  91. /// merging rules.
  92. /// For repeated fields, the elements in <paramref name="other"/> are concatenated
  93. /// with the elements in this message.
  94. /// </summary>
  95. /// <param name="other"></param>
  96. /// <returns></returns>
  97. public abstract TBuilder MergeFrom(TMessage other);
  98. public virtual TBuilder MergeFrom(IMessage other)
  99. {
  100. if (other.DescriptorForType != DescriptorForType)
  101. {
  102. throw new ArgumentException("MergeFrom(IMessage) can only merge messages of the same type.");
  103. }
  104. // Note: We don't attempt to verify that other's fields have valid
  105. // types. Doing so would be a losing battle. We'd have to verify
  106. // all sub-messages as well, and we'd have to make copies of all of
  107. // them to insure that they don't change after verification (since
  108. // the Message interface itself cannot enforce immutability of
  109. // implementations).
  110. // TODO(jonskeet): Provide a function somewhere called MakeDeepCopy()
  111. // which allows people to make secure deep copies of messages.
  112. foreach (KeyValuePair<FieldDescriptor, object> entry in other.AllFields)
  113. {
  114. FieldDescriptor field = entry.Key;
  115. if (field.IsRepeated)
  116. {
  117. // Concatenate repeated fields
  118. foreach (object element in (IEnumerable) entry.Value)
  119. {
  120. AddRepeatedField(field, element);
  121. }
  122. }
  123. else if (field.MappedType == MappedType.Message)
  124. {
  125. // Merge singular messages
  126. IMessageLite existingValue = (IMessageLite) this[field];
  127. if (existingValue == existingValue.WeakDefaultInstanceForType)
  128. {
  129. this[field] = entry.Value;
  130. }
  131. else
  132. {
  133. this[field] = existingValue.WeakCreateBuilderForType()
  134. .WeakMergeFrom(existingValue)
  135. .WeakMergeFrom((IMessageLite) entry.Value)
  136. .WeakBuild();
  137. }
  138. }
  139. else
  140. {
  141. // Overwrite simple values
  142. this[field] = entry.Value;
  143. }
  144. }
  145. //Fix for unknown fields not merging, see java's AbstractMessage.Builder<T> line 236
  146. MergeUnknownFields(other.UnknownFields);
  147. return ThisBuilder;
  148. }
  149. public override TBuilder MergeFrom(ICodedInputStream input, ExtensionRegistry extensionRegistry)
  150. {
  151. UnknownFieldSet.Builder unknownFields = UnknownFieldSet.CreateBuilder(UnknownFields);
  152. unknownFields.MergeFrom(input, extensionRegistry, this);
  153. UnknownFields = unknownFields.Build();
  154. return ThisBuilder;
  155. }
  156. public virtual TBuilder MergeUnknownFields(UnknownFieldSet unknownFields)
  157. {
  158. UnknownFields = UnknownFieldSet.CreateBuilder(UnknownFields)
  159. .MergeFrom(unknownFields)
  160. .Build();
  161. return ThisBuilder;
  162. }
  163. public virtual IBuilder SetField(FieldDescriptor field, object value)
  164. {
  165. this[field] = value;
  166. return ThisBuilder;
  167. }
  168. public virtual IBuilder SetRepeatedField(FieldDescriptor field, int index, object value)
  169. {
  170. this[field, index] = value;
  171. return ThisBuilder;
  172. }
  173. #region Explicit Implementations
  174. IMessage IBuilder.WeakBuild()
  175. {
  176. return Build();
  177. }
  178. IBuilder IBuilder.WeakAddRepeatedField(FieldDescriptor field, object value)
  179. {
  180. return AddRepeatedField(field, value);
  181. }
  182. IBuilder IBuilder.WeakClear()
  183. {
  184. return Clear();
  185. }
  186. IBuilder IBuilder.WeakMergeFrom(IMessage message)
  187. {
  188. return MergeFrom(message);
  189. }
  190. IBuilder IBuilder.WeakMergeFrom(ICodedInputStream input)
  191. {
  192. return MergeFrom(input);
  193. }
  194. IBuilder IBuilder.WeakMergeFrom(ICodedInputStream input, ExtensionRegistry registry)
  195. {
  196. return MergeFrom(input, registry);
  197. }
  198. IBuilder IBuilder.WeakMergeFrom(ByteString data)
  199. {
  200. return MergeFrom(data);
  201. }
  202. IBuilder IBuilder.WeakMergeFrom(ByteString data, ExtensionRegistry registry)
  203. {
  204. return MergeFrom(data, registry);
  205. }
  206. IMessage IBuilder.WeakBuildPartial()
  207. {
  208. return BuildPartial();
  209. }
  210. IBuilder IBuilder.WeakClone()
  211. {
  212. return Clone();
  213. }
  214. IMessage IBuilder.WeakDefaultInstanceForType
  215. {
  216. get { return DefaultInstanceForType; }
  217. }
  218. IBuilder IBuilder.WeakClearField(FieldDescriptor field)
  219. {
  220. return ClearField(field);
  221. }
  222. IBuilder IBuilder.WeakClearOneof(OneofDescriptor oneof)
  223. {
  224. return ClearOneof(oneof);
  225. }
  226. #endregion
  227. /// <summary>
  228. /// Converts this builder to a string using <see cref="TextFormat" />.
  229. /// </summary>
  230. /// <remarks>
  231. /// This method is not sealed (in the way that it is in <see cref="AbstractMessage{TMessage, TBuilder}" />
  232. /// as it was added after earlier releases; some other implementations may already be overriding the
  233. /// method.
  234. /// </remarks>
  235. public override string ToString()
  236. {
  237. return TextFormat.PrintToString(this);
  238. }
  239. }
  240. }