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

io.atomix.copycat.protocol.AbstractResponse Maven / Gradle / Ivy

There is a newer version: 1.2.8
Show newest version
/*
 * 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.protocol;

import io.atomix.catalyst.buffer.BufferInput;
import io.atomix.catalyst.buffer.BufferOutput;
import io.atomix.catalyst.serializer.Serializer;
import io.atomix.catalyst.util.Assert;
import io.atomix.copycat.error.CopycatError;

import java.util.Objects;

/**
 * Base response for all client responses.
 *
 * @author Jordan Halterman
 */
public abstract class AbstractResponse implements Response {
  protected Status status = Status.OK;
  protected CopycatError error;

  @Override
  public void writeObject(BufferOutput buffer, Serializer serializer) {
  }

  @Override
  public void readObject(BufferInput buffer, Serializer serializer) {
  }

  @Override
  public Status status() {
    return status;
  }

  @Override
  public CopycatError error() {
    return error;
  }

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

  /**
   * Abstract response builder.
   *
   * @param  The builder type.
   * @param  The response type.
   */
  protected static abstract class Builder, U extends AbstractResponse> implements Response.Builder {
    protected U response;

    /**
     * @throws NullPointerException if {@code factory} is null
     */
    protected Builder(U response) {
      this.response = response;
    }

    @Override
    @SuppressWarnings("unchecked")
    public T withStatus(Status status) {
      response.status = Assert.notNull(status, "status");
      return (T) this;
    }

    @Override
    @SuppressWarnings("unchecked")
    public T withError(CopycatError error) {
      response.error = Assert.notNull(error, "error");
      return (T) this;
    }

    /**
     * @throws IllegalStateException if status is null
     */
    @Override
    public U build() {
      Assert.stateNot(response.status == null, "status cannot be null");
      return response;
    }

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

    @Override
    public boolean equals(Object object) {
      return getClass().isAssignableFrom(object.getClass()) && ((Builder) object).response.equals(response);
    }

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

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy