/*
 * Copyright (C) 2014 The Dagger 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 dagger.producers;

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

import com.google.common.base.Objects;
import com.google.errorprone.annotations.CheckReturnValue;
import dagger.internal.Beta;
import java.util.concurrent.ExecutionException;
import org.checkerframework.checker.nullness.compatqual.NullableDecl;

/**
 * An interface that represents the result of a {@linkplain Producer production} of type {@code T},
 * or an exception that was thrown during that production. For any type {@code T} that can be
 * injected, you can also inject {@code Produced<T>}, which enables handling of any exceptions that
 * were thrown during the production of {@code T}.
 *
 * <p>For example: <pre><code>
 *   {@literal @}Produces Html getResponse(
 *       UserInfo criticalInfo, {@literal Produced<ExtraInfo>} noncriticalInfo) {
 *     try {
 *       return new Html(criticalInfo, noncriticalInfo.get());
 *     } catch (ExecutionException e) {
 *       logger.warning(e, "Noncritical info");
 *       return new Html(criticalInfo);
 *     }
 *   }
 * </code></pre>
 *
 * @author Jesse Beder
 */
@Beta
@CheckReturnValue
public abstract class Produced<T> {
  /**
   * Returns the result of a production.
   *
   * @throws ExecutionException if the production threw an exception
   */
  public abstract T get() throws ExecutionException;

  /**
   * Two {@code Produced} objects compare equal if both are successful with equal values, or both
   * are failed with equal exceptions.
   */
  @Override
  public abstract boolean equals(Object o);

  /** Returns an appropriate hash code to match {@link #equals(Object)}. */
  @Override
  public abstract int hashCode();

  /** Returns a successful {@code Produced}, whose {@link #get} will return the given value. */
  public static <T> Produced<T> successful(@NullableDecl T value) {
    return new Successful<T>(value);
  }

  /**
   * Returns a failed {@code Produced}, whose {@link #get} will throw an
   * {@code ExecutionException} with the given cause.
   */
  public static <T> Produced<T> failed(Throwable throwable) {
    return new Failed<T>(checkNotNull(throwable));
  }

  private static final class Successful<T> extends Produced<T> {
    @NullableDecl private final T value;

    private Successful(@NullableDecl T value) {
      this.value = value;
    }

    @Override
    @NullableDecl
    public T get() {
      return value;
    }

    @Override public boolean equals(Object o) {
      if (o == this) {
        return true;
      } else if (o instanceof Successful) {
        Successful<?> that = (Successful<?>) o;
        return Objects.equal(this.value, that.value);
      } else {
        return false;
      }
    }

    @Override public int hashCode() {
      return value == null ? 0 : value.hashCode();
    }
  }

  private static final class Failed<T> extends Produced<T> {
    private final Throwable throwable;

    private Failed(Throwable throwable) {
      this.throwable = checkNotNull(throwable);
    }

    @Override public T get() throws ExecutionException {
      throw new ExecutionException(throwable);
    }

    @Override public boolean equals(Object o) {
      if (o == this) {
        return true;
      } else if (o instanceof Failed) {
        Failed<?> that = (Failed<?>) o;
        return this.throwable.equals(that.throwable);
      } else {
        return false;
      }
    }

    @Override public int hashCode() {
      return throwable.hashCode();
    }
  }

  private Produced() {}
}

@@ -3018,32 +3018,36 @@
 %7D%0A%0A    @Override
+%0A   
  public boolean 
@@ -3057,32 +3057,32 @@
 als(Object o) %7B%0A
-
       if (o == t
@@ -3312,32 +3312,36 @@
 %7D%0A%0A    @Override
+%0A   
  public int hash
@@ -3397,32 +3397,124 @@
 ashCode();%0A    %7D
+%0A%0A    @Override%0A    public String toString() %7B%0A      return %22Produced%5B%22 + value + %22%5D%22;%0A    %7D
 %0A  %7D%0A%0A  private 
@@ -3706,32 +3706,36 @@
 %7D%0A%0A    @Override
+%0A   
  public T get() 
@@ -3821,32 +3821,36 @@
 %7D%0A%0A    @Override
+%0A   
  public boolean 
@@ -4111,16 +4111,20 @@
 Override
+%0A   
  public 
@@ -4176,24 +4176,162 @@
 ode();%0A    %7D
+%0A%0A    @Override%0A    public String toString() %7B%0A      return %22Produced%5Bfailed with %22 + throwable.getClass().getCanonicalName() + %22%5D%22;%0A    %7D
 %0A  %7D%0A%0A  priv

Add debug toString() for Produced