All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.fmjsjx.libnetty.resp.AbstractRespAggregateMessage Maven / Gradle / Ivy

There is a newer version: 3.7.1
Show newest version
package com.github.fmjsjx.libnetty.resp;

import static com.github.fmjsjx.libnetty.resp.RespConstants.EOL_LENGTH;
import static com.github.fmjsjx.libnetty.resp.RespConstants.EOL_SHORT;
import static com.github.fmjsjx.libnetty.resp.RespConstants.TYPE_LENGTH;

import java.util.List;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufAllocator;
import io.netty.util.AbstractReferenceCounted;

/**
 * The abstract implementation of {@link RespAggregateMessage}.
 * 
 * @param     the type of values in this message
 * @param  the type of the real class
 * 
 * @since 1.1
 *
 * @author MJ Fang
 */
@SuppressWarnings("unchecked")
public abstract class AbstractRespAggregateMessage>
        extends AbstractReferenceCounted implements RespAggregateMessage {

    @Override
    public Self retain() {
        super.retain();
        return (Self) this;
    }

    @Override
    public Self retain(int increment) {
        super.retain(increment);
        return (Self) this;
    }

    @Override
    public Self touch() {
        super.touch();
        return (Self) this;
    }

    @Override
    public abstract Self touch(Object hint);

    @Override
    public void encode(ByteBufAllocator alloc, List out) throws Exception {
        encodeHeader(alloc, out);
        // encode values
        for (E e : values()) {
            encodeValue(alloc, e, out);
        }
    }

    protected void encodeHeader(ByteBufAllocator alloc, List out) throws Exception {
        byte[] sizeBytes = RespCodecUtil.longToAsciiBytes(size());
        ByteBuf header = alloc.buffer(TYPE_LENGTH + sizeBytes.length + EOL_LENGTH).writeByte(type().value())
                .writeBytes(sizeBytes).writeShort(EOL_SHORT);
        out.add(header);
    }

    protected abstract void encodeValue(ByteBufAllocator alloc, E value, List out) throws Exception;

}