diff options
Diffstat (limited to 'java/core/src/main/java/com/google/protobuf/UnknownFieldSetLite.java')
-rw-r--r-- | java/core/src/main/java/com/google/protobuf/UnknownFieldSetLite.java | 139 |
1 files changed, 57 insertions, 82 deletions
diff --git a/java/core/src/main/java/com/google/protobuf/UnknownFieldSetLite.java b/java/core/src/main/java/com/google/protobuf/UnknownFieldSetLite.java index f0b919ad..7c5ec6e9 100644 --- a/java/core/src/main/java/com/google/protobuf/UnknownFieldSetLite.java +++ b/java/core/src/main/java/com/google/protobuf/UnknownFieldSetLite.java @@ -34,24 +34,23 @@ import java.io.IOException; import java.util.Arrays; /** - * {@code UnknownFieldSetLite} is used to keep track of fields which were seen - * when parsing a protocol message but whose field numbers or types are - * unrecognized. This most frequently occurs when new fields are added to a - * message type and then messages containing those fields are read by old - * software that was compiled before the new types were added. + * {@code UnknownFieldSetLite} is used to keep track of fields which were seen when parsing a + * protocol message but whose field numbers or types are unrecognized. This most frequently occurs + * when new fields are added to a message type and then messages containing those fields are read by + * old software that was compiled before the new types were added. * * <p>For use by generated code only. * * @author dweis@google.com (Daniel Weis) */ public final class UnknownFieldSetLite { - + // Arbitrarily chosen. // TODO(dweis): Tune this number? private static final int MIN_CAPACITY = 8; private static final UnknownFieldSetLite DEFAULT_INSTANCE = - new UnknownFieldSetLite(0, new int[0], new Object[0], false /* isMutable */); + new UnknownFieldSetLite(0, new int[0], new Object[0], /* isMutable= */ false); /** * Get an empty {@code UnknownFieldSetLite}. @@ -61,17 +60,15 @@ public final class UnknownFieldSetLite { public static UnknownFieldSetLite getDefaultInstance() { return DEFAULT_INSTANCE; } - - /** - * Returns a new mutable instance. - */ + + /** Returns a new mutable instance. */ static UnknownFieldSetLite newInstance() { return new UnknownFieldSetLite(); } /** - * Returns a mutable {@code UnknownFieldSetLite} that is the composite of {@code first} and - * {@code second}. + * Returns a mutable {@code UnknownFieldSetLite} that is the composite of {@code first} and {@code + * second}. */ static UnknownFieldSetLite mutableCopyOf(UnknownFieldSetLite first, UnknownFieldSetLite second) { int count = first.count + second.count; @@ -79,66 +76,50 @@ public final class UnknownFieldSetLite { System.arraycopy(second.tags, 0, tags, first.count, second.count); Object[] objects = Arrays.copyOf(first.objects, count); System.arraycopy(second.objects, 0, objects, first.count, second.count); - return new UnknownFieldSetLite(count, tags, objects, true /* isMutable */); + return new UnknownFieldSetLite(count, tags, objects, /* isMutable= */ true); } - /** - * The number of elements in the set. - */ + /** The number of elements in the set. */ private int count; - - /** - * The tag numbers for the elements in the set. - */ + + /** The tag numbers for the elements in the set. */ private int[] tags; - - /** - * The boxed values of the elements in the set. - */ + + /** The boxed values of the elements in the set. */ private Object[] objects; - - /** - * The lazily computed serialized size of the set. - */ + + /** The lazily computed serialized size of the set. */ private int memoizedSerializedSize = -1; - - /** - * Indicates that this object is mutable. - */ + + /** Indicates that this object is mutable. */ private boolean isMutable; - /** - * Constructs a mutable {@code UnknownFieldSetLite}. - */ + /** Constructs a mutable {@code UnknownFieldSetLite}. */ private UnknownFieldSetLite() { - this(0, new int[MIN_CAPACITY], new Object[MIN_CAPACITY], true /* isMutable */); + this(0, new int[MIN_CAPACITY], new Object[MIN_CAPACITY], /* isMutable= */ true); } - - /** - * Constructs the {@code UnknownFieldSetLite}. - */ + + /** Constructs the {@code UnknownFieldSetLite}. */ private UnknownFieldSetLite(int count, int[] tags, Object[] objects, boolean isMutable) { this.count = count; this.tags = tags; this.objects = objects; this.isMutable = isMutable; } - + /** * Marks this object as immutable. - * + * * <p>Future calls to methods that attempt to modify this object will throw. */ public void makeImmutable() { this.isMutable = false; } - - /** - * Throws an {@link UnsupportedOperationException} if immutable. - */ + + /** Throws an {@link UnsupportedOperationException} if immutable. */ void checkMutable() { if (!isMutable) { - throw new UnsupportedOperationException(); + throw new UnsupportedOperationException(); } } @@ -197,17 +178,17 @@ public final class UnknownFieldSetLite { if (size != -1) { return size; } - + size = 0; for (int i = 0; i < count; i++) { int tag = tags[i]; int fieldNumber = WireFormat.getTagFieldNumber(tag); - size += CodedOutputStream.computeRawMessageSetExtensionSize( - fieldNumber, (ByteString) objects[i]); + size += + CodedOutputStream.computeRawMessageSetExtensionSize(fieldNumber, (ByteString) objects[i]); } - + memoizedSerializedSize = size; - + return size; } @@ -221,7 +202,7 @@ public final class UnknownFieldSetLite { if (size != -1) { return size; } - + size = 0; for (int i = 0; i < count; i++) { int tag = tags[i]; @@ -240,19 +221,20 @@ public final class UnknownFieldSetLite { size += CodedOutputStream.computeBytesSize(fieldNumber, (ByteString) objects[i]); break; case WireFormat.WIRETYPE_START_GROUP: - size += CodedOutputStream.computeTagSize(fieldNumber) * 2 - + ((UnknownFieldSetLite) objects[i]).getSerializedSize(); + size += + CodedOutputStream.computeTagSize(fieldNumber) * 2 + + ((UnknownFieldSetLite) objects[i]).getSerializedSize(); break; default: throw new IllegalStateException(InvalidProtocolBufferException.invalidWireType()); } } - + memoizedSerializedSize = size; - + return size; } - + private static boolean equals(int[] tags1, int[] tags2, int count) { for (int i = 0; i < count; ++i) { if (tags1[i] != tags2[i]) { @@ -284,8 +266,8 @@ public final class UnknownFieldSetLite { if (!(obj instanceof UnknownFieldSetLite)) { return false; } - - UnknownFieldSetLite other = (UnknownFieldSetLite) obj; + + UnknownFieldSetLite other = (UnknownFieldSetLite) obj; if (count != other.count || !equals(tags, other.tags, count) || !equals(objects, other.objects, count)) { @@ -341,25 +323,23 @@ public final class UnknownFieldSetLite { void storeField(int tag, Object value) { checkMutable(); ensureCapacity(); - + tags[count] = tag; objects[count] = value; count++; } - - /** - * Ensures that our arrays are long enough to store more metadata. - */ + + /** Ensures that our arrays are long enough to store more metadata. */ private void ensureCapacity() { - if (count == tags.length) { + if (count == tags.length) { int increment = count < (MIN_CAPACITY / 2) ? MIN_CAPACITY : count >> 1; int newLength = count + increment; - + tags = Arrays.copyOf(tags, newLength); objects = Arrays.copyOf(objects, newLength); } } - + /** * Parse a single field from {@code input} and merge it into this set. * @@ -387,8 +367,7 @@ public final class UnknownFieldSetLite { case WireFormat.WIRETYPE_START_GROUP: final UnknownFieldSetLite subFieldSet = new UnknownFieldSetLite(); subFieldSet.mergeFrom(input); - input.checkLastTagWas( - WireFormat.makeTag(fieldNumber, WireFormat.WIRETYPE_END_GROUP)); + input.checkLastTagWas(WireFormat.makeTag(fieldNumber, WireFormat.WIRETYPE_END_GROUP)); storeField(tag, subFieldSet); return true; case WireFormat.WIRETYPE_END_GROUP: @@ -399,9 +378,8 @@ public final class UnknownFieldSetLite { } /** - * Convenience method for merging a new field containing a single varint - * value. This is used in particular when an unknown enum value is - * encountered. + * Convenience method for merging a new field containing a single varint value. This is used in + * particular when an unknown enum value is encountered. * * <p>For use by generated code only. */ @@ -412,7 +390,7 @@ public final class UnknownFieldSetLite { } storeField(WireFormat.makeTag(fieldNumber, WireFormat.WIRETYPE_VARINT), (long) value); - + return this; } @@ -421,21 +399,18 @@ public final class UnknownFieldSetLite { * * <p>For use by generated code only. */ - UnknownFieldSetLite mergeLengthDelimitedField(final int fieldNumber, final ByteString value) { + UnknownFieldSetLite mergeLengthDelimitedField(final int fieldNumber, final ByteString value) { checkMutable(); if (fieldNumber == 0) { throw new IllegalArgumentException("Zero is not a valid field number."); } storeField(WireFormat.makeTag(fieldNumber, WireFormat.WIRETYPE_LENGTH_DELIMITED), value); - + return this; } - - /** - * Parse an entire message from {@code input} and merge its fields into - * this set. - */ + + /** Parse an entire message from {@code input} and merge its fields into this set. */ private UnknownFieldSetLite mergeFrom(final CodedInputStream input) throws IOException { // Ensures initialization in mergeFieldFrom. while (true) { |