use of org.infinispan.protostream.descriptors.FieldDescriptor in project protostream by infinispan.
the class ProtoStreamWriterImpl method writeDouble.
@Override
public void writeDouble(String fieldName, Double value) throws IOException {
final FieldDescriptor fd = messageContext.getFieldByName(fieldName);
if (fd.getType() != Type.DOUBLE) {
throw new IllegalArgumentException("The Protobuf declared field type is not compatible with the written type : " + fd.getFullName());
}
checkFieldWrite(fd);
if (value == null) {
if (fd.isRequired()) {
throw new IllegalArgumentException("A required field cannot be null : " + fd.getFullName());
}
return;
}
messageContext.out.writeDouble(fd.getNumber(), value);
}
use of org.infinispan.protostream.descriptors.FieldDescriptor in project protostream by infinispan.
the class ProtoStreamWriterImpl method writeLongs.
@Override
public void writeLongs(String fieldName, long[] array) throws IOException {
final FieldDescriptor fd = messageContext.getFieldByName(fieldName);
checkRepeatedFieldWrite(fd);
if (array == null) {
// a repeated field can never be flagged as required
return;
}
final TagWriter out = messageContext.out;
final int fieldNumber = fd.getNumber();
switch(fd.getType()) {
case INT64:
for (long value : array) {
out.writeInt64(fieldNumber, value);
}
break;
case FIXED64:
for (long value : array) {
out.writeFixed64(fieldNumber, value);
}
break;
case UINT64:
for (long value : array) {
out.writeUInt64(fieldNumber, value);
}
break;
case SFIXED64:
for (long value : array) {
out.writeSFixed64(fieldNumber, value);
}
break;
case SINT64:
for (long value : array) {
out.writeSInt64(fieldNumber, value);
}
break;
default:
throw new IllegalArgumentException("The Protobuf declared field type is not compatible with the written type : " + fd.getFullName());
}
}
use of org.infinispan.protostream.descriptors.FieldDescriptor in project protostream by infinispan.
the class ProtoStreamWriterImpl method writeDoubles.
@Override
public void writeDoubles(String fieldName, double[] array) throws IOException {
final FieldDescriptor fd = messageContext.getFieldByName(fieldName);
if (fd.getType() != Type.DOUBLE) {
throw new IllegalArgumentException("The Protobuf declared field type is not compatible with the written type : " + fd.getFullName());
}
checkRepeatedFieldWrite(fd);
if (array == null) {
// a repeated field can never be flagged as required
return;
}
final TagWriter out = messageContext.out;
final int fieldNumber = fd.getNumber();
for (double value : array) {
out.writeDouble(fieldNumber, value);
}
}
use of org.infinispan.protostream.descriptors.FieldDescriptor in project protostream by infinispan.
the class ProtoStreamWriterImpl method writeCollection.
@Override
public <E> void writeCollection(String fieldName, Collection<? super E> collection, Class<E> elementClass) throws IOException {
final FieldDescriptor fd = messageContext.getFieldByName(fieldName);
checkRepeatedFieldWrite(fd);
if (collection == null) {
// a repeated field can never be flagged as required
return;
}
final TagWriter out = messageContext.out;
final int fieldNumber = fd.getNumber();
switch(fd.getType()) {
case GROUP:
for (Object t : collection) {
validateElement(t, elementClass);
writeGroup(fd, t, elementClass);
}
break;
case MESSAGE:
for (Object t : collection) {
validateElement(t, elementClass);
writeMessage(fd, t, elementClass);
}
break;
case ENUM:
for (Object t : collection) {
validateElement(t, elementClass);
writeEnum(fd, (Enum) t);
}
break;
case DOUBLE:
validateElementClass(elementClass, Double.class);
for (Object value : collection) {
validateElement(value, elementClass);
out.writeDouble(fieldNumber, (Double) value);
}
break;
case FLOAT:
validateElementClass(elementClass, Float.class);
for (Object value : collection) {
validateElement(value, elementClass);
out.writeFloat(fieldNumber, (Float) value);
}
break;
case BOOL:
validateElementClass(elementClass, Boolean.class);
for (Object value : collection) {
validateElement(value, elementClass);
out.writeBool(fieldNumber, (Boolean) value);
}
break;
case STRING:
validateElementClass(elementClass, String.class);
for (Object value : collection) {
validateElement(value, elementClass);
out.writeString(fieldNumber, (String) value);
}
break;
case BYTES:
validateElementClass(elementClass, byte[].class);
for (Object value : collection) {
validateElement(value, elementClass);
out.writeBytes(fieldNumber, (byte[]) value);
}
break;
case INT64:
validateElementClass(elementClass, Long.class);
for (Object value : collection) {
validateElement(value, elementClass);
out.writeInt64(fieldNumber, (Long) value);
}
break;
case UINT64:
validateElementClass(elementClass, Long.class);
for (Object value : collection) {
validateElement(value, elementClass);
out.writeUInt64(fieldNumber, (Long) value);
}
break;
case FIXED64:
validateElementClass(elementClass, Long.class);
for (Object value : collection) {
validateElement(value, elementClass);
out.writeFixed64(fieldNumber, (Long) value);
}
break;
case SFIXED64:
validateElementClass(elementClass, Long.class);
for (Object value : collection) {
validateElement(value, elementClass);
out.writeSFixed64(fieldNumber, (Long) value);
}
break;
case SINT64:
validateElementClass(elementClass, Long.class);
for (Object value : collection) {
validateElement(value, elementClass);
out.writeSInt64(fieldNumber, (Long) value);
}
break;
case INT32:
validateElementClass(elementClass, Integer.class);
for (Object value : collection) {
validateElement(value, elementClass);
out.writeInt32(fieldNumber, (Integer) value);
}
break;
case FIXED32:
validateElementClass(elementClass, Integer.class);
for (Object value : collection) {
validateElement(value, elementClass);
out.writeFixed32(fieldNumber, (Integer) value);
}
break;
case UINT32:
validateElementClass(elementClass, Integer.class);
for (Object value : collection) {
validateElement(value, elementClass);
out.writeUInt32(fieldNumber, (Integer) value);
}
break;
case SFIXED32:
validateElementClass(elementClass, Integer.class);
for (Object value : collection) {
validateElement(value, elementClass);
out.writeSFixed32(fieldNumber, (Integer) value);
}
break;
case SINT32:
validateElementClass(elementClass, Integer.class);
for (Object value : collection) {
validateElement(value, elementClass);
out.writeSInt32(fieldNumber, (Integer) value);
}
break;
default:
throw new IllegalArgumentException("The Protobuf declared field type is not compatible with the written type : " + fd.getFullName());
}
}
use of org.infinispan.protostream.descriptors.FieldDescriptor in project protostream by infinispan.
the class ProtoStreamWriterImpl method writeBytes.
@Override
public void writeBytes(String fieldName, byte[] value) throws IOException {
final FieldDescriptor fd = messageContext.getFieldByName(fieldName);
checkFieldWrite(fd);
if (fd.getType() != Type.BYTES) {
throw new IllegalArgumentException("Declared field type is not of type bytes : " + fd.getFullName());
}
if (value == null) {
if (fd.isRequired()) {
throw new IllegalArgumentException("A required field cannot be null : " + fd.getFullName());
}
return;
}
messageContext.out.writeBytes(fd.getNumber(), value);
}
Aggregations