AbstractBuilder.cs 9.6 KB

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