예제 #1
0
 @Override
 public Builder clear() {
   if (fields.isImmutable()) {
     fields = FieldSet.newFieldSet();
   } else {
     fields.clear();
   }
   unknownFields = UnknownFieldSet.getDefaultInstance();
   return this;
 }
예제 #2
0
 /**
  * Clones the FieldSet. The returned FieldSet will be mutable even if the original FieldSet was
  * immutable.
  *
  * @return the newly cloned FieldSet
  */
 @Override
 public FieldSet<FieldDescriptorType> clone() {
   // We can't just call fields.clone because List objects in the map
   // should not be shared.
   FieldSet<FieldDescriptorType> clone = FieldSet.newFieldSet();
   for (int i = 0; i < fields.getNumArrayEntries(); i++) {
     Map.Entry<FieldDescriptorType, Object> entry = fields.getArrayEntryAt(i);
     FieldDescriptorType descriptor = entry.getKey();
     clone.setField(descriptor, entry.getValue());
   }
   for (Map.Entry<FieldDescriptorType, Object> entry : fields.getOverflowEntries()) {
     FieldDescriptorType descriptor = entry.getKey();
     clone.setField(descriptor, entry.getValue());
   }
   return clone;
 }
예제 #3
0
 /** Construct a {@code Builder} for the given type. */
 private Builder(Descriptor type) {
   this.type = type;
   this.fields = FieldSet.newFieldSet();
   this.unknownFields = UnknownFieldSet.getDefaultInstance();
   this.oneofCases = new FieldDescriptor[type.toProto().getOneofDeclCount()];
 }
예제 #4
0
 /** Construct a {@code Builder} for the given type. */
 private Builder(Descriptor type) {
   this.type = type;
   this.fields = FieldSet.newFieldSet();
   this.unknownFields = UnknownFieldSet.getDefaultInstance();
 }
예제 #5
0
  /** Lite equivalent of {@link GeneratedMessage.ExtendableMessage}. */
  public abstract static class ExtendableMessage<MessageType extends ExtendableMessage<MessageType>>
      extends GeneratedMessageLite {
    protected ExtendableMessage() {}

    private final FieldSet<ExtensionDescriptor> extensions = FieldSet.newFieldSet();

    private void verifyExtensionContainingType(final GeneratedExtension<MessageType, ?> extension) {
      if (extension.getContainingTypeDefaultInstance() != getDefaultInstanceForType()) {
        // This can only happen if someone uses unchecked operations.
        throw new IllegalArgumentException(
            "This extension is for a different message type.  Please make "
                + "sure that you are not suppressing any generics type warnings.");
      }
    }

    /** Check if a singular extension is present. */
    public final boolean hasExtension(final GeneratedExtension<MessageType, ?> extension) {
      verifyExtensionContainingType(extension);
      return extensions.hasField(extension.descriptor);
    }

    /** Get the number of elements in a repeated extension. */
    public final <Type> int getExtensionCount(
        final GeneratedExtension<MessageType, List<Type>> extension) {
      verifyExtensionContainingType(extension);
      return extensions.getRepeatedFieldCount(extension.descriptor);
    }

    /** Get the value of an extension. */
    @SuppressWarnings("unchecked")
    public final <Type> Type getExtension(final GeneratedExtension<MessageType, Type> extension) {
      verifyExtensionContainingType(extension);
      final Object value = extensions.getField(extension.descriptor);
      if (value == null) {
        return extension.defaultValue;
      } else {
        return (Type) value;
      }
    }

    /** Get one element of a repeated extension. */
    @SuppressWarnings("unchecked")
    public final <Type> Type getExtension(
        final GeneratedExtension<MessageType, List<Type>> extension, final int index) {
      verifyExtensionContainingType(extension);
      return (Type) extensions.getRepeatedField(extension.descriptor, index);
    }

    /** Called by subclasses to check if all extensions are initialized. */
    protected boolean extensionsAreInitialized() {
      return extensions.isInitialized();
    }

    /**
     * Used by subclasses to serialize extensions. Extension ranges may be interleaved with field
     * numbers, but we must write them in canonical (sorted by field number) order. ExtensionWriter
     * helps us write individual ranges of extensions at once.
     */
    protected class ExtensionWriter {
      // Imagine how much simpler this code would be if Java iterators had
      // a way to get the next element without advancing the iterator.

      private final Iterator<Map.Entry<ExtensionDescriptor, Object>> iter = extensions.iterator();
      private Map.Entry<ExtensionDescriptor, Object> next;
      private final boolean messageSetWireFormat;

      private ExtensionWriter(boolean messageSetWireFormat) {
        if (iter.hasNext()) {
          next = iter.next();
        }
        this.messageSetWireFormat = messageSetWireFormat;
      }

      public void writeUntil(final int end, final CodedOutputStream output) throws IOException {
        while (next != null && next.getKey().getNumber() < end) {
          ExtensionDescriptor extension = next.getKey();
          if (messageSetWireFormat
              && extension.getLiteJavaType() == WireFormat.JavaType.MESSAGE
              && !extension.isRepeated()) {
            output.writeMessageSetExtension(extension.getNumber(), (MessageLite) next.getValue());
          } else {
            FieldSet.writeField(extension, next.getValue(), output);
          }
          if (iter.hasNext()) {
            next = iter.next();
          } else {
            next = null;
          }
        }
      }
    }

    protected ExtensionWriter newExtensionWriter() {
      return new ExtensionWriter(false);
    }

    protected ExtensionWriter newMessageSetExtensionWriter() {
      return new ExtensionWriter(true);
    }

    /** Called by subclasses to compute the size of extensions. */
    protected int extensionsSerializedSize() {
      return extensions.getSerializedSize();
    }

    protected int extensionsSerializedSizeAsMessageSet() {
      return extensions.getMessageSetSerializedSize();
    }
  }