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

com.github.fmjsjx.libnetty.resp.CachedIntegerMessage 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.*;

import io.netty.buffer.ByteBuf;

/**
 * The cached implementation of the {@link RespIntegerMessage}.
 * 
 * @since 1.0
 *
 * @author MJ Fang
 */
public class CachedIntegerMessage extends CachedRespMessage implements RespIntegerMessage {

    /**
     * Returns a new {@link CachedIntegerMessage} with the specific {@code value}
     * given.
     * 
     * @param value the value
     * @return a {@code CachedIntegerMessage}
     */
    public static final CachedIntegerMessage create(long value) {
        byte[] bytes = RespCodecUtil.longToAsciiBytes(value);
        int length = bytes.length;
        ByteBuf fullContent = RespCodecUtil.buffer(TYPE_LENGTH + length + EOL_LENGTH)
                .writeByte(RespMessageType.INTEGER.value()).writeBytes(bytes).writeShort(EOL_SHORT).asReadOnly();
        return new CachedIntegerMessage(fullContent, value);
    }

    private final long value;

    private CachedIntegerMessage(ByteBuf fullContent, long value) {
        super(fullContent);
        this.value = value;
    }

    @Override
    public long value() {
        return value;
    }

    @Override
    public String toString() {
        return getClass().getSimpleName() + "[" + type() + value + "]";
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy