Example usage for org.apache.thrift.protocol TProtocol writeSetEnd

List of usage examples for org.apache.thrift.protocol TProtocol writeSetEnd

Introduction

In this page you can find the example usage for org.apache.thrift.protocol TProtocol writeSetEnd.

Prototype

public abstract void writeSetEnd() throws TException;

Source Link

Usage

From source file:com.ebay.nest.io.sede.dynamic_type.DynamicSerDeTypeSet.java

License:Apache License

@Override
public void serialize(Object o, ObjectInspector oi, TProtocol oprot)
        throws TException, SerDeException, NoSuchFieldException, IllegalAccessException {

    ListObjectInspector loi = (ListObjectInspector) oi;

    Set<Object> set = (Set<Object>) o;
    DynamicSerDeTypeBase mt = getElementType();
    tset = new TSet(mt.getType(), set.size());
    oprot.writeSetBegin(tset);/*from   w ww.j  av a  2  s .c o  m*/
    for (Object element : set) {
        mt.serialize(element, loi.getListElementObjectInspector(), oprot);
    }
    // in theory, the below call isn't needed in non thrift_mode, but let's not
    // get too crazy
    oprot.writeSetEnd();
}

From source file:mt.swift.MapSerializer.java

License:Apache License

private void write(TProtocol protocol, Object value, Type type, Field field) throws TException {
    if (type == BasicType.BINARY) {
        if (value instanceof byte[]) {
            protocol.writeBinary((byte[]) value);
        } else if (value instanceof ByteBuffer) {
            protocol.writeBinary(((ByteBuffer) value).array());
        } else {//from w  w w.j av a 2s.c om
            throwInvalidTypeException(field, value);
        }
    } else if (type == BasicType.BOOLEAN) {
        if (value instanceof Boolean) {
            protocol.writeBool((Boolean) value);
        } else {
            throwInvalidTypeException(field, value);
        }
    } else if (type == BasicType.BYTE) {
        if (value instanceof Byte || value instanceof Short || value instanceof Integer || value instanceof Long
                || value instanceof BigInteger) {
            Number number = (Number) value;
            if (number.shortValue() < Byte.MIN_VALUE || number.shortValue() > Byte.MAX_VALUE) {
                throwOverflowException(field, number, Byte.MIN_VALUE, Byte.MAX_VALUE);
            }

            protocol.writeByte(number.byteValue());
        } else {
            throwInvalidTypeException(field, value);
        }
    } else if (type == BasicType.I16) {
        if (value instanceof Byte || value instanceof Short || value instanceof Integer || value instanceof Long
                || value instanceof BigInteger) {
            Number number = (Number) value;
            if (number.intValue() < Short.MIN_VALUE || number.intValue() > Short.MAX_VALUE) {
                throwOverflowException(field, number, Short.MIN_VALUE, Short.MAX_VALUE);
            }

            protocol.writeI16(number.shortValue());
        } else {
            throwInvalidTypeException(field, value);
        }
    } else if (type == BasicType.I32) {
        if (value instanceof Byte || value instanceof Short || value instanceof Integer || value instanceof Long
                || value instanceof BigInteger) {
            Number number = (Number) value;
            if (number.longValue() < Integer.MIN_VALUE || number.longValue() > Integer.MAX_VALUE) {
                throwOverflowException(field, number, Integer.MIN_VALUE, Integer.MAX_VALUE);
            }

            protocol.writeI32(number.intValue());
        } else {
            throwInvalidTypeException(field, value);
        }
    } else if (type == BasicType.I64) {
        if (value instanceof Byte || value instanceof Short || value instanceof Integer
                || value instanceof Long) {
            protocol.writeI64(((Number) value).longValue());
        } else if (value instanceof BigInteger) {
            BigInteger number = (BigInteger) value;
            if (number.compareTo(BigInteger.valueOf(Long.MAX_VALUE)) > 0
                    || number.compareTo(BigInteger.valueOf(Long.MIN_VALUE)) < 0) {
                throwOverflowException(field, number, Long.MAX_VALUE, Long.MIN_VALUE);
            }

            protocol.writeI64(((Number) value).longValue());
        } else {
            throwInvalidTypeException(field, value);
        }
    } else if (type == BasicType.DOUBLE) {
        if (value instanceof Byte || value instanceof Short || value instanceof Integer || value instanceof Long
                || value instanceof Float || value instanceof Double) {
            protocol.writeDouble(((Number) value).doubleValue());
        }
        // TODO: support BigDecimal, BigInteger if they fit
        else {
            throwInvalidTypeException(field, value);
        }
    } else if (type == BasicType.STRING) {
        if (value instanceof String) {
            protocol.writeString((String) value);
        } else if (value instanceof CharSequence) {
            protocol.writeString(value.toString());
        }
        // TODO: support any object by calling toString
        else {
            throwInvalidTypeException(field, value);
        }
    } else if (type instanceof MapType) {
        Map<?, ?> mapValue = (Map<?, ?>) value;
        MapType mapType = (MapType) type;
        TMap tmap = new TMap(mapType.getKeyType().getTType(), mapType.getValueType().getTType(),
                mapValue.size());
        protocol.writeMapBegin(tmap);
        for (Map.Entry<?, ?> entry : mapValue.entrySet()) {
            write(protocol, entry.getKey(), mapType.getKeyType(), field);
            write(protocol, entry.getValue(), mapType.getValueType(), field);
        }
        protocol.writeMapEnd();
    } else if (type instanceof ListType) {
        List<?> list = (List<?>) value;
        ListType listType = (ListType) type;
        TList tlist = new TList(listType.getValueType().getTType(), list.size());
        protocol.writeListBegin(tlist);
        for (Object obj : list) {
            write(protocol, obj, listType.getValueType(), field);
        }
        protocol.writeListEnd();
    } else if (type instanceof SetType) {
        Set<?> set = (Set<?>) value;
        SetType setType = (SetType) type;
        TSet tset = new TSet(setType.getValueType().getTType(), set.size());
        protocol.writeSetBegin(tset);
        for (Object obj : set) {
            write(protocol, obj, setType.getValueType(), field);
        }
        protocol.writeSetEnd();
    } else if (type instanceof StructureType) {
        if (value instanceof Map) {
            Map<String, ?> child = (Map<String, ?>) value;
            StructureType structureType = (StructureType) type;
            String structureName = structureType.getName();
            serialize(child, structureName, protocol);
        } else {
            throwInvalidTypeException(field, value);
        }
    } else {
        throw new IllegalArgumentException(String.format("Don't know how to serialize '%s' (%s)",
                field.getName(), field.getType().getSignature()));
    }
}

From source file:net.morimekta.providence.thrift.TProtocolSerializer.java

License:Apache License

private void writeTypedValue(Object item, PDescriptor type, TProtocol protocol)
        throws TException, SerializerException {
    switch (type.getType()) {
    case BOOL:/*from  w ww. j  a va 2  s .c  o m*/
        protocol.writeBool((Boolean) item);
        break;
    case BYTE:
        protocol.writeByte((Byte) item);
        break;
    case I16:
        protocol.writeI16((Short) item);
        break;
    case I32:
        protocol.writeI32((Integer) item);
        break;
    case I64:
        protocol.writeI64((Long) item);
        break;
    case DOUBLE:
        protocol.writeDouble((Double) item);
        break;
    case STRING:
        protocol.writeString((String) item);
        break;
    case BINARY:
        protocol.writeBinary(((Binary) item).getByteBuffer());
        break;
    case ENUM:
        PEnumValue<?> value = (PEnumValue<?>) item;
        protocol.writeI32(value.getValue());
        break;
    case MESSAGE:
        writeMessage((PMessage<?, ?>) item, protocol);
        break;
    case LIST:
        PList<?> lType = (PList<?>) type;
        List<?> list = (List<?>) item;
        TList listInfo = new TList(getFieldType(lType.itemDescriptor()), list.size());
        protocol.writeListBegin(listInfo);
        for (Object i : list) {
            writeTypedValue(i, lType.itemDescriptor(), protocol);
        }
        protocol.writeListEnd();
        break;
    case SET:
        PSet<?> sType = (PSet<?>) type;
        Set<?> set = (Set<?>) item;
        TSet setInfo = new TSet(getFieldType(sType.itemDescriptor()), set.size());
        protocol.writeSetBegin(setInfo);
        for (Object i : set) {
            writeTypedValue(i, sType.itemDescriptor(), protocol);
        }
        protocol.writeSetEnd();
        break;
    case MAP:
        PMap<?, ?> mType = (PMap<?, ?>) type;
        Map<?, ?> map = (Map<?, ?>) item;
        protocol.writeMapBegin(new TMap(getFieldType(mType.keyDescriptor()),
                getFieldType(mType.itemDescriptor()), map.size()));

        for (Map.Entry<?, ?> entry : map.entrySet()) {
            writeTypedValue(entry.getKey(), mType.keyDescriptor(), protocol);
            writeTypedValue(entry.getValue(), mType.itemDescriptor(), protocol);
        }

        protocol.writeMapEnd();
        break;
    default:
        break;
    }
}

From source file:org.apache.cassandra.thrift.BatchMutateResult.java

License:Apache License

public void write(org.apache.thrift.protocol.TProtocol oprot) throws org.apache.thrift.TException {
    validate();//from  w  ww  .j ava2s  .c o  m

    oprot.writeStructBegin(STRUCT_DESC);
    if (this.deps != null) {
        oprot.writeFieldBegin(DEPS_FIELD_DESC);
        {
            oprot.writeSetBegin(new org.apache.thrift.protocol.TSet(org.apache.thrift.protocol.TType.STRUCT,
                    this.deps.size()));
            for (Dep _iter129 : this.deps) {
                _iter129.write(oprot);
            }
            oprot.writeSetEnd();
        }
        oprot.writeFieldEnd();
    }
    oprot.writeFieldBegin(LTS_FIELD_DESC);
    oprot.writeI64(this.lts);
    oprot.writeFieldEnd();
    oprot.writeFieldStop();
    oprot.writeStructEnd();
}

From source file:org.apache.cassandra.thrift.CountWithMetadata.java

License:Apache License

public void write(org.apache.thrift.protocol.TProtocol oprot) throws org.apache.thrift.TException {
    validate();/*from w w w  . j ava 2 s.c  o  m*/

    oprot.writeStructBegin(STRUCT_DESC);
    oprot.writeFieldBegin(COUNT_FIELD_DESC);
    oprot.writeI32(this.count);
    oprot.writeFieldEnd();
    oprot.writeFieldBegin(EARLIEST_VALID_TIME_FIELD_DESC);
    oprot.writeI64(this.earliest_valid_time);
    oprot.writeFieldEnd();
    oprot.writeFieldBegin(LATEST_VALID_TIME_FIELD_DESC);
    oprot.writeI64(this.latest_valid_time);
    oprot.writeFieldEnd();
    if (this.deps != null) {
        oprot.writeFieldBegin(DEPS_FIELD_DESC);
        {
            oprot.writeSetBegin(new org.apache.thrift.protocol.TSet(org.apache.thrift.protocol.TType.STRUCT,
                    this.deps.size()));
            for (Dep _iter112 : this.deps) {
                _iter112.write(oprot);
            }
            oprot.writeSetEnd();
        }
        oprot.writeFieldEnd();
    }
    oprot.writeFieldStop();
    oprot.writeStructEnd();
}

From source file:org.apache.cassandra.thrift.GetCountResult.java

License:Apache License

public void write(org.apache.thrift.protocol.TProtocol oprot) throws org.apache.thrift.TException {
    validate();// w  w  w  . j a  v  a2s . com

    oprot.writeStructBegin(STRUCT_DESC);
    oprot.writeFieldBegin(VALUE_FIELD_DESC);
    oprot.writeI32(this.value);
    oprot.writeFieldEnd();
    if (this.deps != null) {
        oprot.writeFieldBegin(DEPS_FIELD_DESC);
        {
            oprot.writeSetBegin(new org.apache.thrift.protocol.TSet(org.apache.thrift.protocol.TType.STRUCT,
                    this.deps.size()));
            for (Dep _iter99 : this.deps) {
                _iter99.write(oprot);
            }
            oprot.writeSetEnd();
        }
        oprot.writeFieldEnd();
    }
    oprot.writeFieldBegin(LTS_FIELD_DESC);
    oprot.writeI64(this.lts);
    oprot.writeFieldEnd();
    oprot.writeFieldStop();
    oprot.writeStructEnd();
}

From source file:org.apache.parquet.thrift.ProtocolReadToWrite.java

License:Apache License

private void readOneSet(TProtocol in, TProtocol out) throws TException {
    final TSet set = in.readSetBegin();
    out.writeSetBegin(set);//ww w  .ja va2 s .co  m
    readCollectionElements(in, out, set.size, set.elemType);
    in.readSetEnd();
    out.writeSetEnd();
}