/** * Verifies that a ColumnValue has only a scalar or array value, not both and not neither. * * @param column The protobuf ColumnValue object * @throws IllegalArgumentException When the above condition is not met */ static void validateColumnValue(Common.ColumnValue column) { final boolean hasScalar = ProtobufService.hasField(column, column.getDescriptorForType(), Common.ColumnValue.SCALAR_VALUE_FIELD_NUMBER); final boolean hasArrayValue = column.getHasArrayValue(); // These should always be different if (hasScalar == hasArrayValue) { throw new IllegalArgumentException("A column must have a scalar or array value, not " + (hasScalar ? "both" : "neither")); } }
/** * Parses the value for a ColumnValue using the separated array and scalar attributes. * * @param column The protobuf ColumnValue object * @return The parse value for this column */ static Object parseColumn(Common.ColumnValue column) { // Verify that we have one or the other (scalar or array) validateColumnValue(column); if (!ProtobufService.hasField(column, column.getDescriptorForType(), Common.ColumnValue.SCALAR_VALUE_FIELD_NUMBER)) { // Array List<Object> array = new ArrayList<>(column.getArrayValueCount()); for (Common.TypedValue arrayValue : column.getArrayValueList()) { array.add(getScalarValue(arrayValue)); } return array; } else { // Scalar return getScalarValue(column.getScalarValue()); } }
Common.ColumnValue column = iter.next(); assertTrue("The Column should have contained a scalar: " + column, ProtobufService.hasField(column, column.getDescriptorForType(), ColumnValue.SCALAR_VALUE_FIELD_NUMBER)); column = iter.next(); assertTrue("The Column should have contained a scalar: " + column, ProtobufService.hasField(column, column.getDescriptorForType(), ColumnValue.SCALAR_VALUE_FIELD_NUMBER)); value = column.getScalarValue();
/** * Determines whether this message contains the new attributes in the * message. We can't directly test for the negative because our * {@code hasField} trick does not work on repeated fields. * * @param column The protobuf column object * @return True if the message is the new style, false otherwise. */ static boolean isNewStyleColumn(Common.ColumnValue column) { final Descriptor desc = column.getDescriptorForType(); return ProtobufService.hasField(column, desc, Common.ColumnValue.HAS_ARRAY_VALUE_FIELD_NUMBER) || ProtobufService.hasField(column, desc, Common.ColumnValue.SCALAR_VALUE_FIELD_NUMBER); }