序
本文主要研究一下RedisToken
RedisToken
resp-server-0.16.0/src/main/java/com/github/tonivade/resp/protocol/RedisToken.java
public interface RedisToken {
RedisToken NULL_STRING = string((SafeString) null);
RedisToken RESPONSE_OK = status("OK");
RedisTokenType getType();
T accept(RedisTokenVisitor visitor);
static RedisToken nullString() {
return NULL_STRING;
}
static RedisToken responseOk() {
return RESPONSE_OK;
}
static RedisToken string(SafeString str) {
return new StringRedisToken(str);
}
static RedisToken string(String str) {
return new StringRedisToken(safeString(str));
}
static RedisToken status(String str) {
return new StatusRedisToken(str);
}
static RedisToken integer(boolean b) {
return new IntegerRedisToken(b ? 1 : 0);
}
static RedisToken integer(int i) {
return new IntegerRedisToken(i);
}
static RedisToken error(String str) {
return new ErrorRedisToken(str);
}
static RedisToken array(RedisToken... redisTokens) {
return new ArrayRedisToken(ImmutableList.of(redisTokens));
}
static RedisToken array(Collection redisTokens) {
return new ArrayRedisToken(ImmutableList.from(redisTokens));
}
static RedisToken array(Sequence redisTokens) {
return new ArrayRedisToken(redisTokens.asArray());
}
static Stream visit(Stream tokens, RedisTokenVisitor visitor) {
return tokens.map(token -> token.accept(visitor));
}
}
- RedisToken接口定义了getType、accept方法;它还提供了nullString、responseOk、string、status、integer、error、array、visit静态方法
AbstractRedisToken
resp-server-0.16.0/src/main/java/com/github/tonivade/resp/protocol/AbstractRedisToken.java
public abstract class AbstractRedisToken implements RedisToken {
private static final String SEPARATOR = "=>";
private static final Equal> EQUAL =
Equal.>of()
.comparing(AbstractRedisToken::getType)
.comparing(AbstractRedisToken::getValue);
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.hash(type, value);
}
@Override
public boolean equals(Object obj) {
return EQUAL.applyTo(this, obj);
}
@Override
public String toString() {
return type + SEPARATOR + value;
}
}
- AbstractRedisToken声明实现RedisToken接口,它的构造器要求输入type、value
UnknownRedisToken
resp-server-0.16.0/src/main/java/com/github/tonivade/resp/protocol/AbstractRedisToken.java
public static final class UnknownRedisToken extends AbstractRedisToken {
UnknownRedisToken(SafeString value) {
super(RedisTokenType.UNKNOWN, value);
}
@Override
public T accept(RedisTokenVisitor visitor) {
return visitor.unknown(this);
}
}
- UnknownRedisToken继承了AbstractRedisToken,其value为SafeString类型,其type为RedisTokenType.UNKNOWN,其accept执行的是visitor.unknown(this)
StringRedisToken
resp-server-0.16.0/src/main/java/com/github/tonivade/resp/protocol/AbstractRedisToken.java
public static final class StringRedisToken extends AbstractRedisToken {
StringRedisToken(SafeString value) {
super(RedisTokenType.STRING, value);
}
@Override
public T accept(RedisTokenVisitor visitor) {
return visitor.string(this);
}
}
- StringRedisToken继承了AbstractRedisToken,其value为SafeString,其type为RedisTokenType.STRING,其accept执行的是visitor.string(this)
StatusRedisToken
resp-server-0.16.0/src/main/java/com/github/tonivade/resp/protocol/AbstractRedisToken.java
public static final class StatusRedisToken extends AbstractRedisToken {
StatusRedisToken(String value) {
super(RedisTokenType.STATUS, value);
}
@Override
public T accept(RedisTokenVisitor visitor) {
return visitor.status(this);
}
}
- StatusRedisToken继承了AbstractRedisToken,其value为String,其type为RedisTokenType.STATUS,其accept执行的是visitor.status(this)
ErrorRedisToken
resp-server-0.16.0/src/main/java/com/github/tonivade/resp/protocol/AbstractRedisToken.java
public static final class ErrorRedisToken extends AbstractRedisToken {
ErrorRedisToken(String value) {
super(RedisTokenType.ERROR, value);
}
@Override
public T accept(RedisTokenVisitor visitor) {
return visitor.error(this);
}
}
- ErrorRedisToken继承了AbstractRedisToken,其value为String,其type为RedisTokenType.ERROR,其accept执行的是visitor.error(this)
IntegerRedisToken
resp-server-0.16.0/src/main/java/com/github/tonivade/resp/protocol/AbstractRedisToken.java
public static final class IntegerRedisToken extends AbstractRedisToken {
IntegerRedisToken(Integer value) {
super(RedisTokenType.INTEGER, value);
}
@Override
public T accept(RedisTokenVisitor visitor) {
return visitor.integer(this);
}
}
- IntegerRedisToken继承了AbstractRedisToken,其value为Integer,其type为RedisTokenType.INTEGER,其accept执行的是visitor.integer(this)
ArrayRedisToken
resp-server-0.16.0/src/main/java/com/github/tonivade/resp/protocol/AbstractRedisToken.java
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();
}
}
- ArrayRedisToken继承了AbstractRedisToken,其value为Sequence
,其type为RedisTokenType.ARRAY,其accept执行的是visitor.array(this)
小结
RedisToken接口定义了getType、accept方法;它还提供了nullString、responseOk、string、status、integer、error、array、visit静态方法;AbstractRedisToken声明实现RedisToken接口,它的构造器要求输入type、value,它有UnknownRedisToken、StringRedisToken、StatusRedisToken、ErrorRedisToken、IntegerRedisToken、ArrayRedisToken这些实现类