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

com.github.tonivade.resp.command.DefaultRequest 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.command;

import static java.util.Objects.requireNonNull;

import com.github.tonivade.purefun.data.ImmutableArray;
import com.github.tonivade.purefun.type.Option;
import com.github.tonivade.resp.protocol.SafeString;

public class DefaultRequest implements Request {

  private final SafeString command;

  private final ImmutableArray params;

  private final Session session;

  private final ServerContext server;

  public DefaultRequest(ServerContext server, Session session, SafeString command, ImmutableArray params) {
    this.server = server;
    this.session = session;
    this.command = requireNonNull(command);
    this.params = requireNonNull(params);
  }

  @Override
  public String getCommand() {
    return command.toString();
  }

  @Override
  public ImmutableArray getParams() {
    return params;
  }

  @Override
  public SafeString getParam(int i) {
    if (i < params.size()) {
      return params.get(i);
    }
    return null;
  }

  @Override
  public Option getOptionalParam(int i) {
    return Option.of(() -> getParam(i));
  }

  @Override
  public int getLength() {
    return params.size();
  }

  @Override
  public boolean isEmpty() {
    return params.isEmpty();
  }

  @Override
  public boolean isExit() {
    return command.toString().equalsIgnoreCase("quit");
  }

  @Override
  public Session getSession() {
    return session;
  }

  @Override
  public ServerContext getServerContext() {
    return server;
  }

  @Override
  public String toString() {
    return command + "[" + params.size() + "]: " + params;
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy