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

dagger.producers.Produced Maven / Gradle / Ivy

There is a newer version: 2.52
Show newest version
/*
 * 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}, 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);
 *     }
 *   }
 * 
* * @since 2.0 */ @Beta @CheckReturnValue 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(Object)}. */ @Override public abstract int hashCode(); /** Returns a successful {@code Produced}, whose {@link #get} will return the given value. */ public static Produced successful(@NullableDecl 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 { @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(); } @Override public String toString() { return "Produced[" + value + "]"; } } 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(); } @Override public String toString() { return "Produced[failed with " + throwable.getClass().getCanonicalName() + "]"; } } private Produced() {} }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy