writeToProtoWithType(builder, o, Common.Rep.BYTE); return Common.Rep.BYTE; } else if (o instanceof Short) { writeToProtoWithType(builder, o, Common.Rep.SHORT); return Common.Rep.SHORT; } else if (o instanceof Integer) { writeToProtoWithType(builder, o, Common.Rep.INTEGER); return Common.Rep.INTEGER; } else if (o instanceof Long) { writeToProtoWithType(builder, o, Common.Rep.LONG); return Common.Rep.LONG; } else if (o instanceof Double) { writeToProtoWithType(builder, o, Common.Rep.DOUBLE); return Common.Rep.DOUBLE; } else if (o instanceof Float) { writeToProtoWithType(builder, ((Float) o).longValue(), Common.Rep.FLOAT); return Common.Rep.FLOAT; } else if (o instanceof BigDecimal) { writeToProtoWithType(builder, o, Common.Rep.BIG_DECIMAL); return Common.Rep.BIG_DECIMAL; writeToProtoWithType(builder, o, Common.Rep.STRING); return Common.Rep.STRING; } else if (o instanceof Character) { writeToProtoWithType(builder, o.toString(), Common.Rep.CHARACTER); return Common.Rep.CHARACTER; writeToProtoWithType(builder, o, Common.Rep.BYTE_STRING); return Common.Rep.BYTE_STRING;
writeToProtoWithType(builder, o, Common.Rep.BYTE); return Common.Rep.BYTE; } else if (o instanceof Short) { writeToProtoWithType(builder, o, Common.Rep.SHORT); return Common.Rep.SHORT; } else if (o instanceof Integer) { writeToProtoWithType(builder, o, Common.Rep.INTEGER); return Common.Rep.INTEGER; } else if (o instanceof Long) { writeToProtoWithType(builder, o, Common.Rep.LONG); return Common.Rep.LONG; } else if (o instanceof Double) { writeToProtoWithType(builder, o, Common.Rep.DOUBLE); return Common.Rep.DOUBLE; } else if (o instanceof Float) { writeToProtoWithType(builder, ((Float) o).longValue(), Common.Rep.FLOAT); return Common.Rep.FLOAT; } else if (o instanceof BigDecimal) { writeToProtoWithType(builder, o, Common.Rep.BIG_DECIMAL); return Common.Rep.BIG_DECIMAL; writeToProtoWithType(builder, o, Common.Rep.STRING); return Common.Rep.STRING; } else if (o instanceof Character) { writeToProtoWithType(builder, o.toString(), Common.Rep.CHARACTER); return Common.Rep.CHARACTER; writeToProtoWithType(builder, o, Common.Rep.BYTE_STRING); return Common.Rep.BYTE_STRING;
Common.TypedValue serializeArray(List<Object> list, Common.TypedValue.Builder builder, Common.Rep protoArrayComponentRep) { for (Object element : list) { if (element instanceof List) { // We have a list of lists: recursively build up the protobuf @SuppressWarnings("unchecked") List<Object> subList = (List<Object>) element; Common.TypedValue.Builder subListBuilder = Common.TypedValue.newBuilder(); // This is "technically" an array, but we just persist the underlying component type subListBuilder.setType(protoArrayComponentRep); Common.TypedValue protoSubList = serializeArray(subList, subListBuilder, protoArrayComponentRep); builder.addArrayValue(protoSubList); } else { // We have a list of "scalars", just serialize the value Common.TypedValue.Builder elementBuilder = Common.TypedValue.newBuilder(); if (null == element) { writeToProtoWithType(elementBuilder, null, Common.Rep.NULL); } else { writeToProtoWithType(elementBuilder, element, protoArrayComponentRep); } builder.addArrayValue(elementBuilder.build()); } } return builder.build(); }
Common.TypedValue serializeArray(List<Object> list, Common.TypedValue.Builder builder, Common.Rep protoArrayComponentRep) { for (Object element : list) { if (element instanceof List) { // We have a list of lists: recursively build up the protobuf @SuppressWarnings("unchecked") List<Object> subList = (List<Object>) element; Common.TypedValue.Builder subListBuilder = Common.TypedValue.newBuilder(); // This is "technically" an array, but we just persist the underlying component type subListBuilder.setType(protoArrayComponentRep); Common.TypedValue protoSubList = serializeArray(subList, subListBuilder, protoArrayComponentRep); builder.addArrayValue(protoSubList); } else { // We have a list of "scalars", just serialize the value Common.TypedValue.Builder elementBuilder = Common.TypedValue.newBuilder(); if (null == element) { writeToProtoWithType(elementBuilder, null, Common.Rep.NULL); } else { writeToProtoWithType(elementBuilder, element, protoArrayComponentRep); } builder.addArrayValue(elementBuilder.build()); } } return builder.build(); }
/** * Creates a protocol buffer equivalent object for <code>this</code>. * @return A protobuf TypedValue equivalent for <code>this</code> */ public Common.TypedValue toProto() { final Common.TypedValue.Builder builder = Common.TypedValue.newBuilder(); // This isn't a static method, therefore we have a non-null TypedValue. Thus, this message // cannot be implicitly null builder.setImplicitlyNull(false); Common.Rep protoRep = type.toProto(); // Protobuf has an explicit BIG_DECIMAL representation enum value. if (Common.Rep.NUMBER == protoRep && value instanceof BigDecimal) { protoRep = Common.Rep.BIG_DECIMAL; } else if (Common.Rep.ARRAY == protoRep) { // This column value is an Array (many TypedValue's) builder.setType(Common.Rep.ARRAY); // Get the array component's type Common.Rep protoComponentRep = componentType.toProto(); // Set the array's component on the message builder.setComponentType(protoComponentRep); // Serialize that array into the builder @SuppressWarnings("unchecked") List<Object> list = (List<Object>) value; return serializeArray(list, builder, protoComponentRep); } // Serialize the type into the protobuf writeToProtoWithType(builder, value, protoRep); return builder.build(); }
/** * Creates a protocol buffer equivalent object for <code>this</code>. * @return A protobuf TypedValue equivalent for <code>this</code> */ public Common.TypedValue toProto() { final Common.TypedValue.Builder builder = Common.TypedValue.newBuilder(); // This isn't a static method, therefore we have a non-null TypedValue. Thus, this message // cannot be implicitly null builder.setImplicitlyNull(false); Common.Rep protoRep = type.toProto(); // Protobuf has an explicit BIG_DECIMAL representation enum value. if (Common.Rep.NUMBER == protoRep && value instanceof BigDecimal) { protoRep = Common.Rep.BIG_DECIMAL; } else if (Common.Rep.ARRAY == protoRep) { // This column value is an Array (many TypedValue's) builder.setType(Common.Rep.ARRAY); // Get the array component's type Common.Rep protoComponentRep = componentType.toProto(); // Set the array's component on the message builder.setComponentType(protoComponentRep); // Serialize that array into the builder @SuppressWarnings("unchecked") List<Object> list = (List<Object>) value; return serializeArray(list, builder, protoComponentRep); } // Serialize the type into the protobuf writeToProtoWithType(builder, value, protoRep); return builder.build(); }