UnknownFieldSetLite.java
Go to the documentation of this file.
1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2008 Google Inc. All rights reserved.
3 // https://developers.google.com/protocol-buffers/
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are
7 // met:
8 //
9 // * Redistributions of source code must retain the above copyright
10 // notice, this list of conditions and the following disclaimer.
11 // * Redistributions in binary form must reproduce the above
12 // copyright notice, this list of conditions and the following disclaimer
13 // in the documentation and/or other materials provided with the
14 // distribution.
15 // * Neither the name of Google Inc. nor the names of its
16 // contributors may be used to endorse or promote products derived from
17 // this software without specific prior written permission.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 
31 package com.google.protobuf;
32 
33 import java.io.IOException;
34 import java.util.Arrays;
35 
46 public final class UnknownFieldSetLite {
47 
48  // Arbitrarily chosen.
49  // TODO(dweis): Tune this number?
50  private static final int MIN_CAPACITY = 8;
51 
52  private static final UnknownFieldSetLite DEFAULT_INSTANCE =
53  new UnknownFieldSetLite(0, new int[0], new Object[0], /* isMutable= */ false);
54 
61  return DEFAULT_INSTANCE;
62  }
63 
65  static UnknownFieldSetLite newInstance() {
66  return new UnknownFieldSetLite();
67  }
68 
74  int count = first.count + second.count;
75  int[] tags = Arrays.copyOf(first.tags, count);
76  System.arraycopy(second.tags, 0, tags, first.count, second.count);
77  Object[] objects = Arrays.copyOf(first.objects, count);
78  System.arraycopy(second.objects, 0, objects, first.count, second.count);
79  return new UnknownFieldSetLite(count, tags, objects, /* isMutable= */ true);
80  }
81 
83  private int count;
84 
86  private int[] tags;
87 
89  private Object[] objects;
90 
92  private int memoizedSerializedSize = -1;
93 
95  private boolean isMutable;
96 
98  private UnknownFieldSetLite() {
99  this(0, new int[MIN_CAPACITY], new Object[MIN_CAPACITY], /* isMutable= */ true);
100  }
101 
103  private UnknownFieldSetLite(int count, int[] tags, Object[] objects, boolean isMutable) {
104  this.count = count;
105  this.tags = tags;
106  this.objects = objects;
107  this.isMutable = isMutable;
108  }
109 
115  public void makeImmutable() {
116  this.isMutable = false;
117  }
118 
120  void checkMutable() {
121  if (!isMutable) {
122  throw new UnsupportedOperationException();
123  }
124  }
125 
131  public void writeTo(CodedOutputStream output) throws IOException {
132  for (int i = 0; i < count; i++) {
133  int tag = tags[i];
134  int fieldNumber = WireFormat.getTagFieldNumber(tag);
135  switch (WireFormat.getTagWireType(tag)) {
137  output.writeUInt64(fieldNumber, (Long) objects[i]);
138  break;
140  output.writeFixed32(fieldNumber, (Integer) objects[i]);
141  break;
143  output.writeFixed64(fieldNumber, (Long) objects[i]);
144  break;
146  output.writeBytes(fieldNumber, (ByteString) objects[i]);
147  break;
149  output.writeTag(fieldNumber, WireFormat.WIRETYPE_START_GROUP);
150  ((UnknownFieldSetLite) objects[i]).writeTo(output);
151  output.writeTag(fieldNumber, WireFormat.WIRETYPE_END_GROUP);
152  break;
153  default:
154  throw InvalidProtocolBufferException.invalidWireType();
155  }
156  }
157  }
158 
164  public void writeAsMessageSetTo(CodedOutputStream output) throws IOException {
165  for (int i = 0; i < count; i++) {
166  int fieldNumber = WireFormat.getTagFieldNumber(tags[i]);
167  output.writeRawMessageSetExtension(fieldNumber, (ByteString) objects[i]);
168  }
169  }
170 
172  void writeAsMessageSetTo(Writer writer) throws IOException {
173  if (writer.fieldOrder() == Writer.FieldOrder.DESCENDING) {
174  // Write fields in descending order.
175  for (int i = count - 1; i >= 0; i--) {
176  int fieldNumber = WireFormat.getTagFieldNumber(tags[i]);
177  writer.writeMessageSetItem(fieldNumber, objects[i]);
178  }
179  } else {
180  // Write fields in ascending order.
181  for (int i = 0; i < count; i++) {
182  int fieldNumber = WireFormat.getTagFieldNumber(tags[i]);
183  writer.writeMessageSetItem(fieldNumber, objects[i]);
184  }
185  }
186  }
187 
189  public void writeTo(Writer writer) throws IOException {
190  if (count == 0) {
191  return;
192  }
193 
194  // TODO: tags are not sorted, so there's no write order guarantees
195  if (writer.fieldOrder() == Writer.FieldOrder.ASCENDING) {
196  for (int i = 0; i < count; ++i) {
197  writeField(tags[i], objects[i], writer);
198  }
199  } else {
200  for (int i = count - 1; i >= 0; --i) {
201  writeField(tags[i], objects[i], writer);
202  }
203  }
204  }
205 
206  private static void writeField(int tag, Object object, Writer writer) throws IOException {
207  int fieldNumber = WireFormat.getTagFieldNumber(tag);
208  switch (WireFormat.getTagWireType(tag)) {
210  writer.writeInt64(fieldNumber, (Long) object);
211  break;
213  writer.writeFixed32(fieldNumber, (Integer) object);
214  break;
216  writer.writeFixed64(fieldNumber, (Long) object);
217  break;
219  writer.writeBytes(fieldNumber, (ByteString) object);
220  break;
222  if (writer.fieldOrder() == Writer.FieldOrder.ASCENDING) {
223  writer.writeStartGroup(fieldNumber);
224  ((UnknownFieldSetLite) object).writeTo(writer);
225  writer.writeEndGroup(fieldNumber);
226  } else {
227  writer.writeEndGroup(fieldNumber);
228  ((UnknownFieldSetLite) object).writeTo(writer);
229  writer.writeStartGroup(fieldNumber);
230  }
231  break;
232  default:
233  // TODO(liujisi): Change writeTo to throw IOException?
234  throw new RuntimeException(InvalidProtocolBufferException.invalidWireType());
235  }
236  }
237 
244  if (size != -1) {
245  return size;
246  }
247 
248  size = 0;
249  for (int i = 0; i < count; i++) {
250  int tag = tags[i];
251  int fieldNumber = WireFormat.getTagFieldNumber(tag);
252  size +=
254  }
255 
257 
258  return size;
259  }
260 
266  public int getSerializedSize() {
268  if (size != -1) {
269  return size;
270  }
271 
272  size = 0;
273  for (int i = 0; i < count; i++) {
274  int tag = tags[i];
275  int fieldNumber = WireFormat.getTagFieldNumber(tag);
276  switch (WireFormat.getTagWireType(tag)) {
278  size += CodedOutputStream.computeUInt64Size(fieldNumber, (Long) objects[i]);
279  break;
281  size += CodedOutputStream.computeFixed32Size(fieldNumber, (Integer) objects[i]);
282  break;
284  size += CodedOutputStream.computeFixed64Size(fieldNumber, (Long) objects[i]);
285  break;
288  break;
290  size +=
291  CodedOutputStream.computeTagSize(fieldNumber) * 2
292  + ((UnknownFieldSetLite) objects[i]).getSerializedSize();
293  break;
294  default:
295  throw new IllegalStateException(InvalidProtocolBufferException.invalidWireType());
296  }
297  }
298 
300 
301  return size;
302  }
303 
304  private static boolean equals(int[] tags1, int[] tags2, int count) {
305  for (int i = 0; i < count; ++i) {
306  if (tags1[i] != tags2[i]) {
307  return false;
308  }
309  }
310  return true;
311  }
312 
313  private static boolean equals(Object[] objects1, Object[] objects2, int count) {
314  for (int i = 0; i < count; ++i) {
315  if (!objects1[i].equals(objects2[i])) {
316  return false;
317  }
318  }
319  return true;
320  }
321 
322  @Override
323  public boolean equals(Object obj) {
324  if (this == obj) {
325  return true;
326  }
327 
328  if (obj == null) {
329  return false;
330  }
331 
332  if (!(obj instanceof UnknownFieldSetLite)) {
333  return false;
334  }
335 
337  if (count != other.count
338  || !equals(tags, other.tags, count)
339  || !equals(objects, other.objects, count)) {
340  return false;
341  }
342 
343  return true;
344  }
345 
346  private static int hashCode(int[] tags, int count) {
347  int hashCode = 17;
348  for (int i = 0; i < count; ++i) {
349  hashCode = 31 * hashCode + tags[i];
350  }
351  return hashCode;
352  }
353 
354  private static int hashCode(Object[] objects, int count) {
355  int hashCode = 17;
356  for (int i = 0; i < count; ++i) {
357  hashCode = 31 * hashCode + objects[i].hashCode();
358  }
359  return hashCode;
360  }
361 
362  @Override
363  public int hashCode() {
364  int hashCode = 17;
365 
366  hashCode = 31 * hashCode + count;
367  hashCode = 31 * hashCode + hashCode(tags, count);
369 
370  return hashCode;
371  }
372 
381  final void printWithIndent(StringBuilder buffer, int indent) {
382  for (int i = 0; i < count; i++) {
383  int fieldNumber = WireFormat.getTagFieldNumber(tags[i]);
384  MessageLiteToString.printField(buffer, indent, String.valueOf(fieldNumber), objects[i]);
385  }
386  }
387 
388  // Package private for unsafe experimental runtime.
389  void storeField(int tag, Object value) {
390  checkMutable();
391  ensureCapacity();
392 
393  tags[count] = tag;
394  objects[count] = value;
395  count++;
396  }
397 
399  private void ensureCapacity() {
400  if (count == tags.length) {
401  int increment = count < (MIN_CAPACITY / 2) ? MIN_CAPACITY : count >> 1;
402  int newLength = count + increment;
403 
404  tags = Arrays.copyOf(tags, newLength);
405  objects = Arrays.copyOf(objects, newLength);
406  }
407  }
408 
417  boolean mergeFieldFrom(final int tag, final CodedInputStream input) throws IOException {
418  checkMutable();
419  final int fieldNumber = WireFormat.getTagFieldNumber(tag);
420  switch (WireFormat.getTagWireType(tag)) {
422  storeField(tag, input.readInt64());
423  return true;
425  storeField(tag, input.readFixed32());
426  return true;
428  storeField(tag, input.readFixed64());
429  return true;
431  storeField(tag, input.readBytes());
432  return true;
434  final UnknownFieldSetLite subFieldSet = new UnknownFieldSetLite();
435  subFieldSet.mergeFrom(input);
436  input.checkLastTagWas(WireFormat.makeTag(fieldNumber, WireFormat.WIRETYPE_END_GROUP));
437  storeField(tag, subFieldSet);
438  return true;
440  return false;
441  default:
442  throw InvalidProtocolBufferException.invalidWireType();
443  }
444  }
445 
452  UnknownFieldSetLite mergeVarintField(int fieldNumber, int value) {
453  checkMutable();
454  if (fieldNumber == 0) {
455  throw new IllegalArgumentException("Zero is not a valid field number.");
456  }
457 
458  storeField(WireFormat.makeTag(fieldNumber, WireFormat.WIRETYPE_VARINT), (long) value);
459 
460  return this;
461  }
462 
468  UnknownFieldSetLite mergeLengthDelimitedField(final int fieldNumber, final ByteString value) {
469  checkMutable();
470  if (fieldNumber == 0) {
471  throw new IllegalArgumentException("Zero is not a valid field number.");
472  }
473 
474  storeField(WireFormat.makeTag(fieldNumber, WireFormat.WIRETYPE_LENGTH_DELIMITED), value);
475 
476  return this;
477  }
478 
480  private UnknownFieldSetLite mergeFrom(final CodedInputStream input) throws IOException {
481  // Ensures initialization in mergeFieldFrom.
482  while (true) {
483  final int tag = input.readTag();
484  if (tag == 0 || !mergeFieldFrom(tag, input)) {
485  break;
486  }
487  }
488  return this;
489  }
490 }
com.google.protobuf.WireFormat.WIRETYPE_VARINT
static final int WIRETYPE_VARINT
Definition: WireFormat.java:55
com.google.protobuf.UnknownFieldSetLite.isMutable
boolean isMutable
Definition: UnknownFieldSetLite.java:95
com.google.protobuf.UnknownFieldSetLite.MIN_CAPACITY
static final int MIN_CAPACITY
Definition: UnknownFieldSetLite.java:50
com.google.protobuf.UnknownFieldSetLite.equals
static boolean equals(Object[] objects1, Object[] objects2, int count)
Definition: UnknownFieldSetLite.java:313
com.google.protobuf.UnknownFieldSetLite.hashCode
int hashCode()
Definition: UnknownFieldSetLite.java:363
com.google.protobuf.CodedOutputStream.computeUInt64Size
static int computeUInt64Size(final int fieldNumber, final long value)
Definition: CodedOutputStream.java:556
com.google.protobuf.CodedOutputStream.computeFixed64Size
static int computeFixed64Size(final int fieldNumber, final long value)
Definition: CodedOutputStream.java:572
com.google.protobuf.UnknownFieldSetLite.UnknownFieldSetLite
UnknownFieldSetLite()
Definition: UnknownFieldSetLite.java:98
com.google.protobuf.CodedOutputStream.computeRawMessageSetExtensionSize
static int computeRawMessageSetExtensionSize(final int fieldNumber, final ByteString value)
Definition: CodedOutputStream.java:687
indent
static int indent(upb_textprinter *p)
Definition: php/ext/google/protobuf/upb.c:8400
input
std::string input
Definition: tokenizer_unittest.cc:197
com.google.protobuf.UnknownFieldSetLite.ensureCapacity
void ensureCapacity()
Definition: UnknownFieldSetLite.java:399
com.google.protobuf.UnknownFieldSetLite.makeImmutable
void makeImmutable()
Definition: UnknownFieldSetLite.java:115
com.google.protobuf.UnknownFieldSetLite
Definition: UnknownFieldSetLite.java:46
com.google.protobuf.WireFormat.WIRETYPE_FIXED64
static final int WIRETYPE_FIXED64
Definition: WireFormat.java:56
com.google.protobuf.UnknownFieldSetLite.hashCode
static int hashCode(int[] tags, int count)
Definition: UnknownFieldSetLite.java:346
com.google.protobuf.WireFormat
Definition: WireFormat.java:45
com.google.protobuf.UnknownFieldSetLite.writeTo
void writeTo(Writer writer)
Definition: UnknownFieldSetLite.java:189
com.google.protobuf.UnknownFieldSetLite.equals
static boolean equals(int[] tags1, int[] tags2, int count)
Definition: UnknownFieldSetLite.java:304
obj
GLsizei GLsizei GLuint * obj
Definition: glcorearb.h:3066
com.google.protobuf.CodedInputStream
Definition: CodedInputStream.java:61
com.google.protobuf.CodedOutputStream.computeFixed32Size
static int computeFixed32Size(final int fieldNumber, final int value)
Definition: CodedOutputStream.java:532
com.google.protobuf.UnknownFieldSetLite.getSerializedSizeAsMessageSet
int getSerializedSizeAsMessageSet()
Definition: UnknownFieldSetLite.java:242
com.google.protobuf.UnknownFieldSetLite.count
int count
Definition: UnknownFieldSetLite.java:83
com.google.protobuf.UnknownFieldSetLite.getDefaultInstance
static UnknownFieldSetLite getDefaultInstance()
Definition: UnknownFieldSetLite.java:60
com.google.protobuf.UnknownFieldSetLite.writeTo
void writeTo(CodedOutputStream output)
Definition: UnknownFieldSetLite.java:131
com.google.protobuf.CodedOutputStream.computeTagSize
static int computeTagSize(final int fieldNumber)
Definition: CodedOutputStream.java:709
size
#define size
Definition: glcorearb.h:2944
com.google.protobuf.UnknownFieldSetLite.writeAsMessageSetTo
void writeAsMessageSetTo(CodedOutputStream output)
Definition: UnknownFieldSetLite.java:164
buffer
Definition: buffer_processor.h:43
com.google.protobuf.WireFormat.WIRETYPE_END_GROUP
static final int WIRETYPE_END_GROUP
Definition: WireFormat.java:59
com.google.protobuf.UnknownFieldSetLite.mergeFrom
UnknownFieldSetLite mergeFrom(final CodedInputStream input)
Definition: UnknownFieldSetLite.java:480
com.google.protobuf.UnknownFieldSetLite.equals
boolean equals(Object obj)
Definition: UnknownFieldSetLite.java:323
com.google.protobuf.UnknownFieldSetLite.UnknownFieldSetLite
UnknownFieldSetLite(int count, int[] tags, Object[] objects, boolean isMutable)
Definition: UnknownFieldSetLite.java:103
com.google.protobuf.WireFormat.WIRETYPE_FIXED32
static final int WIRETYPE_FIXED32
Definition: WireFormat.java:60
com.google.protobuf.WireFormat.WIRETYPE_START_GROUP
static final int WIRETYPE_START_GROUP
Definition: WireFormat.java:58
i
int i
Definition: gmock-matchers_test.cc:764
java
com.google.protobuf.WireFormat.WIRETYPE_LENGTH_DELIMITED
static final int WIRETYPE_LENGTH_DELIMITED
Definition: WireFormat.java:57
com.google.protobuf.UnknownFieldSetLite.memoizedSerializedSize
int memoizedSerializedSize
Definition: UnknownFieldSetLite.java:92
com.google.protobuf.WireFormat.getTagWireType
static int getTagWireType(final int tag)
Definition: WireFormat.java:66
com.google.protobuf.UnknownFieldSetLite.objects
Object[] objects
Definition: UnknownFieldSetLite.java:89
com.google.protobuf.UnknownFieldSetLite.tags
int[] tags
Definition: UnknownFieldSetLite.java:86
com.google.protobuf.UnknownFieldSetLite.getSerializedSize
int getSerializedSize()
Definition: UnknownFieldSetLite.java:266
size
GLsizeiptr size
Definition: glcorearb.h:2943
com.google.protobuf.UnknownFieldSetLite.writeField
static void writeField(int tag, Object object, Writer writer)
Definition: UnknownFieldSetLite.java:206
first
GLint first
Definition: glcorearb.h:2830
com.google.protobuf.UnknownFieldSetLite.DEFAULT_INSTANCE
static final UnknownFieldSetLite DEFAULT_INSTANCE
Definition: UnknownFieldSetLite.java:52
com.google.protobuf.CodedOutputStream
Definition: CodedOutputStream.java:59
com.google.protobuf.WireFormat.getTagFieldNumber
static int getTagFieldNumber(final int tag)
Definition: WireFormat.java:71
com.google.protobuf.InvalidProtocolBufferException
Definition: InvalidProtocolBufferException.java:41
value
GLsizei const GLfloat * value
Definition: glcorearb.h:3093
output
const upb_json_parsermethod const upb_symtab upb_sink * output
Definition: ruby/ext/google/protobuf_c/upb.h:10503
count
GLint GLsizei count
Definition: glcorearb.h:2830
com.google.protobuf.CodedOutputStream.computeBytesSize
static int computeBytesSize(final int fieldNumber, final ByteString value)
Definition: CodedOutputStream.java:628
com.google.protobuf.UnknownFieldSetLite.hashCode
static int hashCode(Object[] objects, int count)
Definition: UnknownFieldSetLite.java:354
com.google.protobuf.ByteString
Definition: ByteString.java:67


libaditof
Author(s):
autogenerated on Wed May 21 2025 02:07:00