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

com.github.tonivade.resp.protocol.AbstractRedisToken Maven / Gradle / Ivy

Go to download

Netty implementation of REdis Serialization Protocol, and a simple framework to implement command based protocols

There is a newer version: 0.24.0
Show newest version
/*
 * Copyright (c) 2015-2019, Antonio Gabriel Muñoz Conejo 
 * Distributed under the terms of the MIT License
 */
package com.github.tonivade.resp.protocol;

import static java.util.Objects.requireNonNull;

import java.util.Objects;

import com.github.tonivade.purefun.Equal;
import com.github.tonivade.purefun.data.Sequence;

public abstract class AbstractRedisToken implements RedisToken {

  private static final String SEPARATOR = "=>";

  private final RedisTokenType type;
  private final T value;

  private AbstractRedisToken(RedisTokenType type, T value) {
    this.type = requireNonNull(type);
    this.value = value;
  }

  @Override
  public RedisTokenType getType() {
    return type;
  }

  public T getValue() {
    return value;
  }

  @Override
  public int hashCode() {
    return Objects.hashCode(value);
  }

  @Override
  public boolean equals(Object obj) {
    return Equal.of(this)
        .comparing(AbstractRedisToken::getValue)
        .applyTo(obj);
  }

  @Override
  public String toString() {
    return type + SEPARATOR + value;
  }

  public static final class UnknownRedisToken extends AbstractRedisToken {
    UnknownRedisToken(SafeString value) {
      super(RedisTokenType.UNKNOWN, value);
    }

    @Override
    public  T accept(RedisTokenVisitor visitor) {
      return visitor.unknown(this);
    }
  }

  public static final class StringRedisToken extends AbstractRedisToken {
    StringRedisToken(SafeString value) {
      super(RedisTokenType.STRING, value);
    }

    @Override
    public  T accept(RedisTokenVisitor visitor) {
      return visitor.string(this);
    }
  }

  public static final class StatusRedisToken extends AbstractRedisToken {
    StatusRedisToken(String value) {
      super(RedisTokenType.STATUS, value);
    }

    @Override
    public  T accept(RedisTokenVisitor visitor) {
      return visitor.status(this);
    }
  }

  public static final class ErrorRedisToken extends AbstractRedisToken {
    ErrorRedisToken(String value) {
      super(RedisTokenType.ERROR, value);
    }

    @Override
    public  T accept(RedisTokenVisitor visitor) {
      return visitor.error(this);
    }
  }

  public static final class IntegerRedisToken extends AbstractRedisToken {
    IntegerRedisToken(Integer value) {
      super(RedisTokenType.INTEGER, value);
    }

    @Override
    public  T accept(RedisTokenVisitor visitor) {
      return visitor.integer(this);
    }
  }

  public static final class ArrayRedisToken extends AbstractRedisToken> {
    ArrayRedisToken(Sequence value) {
      super(RedisTokenType.ARRAY, requireNonNull(value).asArray());
    }

    @Override
    public  T accept(RedisTokenVisitor visitor) {
      return visitor.array(this);
    }

    public int size() {
      return getValue().size();
    }
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy