Java Code Examples for org.msgpack.core.MessageBufferPacker#packDouble()

The following examples show how to use org.msgpack.core.MessageBufferPacker#packDouble() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: StatsResponse.java    From maestro-java with Apache License 2.0 5 votes vote down vote up
@Override
protected MessageBufferPacker pack() throws IOException {
    MessageBufferPacker packer = super.pack();

    packer.packLong(this.childCount);
    packer.packString(this.roleInfo);
    packer.packShort(this.statsType);
    packer.packString(this.timestamp);
    packer.packLong(this.count);
    packer.packDouble(this.rate);
    packer.packDouble(this.latency);

    return packer;
}
 
Example 2
Source File: DataDstMsgPack.java    From xresloader with MIT License 4 votes vote down vote up
@SuppressWarnings("unchecked")
private void writeData(MessageBufferPacker packer, Object data) throws IOException {
    // null
    if (null == data) {
        packer.packNil();
        return;
    }

    // 字符串&二进制
    if (data instanceof String) {
        packer.packString((String) data);
        return;
    }

    // 数字
    // 枚举值已被转为Java Long,会在这里执行
    if (data instanceof Integer) {
        packer.packInt((Integer) data);
        return;
    }
    if (data instanceof Long) {
        packer.packLong((long) data);
        return;
    }
    if (data instanceof Short) {
        packer.packLong((short) data);
        return;
    }
    if (data instanceof Float) {
        packer.packFloat((float) data);
        return;
    }
    if (data instanceof Double) {
        packer.packDouble((double) data);
        return;
    }
    if (data instanceof Byte) {
        packer.packByte((byte) data);
        return;
    }
    if (data instanceof java.math.BigInteger) {
        packer.packBigInteger((java.math.BigInteger) data);
        return;
    }

    // 布尔
    if (data instanceof Boolean) {
        packer.packBoolean((Boolean) data);
        return;
    }

    // 列表
    if (data instanceof List<?>) {
        packer.packArrayHeader(((List<?>) data).size());
        for (Object subobj : (List<?>) data) {
            writeData(packer, subobj);
        }
        return;
    }

    // Hashmap
    if (data instanceof Map<?, ?>) {
        Map<?, ?> mp = (Map<?, ?>) data;

        ArrayList<Map.Entry<?, ?>> sorted_array = new ArrayList<Map.Entry<?, ?>>();
        sorted_array.ensureCapacity(mp.size());
        sorted_array.addAll(mp.entrySet());
        sorted_array.sort((l, r) -> {
            if (l.getValue() instanceof Integer && r.getValue() instanceof Integer) {
                return ((Integer) l.getValue()).compareTo((Integer) r.getValue());
            }

            if (l.getKey() instanceof Integer && r.getKey() instanceof Integer) {
                return ((Integer) l.getKey()).compareTo((Integer) r.getKey());
            } else if (l.getKey() instanceof Long && r.getKey() instanceof Long) {
                return ((Long) l.getKey()).compareTo((Long) r.getKey());
            } else {
                return l.getKey().toString().compareTo(r.getKey().toString());
            }
        });

        packer.packMapHeader(sorted_array.size());
        for (Map.Entry<?, ?> item : sorted_array) {
            writeData(packer, item.getKey());
            writeData(packer, item.getValue());
        }

        return;
    }

    packer.packString(data.toString());
}