/* * Copyright (C) 2014 Google Inc. * * 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 com.google.common.base.Objects; import dagger.internal.Beta; import java.util.concurrent.ExecutionException; import javax.annotation.Nullable; import static com.google.common.base.Preconditions.checkNotNull; /** * 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}, which enables handling of any exceptions that * were thrown during the production of {@code T}. * *

For example:


 *   {@literal @}Produces Html getResponse(
 *       UserInfo criticalInfo, {@literal Produced} noncriticalInfo) {
 *     try {
 *       return new Html(criticalInfo, noncriticalInfo.get());
 *     } catch (ExecutionException e) {
 *       logger.warning(e, "Noncritical info");
 *       return new Html(criticalInfo);
 *     }
 *   }
 * 
* * @author Jesse Beder */ @Beta public abstract class Produced { /** * 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). */ @Override public abstract int hashCode(); /** Returns a successful {@code Produced}, whose {@link #get} will return the given value. */ public static Produced successful(@Nullable T value) { return new Successful(value); } /** * Returns a failed {@code Produced}, whose {@link #get} will throw an * {@code ExecutionException} with the given cause. */ public static Produced failed(Throwable throwable) { return new Failed(checkNotNull(throwable)); } private static final class Successful extends Produced { @Nullable private final T value; private Successful(@Nullable T value) { this.value = value; } @Override 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 extends Produced { 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() {} }