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

io.lettuce.core.metrics.CommandLatencyId Maven / Gradle / Ivy

Go to download

Advanced and thread-safe Java Redis client for synchronous, asynchronous, and reactive usage. Supports Cluster, Sentinel, Pipelining, Auto-Reconnect, Codecs and much more.

The newest version!
package io.lettuce.core.metrics;

import java.io.Serializable;
import java.net.SocketAddress;

import io.lettuce.core.internal.LettuceAssert;
import io.lettuce.core.protocol.ProtocolKeyword;

/**
 * Identifier for a command latency. Consists of a local/remote tuple of {@link SocketAddress}es and a
 * {@link io.lettuce.core.protocol.ProtocolKeyword commandType} part.
 *
 * @author Mark Paluch
 */
@SuppressWarnings("serial")
public class CommandLatencyId implements Serializable, Comparable {

    private final SocketAddress localAddress;

    private final SocketAddress remoteAddress;

    private final ProtocolKeyword commandType;

    private final String commandName;

    protected CommandLatencyId(SocketAddress localAddress, SocketAddress remoteAddress, ProtocolKeyword commandType) {
        LettuceAssert.notNull(localAddress, "LocalAddress must not be null");
        LettuceAssert.notNull(remoteAddress, "RemoteAddress must not be null");
        LettuceAssert.notNull(commandType, "CommandType must not be null");

        this.localAddress = localAddress;
        this.remoteAddress = remoteAddress;
        this.commandType = commandType;
        this.commandName = commandType.toString();
    }

    /**
     * Create a new instance of {@link CommandLatencyId}.
     *
     * @param localAddress the local address
     * @param remoteAddress the remote address
     * @param commandType the command type
     * @return a new instance of {@link CommandLatencyId}
     */
    public static CommandLatencyId create(SocketAddress localAddress, SocketAddress remoteAddress,
            ProtocolKeyword commandType) {
        return new CommandLatencyId(localAddress, remoteAddress, commandType);
    }

    /**
     * Returns the local address.
     *
     * @return the local address
     */
    public SocketAddress localAddress() {
        return localAddress;
    }

    /**
     * Returns the remote address.
     *
     * @return the remote address
     */
    public SocketAddress remoteAddress() {
        return remoteAddress;
    }

    /**
     * Returns the command type.
     *
     * @return the command type
     */
    public ProtocolKeyword commandType() {
        return commandType;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o)
            return true;
        if (!(o instanceof CommandLatencyId))
            return false;

        CommandLatencyId that = (CommandLatencyId) o;

        if (!localAddress.equals(that.localAddress))
            return false;
        if (!remoteAddress.equals(that.remoteAddress))
            return false;
        return commandName.equals(that.commandName);
    }

    @Override
    public int hashCode() {
        int result = localAddress.hashCode();
        result = 31 * result + remoteAddress.hashCode();
        result = 31 * result + commandName.hashCode();
        return result;
    }

    @Override
    public int compareTo(CommandLatencyId o) {

        if (o == null) {
            return -1;
        }

        int remoteResult = remoteAddress.toString().compareTo(o.remoteAddress.toString());
        if (remoteResult != 0) {
            return remoteResult;
        }

        int localResult = localAddress.toString().compareTo(o.localAddress.toString());
        if (localResult != 0) {
            return localResult;
        }

        return commandName.compareTo(o.commandName);
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append("[").append(localAddress);
        sb.append(" -> ").append(remoteAddress);
        sb.append(", commandType=").append(commandType);
        sb.append(']');
        return sb.toString();
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy