Search in sources :

Example 36 with FieldDescriptor

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);
}
Also used : FieldDescriptor(org.infinispan.protostream.descriptors.FieldDescriptor)

Example 37 with FieldDescriptor

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());
    }
}
Also used : TagWriter(org.infinispan.protostream.TagWriter) FieldDescriptor(org.infinispan.protostream.descriptors.FieldDescriptor)

Example 38 with FieldDescriptor

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);
    }
}
Also used : TagWriter(org.infinispan.protostream.TagWriter) FieldDescriptor(org.infinispan.protostream.descriptors.FieldDescriptor)

Example 39 with FieldDescriptor

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());
    }
}
Also used : TagWriter(org.infinispan.protostream.TagWriter) FieldDescriptor(org.infinispan.protostream.descriptors.FieldDescriptor)

Example 40 with FieldDescriptor

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);
}
Also used : FieldDescriptor(org.infinispan.protostream.descriptors.FieldDescriptor)

Aggregations

FieldDescriptor (org.infinispan.protostream.descriptors.FieldDescriptor)51 Descriptor (org.infinispan.protostream.descriptors.Descriptor)16 EnumDescriptor (org.infinispan.protostream.descriptors.EnumDescriptor)12 TagWriter (org.infinispan.protostream.TagWriter)9 IOException (java.io.IOException)7 GenericDescriptor (org.infinispan.protostream.descriptors.GenericDescriptor)7 EnumValueDescriptor (org.infinispan.protostream.descriptors.EnumValueDescriptor)6 FileDescriptor (org.infinispan.protostream.descriptors.FileDescriptor)6 Map (java.util.Map)5 ArrayList (java.util.ArrayList)4 List (java.util.List)4 FileDescriptorSource (org.infinispan.protostream.FileDescriptorSource)4 Configuration (org.infinispan.protostream.config.Configuration)4 ExtendDescriptor (org.infinispan.protostream.descriptors.ExtendDescriptor)4 Test (org.junit.Test)4 HashMap (java.util.HashMap)3 Objects (java.util.Objects)3 AnnotationElement (org.infinispan.protostream.descriptors.AnnotationElement)3 Set (java.util.Set)2 Function (java.util.function.Function)2