OLD | NEW |
1 // Protocol Buffers - Google's data interchange format | 1 // Protocol Buffers - Google's data interchange format |
2 // Copyright 2008 Google Inc. All rights reserved. | 2 // Copyright 2008 Google Inc. All rights reserved. |
3 // https://developers.google.com/protocol-buffers/ | 3 // https://developers.google.com/protocol-buffers/ |
4 // | 4 // |
5 // Redistribution and use in source and binary forms, with or without | 5 // Redistribution and use in source and binary forms, with or without |
6 // modification, are permitted provided that the following conditions are | 6 // modification, are permitted provided that the following conditions are |
7 // met: | 7 // met: |
8 // | 8 // |
9 // * Redistributions of source code must retain the above copyright | 9 // * Redistributions of source code must retain the above copyright |
10 // notice, this list of conditions and the following disclaimer. | 10 // notice, this list of conditions and the following disclaimer. |
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
113 | 113 |
114 /** | 114 /** |
115 * Returns whether the FieldSet is immutable. This is true if it is the | 115 * Returns whether the FieldSet is immutable. This is true if it is the |
116 * {@link #emptySet} or if {@link #makeImmutable} were called. | 116 * {@link #emptySet} or if {@link #makeImmutable} were called. |
117 * | 117 * |
118 * @return whether the FieldSet is immutable. | 118 * @return whether the FieldSet is immutable. |
119 */ | 119 */ |
120 public boolean isImmutable() { | 120 public boolean isImmutable() { |
121 return isImmutable; | 121 return isImmutable; |
122 } | 122 } |
123 | 123 |
124 @Override | 124 @Override |
125 public boolean equals(Object o) { | 125 public boolean equals(Object o) { |
126 if (this == o) { | 126 if (this == o) { |
127 return true; | 127 return true; |
128 } | 128 } |
129 | 129 |
130 if (!(o instanceof FieldSet)) { | 130 if (!(o instanceof FieldSet)) { |
131 return false; | 131 return false; |
132 } | 132 } |
133 | 133 |
134 FieldSet<?> other = (FieldSet<?>) o; | 134 FieldSet<?> other = (FieldSet<?>) o; |
135 return other.fields.equals(other.fields); | 135 return fields.equals(other.fields); |
136 } | 136 } |
137 | 137 |
138 @Override | 138 @Override |
139 public int hashCode() { | 139 public int hashCode() { |
140 return fields.hashCode(); | 140 return fields.hashCode(); |
141 } | 141 } |
142 | 142 |
143 /** | 143 /** |
144 * Clones the FieldSet. The returned FieldSet will be mutable even if the | 144 * Clones the FieldSet. The returned FieldSet will be mutable even if the |
145 * original FieldSet was immutable. | 145 * original FieldSet was immutable. |
146 * | 146 * |
147 * @return the newly cloned FieldSet | 147 * @return the newly cloned FieldSet |
(...skipping 338 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
486 static int getWireFormatForFieldType(final WireFormat.FieldType type, | 486 static int getWireFormatForFieldType(final WireFormat.FieldType type, |
487 boolean isPacked) { | 487 boolean isPacked) { |
488 if (isPacked) { | 488 if (isPacked) { |
489 return WireFormat.WIRETYPE_LENGTH_DELIMITED; | 489 return WireFormat.WIRETYPE_LENGTH_DELIMITED; |
490 } else { | 490 } else { |
491 return type.getWireType(); | 491 return type.getWireType(); |
492 } | 492 } |
493 } | 493 } |
494 | 494 |
495 /** | 495 /** |
496 * Like {@link Message.Builder#mergeFrom(Message)}, but merges from another | 496 * Like {@link Message.Builder#mergeFrom(Message)}, but merges from another |
497 * {@link FieldSet}. | 497 * {@link FieldSet}. |
498 */ | 498 */ |
499 public void mergeFrom(final FieldSet<FieldDescriptorType> other) { | 499 public void mergeFrom(final FieldSet<FieldDescriptorType> other) { |
500 for (int i = 0; i < other.fields.getNumArrayEntries(); i++) { | 500 for (int i = 0; i < other.fields.getNumArrayEntries(); i++) { |
501 mergeFromField(other.fields.getArrayEntryAt(i)); | 501 mergeFromField(other.fields.getArrayEntryAt(i)); |
502 } | 502 } |
503 for (final Map.Entry<FieldDescriptorType, Object> entry : | 503 for (final Map.Entry<FieldDescriptorType, Object> entry : |
504 other.fields.getOverflowEntries()) { | 504 other.fields.getOverflowEntries()) { |
505 mergeFromField(entry); | 505 mergeFromField(entry); |
506 } | 506 } |
(...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
631 * Write a single tag-value pair to the stream. | 631 * Write a single tag-value pair to the stream. |
632 * | 632 * |
633 * @param output The output stream. | 633 * @param output The output stream. |
634 * @param type The field's type. | 634 * @param type The field's type. |
635 * @param number The field's number. | 635 * @param number The field's number. |
636 * @param value Object representing the field's value. Must be of the exact | 636 * @param value Object representing the field's value. Must be of the exact |
637 * type which would be returned by | 637 * type which would be returned by |
638 * {@link Message#getField(Descriptors.FieldDescriptor)} for | 638 * {@link Message#getField(Descriptors.FieldDescriptor)} for |
639 * this field. | 639 * this field. |
640 */ | 640 */ |
641 private static void writeElement(final CodedOutputStream output, | 641 static void writeElement( |
642 final WireFormat.FieldType type, | 642 final CodedOutputStream output, |
643 final int number, | 643 final WireFormat.FieldType type, |
644 final Object value) throws IOException { | 644 final int number, |
| 645 final Object value) throws IOException { |
645 // Special case for groups, which need a start and end tag; other fields | 646 // Special case for groups, which need a start and end tag; other fields |
646 // can just use writeTag() and writeFieldNoTag(). | 647 // can just use writeTag() and writeFieldNoTag(). |
647 if (type == WireFormat.FieldType.GROUP) { | 648 if (type == WireFormat.FieldType.GROUP) { |
648 output.writeGroup(number, (MessageLite) value); | 649 output.writeGroup(number, (MessageLite) value); |
649 } else { | 650 } else { |
650 output.writeTag(number, getWireFormatForFieldType(type, false)); | 651 output.writeTag(number, getWireFormatForFieldType(type, false)); |
651 writeElementNoTag(output, type, value); | 652 writeElementNoTag(output, type, value); |
652 } | 653 } |
653 } | 654 } |
654 | 655 |
(...skipping 142 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
797 * Compute the number of bytes that would be needed to encode a | 798 * Compute the number of bytes that would be needed to encode a |
798 * single tag/value pair of arbitrary type. | 799 * single tag/value pair of arbitrary type. |
799 * | 800 * |
800 * @param type The field's type. | 801 * @param type The field's type. |
801 * @param number The field's number. | 802 * @param number The field's number. |
802 * @param value Object representing the field's value. Must be of the exact | 803 * @param value Object representing the field's value. Must be of the exact |
803 * type which would be returned by | 804 * type which would be returned by |
804 * {@link Message#getField(Descriptors.FieldDescriptor)} for | 805 * {@link Message#getField(Descriptors.FieldDescriptor)} for |
805 * this field. | 806 * this field. |
806 */ | 807 */ |
807 private static int computeElementSize( | 808 static int computeElementSize( |
808 final WireFormat.FieldType type, | 809 final WireFormat.FieldType type, final int number, final Object value) { |
809 final int number, final Object value) { | |
810 int tagSize = CodedOutputStream.computeTagSize(number); | 810 int tagSize = CodedOutputStream.computeTagSize(number); |
811 if (type == WireFormat.FieldType.GROUP) { | 811 if (type == WireFormat.FieldType.GROUP) { |
812 // Only count the end group tag for proto2 messages as for proto1 the end | 812 // Only count the end group tag for proto2 messages as for proto1 the end |
813 // group tag will be counted as a part of getSerializedSize(). | 813 // group tag will be counted as a part of getSerializedSize(). |
814 tagSize *= 2; | 814 tagSize *= 2; |
815 } | 815 } |
816 return tagSize + computeElementSizeNoTag(type, value); | 816 return tagSize + computeElementSizeNoTag(type, value); |
817 } | 817 } |
818 | 818 |
819 /** | 819 /** |
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
899 for (final Object element : (List<?>)value) { | 899 for (final Object element : (List<?>)value) { |
900 size += computeElementSize(type, number, element); | 900 size += computeElementSize(type, number, element); |
901 } | 901 } |
902 return size; | 902 return size; |
903 } | 903 } |
904 } else { | 904 } else { |
905 return computeElementSize(type, number, value); | 905 return computeElementSize(type, number, value); |
906 } | 906 } |
907 } | 907 } |
908 } | 908 } |
OLD | NEW |