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

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

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

import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

import io.netty.util.internal.SystemPropertyUtil;
import io.netty.util.internal.logging.InternalLogger;
import io.netty.util.internal.logging.InternalLoggerFactory;

/**
 * Some common {@link CachedRespMessage} instances.
 * 
 * @since 1.0
 * 
 * @author MJ Fang
 */
@SuppressWarnings("unchecked")
public class CachedRespMessages {

    private static final InternalLogger logger = InternalLoggerFactory.getInstance(CachedRespMessages.class);

    private static final int maxCachedIntegerLimit = 65535;

    /**
     * {@code -NOAUTH} Authentication required.
     */
    public static final CachedErrorMessage NOAUTH = CachedErrorMessage.createAscii("NOAUTH Authentication required.");

    /**
     * {@code -ERR} invalid password
     * 
     * @since 2.2.2
     */
    public static final CachedErrorMessage ERR_INVALID_PASSWORD = CachedErrorMessage
            .createAscii("ERR invalid password");
    /**
     * {@code -ERR} syntax error
     * 
     * @since 2.2.3
     */
    public static final CachedErrorMessage ERR_SYNTAX_ERROR = CachedErrorMessage.createAscii("ERR syntax error");
    /**
     * {@code -ERR} value is not an integer or out of range
     */
    public static final CachedErrorMessage ERR_VALUE_IS_NOT_AN_INTEGER_OR_OUT_OF_RANGE = CachedErrorMessage
            .createErrAscii("value is not an integer or out of range");
    /**
     * {@code -ERR} increment or decrement would overflow
     */
    public static final CachedErrorMessage ERR_INCREMENT_OR_DECREMENT_WOULD_OVERFLOW = CachedErrorMessage
            .createErrAscii("increment or decrement would overflow");

    /**
     * Cached {@code nil}.
     */
    public static final CachedNullMessage NULL = CachedNullMessage.instance();

    /**
     * Empty bulk string.
     */
    public static final CachedBulkStringMessage EMPTY_BULK = CachedBulkStringMessage.createAscii("");

    /**
     * {@code +OK}
     */
    public static final CachedSimpleStringMessage OK = CachedSimpleStringMessage.createAscii("OK");
    /**
     * {@code +PONG}
     */
    public static final CachedSimpleStringMessage PONG = CachedSimpleStringMessage.createAscii("PONG");

    /**
     * {@code :0}
     */
    public static final CachedIntegerMessage ZERO = CachedIntegerMessage.create(0);

    /**
     * {@code :1}
     */
    public static final CachedIntegerMessage ONE = CachedIntegerMessage.create(1);

    private static final Optional[] cachedIntegerMessages;

    private static final ConcurrentMap cachedWrongNumberOfArgumentsRorCommands = new ConcurrentHashMap<>();

    static {

        int maxCachedInteger = SystemPropertyUtil.getInt("io.netty.resp.maxCachedIntegerMessage", 127);
        maxCachedInteger = Math.min(maxCachedIntegerLimit, Math.max(1, maxCachedInteger));
        logger.debug("-Dio.netty.resp.maxCachedIntegerMessage: {}", maxCachedInteger);
        cachedIntegerMessages = new Optional[maxCachedInteger + 1];
        cachedIntegerMessages[0] = Optional.of(ZERO);
        cachedIntegerMessages[1] = Optional.of(ONE);
        for (int i = 2; i < cachedIntegerMessages.length; i++) {
            cachedIntegerMessages[i] = Optional.of(CachedIntegerMessage.create(i));
        }
    }

    /**
     * Returns the cached {@link CachedIntegerMessage} with the specific
     * {@code value} given if exists.
     * 
     * @param value the value as {@code long} type
     * @return an {@code Optional}
     */
    public static final Optional cachedIntegerMessage(long value) {
        if (value < 0 || value >= cachedIntegerMessages.length) {
            return Optional.empty();
        }
        return cachedIntegerMessages[(int) value];
    }

    /**
     * Returns the cached {@link CachedIntegerMessage} with the specific
     * {@code value} given if exists.
     * 
     * @param value the value as {@code int} type
     * @return an {@code Optional}
     */
    public static final Optional cachedIntegerMessage(int value) {
        if (value < 0 || value >= cachedIntegerMessages.length) {
            return Optional.empty();
        }
        return cachedIntegerMessages[value];
    }

    /**
     * Returns the cached {@link CachedErrorMessage} "wrong number of arguments for
     * '$command' command" with the specific {@code command} given
     * 
     * @param command the command text
     * @return a {@code CachedErrorMessage}
     */
    public static CachedErrorMessage cachedWrongNumberOfArgumentsForCommand(String command) {
        return cachedWrongNumberOfArgumentsRorCommands.computeIfAbsent(command,
                CachedRespMessages::createWrondNumberOfArgumentsForCommand);
    }

    private static CachedErrorMessage createWrondNumberOfArgumentsForCommand(String command) {
        return CachedErrorMessage.createErrAscii("wrong number of arguments for '" + command + "' command");
    }

    private CachedRespMessages() {
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy