Search in sources :

Example 1 with Descriptor

use of org.apache.hadoop.hbase.shaded.com.google.protobuf.Descriptors.Descriptor in project hbase by apache.

the class GeneratedMessage method getAllFieldsMutable.

/**
   * Internal helper to return a modifiable map containing all the fields.
   * The returned Map is modifialbe so that the caller can add additional
   * extension fields to implement {@link #getAllFields()}.
   *
   * @param getBytesForString whether to generate ByteString for string fields
   */
private Map<FieldDescriptor, Object> getAllFieldsMutable(boolean getBytesForString) {
    final TreeMap<FieldDescriptor, Object> result = new TreeMap<FieldDescriptor, Object>();
    final Descriptor descriptor = internalGetFieldAccessorTable().descriptor;
    final List<FieldDescriptor> fields = descriptor.getFields();
    for (int i = 0; i < fields.size(); i++) {
        FieldDescriptor field = fields.get(i);
        final OneofDescriptor oneofDescriptor = field.getContainingOneof();
        /*
       * If the field is part of a Oneof, then at maximum one field in the Oneof is set
       * and it is not repeated. There is no need to iterate through the others.
       */
        if (oneofDescriptor != null) {
            // Skip other fields in the Oneof we know are not set
            i += oneofDescriptor.getFieldCount() - 1;
            if (!hasOneof(oneofDescriptor)) {
                // If no field is set in the Oneof, skip all the fields in the Oneof
                continue;
            }
            // Get the pointer to the only field which is set in the Oneof
            field = getOneofFieldDescriptor(oneofDescriptor);
        } else {
            // If we are not in a Oneof, we need to check if the field is set and if it is repeated
            if (field.isRepeated()) {
                final List<?> value = (List<?>) getField(field);
                if (!value.isEmpty()) {
                    result.put(field, value);
                }
                continue;
            }
            if (!hasField(field)) {
                continue;
            }
        }
        // Add the field to the map
        if (getBytesForString && field.getJavaType() == FieldDescriptor.JavaType.STRING) {
            result.put(field, getFieldRaw(field));
        } else {
            result.put(field, getField(field));
        }
    }
    return result;
}
Also used : OneofDescriptor(org.apache.hadoop.hbase.shaded.com.google.protobuf.Descriptors.OneofDescriptor) Descriptor(org.apache.hadoop.hbase.shaded.com.google.protobuf.Descriptors.Descriptor) FileDescriptor(org.apache.hadoop.hbase.shaded.com.google.protobuf.Descriptors.FileDescriptor) EnumValueDescriptor(org.apache.hadoop.hbase.shaded.com.google.protobuf.Descriptors.EnumValueDescriptor) EnumDescriptor(org.apache.hadoop.hbase.shaded.com.google.protobuf.Descriptors.EnumDescriptor) FieldDescriptor(org.apache.hadoop.hbase.shaded.com.google.protobuf.Descriptors.FieldDescriptor) ArrayList(java.util.ArrayList) List(java.util.List) TreeMap(java.util.TreeMap) OneofDescriptor(org.apache.hadoop.hbase.shaded.com.google.protobuf.Descriptors.OneofDescriptor) FieldDescriptor(org.apache.hadoop.hbase.shaded.com.google.protobuf.Descriptors.FieldDescriptor)

Example 2 with Descriptor

use of org.apache.hadoop.hbase.shaded.com.google.protobuf.Descriptors.Descriptor in project hbase by apache.

the class GeneratedMessageV3 method getAllFieldsMutable.

/**
   * Internal helper to return a modifiable map containing all the fields.
   * The returned Map is modifialbe so that the caller can add additional
   * extension fields to implement {@link #getAllFields()}.
   *
   * @param getBytesForString whether to generate ByteString for string fields
   */
private Map<FieldDescriptor, Object> getAllFieldsMutable(boolean getBytesForString) {
    final TreeMap<FieldDescriptor, Object> result = new TreeMap<FieldDescriptor, Object>();
    final Descriptor descriptor = internalGetFieldAccessorTable().descriptor;
    final List<FieldDescriptor> fields = descriptor.getFields();
    for (int i = 0; i < fields.size(); i++) {
        FieldDescriptor field = fields.get(i);
        final OneofDescriptor oneofDescriptor = field.getContainingOneof();
        /*
       * If the field is part of a Oneof, then at maximum one field in the Oneof is set
       * and it is not repeated. There is no need to iterate through the others.
       */
        if (oneofDescriptor != null) {
            // Skip other fields in the Oneof we know are not set
            i += oneofDescriptor.getFieldCount() - 1;
            if (!hasOneof(oneofDescriptor)) {
                // If no field is set in the Oneof, skip all the fields in the Oneof
                continue;
            }
            // Get the pointer to the only field which is set in the Oneof
            field = getOneofFieldDescriptor(oneofDescriptor);
        } else {
            // If we are not in a Oneof, we need to check if the field is set and if it is repeated
            if (field.isRepeated()) {
                final List<?> value = (List<?>) getField(field);
                if (!value.isEmpty()) {
                    result.put(field, value);
                }
                continue;
            }
            if (!hasField(field)) {
                continue;
            }
        }
        // Add the field to the map
        if (getBytesForString && field.getJavaType() == FieldDescriptor.JavaType.STRING) {
            result.put(field, getFieldRaw(field));
        } else {
            result.put(field, getField(field));
        }
    }
    return result;
}
Also used : OneofDescriptor(org.apache.hadoop.hbase.shaded.com.google.protobuf.Descriptors.OneofDescriptor) Descriptor(org.apache.hadoop.hbase.shaded.com.google.protobuf.Descriptors.Descriptor) FileDescriptor(org.apache.hadoop.hbase.shaded.com.google.protobuf.Descriptors.FileDescriptor) EnumValueDescriptor(org.apache.hadoop.hbase.shaded.com.google.protobuf.Descriptors.EnumValueDescriptor) EnumDescriptor(org.apache.hadoop.hbase.shaded.com.google.protobuf.Descriptors.EnumDescriptor) FieldDescriptor(org.apache.hadoop.hbase.shaded.com.google.protobuf.Descriptors.FieldDescriptor) ArrayList(java.util.ArrayList) List(java.util.List) TreeMap(java.util.TreeMap) OneofDescriptor(org.apache.hadoop.hbase.shaded.com.google.protobuf.Descriptors.OneofDescriptor) FieldDescriptor(org.apache.hadoop.hbase.shaded.com.google.protobuf.Descriptors.FieldDescriptor)

Aggregations

ArrayList (java.util.ArrayList)2 List (java.util.List)2 TreeMap (java.util.TreeMap)2 Descriptor (org.apache.hadoop.hbase.shaded.com.google.protobuf.Descriptors.Descriptor)2 EnumDescriptor (org.apache.hadoop.hbase.shaded.com.google.protobuf.Descriptors.EnumDescriptor)2 EnumValueDescriptor (org.apache.hadoop.hbase.shaded.com.google.protobuf.Descriptors.EnumValueDescriptor)2 FieldDescriptor (org.apache.hadoop.hbase.shaded.com.google.protobuf.Descriptors.FieldDescriptor)2 FileDescriptor (org.apache.hadoop.hbase.shaded.com.google.protobuf.Descriptors.FileDescriptor)2 OneofDescriptor (org.apache.hadoop.hbase.shaded.com.google.protobuf.Descriptors.OneofDescriptor)2