/*
 * Copyright 2020 The gRPC 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.grpc;

import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.base.Preconditions.checkState;

import javax.annotation.Nullable;

// The class can not be located in io.grpc.internal since it is used as a cross-module API.
// Otherwise, shading would break it.
/**
 * Per method config selector that the channel or load balancers will use to choose the appropriate
 * config or take config related actions for an RPC.
 */
@Internal
public abstract class InternalConfigSelector {
  @NameResolver.ResolutionResultAttr
  public static final Attributes.Key<io.grpc.InternalConfigSelector> KEY
      = Attributes.Key.create("io.grpc.config-selector");

  // Use PickSubchannelArgs for SelectConfigArgs for now. May change over time.
  /** Selects the config for an PRC. */
  public abstract Result selectConfig(LoadBalancer.PickSubchannelArgs args);

  public static final class Result {
    private final Status status;
    @Nullable
    private final Object config;
    @Nullable
    private final CallOptions callOptions;
    @Nullable
    private final Runnable committedCallback;

    private Result(
        Status status, Object config, CallOptions callOptions, Runnable committedCallback) {
      this.status = checkNotNull(status, "status");
      this.config = config;
      this.callOptions = callOptions;
      this.committedCallback = committedCallback;
    }

    /**
     * Creates a {@code Result} with the given error status.
     */
    public static Result forError(Status status) {
      checkArgument(!status.isOk(), "status is OK");
      return new Result(status, null, null, null);
    }

    /**
     * Returns the status of the config selection operation. If status is not {@link Status#OK},
     * this result should not be used.
     */
    public Status getStatus() {
      return status;
    }

    /**
     * Returns a parsed config. Must have been returned via
     * ServiceConfigParser.parseServiceConfig().getConfig()
     */
    @Nullable
    public Object getConfig() {
      return config;
    }

    /**
     * Returns a config-selector-modified CallOptions for the RPC.
     */
    @Nullable
    public CallOptions getCallOptions() {
      return callOptions;
    }

    /**
     * Returns a callback to be invoked when the RPC no longer needs a picker.
     */
    @Nullable
    public Runnable getCommittedCallback() {
      return committedCallback;
    }

    public static Builder newBuilder() {
      return new Builder();
    }

    public static final class Builder {
      private Object config;
      private CallOptions callOptions;
      private Runnable committedCallback;

      private Builder() {}

      /**
       * Sets the parsed config.
       *
       * @return this
       */
      public Builder setConfig(Object config) {
        this.config = checkNotNull(config, "config");
        return this;
      }

      /**
       * Sets the CallOptions.
       *
       * @return this
       */
      public Builder setCallOptions(CallOptions callOptions) {
        this.callOptions = checkNotNull(callOptions, "callOptions");
        return this;
      }

      /**
       * Sets the interceptor.
       *
       * @return this
       */
      public Builder setCommittedCallback(@Nullable Runnable committedCallback) {
        this.committedCallback = committedCallback;
        return this;
      }

      /**
       * Build this {@link Result}.
       */
      public Result build() {
        checkState(config != null, "config is not set");
        checkState(callOptions != null, "callOptions is not set");
        return new Result(Status.OK, config, callOptions, committedCallback);
      }
    }
  }
}

@@ -1627,30 +1627,16 @@
 status;%0A
-    @Nullable%0A
     priv
@@ -1660,30 +1660,16 @@
 config;%0A
-    @Nullable%0A
     priv
@@ -2635,38 +2635,24 @@
 g()%0A     */%0A
-    @Nullable%0A
     public O
@@ -2778,38 +2778,24 @@
 PC.%0A     */%0A
-    @Nullable%0A
     public C
@@ -3347,16 +3347,40 @@
  config.
+ This field is required.
 %0A       
@@ -3585,16 +3585,40 @@
 Options.
+ This field is required.
 %0A       
@@ -3853,16 +3853,40 @@
 rceptor.
+ This field is optional.
 %0A       
@@ -3965,18 +3965,8 @@
 ack(
-@Nullable 
 Runn
@@ -4016,32 +4016,45 @@
 ittedCallback = 
+checkNotNull(
 committedCallbac
@@ -4050,24 +4050,46 @@
 ttedCallback
+, %22committedCallback%22)
 ;%0A        re

clarify required and optional fields in config selector result (#7496)