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

io.atomix.copycat.server.response.AcceptResponse Maven / Gradle / Ivy

/*
 * Copyright 2015 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package io.atomix.copycat.server.response;

import io.atomix.catalyst.buffer.BufferInput;
import io.atomix.catalyst.buffer.BufferOutput;
import io.atomix.catalyst.serializer.SerializeWith;
import io.atomix.catalyst.serializer.Serializer;
import io.atomix.copycat.client.error.RaftError;
import io.atomix.copycat.client.response.SessionResponse;

import java.util.Objects;

/**
 * Protocol accept client response.
 *
 * @author Jordan Halterman
 */
@SerializeWith(id=206)
public class AcceptResponse extends SessionResponse {

  /**
   * Returns a new accept client response builder.
   *
   * @return A new accept client response builder.
   */
  public static Builder builder() {
    return new Builder(new AcceptResponse());
  }

  /**
   * Returns a accept client response builder for an existing response.
   *
   * @param response The response to build.
   * @return The accept client response builder.
   * @throws NullPointerException if {@code response} is null
   */
  public static Builder builder(AcceptResponse response) {
    return new Builder(response);
  }

  @Override
  public void readObject(BufferInput buffer, Serializer serializer) {
    status = Status.forId(buffer.readByte());
    if (status == Status.OK) {
      error = null;
    } else {
      error = RaftError.forId(buffer.readByte());
    }
  }

  @Override
  public void writeObject(BufferOutput buffer, Serializer serializer) {
    buffer.writeByte(status.id());
    if (status == Status.ERROR) {
      buffer.writeByte(error.id());
    }
  }

  @Override
  public int hashCode() {
    return Objects.hash(getClass(), status);
  }

  @Override
  public boolean equals(Object object) {
    if (object instanceof AcceptResponse) {
      AcceptResponse response = (AcceptResponse) object;
      return response.status == status;
    }
    return false;
  }

  @Override
  public String toString() {
    return String.format("%s[status=%s]", getClass().getSimpleName(), status);
  }

  /**
   * Register response builder.
   */
  public static class Builder extends SessionResponse.Builder {
    protected Builder(AcceptResponse response) {
      super(response);
    }
  }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy