下面列出了com.google.protobuf.Internal#EnumLite ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private static Descriptors.FieldDescriptor.JavaType getJavaType(@Nonnull Object o) {
if (o instanceof Boolean) {
return JavaType.BOOLEAN;
} else if (o instanceof ByteString || o instanceof byte[]) {
return JavaType.BYTE_STRING;
} else if (o instanceof Double) {
return JavaType.DOUBLE;
} else if (o instanceof Float) {
return JavaType.FLOAT;
} else if (o instanceof Long) {
return JavaType.LONG;
} else if (o instanceof Integer) {
return JavaType.INT;
} else if (o instanceof String) {
return JavaType.STRING;
} else if (o instanceof Internal.EnumLite) {
return JavaType.ENUM;
} else {
throw new RecordCoreException(o.getClass() + " is an invalid type for a comparand");
}
}
/**
* Write a field of arbitrary type, without its tag, to the stream.
*
* @param output The output stream.
* @param type The field's type.
* @param value Object representing the field's value. Must be of the exact type which would be returned by
* {@link Message#getField(Descriptors.FieldDescriptor)} for this field.
* @throws IOException Signals that an I/O exception has occurred.
*/
public static void writeElementNoTag(final CodedOutputStream output, final WireFormat.FieldType type,
final Object value) throws IOException {
switch (type) {
case DOUBLE:
output.writeDoubleNoTag((Double) value);
break;
case FLOAT:
output.writeFloatNoTag((Float) value);
break;
case INT64:
output.writeInt64NoTag((Long) value);
break;
case UINT64:
output.writeUInt64NoTag((Long) value);
break;
case INT32:
output.writeInt32NoTag((Integer) value);
break;
case FIXED64:
output.writeFixed64NoTag((Long) value);
break;
case FIXED32:
output.writeFixed32NoTag((Integer) value);
break;
case BOOL:
output.writeBoolNoTag((Boolean) value);
break;
case STRING:
output.writeStringNoTag((String) value);
break;
// group not support yet
// case GROUP : output.writeGroupNoTag ((MessageLite) value); break;
case MESSAGE:
writeObject(output, 0, FieldType.OBJECT, value, false, false);
break;
case BYTES:
if (value instanceof ByteString) {
output.writeBytesNoTag((ByteString) value);
} else {
byte[] v;
if (value instanceof Byte[]) {
v = toByteArray((Byte[]) value);
} else {
v = (byte[]) value;
}
output.writeByteArrayNoTag(v);
}
break;
case UINT32:
output.writeUInt32NoTag((Integer) value);
break;
case SFIXED32:
output.writeSFixed32NoTag((Integer) value);
break;
case SFIXED64:
output.writeSFixed64NoTag((Long) value);
break;
case SINT32:
output.writeSInt32NoTag((Integer) value);
break;
case SINT64:
output.writeSInt64NoTag((Long) value);
break;
case ENUM:
if (value instanceof Internal.EnumLite) {
output.writeEnumNoTag(((Internal.EnumLite) value).getNumber());
} else {
if (value instanceof EnumReadable) {
output.writeEnumNoTag(((EnumReadable) value).value());
} else if (value instanceof Enum) {
output.writeEnumNoTag(((Enum) value).ordinal());
} else {
output.writeEnumNoTag(((Integer) value).intValue());
}
}
break;
}
}
/**
* Compute the number of bytes that would be needed to encode a particular value of arbitrary type, excluding tag.
*
* @param type The field's type.
* @param value Object representing the field's value. Must be of the exact type which would be returned by
* {@link Message#getField(Descriptors.FieldDescriptor)} for this field.
* @return the int
*/
public static int computeElementSizeNoTag(final WireFormat.FieldType type, final Object value) {
switch (type) {
// Note: Minor violation of 80-char limit rule here because this would
// actually be harder to read if we wrapped the lines.
case DOUBLE:
return CodedOutputStream.computeDoubleSizeNoTag((Double) value);
case FLOAT:
return CodedOutputStream.computeFloatSizeNoTag((Float) value);
case INT64:
return CodedOutputStream.computeInt64SizeNoTag((Long) value);
case UINT64:
return CodedOutputStream.computeUInt64SizeNoTag((Long) value);
case INT32:
return CodedOutputStream.computeInt32SizeNoTag((Integer) value);
case FIXED64:
return CodedOutputStream.computeFixed64SizeNoTag((Long) value);
case FIXED32:
return CodedOutputStream.computeFixed32SizeNoTag((Integer) value);
case BOOL:
return CodedOutputStream.computeBoolSizeNoTag((Boolean) value);
case STRING:
return CodedOutputStream.computeStringSizeNoTag((String) value);
case GROUP:
return CodedOutputStream.computeGroupSizeNoTag((MessageLite) value);
case BYTES:
if (value instanceof ByteString) {
return CodedOutputStream.computeBytesSizeNoTag((ByteString) value);
} else {
if (value instanceof Byte[]) {
return computeLengthDelimitedFieldSize(((Byte[]) value).length);
}
return CodedOutputStream.computeByteArraySizeNoTag((byte[]) value);
}
case UINT32:
return CodedOutputStream.computeUInt32SizeNoTag((Integer) value);
case SFIXED32:
return CodedOutputStream.computeSFixed32SizeNoTag((Integer) value);
case SFIXED64:
return CodedOutputStream.computeSFixed64SizeNoTag((Long) value);
case SINT32:
return CodedOutputStream.computeSInt32SizeNoTag((Integer) value);
case SINT64:
return CodedOutputStream.computeSInt64SizeNoTag((Long) value);
case MESSAGE:
if (value instanceof LazyField) {
return CodedOutputStream.computeLazyFieldSizeNoTag((LazyField) value);
} else {
return computeObjectSizeNoTag(value);
}
case ENUM:
if (value instanceof Internal.EnumLite) {
return CodedOutputStream.computeEnumSizeNoTag(((Internal.EnumLite) value).getNumber());
} else {
if (value instanceof EnumReadable) {
return CodedOutputStream.computeEnumSizeNoTag(((EnumReadable) value).value());
} else if (value instanceof Enum) {
return CodedOutputStream.computeEnumSizeNoTag(((Enum) value).ordinal());
}
return CodedOutputStream.computeEnumSizeNoTag((Integer) value);
}
}
throw new RuntimeException("There is no way to get here, but the compiler thinks otherwise.");
}